It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days?
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
There is only one line containing one integer N (1 <= N <= 1000000000).
<h4< style="background-color: transparent; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; box-shadow: none; font-family: "Merriweather",serif; font-size: 18px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" dd=""> OutputFor each test case, output one string indicating the day of week.
<h4< style="background-color: transparent; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; box-shadow: none; font-family: "Merriweather",serif; font-size: 18px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" dd=""> Sample Input2 1 2<h4< style="background-color: transparent; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; box-shadow: none; font-family: "Merriweather",serif; font-size: 18px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" dd=""> Sample Output
Sunday Thursday<h4< style="background-color: transparent; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; box-shadow: none; font-family: "Merriweather",serif; font-size: 18px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" dd=""> Hint
A week consists of Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday.
题意就是快速求
1
1
+ 2
2
+ 3
3
+ ... +
N
N 对7取余的余数
思路见dalao coldfresh——https://blog.csdn.net/Coldfresh/article/details/80094497
import java.io.BufferedInputStream;
import java.util.Scanner;
public class Main {
static int fun(int a,int b) {
int ans = 1;
while(b!=0) {
if(b%2==1)ans=(ans*a)%7;
a=(a*a)%7;
b>>=1;
}
//System.out.println(ans);
return (int)(ans%7);
}
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int n;
n = sc.nextInt();
String[] week = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday"};
while(n--!=0) {
int t = sc.nextInt();
int lim;
if(t>6)lim = 6;
else lim = t;
int ans=0;
for(int i=1;i<=lim;i++) {
int base = t/7;
int items = (t-i+7)/7;//项数 t-i防止小于0 直接加7 符合对应大小关系
if(i==1) {
ans = (ans + 1*items)%7;
}
else {
int q = fun(i,7);//q的大小
int ny = fun(q+6,5);//逆元 防止变成负数
int now = (((fun(i,i)*(fun(q,items)+6))%7)*ny)%7;// a1*(q^n-1)*ny 防止变成负数导致计算错误:fun-1->fun-1+7
ans = (ans+now)%7;
}
}
System.out.println(week[(int) ((ans+6)%7)]);
}
}
}