zoj3785 What day is that day?
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.
题目大意:今天是星期六,输入n,问11 + 22 + 33 + ... + NN 天后是星期几?
思路:因为n是10亿,解法必定是数学方法或者找规律。注意到7是素数,所以可以使用费马小定理进行简化。即当p是素数是,a^(p-1) mod p=1,简化后发现每42个数就会出现一个循环,得数mod7=6。所以求解过程就是先求出n由几个42组成,对于42取余的剩下的部分就可以直接模拟做了。
1 /* 2 * Author: Joshua 3 * Created Time: 2014/5/17 13:18:55 4 * File Name: j.cpp 5 */ 6 #include<cstdio> 7 #include<iostream> 8 #include<cstring> 9 #include<cstdlib> 10 #include<cmath> 11 #include<algorithm> 12 #include<string> 13 #include<map> 14 #include<set> 15 #include<vector> 16 #include<queue> 17 #include<stack> 18 #include<ctime> 19 #include<utility> 20 #define M0(x) memset(x, 0, sizeof(x)) 21 #define MP make_pair 22 #define Fi first 23 #define Se second 24 #define rep(i, a, b) for (int i = (a); i <= (b); ++i) 25 #define red(i, a, b) for (int i = (a); i >= (b); --i) 26 #define PB push_back 27 #define Inf 0x3fffffff 28 #define eps 1e-8 29 typedef long long LL; 30 using namespace std; 31 32 string ans[7]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"}; 33 34 void solve() 35 { 36 int n,m,t=0,temp; 37 scanf("%d",&n); 38 m=n/42; 39 t=(t+m*6)%7; 40 for (int i=1;i<=n%42;i++) 41 { 42 temp=1; 43 for (int j=1;j<=i;j++) 44 temp=(temp*i)%7; 45 t=(t+temp)%7; 46 } 47 cout<<ans[t]<<endl; 48 } 49 int main() 50 { 51 int tt; 52 scanf("%d",&tt); 53 while (tt) 54 { 55 tt--; 56 solve(); 57 } 58 return 0; 59 }
许久没写了,代码太丑,大家凑合着看。。。