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
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
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).
Output For each test case, print a single line containing an integer modulo 109+7. Sample Input
3 2 5 4 2 4 1Sample Output
33 30 10
思路:题目输入n和k两个数字,要求的是1^k到n^k之和,数字较大,如果不取余的话,就是wrong answer
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
long long int N=1000000000+7;
using namespace std;
int main(){
int T,i,j;
long long int n,k;
long long int p;
cin>>T;
while(T--){
long long int sum=0;
scanf("%lld %lld",&n,&k);
for(i=1;i<=n;i++){
p=1;
for(j=1;j<=k;j++){
p=p*i;
p=p%N;
}
sum=sum+p;
sum=sum%N;
}
printf("%lld\n",sum);
}
return 0;
}