【CF961G】Partitions
题面
题解
显然对于所有点对答案的贡献都有一个相同的系数,设这个系数为\(X\),那么\(ans=X\sum w_i\)。
枚举一个点所在集合的大小,有
\[\begin{aligned}\\
X&=\sum_{i=1}^n i{n-1\choose i-1}\begin{Bmatrix}n-i\\k-1\end{Bmatrix}\\
&=\sum_{i=1}^n i{n-1\choose i-1}\frac{1}{(k-1)!}\sum_{j=0}^{k-1}(-1)^j{k-1\choose j}(k-1-j)^{n-i}\\
&=\sum_{i=1}^n i{n-1\choose i-1}\sum_{j=0}^{k-1}\frac{(-1)^j}{j!}\frac{(k-j-1)^{n-i}}{(k-j-1)!}\\
&=\sum_{j=0}^{k-1}\frac{(-1)^j}{j!(k-j-1)!}\sum_{i=1}^n i {n-1\choose i-1}(k-j-1)^{n-i}\\
&=\sum_{j=0}^{k-1}\frac{(-1)^j}{j!(k-j-1)!}(\sum_{i=1}^n{n-1\choose i-1}(k-j-1)^{n-i}+\sum_{i=1}^n (i-1){n-1\choose i-1}(k-j-1)^{n-i})\\
&=\sum_{j=0}^{k-1}\frac{(-1)^j}{j!(k-j-1)!}(\sum_{i=1}^n{n-1\choose i-1}(k-j-1)^{n-i}+(n-1)\sum_{i=1}^n {n-2\choose i-2}(k-j-1)^{n-i})\\
&=\sum_{j=0}^{k-1}\frac{(-1)^j}{j!(k-j-1)!}((k-j)^{n-1}+(n-1)(k-j)^{n-2})\\
&=\sum_{j=0}^{k-1}\frac{(-1)^j}{j!(k-j-1)!}(k-j)^{n-2}(k-j+n-1)\\
\end{aligned}
\]
这种推式子的方法比较不要脑子,还有一种要脑子的方法:
考虑什么对一个点的贡献,那么他自己对自己的贡献就为\(\begin{Bmatrix}n\\k\end{Bmatrix}\),
别人对他的贡献就是别人先分好然后他往别人分好的集合丢的贡献,也就是\((n-1)\begin{Bmatrix}n-1\\k\end{Bmatrix}\)
所以又有\(X=\begin{Bmatrix}n\\k\end{Bmatrix}+(n-1)\begin{Bmatrix}n-1\\k\end{Bmatrix}\)
代码
代码是第二种方法的。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 2e5 + 5, Mod = 1e9 + 7;
int fpow(int x, int y) {
int res = 1;
while (y) {
if (y & 1) res = 1ll * res * x % Mod;
x = 1ll * x * x % Mod;
y >>= 1;
}
return res;
}
int fac[MAX_N], ifc[MAX_N];
int C(int n, int m) {
if (n < m) return 0;
else return 1ll * fac[n] * ifc[n - m] % Mod * ifc[m] % Mod;
}
int N, K, w[MAX_N];
int S(int n, int m) {
int res = 0, p = 1;
for (int i = 0; i <= m; i++) {
res = (res + 1ll * p * C(m, i) % Mod * fpow(m - i, n)) % Mod;
p = Mod - p;
}
res = 1ll * res * ifc[m] % Mod;
return res;
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
N = gi(), K = gi();
for (int i = 1; i <= N; i++) w[i] = gi();
fac[0] = 1; for (int i = 1; i <= N; i++) fac[i] = 1ll * i * fac[i - 1] % Mod;
ifc[N] = fpow(fac[N], Mod - 2);
for (int i = N - 1; ~i; i--) ifc[i] = 1ll * ifc[i + 1] * (i + 1) % Mod;
int ans = 0;
for (int i = 1; i <= N; i++) ans = (ans + w[i]) % Mod;
printf("%lld\n", 1ll * ans * (S(N, K) + 1ll * S(N - 1, K) * (N - 1) % Mod) % Mod);
return 0;
}