Title Description:
http://acm.nyist.net/JudgeOnline/problem.php?pid=420
- A very simple question, beg 1^p+2^p+3^p+......+n^p and.
- Input
- The first line of a single number t represents the number of test data groups. Next there will be T-line numbers, each line consisting of two digital n,p,
Enter the guaranteed 0<n<=1000,0<=p<=1000.
- Output
- Output 1^p+2^p+3^p+......+n^p The result of 10003, each result is a single row.
- Sample input
-
210 110 2
- Sample output
-
55385
-
Topic Analysis:
The problem of fast Power + congruence theorem, although has been written many times the fast power but still do not remember, each time is to see the template, this time must remember it.
AC Code:
/** * Quick Power get touch + +/#include <iostream> #include <cstdio> #include <map> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <vector> #include <stack># include<cstdlib> #include <cctype> #include <cstring> #include <cmath>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<<t<<endl; Res= (res%10003+t%10003)%10003; } cout<<res<<endl; } return 0;}
-
-
NYOJ 420 P-Squared summation (fast Power + congruence theorem)