luolgu CF622F The Sum of the k-th Powers |拉格朗日插值

题意翻译

求 \(\sum_{i=1}^ni^k \bmod (10^9+7)\)


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e6+5,mod=1e9+7;
#define int long long
int n,k,y[N];
inline int ksm(int x,int y){
	int res=1;
	while(y){
		if(y&1)res=res*x%mod;
		x=x*x%mod; y>>=1;
	}
	return res;
}
int pl[N],pr[N],fac[N];
signed main(){
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=k+2;i++)y[i]=(y[i-1]+ksm(i,k))%mod;
	pl[0]=pr[k+3]=fac[0]=1;
	
    for(int i=1;i<=k+2;i++)pl[i]=pl[i-1]*(n-i)%mod;
    for(int i=k+2;i>=1;i--)pr[i]=pr[i+1]*(n-i)%mod;
	for(int i=1;i<=k+2;i++)fac[i]=fac[i-1]*i%mod;
	int ans=0;
	for(int i=1;i<=k+2;i++){
		int op=y[i];
		int a=pl[i-1]*pr[i+1]%mod;
		int b=fac[i-1]*((k-i)&1?-1:1)*fac[k+2-i]%mod;
		ans=(ans+op*a%mod*ksm(b,mod-2)%mod)%mod;
	}
	cout<<(ans+mod)%mod<<endl;
	
}
上一篇:Thymeleaf 速查文档


下一篇:模板引擎--Thymeleaf