题目链接
题解
我们要求
\[\sum\limits_{i = 1}^{n}i^{k}r^{i}\]
如果\(r = 1\),就是自然数幂求和,上伯努利数即可\(O(k^2)\)
否则,我们需要将式子进行变形
要与\(n\)无关
设
\[F(k) = \sum\limits_{i = 1}^{n} i^{k}r^{i}\]
自然数幂应该是不用去动了,两边乘个\(r\)
\[rF(k) = \sum\limits_{i = 2}^{n + 1}r^{i}(i - 1)^{k}\]
相减得
\[
\begin{aligned}
(r - 1)F(k) &= r^{n + 1}n^{k} - r + \sum\limits_{i = 2}^{n}r^{i}((i - 1)^{k} - i^{k}) \&= r^{n + 1}n^{k} - r + \sum\limits_{i = 2}^{n}r^{i}(\sum\limits_{j = 0}^{k}{k \choose j}(-1)^{k - j}i^{j} - i^{k}) \&= r^{n + 1}n^{k} - r + \sum\limits_{i = 2}^{n}r^{i}\sum\limits_{j = 0}^{k - 1}{k \choose j}(-1)^{k - j}i^{j} \&= r^{n + 1}n^{k} - r + \sum\limits_{i = 2}^{n}\sum\limits_{j = 0}^{k - 1}{k \choose j}(-1)^{k - j}i^{j}r^{i} \&= r^{n + 1}n^{k} - r + \sum\limits_{j = 0}^{k - 1}{k \choose j}(-1)^{k - j}\sum\limits_{i = 2}^{n}i^{j}r^{i} \&= r^{n + 1}n^{k} - r + \sum\limits_{j = 0}^{k - 1}{k \choose j}(-1)^{k - j}(F(j) - r) \\end{aligned}
\]
故
\[F(k) = \frac{r^{n + 1}n^{k} - r + \sum\limits_{j = 0}^{k - 1}{k \choose j}(-1)^{k - j}(F(j) - r)}{r - 1}\]
边界
\[F(0) = \sum\limits_{i = 1}^{n}r^{i} = r\frac{r^{n} - 1}{r -1}\]
同样可以实现\(O(k^2)\)递推
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 2010,maxm = 100005,INF = 0x3f3f3f3f;
inline LL read(){
LL out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = 0; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 1) + (out << 3) + c - 48; c = getchar();}
return flag ? out : -out;
}
const int P = 1000000007;
LL F[maxn],B[maxn],fac[maxn],inv[maxn],fv[maxn],N = 2005;
inline LL qpow(LL a,LL b){
LL re = 1; a %= P;
for (; b; b >>= 1,a = 1ll * a * a % P)
if (b & 1) re = 1ll * re * a % P;
return re;
}
inline LL C(LL n,LL m){
if (m > n) return 0;
return 1ll * fac[n] * fv[m] % P * fv[n - m] % P;
}
void init(){
fac[0] = fac[1] = fv[0] = fv[1] = inv[0] = inv[1] = 1;
for (int i = 2; i <= N; i++){
fac[i] = fac[i - 1] * i % P;
inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
fv[i] = fv[i - 1] * inv[i] % P;
}
B[0] = 1;
for (int k = 1; k < N; k++){
for (int i = 0; i < k; i++)
B[k] = (B[k] + C(k + 1,i) * B[i] % P) % P;
B[k] = 1ll * (P - 1) * inv[k + 1] % P * B[k] % P;
}
}
LL n,K,r;
void work1(){
n %= P;
LL tmp = n,ans = 0;
for (int i = K; ~i; i--){
ans = (ans + C(K + 1,i) * B[i] % P * tmp % P) % P;
tmp = tmp * n % P;
}
ans = ans * inv[K + 1] % P;
printf("%lld\n",(ans + qpow(n,K)) % P);
}
void work2(){
r %= P;
LL tmp = qpow(r,n + 1),t,tt = 1,rv = qpow(r - 1,P - 2);
F[0] = 1ll * (qpow(r,n) + P - 1) % P * rv % P * r % P;
for (int k = 1; k <= K; k++){
t = 0; tt = 1ll * tt * (n % P) % P;
for (int j = 0; j < k; j++)
t = (t + (((k - j) & 1) ? -1ll : 1ll) * C(k,j) * ((F[j] - r) % P) % P) % P;
t = (t + P) % P;
F[k] = ((tmp * tt % P - r) % P + t) % P * rv % P;
}
printf("%lld\n",(F[K] + P) % P);
}
int main(){
init();
int T = read();
while (T--){
n = read(); K = read(); r = read();
if (r == 1) work1();
else work2();
}
return 0;
}
原文地址:https://www.cnblogs.com/Mychael/p/9296868.html