HDU - 6027 Easy Summation(快速幂)解题

                      Easy Summation

                                Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
                                      Total Submission(s): 1107    Accepted Submission(s): 443


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 .
 

 

Input
The first line of the input contains an integer T(1T20) , denoting the number of test cases.
Each of the following T lines contains two integers n(1n10000) and k(0k5) .
 

 

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
 
标准快速幂~~~
 1 #include <stdio.h>
 2 long long kuaisumi(long long a,int b)
 3 {
 4     long long c=1,base=a;
 5     while(b)
 6     {
 7         if(b&1)
 8         {
 9             c=c*a%1000000007;
10         }
11         a=a*a%1000000007;
12         b>>=1;
13     }
14     return c;
15 }
16 int main()
17 {
18     int t,a,b,i,c;
19     long long sum;
20     scanf("%d",&t);
21     while(t--)
22     {
23         sum=0;
24         scanf("%d%d",&a,&b);
25         for(i=1;i<=a;i++)
26         {
27             c=kuaisumi(i,b);
28             sum=(sum+c)%1000000007;
29         }
30         printf("%lld\n",sum);
31     }
32     return 0;
33 }    

 

posted @ 2017-07-19 11:36  丿月华丶唯少  阅读(207)  评论(0编辑  收藏  举报