http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3785
topic
Input n, output $ 1 ^ 1 + 2 ^ 2 + 3 ^ 3 + \ cdots + n ^ n \ mod 7 $
answer
I in accordance with the system, the $ i ^ i $ assigned congruence lines 7 to give
\[\begin{array}{ccccccc}1^1&2^2&3^3&4^4&5^5&6^6&7^7\\1^8&2^9&3^{10}&4^{11}&5^{12}&6^{13}&7^{14}\\\vdots\\1^{1+7k}&2^{2+7k}&3^{3+7k}&\cdots\\\end{array}\]
Five sets of geometric series summation formula
$\frac{i-i^{i+7k+7}}{1-i^7}\equiv ans \pmod{7}$
Verify find $ i = 2..6 $ are inverse, inverse to multiply
AC Code:
#include <bits/stdc++.h> #define REP(r,x,y) for(register int r=(x); r<(y); r++) using namespace std; typedef long long ll; int read() { int K = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { ret = ret * 10 + c - '0'; c = getchar(); } Return right * f; } inline void read(int& x) { x = read(); } const int mod = 7; inline int qpow(int a, ll b) { int year = 1; for (; b; b >>= 1) { if (b & 1) ans = (ans * a) % 7; a = (a * a) % 7; } return years; } int cnts[7]; or const int [] = {0,0,6,3,2,5,4}; const char w[][10] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; int main () { int t; read(t); while (0 < t--) { int n; read(n); cnts[0] = n / 7; REP(i, 1, 7) cnts[i] = cnts[0]; int k = n % 7; REP(i, 1, 7) { if (i > k) break; cnts[i]++; } int years = cnts [1]; REP(i, 2, 7) { if (cnts[i] == 0)continue; int k = qpow(i, i) - qpow(i, (ll)7*cnts[i]+i); k = n * [i]; k %= 7; years + = k; } Years% = 7; years--; if (year <0) year = + 7; puts(w[ans]); } }