Original question: zoj 3785 http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3785.
The day is Saturday. After 1 ^ 1 + 2 ^ 2 + 3 ^ 3... + N ^ N, what is the day of the week?
At first, I thought it was a problem with the summation of such sub-statements. I did not go to the formula after half a day. The result is not displayed. Two methods were found later.
Method 1: Find the rule
As can be seen from the table, the results of these numbers have 42 loops, so the first 42 are processed directly, and the previous one is used later.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>using namespace std;#define N 20007int sum[44];string ss[8] = {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};void init(){ int n,i,j; sum[0] = 0; for(n=1;n<=44;n++) { int flag = n%7; int ans = 1; for(j=1;j<=n;j++) ans = (ans*flag)%7; sum[n] = ans; } for(i=1;i<=44;i++) sum[i] += sum[i-1];}int main(){ int i,j,t,n,ans; init(); scanf("%d",&t); while(t--) { scanf("%d",&n); ans = (((n/42)%7*(sum[42]%7))%7 + sum[n%42]%7)%7; cout<<ss[ans]<<endl; } return 0;}
View code
Method 2: Matrix Multiplication
To be updated