题目链接
题解
这种神仙数学题我当然不会做。。
不过推导倒是挺简单
由题我们只需模拟出代价,只需使用\(S(n,k) = \sum\limits_{i = 1}^{n} i^{k}\)这样的前缀和计算
我不知道怎么来的这样一个公式【如果有那位dalao知道欢迎留言】:
\[(n + 1)^{k + 1} - n^{k + 1} = \sum\limits_{i = 1}^{k + 1} {k + 1\choose i}n^{k + 1 - i}\]
我们发现这个公式右侧涵盖了所有\(n^i\)的项,如果我们将所有\(n\)枚举出来,将会的得到:
\[
\begin{aligned}
(n + 1)^{k + 1} - n^{k + 1} &= \sum\limits_{i = 1}^{k + 1} {k + 1\choose i}n^{k + 1 - i} \n^{k + 1} - (n - 1)^{k + 1} &= \sum\limits_{i = 1}^{k + 1} {k + 1\choose i}(n - 1)^{k + 1 - i} \......... \2^{k + 1} - 1^{k + 1} &= \sum\limits_{i = 1}^{k + 1} {k + 1\choose i}1^{k + 1 - i} \\end{aligned}
\]
全部相加,得到:
\[(n + 1)^{k + 1} - 1 = \sum\limits_{i= 1}^{k + 1} {k + 1 \choose i} S(n,k + 1 - i)\]
取出\(S(n,k)\)
\[S(n,k) = \frac{(n + 1)^{k + 1} - 1 - \sum\limits_{i = 2}^{k + 1}{k + 1 \choose i} S(n,k + 1 - i)}{k + 1}\]
发现就可以\(O(k^2)\)递推了
由于模拟也是\(O(k^2)\)的
所以最终复杂度\(O(k^4)\)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 105,maxm = 100005,INF = 1000000000,P = 1000000007;
inline LL read(){
LL out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == ‘-‘) flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,K,fac[maxn],fv[maxn],inv[maxn];
LL a[maxn];
void init(){
fac[0] = fac[1] = fv[0] = fv[1] = inv[0] = inv[1] = 1;
for (int i = 2; i < maxn; i++){
fac[i] = 1ll * fac[i - 1] * i % P;
inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
fv[i] = 1ll * fv[i - 1] * inv[i] % P;
}
}
LL C(LL n,LL m){
return 1ll * fac[n] * fv[m] % P * fv[n - m] % P;
}
LL qpow(LL a,LL b){
LL ans = 1; a %= P;
for (; b; b >>= 1,a = a * a % P)
if (b & 1) ans = ans * a % P;
return ans;
}
LL f[maxn];
LL S(LL n,LL k){
if (!n) return 0;
f[0] = n;
for (int i = 1; i <= k; i++){
LL tmp = 0;
for (int j = 2; j <= i + 1; j++)
tmp = (tmp + C(i + 1,j) * f[i + 1 - j]) % P;
f[i] = (((qpow(n + 1,i + 1) - 1) % P - tmp) % P + P) % P * inv[i + 1] % P;
}
return f[k];
}
LL b[maxn];
int main(){
init();
int T = read();
while (T--){
n = read(); m = read(); K = m + 1;
REP(i,m) a[i] = read(); a[K] = n + 1;
sort(a + 1,a + 1 + K);
LL ans = 0;
for (int i = 0; i <= m; i++){
for (int j = i; j <= m; j++){
ans = ((ans + (S(a[j + 1] - 1,K) - S(a[j],K)) % P) % P + P) % P;
}
LL len = a[i + 1] - a[i];
for (int j = i + 1; j <= K; j++) a[j] -= len;
}
printf("%lld\n",ans);
}
return 0;
}
原文地址:https://www.cnblogs.com/Mychael/p/9052067.html