标签:class not ali test simple from count i++ and
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 4112 Accepted Submission(s): 1676
题意:给出n,k,定义f(i)=i^k,求出[f(1)+f(2)+……+f(n)]mod 1e9+7
题解:直接套快速幂,注意用long long
1 #include<bits/stdc++.h> 2 using namespace std; 3 const long long mod=1e9+7; 4 long long PowerMod(long long a, long long b,long long c) { 5 long long ans = 1; 6 a = a % c; 7 while(b>0) { 8 9 if(b % 2 == 1) 10 ans = (ans * a) % c; 11 b = b/2; 12 a = (a * a) % c; 13 } 14 return ans; 15 } 16 int main() 17 { 18 int t; 19 while(~scanf("%d",&t)) 20 { 21 while(t--) 22 { 23 long long n,k,sum=0; 24 scanf("%lld %lld",&n,&k); 25 for(int i=1;i<=n;i++) 26 { 27 sum+=PowerMod(i,k,mod); 28 sum=sum%mod; 29 } 30 printf("%lld\n",sum); 31 } 32 } 33 return 0; 34 }
标签:class not ali test simple from count i++ and
原文地址:https://www.cnblogs.com/fqfzs/p/9858893.html