zoj 3785 What day is that day? (打表找规律)
思路:比赛的时候有想过找循环节,但是,打表打错了。 后来,看着过了挺多人,就急了, 看了一下别人的时间 耗时都挺长的,就以为不是找规律,
没想到真是找规律,不过,这个题的数据可能挺大的。
AC代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 char s[10][20] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; 7 int a[300]; 8 9 int main() 10 { 11 int t, i, n; 12 int sum, x; 13 sum = 0; 14 for(n = 1; n <= 294; n++) 15 { 16 x = 1; 17 for(i = 1; i <= n; i++) 18 { 19 x = x*n; 20 x = x%7; 21 } 22 sum += x; 23 sum = sum%7; 24 a[n] = sum; 25 } 26 while(cin>>t) 27 { 28 while(t--) 29 { 30 cin>>n; 31 x = n%294; 32 if(x==0) 33 x = 294; 34 cout<<s[a[x]]<<endl; 35 } 36 } 37 return 0; 38 }
打表代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 11000; 7 int a[maxn], b[maxn], f[maxn]; 8 9 int main() 10 { 11 int t, i, j, n; 12 int sum, x, cnt, ad; 13 while(cin>>t) 14 { 15 while(t--) 16 { 17 //cin>>n; 18 sum = 0; 19 memset(a, 0, sizeof(a)); 20 memset(b, 0, sizeof(b)); 21 memset(f, 0, sizeof(f)); 22 for(n = 1; n <= 10005; n++) 23 { 24 x = 1; 25 for(i = 1; i <= n; i++) 26 { 27 x = x*n; 28 x = x%7; //因为数会非常大,所以利用公式,取余 29 } 30 sum += x; 31 sum = sum%7; //因为数会非常大,所以利用公式,取余 32 a[n] = sum; //把所有结果存入a数组 33 } 34 cnt = 1; 35 for(i = 1; i <= 10000; i++) 36 { 37 if(a[i]==1) 38 { 39 b[cnt++] = i; //把结果为1的序号 存入b 40 f[i] = 1; 41 } 42 /*cout<<a[i]; 43 if(i%294==0) 44 cout<<endl<<endl;*/ 45 } 46 for(i = 2; i < cnt; i++) 47 { 48 ad = b[i]-1; //每一个 的间隔 49 for(j = 1; j <= 6; j++) //如果照这个间隔向后推 几次,还是符合间隔的话,就找到了循环节 50 { 51 if(f[b[i]+ad*j] != 1) 52 break; 53 } 54 if(j==7) 55 { 56 cout<<b[i]<<endl; 57 break; 58 } 59 } 60 } 61 } 62 return 0; 63 }