HDU 6027 - Easy Summation ( 快速幂取模 )
题意
给出n, k, 已知: f(i)=i^k , 求 sum = f(1)+f(2)+…+f(n) , 并将结果模10e9+7
思路
其实这个题直接暴力就能暴过去
用快速幂的时候忘了中间的a也要用LL结果WA了两发
ll quickmi(ll a, ll b){
ll ans = 1;
a %= mod;
while(b){
if( b & 1 ) ans = (ans*a) % mod;
b >>= 1;
a = (a*a) % mod;
}
return ans;
}
AC代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
const int maxn = 1e4+5;
ll quickmi(ll a, ll b){
ll ans = 1;
a %= mod;
while(b){
if( b & 1 ) ans = (ans*a) % mod;
b >>= 1;
a = (a*a) % mod;
}
return ans;
}
int main()
{
int T;
ll n, k;
scanf("%d",&T);
while(T--){
scanf("%lld%lld", &n, &k);
ll sum = 0;
for( ll i = 1; i <= n; i++ )
sum = (sum + quickmi(i, k)) % mod;
printf("%lld\n", sum % mod);
}
return 0;
}