[题解]CF622F The Sum of the k-th Powers
更好的阅读体验
思路
首先发现
不难通过拉格朗日插值法,将
但是,普通的拉格朗日插值法是
可以轻易将原式转化为:
发现
然后你对于
对于分子:
然后处理出
对于分母:
定义
发现前一个
因此
将分子、分母代入原式即可。
观察到当
Code
#include <bits/stdc++.h>
#define re register
#define int long long
#define Add(a,b) ((((a) % mod + (b) % mod) % mod + mod) % mod)
#define Mul(a,b) ((((a) % mod) * ((b) % mod) % mod + mod) % mod)
#define Div(a,b) (Mul(a,qmi(((b) % mod + mod) % mod,mod - 2)))
using namespace std;
const int mod = 1e9 + 7;
int n,k,ans;
int mul = 1,g = 1,y;
inline int read(){
int r = 0,w = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
r = (r << 3) + (r << 1) + (c ^ 48);
c = getchar();
}
return r * w;
}
inline int qmi(int a,int b){
int res = 1;
while (b){
if (b & 1) res = Mul(res,a);
a = Mul(a,a),b >>= 1;
}
return res;
}
inline void solve1(){
for (re int i = 1;i <= n;i++) ans = Add(ans,qmi(i,k));
}
inline void solve2(){
for (re int i = 1;i <= k + 2;i++) mul = Mul(mul,n - i);
for (re int i = 1;i <= k + 2;i++){
y = Add(y,qmi(i,k));
if (i == 1){
for (re int j = -1;j >= -k - 1;j--) g = Mul(g,j);
}
else g = Mul(g,Div(i - 1,i - k - 3));
int a = Div(mul,n - i);
ans = Add(ans,Mul(y,Div(a,g)));
}
}
signed main(){
n = read(),k = read();
if (k + 2 >= n) solve1();
else solve2();
printf("%lld",ans);
return 0;
}