Description
It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days?
Input
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).
Output
For each test case, output one string indicating the day of week.
Sample Input
2 1 2
Sample Output
Sunday Thursday
Hint
A week consists of Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday.
做法:先暴力地把结果写入文件,在文件里面观察,然后得到规律,发现循环周期为294,之后就好办了,打一个1-294的表,之后每次去模查表就行了。
当然做法有很多,貌似还有等比数列求和的做法。把每一项的底数%7,7个排一行,会发现,上下对应的数刚好能成为等比数列,然后用等比数列的求和公式做。
个人认为我的方法算是比较快了。20ms
#include <stdio.h>
#define MOD 7
#define N 300
int table[N];
long long PowMod(long long a, long long n)
{
long long ret = 1;
while(n) {
if(n & 1) ret = ret * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return ret%MOD;
}
void gettable()
{
for(int i=1;i<=294;i++)
{
long long sum=0;
for(int j=1;j<=i;j++)
sum+=PowMod(j,j)%MOD;
table[i]=sum%MOD;
}
}
int main()
{
gettable();
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int cnt=table[n%294];
switch(cnt)
{
case 0:
printf("Saturday\n");
break;
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
}
}
return 0;
}