# codeforces 622F. The Sum of the k-th Powers （拉格朗日插值法）★

549人阅读 评论(0)

modulo 109 + 7

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <queue>
#include <map>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-3
#define maxn 1000100
#define MOD 1000000007
const long long mod = 1000000007;
long long n,k;
long long f[maxn],fac[maxn];
long long pow(long long a,long long b)
{
long long ret = 1;
while(b)
{
if(b & 1)
ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
int main()
{
int t,C = 1;
//scanf("%d",&t);
while(scanf("%lld%lld",&n,&k) != EOF)
{
for(int i = 1; i <= k+2; i++)
f[i] = (f[i-1] + pow(i*1LL,k))%mod;
fac[0] = 1;
for(int i = 1; i < maxn; i++)
fac[i] = fac[i-1] * i % mod;
if(n <= k+2)
{
printf("%lld\n",f[n]);
continue;
}
long long cur = 1,ans = 0;
for(int i = 1; i <= k+2; i++)
cur = cur * (n-i) % mod;
for(int i = 1; i <= k+2; i++)
{
long long tmp = pow(n-i,mod-2) % mod;
long long tmp1 = pow(fac[i-1]%mod*fac[k+2-i]%mod,mod-2)%mod;
int flag = (k+2-i)%2?-1:1;
ans = (ans + cur%mod*tmp%mod*flag*tmp1%mod*f[i]%mod)%mod;
}
ans = (ans + mod) % mod;
printf("%lld\n",ans);
}
return 0;
}



0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：98604次
• 积分：4173
• 等级：
• 排名：第7361名
• 原创：306篇
• 转载：187篇
• 译文：0篇
• 评论：1条
最新评论