A Boring Problem
题解
其实这题不难,只要想到了前缀和差分就基本OK了.
我们要求的是第iii项的式子:
F(i)=(a1+a2+...+ai)k+(a2+...+ai)k+...+(ai)kF(i)=(a_1+a_2+...+a_i)^k+(a2+...+a_i)^k+...+(a_i)^kF(i)=(a1+a2+...+ai)k+(a2+...+ai)k+...+(ai)k
记Si=a1+a2+...+ai,S0=0S_i = a_1+a_2+...+a_i,S_0=0Si=a1+a2+...+ai,S0=0
F(i)=(Si−S0)k+(Si−S1)k+...+(Si−Si−1)kF(i)=(S_i-S_0)^k+(S_i-S_1)^k+...+(S_i-S_{i-1})^kF(i)=(Si−S0)k+(Si−S1)k+...+(Si−Si−1)k
二项式定理展开:
F(i)=∑t=0kCktSit(−S0)k−t+∑t=0kCktSit(−S1)k−t+...+∑t=0kCktSit(−Si−1)k−tF(i)=\sum_{t=0}^kC_k^tS_i^t(-S_0)^{k-t}+\sum_{t=0}^kC_k^tS_i^t(-S_1)^{k-t}+...+\sum_{t=0}^kC_k^tS_i^t(-S_{i-1})^{k-t}F(i)=∑t=0kCktSit(−S0)k−t+∑t=0kCktSit(−S1)k−t+...+∑t=0kCktSit(−Si−1)k−t
整理得:
F(i)=∑t=0kCktSit(−1)k−t(S0k−t+S1k−t+...+Si−1k−t)F(i) = \sum_{t=0}^kC_k^tS_i^t(-1)^{k-t}(S_0^{k-t}+S_1^{k-t}+...+S_{i-1}^{k-t})F(i)=∑t=0kCktSit(−1)k−t(S0k−t+S1k−t+...+Si−1k−t)
再记
SS[i][j]=S0i+S1i+...+Sj−1iSS[i][j] =S_0^i+S_1^i+...+S_{j-1}^iSS[i][j]=S0i+S1i+...+Sj−1i
那么
F(i)=∑t=0kCktSit(−1)k−t(SS[k−t][i−1])F(i) = \sum_{t=0}^kC_k^tS_i^t(-1)^{k-t}(SS[k-t][i-1])F(i)=∑t=0kCktSit(−1)k−t(SS[k−t][i−1])
注意到SSSSSS可以O(nk)O(nk)O(nk)预处理出来,SSS可以O(n)O(n)O(n)预处理出来,而F(i)F(i)F(i)就可以O(k)O(k)O(k)得到.
注意!S00=1S_0^0 = 1S00=1
代码
#include <iostream>
#include <algorithm>
#include <cstring>
#define pr(x) std::cout << #x << ':' << x << std::endl
#define rep(i,a,b) for(int i = a;i <= b;++i)typedef long long LL;
const int N = 100010;
const LL P = 1e9+7;
int T,n,k;
char s[N];
long long S[101][N],SS[101][N];
long long C[110][110];
void init() {C[0][0] = 1;for(int i = 1;i <= 100;++i) {C[i][0] = 1;for(int j = 1;j <= i;++j) {C[i][j] = (C[i-1][j-1] + C[i-1][j]) % P;}}
}
int main() {std::ios::sync_with_stdio(false);init();std::cin >> T;while(T--) {std::cin >> n >> k;std::cin >> s;for(int i = 0;i <= n;++i) S[0][i] = 1;for(int i = 1;i <= n;++i) S[1][i] = (s[i-1]-'0') + S[1][i-1] ;for(int i = 2;i <= k;++i) for(int j = 1;j <= n;++j)S[i][j] = S[1][j] * S[i-1][j] % P;SS[0][0] = 1;for(int i = 0;i <= k;++i) {for(int j = 1;j <= n;++j) SS[i][j] = (SS[i][j-1] + S[i][j])% P;}for(int i = 1;i <= n;++i) {long long ans = 0;for(int j = 0;j <= k;++j) {long long res = C[k][j]*S[j][i]%P*SS[k-j][i-1]%P;if((k-j)%2==0) ans = (ans + res) % P;else ans = (ans - res + P) % P;}if(i != 1) std::cout << " ";std::cout << ans;}std::cout << std::endl;}return 0;
}