Link
https://www.luogu.org/problemnew/show/CF961G
V
(
R
)
=
∑
S
∈
R
W
(
S
)
=
∑
S
∈
R
∣
S
∣
∑
x
∈
S
w
x
V(R)=\sum\limits_{S\in R}W(S)=\sum\limits_{S\in R}|S|\sum\limits_{x\in S}w_x
V(R)=S∈R∑W(S)=S∈R∑∣S∣x∈S∑wx
考虑换一下,计数每个数产生贡献的次数?
不难发现应该是
∑
s
=
1
n
(
n
−
1
s
−
1
)
\{
n
−
s
k
−
1
\}
s
\sum\limits_{s=1}^n{n-1\choose s-1}{n-s\brace k-1}s
s=1∑n(s−1n−1){k−1n−s}s
V
(
R
)
=
V(R)=
V(R)= 上面那个东西乘上
∑
x
=
1
n
w
x
\sum\limits_{x=1}^nw_x
x=1∑nwx
然后怎么做呢?斯特林数那一部分不好直接搞吧,那就弄开
推一下斯特林通项?
我们知道
\{
n
k
\}
{n\brace k}
{kn} 如果没有那个集合非空的限制而且集合有序就是等于
n
k
n^k
nk
但是现在有非空的限制。我们考虑容斥:
n
n
n 个有序元素划分为
k
k
k 个有序集合,限制至少
i
i
i 个集合是空哒,方案数
(
k
i
)
(
k
−
i
)
n
{k\choose i}(k-i)^n
(ik)(k−i)n
容斥:
\{
n
k
\}
=
1
k
!
∑
i
=
0
k
(
−
1
)
i
(
k
i
)
(
k
−
i
)
n
{n\brace k}=\frac{1}{k!}\sum\limits_{i=0}^k(-1)^i{k\choose i}(k-i)^n
{kn}=k!1i=0∑k(−1)i(ik)(k−i)n 。外面乘上的
1
k
!
\frac{1}{k!}
k!1 使集合无序。
最后那个就先不管了,我们暂时只考虑前面的系数
P
P
P
P
=
∑
s
=
1
n
(
n
−
1
s
−
1
)
⋅
s
(
k
−
1
)
!
∑
i
=
0
k
−
1
(
−
1
)
i
(
k
−
1
i
)
(
k
−
i
−
1
)
n
−
s
=
∑
s
=
1
n
(
n
−
1
s
−
1
)
s
⋅
∑
i
=
0
k
−
1
(
−
1
)
i
(
k
−
i
−
1
)
n
−
s
i
!
(
k
−
i
−
1
)
!
=
∑
i
=
0
k
−
1
(
−
1
)
i
i
!
(
k
−
i
−
1
)
!
∑
s
=
1
n
s
(
n
−
1
s
−
1
)
(
k
−
i
−
1
)
n
−
s
\begin{array}{rcl} P&=&\sum\limits_{s=1}^n{n-1\choose s-1}\cdot\frac{s}{(k-1)!}\sum\limits_{i=0}^{k-1}(-1)^i{k-1\choose i}(k-i-1)^{n-s}\\ &=&\sum\limits_{s=1}^n{n-1\choose s-1}s\cdot\sum\limits_{i=0}^{k-1}\frac{(-1)^i(k-i-1)^{n-s}}{i!(k-i-1)!}\\ &=&\sum\limits_{i=0}^{k-1}\frac{(-1)^i}{i!(k-i-1)!}\sum\limits_{s=1}^ns{n-1\choose s-1}(k-i-1)^{n-s} \end{array}
P===s=1∑n(s−1n−1)⋅(k−1)!si=0∑k−1(−1)i(ik−1)(k−i−1)n−ss=1∑n(s−1n−1)s⋅i=0∑k−1i!(k−i−1)!(−1)i(k−i−1)n−si=0∑k−1i!(k−i−1)!(−1)is=1∑ns(s−1n−1)(k−i−1)n−s
前面那个好弄了
搞后面那个 凑系数 凑组合恒等(挪走
s
s
s ) 凑二项式
Q
=
∑
s
=
1
n
s
(
n
−
1
s
−
1
)
(
k
−
i
−
1
)
n
−
s
=
∑
s
=
1
n
(
n
−
1
s
−
1
)
(
s
−
1
)
(
k
−
i
−
1
)
n
−
s
+
∑
s
=
1
n
(
n
−
1
s
−
1
)
(
k
−
i
−
1
)
n
−
s
=
(
n
−
1
)
∑
s
=
2
n
(
n
−
2
s
−
2
)
(
k
−
i
−
1
)
n
−
s
+
∑
s
=
0
n
−
1
(
n
−
1
s
)
(
k
−
i
−
1
)
n
−
1
−
s
=
(
n
−
1
)
∑
s
=
0
n
−
2
(
n
−
2
s
)
(
k
−
i
−
1
)
n
−
2
−
s
+
(
k
−
i
)
n
−
1
=
(
n
−
1
)
(
k
−
i
)
n
−
2
+
(
k
−
i
)
n
−
1
\begin{array}{rcl} Q&=&\sum\limits_{s=1}^ns{n-1\choose s-1}(k-i-1)^{n-s}\\ &=&\sum\limits_{s=1}^n{n-1\choose s-1}(s-1)(k-i-1)^{n-s}+\sum\limits_{s=1}^n{n-1\choose s-1}(k-i-1)^{n-s}\\ &=&(n-1)\sum\limits_{s=2}^n{n-2\choose s-2}(k-i-1)^{n-s}+\sum\limits_{s=0}^{n-1}{n-1\choose s}(k-i-1)^{n-1-s}\\ &=&(n-1)\sum\limits_{s=0}^{n-2}{n-2\choose s}(k-i-1)^{n-2-s}+(k-i)^{n-1}\\ &=&(n-1)(k-i)^{n-2}+(k-i)^{n-1}\\ \end{array}
Q=====s=1∑ns(s−1n−1)(k−i−1)n−ss=1∑n(s−1n−1)(s−1)(k−i−1)n−s+s=1∑n(s−1n−1)(k−i−1)n−s(n−1)s=2∑n(s−2n−2)(k−i−1)n−s+s=0∑n−1(sn−1)(k−i−1)n−1−s(n−1)s=0∑n−2(sn−2)(k−i−1)n−2−s+(k−i)n−1(n−1)(k−i)n−2+(k−i)n−1
则
P
=
∑
i
=
0
k
−
1
(
−
1
)
i
(
k
−
i
)
n
−
2
(
k
+
n
−
i
−
1
)
i
!
(
k
−
i
−
1
)
!
P=\sum\limits_{i=0}^{k-1}\frac{(-1)^i(k-i)^{n-2}(k+n-i-1)}{i!(k-i-1)!}
P=i=0∑k−1i!(k−i−1)!(−1)i(k−i)n−2(k+n−i−1)
另一种思路是考虑统计每一个数对某个数的贡献
P
=
\{
n
k
\}
+
(
n
−
1
)
\{
n
−
1
k
\}
P={n\brace k}+(n-1){n-1\brace k}
P={kn}+(n−1){kn−1}
也一样。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
using namespace std;
const long long p = 1e9+7;
const int MAXN = 2e5+5;
int n, k;
long long ans, sum;
long long stair[MAXN];
inline long long qpow(long long a, long long b)
{
if (b < 0) return 1;
register long long ret = 1;
while (b)
{
if (b & 1)
{
ret *= a;
ret %= p;
}
a *= a;
a %= p;
b >>= 1;
}
return ret;
}
inline void Adjust(long long& x)
{
x = (x % p) + p;
if (x >= p) x -= p;
}
int main()
{
scanf("%d%d", &n, &k);
for (register int t, i = 1; i <= n; ++i)
{
scanf("%d", &t);
sum += t;
if (sum >= p) sum -= p;
}
stair[0] = 1;
for (register int i = 1; i <= k; ++i)
{
stair[i] = stair[i-1] * i % p;
}
for (register int i = 0; i < k; ++i)
{
if (i & 1) ans -= qpow(k-i,n-2) * (k+n-i-1) % p * qpow(stair[i]*stair[k-i-1]%p, p-2) % p, Adjust(ans);
else ans += qpow(k-i,n-2) * (k+n-i-1) % p * qpow(stair[i]*stair[k-i-1]%p, p-2) % p, ans %= p;
}
printf("%I64d", ans * sum % p);
return 0;
}