NYOJ 420 p power summation (Fast Power + same remainder theorem)
Description:
A very simple question: 1 ^ p + 2 ^ p + 3 ^ p + ...... + The sum of n ^ p. Enter a separate number t in the first line to indicate the number of test data groups. Next, there will be t rows of numbers, each line includes two numbers n, p,
Input 0 Output 1 ^ p + 2 ^ p + 3 ^ p + ...... + N ^ p returns the remainder of 10003, and each result occupies a single row. Sample Input
210 110 2
Sample output
55385
Question Analysis:
The question of the Fast Power + conremainder theorem is that although the fast power has been written for many times, I still don't remember it. Every time I read the template, I must remember it this time.
AC code:
/*** Quick power touch acquisition + same remainder */# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; int mod (int a, int B, int n) {int t = 1; if (B = 0) return 1; if (B = 1) return a % n; t = mod (a, B> 1, n); t = t * t % n; if (B & 1) {// B is an odd t = t * a % n;} return t;} int main () {int t; cin> t; while (t --) {int p, n; cin >>n> p; int res = 0; for (int I = 1; I <= n; I ++) {int t = mod (I, p, 10003); // cout <