sum of power
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
Calculate mod (1000000000+7) for given n,m.
Input
Input contains two integers n,m(1≤n≤1000,0≤m≤10).
Output
Output the answer in a single line.
Example Input
10 0
Example Output
10
Hint
根据公式求和 用c++直接写是超出范围的,一直WA,比赛的时候队友用现场学的Java解决了这个
这是我后来看以为大佬的代码
01 | #include <iostream> |
02 | #include<string.h> |
03 | #include<string> |
04 | #include<algorithm> |
05 | #include<stdio.h> |
06 | using namespace std; |
07 | long long p=1000000007; |
08 | long long ppow( long long n, long long m) |
09 | { |
10 | long long ans=1; |
11 | while (m>=1) |
12 | { |
13 | if (m%2==1) //m&1 |
14 | ans=(ans*n)%p; |
15 | n=(n*n)%p; |
16 | m/=2; //m>>=1 |
17 | } |
18 | return ans; |
19 | } |
20 | int main(){ |
21 | long long n,m; |
22 | while (cin>>n>>m) |
23 | { |
24 | long long sum=0; |
25 | for ( int i=1;i<=n;i++) |
26 | sum=(sum+ppow(i,m))%p; |
27 | cout<<sum<<endl; |
28 | } |
29 | return 0; |
30 | } |
31 |
32 |