Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

CF961G Partitions

LuoGu: CF961G Partitions

CF: G. Partitions

考虑每个数的贡献,有

$$
ANS = \sum _ {i = 1} ^ n w _ i \sum _ {j = 1} ^ n j \binom {n - 1} {j - 1} {n - j \brace k - 1}
$$

重新考虑贡献,对于所有的 ${n \brace k}$ 种方案,每个数所在的集合中,自己一定能对自己有一个贡献;再考虑将剩下 $n -1 $ 划分集合的 ${n - 1 \brace k}$ 种方案中,再将一个数放入其中一个集合,选择一个大小为 $x$ 的集合对其有 $x$ 的贡献,而 $\sum _ x = n - 1$ ,故这部分的贡献为 $(n - 1) {n - 1 \brace k}$ 。答案为:

$$
ANS = \sum _ {i - 1} ^ n w _ i (n - 1) {n - 1 \brace k}$
$$

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdio>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
flag |= c == '-';
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...Rest>
void read (Type &x, Rest &...y) { read(x); read(y...); }
template <class Type>
void write (Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
typedef long long LL;
const int N = 2e5 + 10, mod = 1e9 + 7;
int n, m, fact[N], ifact[N];
int binpow (int b, int k = mod - 2)
{
int res = 1;
for (; k; k >>= 1, b = (LL)b * b % mod)
if (k & 1) res = (LL)res * b % mod;
return res;
}
void init ()
{
fact[0] = 1;
for (int i = 1; i < N; ++i)
fact[i] = (LL)fact[i - 1] * i % mod;
ifact[N - 1] = binpow(fact[N - 1]);
for (int i = N - 1; i; --i)
ifact[i - 1] = (LL)ifact[i] * i % mod;
}
int S (int a, int b)
{
int res = 0;
for (int i = 0; i <= b; ++i)
res = (res + (b - i & 1 ? -1ll : 1ll) * ifact[i] * ifact[b - i] % mod * binpow(i, a)) % mod;
return res;
}
int main ()
{
init();
read(n, m);
int sum = 0;
for (int i = 1, a; i <= n; ++i)
read(a), sum = (sum + a) % mod;
write(((LL)sum * (S(n, m) + (n - 1ll) * S(n - 1, m) % mod) % mod + mod) % mod);
return 0;
}