【CF961G】 Partitions

传送门

题目

题意翻译
给出nn 个物品, 每个物品有一个权值w_iw
i

定义一个集合SS 的权值W(S)=|S|\sum\limits_{x\in S}w_xW(S)=∣S∣
x∈S

​ w
x

定义一个划分的权值为W’®=\sum\limits_{S\subseteq R}W(S)W

®=
S⊆R

​ W(S)

求将nn 个物品划分成kk 个集合的所有方案的权值和

n,k\le2\times105,w_i\le109n,k≤2×10
5
,w
i
​ ≤10
9

感谢@Kelin 提供的翻译

题目描述
You are given a set of n n elements indexed from 1 1 to n n . The weight of i i -th element is w_{i} w
i
​ . The weight of some subset of a given set is denoted as . The weight of some partition R R of a given set into k k subsets is (recall that a partition of a given set is a set of its subsets such that every element of the given set belongs to exactly one subset in partition).

Calculate the sum of weights of all partitions of a given set into exactly k k non-empty subsets, and print it modulo 10^{9}+7 10
9
+7 . Two partitions are considered different iff there exist two elements x x and y y such that they belong to the same set in one of the partitions, and to different sets in another partition.

输入格式
The first line contains two integers n n and k k ( 1<=k<=n<=2·10^{5} 1<=k<=n<=2⋅10
5
) — the number of elements and the number of subsets in each partition, respectively.

The second line contains n n integers w_{i} w
i
​ ( 1<=w_{i}<=10^{9} 1<=w
i
​ <=10
9
)— weights of elements of the set.

输出格式
Print one integer — the sum of weights of all partitions of a given set into k k non-empty subsets, taken modulo 10^{9}+7 10
9
+7 .

输入输出样例
输入 #1 复制
4 2
2 3 2 3
输出 #1 复制
160
输入 #2 复制
5 2
1 2 3 4 5
输出 #2 复制
645
说明/提示
Possible partitions in the first sample:

{{1,2,3},{4}} 1,2,3,4 , W®=3·(w_{1}+w_{2}+w_{3})+1·w_{4}=24 W®=3⋅(w
1
​ +w
2
​ +w
3
​ )+1⋅w
4
​ =24 ;
{{1,2,4},{3}} 1,2,4,3 , W®=26 W®=26 ;
{{1,3,4},{2}} 1,3,4,2 , W®=24 W®=24 ;
{{1,2},{3,4}} 1,2,3,4 , W®=2·(w_{1}+w_{2})+2·(w_{3}+w_{4})=20 W®=2⋅(w
1
​ +w
2
​ )+2⋅(w
3
​ +w
4
​ )=20 ;
{{1,3},{2,4}} 1,3,2,4 , W®=20 W®=20 ;
{{1,4},{2,3}} 1,4,2,3 , W®=20 W®=20 ;
{{1},{2,3,4}} 1,2,3,4 , W®=26 W®=26 ;
Possible partitions in the second sample:

{{1,2,3,4},{5}} 1,2,3,4,5 , W®=45 W®=45 ;
{{1,2,3,5},{4}} 1,2,3,5,4 , W®=48 W®=48 ;
{{1,2,4,5},{3}} 1,2,4,5,3 , W®=51 W®=51 ;
{{1,3,4,5},{2}} 1,3,4,5,2 , W®=54 W®=54 ;
{{2,3,4,5},{1}} 2,3,4,5,1 , W®=57 W®=57 ;
{{1,2,3},{4,5}} 1,2,3,4,5 , W®=36 W®=36 ;
{{1,2,4},{3,5}} 1,2,4,3,5 , W®=37 W®=37 ;
{{1,2,5},{3,4}} 1,2,5,3,4 , W®=38 W®=38 ;
{{1,3,4},{2,5}} 1,3,4,2,5 , W®=38 W®=38 ;
{{1,3,5},{2,4}} 1,3,5,2,4 , W®=39 W®=39 ;
{{1,4,5},{2,3}} 1,4,5,2,3 , W®=40 W®=40 ;
{{2,3,4},{1,5}} 2,3,4,1,5 , W®=39 W®=39 ;
{{2,3,5},{1,4}} 2,3,5,1,4 , W®=40 W®=40 ;
{{2,4,5},{1,3}} 2,4,5,1,3 , W®=41 W®=41 ;
{{3,4,5},{1,2}} 3,4,5,1,2 , W®=42 W®=42 .

思路

首先一个很显然的性质,每个数做出贡献的系数是相同的(因为是所有情况的总和)

注意到集合权值的系数为 |S|,可以看成集合中的每一个元 素都会对 wi 做一次贡献,考虑计算每个数对 wi 的贡献。 显然 i 对 wi 的贡献为{n k},满足 j̸= i 的 j 对 wi 的贡献就是 i 和 j 被划分到同一个集合的方案数,也就是{n−1 k }所以每个 wi 的系数就是{n k}+ (n−1){n−1 k },通过通项O (n) 算第二类斯特林数即可。 时间复杂度 O(n)

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=1e9+7,N=2e5+77;
ll fac[N],unfac[N],sum,ans;
ll power(ll x,ll t)
{
	ll b=1;
	while(t)
	{
		if(t&1) b=b*x%mod;
		t>>=1; x=x*x%mod;
	}
	return b;
}
ll getS(int n,int k)
{
	ll res=0;
	for(int i=0; i<=k; i++)
	{
		ll opt=i&1?-1ll:1ll;
		res=(res+opt*unfac[i]%mod*power(k-i,n)%mod*unfac[k-i]%mod+mod)%mod;
	}
	return res;
}
void init(int n)
{
	fac[1]=1;
	for(int i=2; i<=n; i++) fac[i]=fac[i-1]*i%mod;
	unfac[n]=power(fac[n],mod-2);
	for(int i=n-1; i>=0; i--) unfac[i]=unfac[i+1]*(i+1)%mod;
}
int main()
{
	
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=1; i<=n; i++)
	{
		ll x;
		scanf("%lld",&x);
		sum=(sum+x)%mod;
	}
	init(n);
	ll ans=(getS(n,k)+(n-1)*getS(n-1,k)%mod)%mod;
	ans=ans*sum%mod;
	printf("%lld",ans);
}
发布了703 篇原创文章 · 获赞 392 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/Eric1561759334/article/details/99281781