码迷,mamicode.com
首页 > 其他好文 > 详细

hdu6027Easy Summation(快速幂取模)

时间:2018-10-27 00:16:18      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:class   not   ali   test   simple   from   count   i++   and   

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.
 

 

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

题意:给出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 }

 

hdu6027Easy Summation(快速幂取模)

标签:class   not   ali   test   simple   from   count   i++   and   

原文地址:https://www.cnblogs.com/fqfzs/p/9858893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!