hdu6027Easy Summation(快速幂取模)
Posted fqfzs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu6027Easy Summation(快速幂取模)相关的知识,希望对你有一定的参考价值。
Easy Summation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 4112 Accepted Submission(s): 1676
Problem Description
You are encountered with a traditional problem concerning the sums of powers.
Given two integers n and k. Let f(i)=ik, please evaluate the sum f(1)+f(2)+...+f(n). The problem is simple as it looks, apart from the value of n in this question is quite large.
Can you figure the answer out? Since the answer may be too large, please output the answer modulo 109+7.
Given two integers n and k. Let f(i)=ik, please evaluate the sum f(1)+f(2)+...+f(n). The problem is simple as it looks, apart from the value of n in this question is quite large.
Can you figure the answer out? Since the answer may be too large, please output the answer modulo 109+7.
Input
The first line of the input contains an integer T(1≤T≤20), denoting the number of test cases.
Each of the following T lines contains two integers n(1≤n≤10000) and k(0≤k≤5).
Each of the following T lines contains two integers n(1≤n≤10000) and k(0≤k≤5).
Output
For each test case, print a single line containing an integer modulo 109+7.
Sample Input
3
2 5
4 2
4 1
Sample Output
33
30
10
题意:给出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 ",sum); 31 } 32 } 33 return 0; 34 }
以上是关于hdu6027Easy Summation(快速幂取模)的主要内容,如果未能解决你的问题,请参考以下文章
构造共轭函数+矩阵快速幂HDU 4565 So Easy! (2013 长沙赛区邀请赛)