题目链接
题解
upd:经典的自然数幂和,伯努利数裸题
由题我们只需模拟出代价,只需使用\\(S(n,k) = \\sum\\limits_{i = 1}^{n} i^{k}\\)这样的前缀和计算
我不知道怎么来的这样一个公式:
这玩意怎么来的呢?
左边为\\((n + 1)^k - n^k\\),\\((n+1)^k\\)可以看做有\\(k\\)个位置进行染色,每个位置有\\(n + 1\\)种染色的方案数,减去\\(n^k\\),就代表了拥有第\\(n + 1\\)种颜色的染色方案数
那么这个等式就很好理解了,我们枚举第\\(n + 1\\)种颜色染了多少个,就得到了右式
我们发现这个公式右侧涵盖了所有\\(n^i \\quad[ i \\in [0,k]]\\)的项,我们令\\(k = k + 1\\),如果我们将所有\\(n\\)枚举出来,将会的得到:
全部相加,得到:
取出\\(S(n,k)\\)
发现就可以\\(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;
}
LL a[maxn],fac[maxn],fv[maxn],inv[maxn],n,m,K;
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 % P;
for (int i = 1; i <= k; i++){
LL tmp = 0;
for (int j = 0; j <= i - 1; j++)
tmp = (tmp + C(i + 1,j) * f[j] % P) % 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;
}