Partitions

Partitions

对于\(W(S)=|S|\sum\limits_{x\in S}w_x\)考虑其意义,即是集合中每一个数都对\(x\)产生一个\(w_x\)的贡献。
那么考虑每一个\(w_i\)对总答案的贡献,即是自己对自己产生的贡献为\(\begin{Bmatrix} n \\k \end{Bmatrix} * w_i\),那么其他数对\(i\)的贡献就为\(w_i * (n - 1)\begin{Bmatrix} n - 1 \\k \end{Bmatrix}\),相当于将\(i\)加入某一个集合,那么\(n - 1\)个数都能对\(i\)造成贡献。
所以

\[ans = (\sum w_i)(\begin{Bmatrix} n \\k \end{Bmatrix} +(n - 1)\begin{Bmatrix} n - 1 \\k \end{Bmatrix}) \]

用通解求即可。

Code
#include<cstdio>
#include<iostream>
#define IN inline
#define LL long long
using namespace std;
const int P = 1e9 + 7, N = 2e5 + 5;
int n, K; LL inv[N];

IN int read() {
	int t = 0,res = 0; char ch = getchar();
	for (; !isdigit(ch); ch = getchar()) t |= (ch == '-');
	for (; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ 48);
	return t ? -res : res;
}
LL fpow(LL x, LL y) {
	LL res = 1;
	for (; x; x >>= 1, y = y * y % P)
		if (x & 1) res = res * y % P;
	return res;
}
LL gets(int x, int y) {
	LL res = 0;
	for (int i = 0, fl = 1; i <= y; i++, fl *= -1)
		(res += (P + fl) % P * inv[i] % P * fpow(x, y - i) % P * inv[y - i] % P) %= P;
	return res;
}
int main() {
	n = read(), K = read(); LL ans = 0; inv[0] = 1;
	for (int i = 1; i <= n; i++) inv[i] = inv[i - 1] * fpow(P - 2, i) % P; 
	for (int i = 1; i <= n; i++) (ans += read()) %= P;
	ans = ans * (gets(n, K) + (LL)(n - 1) * gets(n - 1, K) % P) % P;
	printf("%lld\n", ans);
}

posted @ 2023-03-15 21:55  RiverSheep  阅读(111)  评论(0)    收藏  举报