Educational Codeforces Round 7 F. The Sum of the k-th Powers
重心拉格朗日插值定理可以解决求和公式。。但是我的跑的也太慢了吧。。。
#include <cmath> #include <cstdio> #include <cstdlib> #include <cassert> #include <cstring> #include <set> #include <map> #include <list> #include <queue> #include <string> #include <iostream> #include <algorithm> #include <functional> #include <stack> using namespace std; typedef long long ll; #define T int t_;Read(t_);while(t_--) #define dight(chr) (chr>='0'&&chr<='9') #define alpha(chr) (chr>='a'&&chr<='z') #define INF (0x3f3f3f3f) #define maxn (1000005) #define maxm (10005) #define mod 1000000007 #define ull unsigned long long #define repne(x,y,i) for(int i=(x);i<(y);++i) #define repe(x,y,i) for(int i=(x);i<=(y);++i) #define repde(x,y,i) for(int i=(x);i>=(y);--i) #define repdne(x,y,i) for(int i=(x);i>(y);--i) #define ri register int inline void Read(int &n){char chr=getchar(),sign=1;for(;!dight(chr);chr=getchar())if(chr=='-')sign=-1; for(n=0;dight(chr);chr=getchar())n=n*10+chr-'0';n*=sign;} inline void Read(ll &n){char chr=getchar(),sign=1;for(;!dight(chr);chr=getchar())if(chr=='-')sign=-1; for(n=0;dight(chr);chr=getchar())n=n*10+chr-'0';n*=sign;} ll quickpow(ll x,ll y){ ll ans = 1; while(y){ if(y & 1){ ans = ans * x; if(ans >= mod) ans %= mod; } x = x * x; if(x >= mod) x %= mod; y >>= 1; } return ans; } int main() { freopen("a.in","r",stdin); freopen("b.out","w",stdout); ll n,k; Read(n),Read(k); if(n <= k + 2){ ll ans = 0; repe(1,n,i){ ans = ans + quickpow(i,k); if(ans >= mod) ans %= mod; } printf("%lld\n",ans); } else{ ll ans = 0,y = 1,w = 1,t = 1; repe(2,k+2,i){ w = w * (1 - i); if(w < 0) w = (w + (-w / mod + 1) * mod) % mod; } repe(1,k+2,i){ ans += quickpow(w,mod - 2) * y % mod * quickpow(n-i,mod-2) % mod; if(ans >= mod) ans %= mod; w = w * i % mod * quickpow(i - k - 2 + mod,mod-2) % mod; y = y + quickpow(i + 1,k); if(y >= mod) y %= mod; } repe(1,k+2,i){ t = t * (n - i); if(t >= mod) t %= mod; } ans = ans * t; if(ans >= mod) ans %= mod; printf("%lld\n",ans); } }
---恢复内容结束---