【题解】Codeforces 961G Partitions

cf961G

好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到

题意

已知\(U=\{w_i\}\),求:

\[\sum _{S}\sum_{s\in S}|s|\sum_{w \in s} w, S是U的一个k划分
\]

转换1

考虑这个\(|s|\)有点麻烦,稍微思考一下可以发现,我们最后的答案和\(w_i\)的分布没有关系,他们的贡献系数是一样的。答案只和他们的和有关。

转换2

考虑定位某个\(w_i\)对答案产生的贡献\(w_i \times p\),最后让\(\sum w\)乘上\(p\)就是答案。

然后就是喜闻乐见的套路时间了。这类与分配的集合的大小有关的计数题,可以用微扰法求出每一个元素的贡献。

考虑\(|s|\)每增大一,\(p\)就大一。考虑自己对于自己的贡献,这部分贡献就是\(\begin{Bmatrix}n\\k\end{Bmatrix}\)。

然后考虑其他元素对自己的贡献,贡献就是钦定当前选定的一个其他元素和\(w_i\)在同一集合的划分方案数,此时可以用微扰法,先让其他\(n-1\)个元素自己先划分好,然后我在直接加入有\(w_i\)的那个集合,那么一个"其他元素"的贡献就是\(\begin{Bmatrix}n-1\\k\end{Bmatrix}\)。总共有\(n-1\)个其他元素。

所以最后的答案是

\[\sum w \times(\begin{Bmatrix}n\\k\end{Bmatrix}+(n-1)\begin{Bmatrix}n-1\\k\end{Bmatrix})
\]

容斥球斯特林数即可。

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; typedef long long ll;
inline int qr(){
register int ret=0,f=0;
register char c=getchar();
while(c<48||c>57)f|=c==45,c=getchar();
while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
return f?-ret:ret;
}
const int mod=1e9+7;
const int maxn=2e5+5;
int jc[maxn];
int inv[maxn];
int n,s,k,ans;
inline int Pow(const int&base,const int&p){
register int ret=1;
for(register int t=p,b=base%mod;t;t>>=1,b=1ll*b*b%mod)
if(t&1) ret=1ll*ret*b%mod;
return ret;
} inline int c(const int&n,const int&m){
if(n<m)return 0;
return 1ll*jc[n]*inv[m]%mod*inv[n-m]%mod;
} inline int stirlin(const int&n,const int&m){
register int ret=0;
for(register int t=0,delta;t<m;++t){
delta=1ll*c(m,t)*Pow(m-t,n)%mod;
if(t&1)delta=mod-delta;
ret=(ret+delta)%mod;
}
return 1ll*ret*inv[m]%mod;
} int main(){ jc[0]=inv[0]=1;
for(register int t=1;t<maxn;++t)
jc[t]=1ll*jc[t-1]*t%mod,inv[t]=Pow(jc[t],mod-2);
n=qr();k=qr();
for(register int t=1;t<=n;++t)
s=(s+qr()%mod)%mod;
if(s<0)s+=mod;
ans=1ll*s*(stirlin(n,k)+1ll*(n-1)*stirlin(n-1,k)%mod)%mod;
cout<<ans<<endl;
return 0;
}

【题解】Codeforces 961G Partitions的更多相关文章

  1. [Codeforces 961G]Partitions

    Description 题库链接 给你 \(n\) 个不同的元素组成的集合 \(R\) ,每个元素有一个权值 \(w\) .对于一个子集集合 \(S\) ,它的价值为 \(W(S)=|S|\cdot\ ...

  2. [题解] Codeforces Round #549 (Div. 2) B. Nirvana

    Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...

  3. 题解——CodeForces 438D The Child and Sequence

    题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...

  4. [题解][Codeforces]Good Bye 2019 简要题解

    构造题好评,虽然这把崩了 原题解 A 题意 二人游戏,一个人有 \(k_1\) 张牌,另一个人 \(k_2\) 张,满足 \(2\le k_1+k_2=n\le 100\),每张牌上有一个数,保证所有 ...

  5. [题解][Codeforces]Codeforces Round #602 (Div. 1) 简要题解

    orz djq_cpp lgm A 题意 给定一个分别含有 \(\frac n2\) 个左括号和右括号的括号序列 每次可以将序列的一个区间翻转 求一个不超过 \(n\) 次的操作方案,使得操作完之后的 ...

  6. [题解]Codeforces Round #709 (Div. 1, based on Technocup 2021 Final Round) - A. Basic Diplomacy

    [题目] A. Basic Diplomacy [描述] Aleksey有n个朋友,有一个m天的假期,每天都需要一个朋友来陪他.给出每天有空的朋友的编号,要求同一个朋友来的天数不能超过m/2上取整.求 ...

  7. [题解]Codeforces Round #254 (Div. 2) B - DZY Loves Chemistry

    链接:http://codeforces.com/contest/445/problem/B 描述:n种药品,m个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...

  8. [题解]Codeforces Round #254 (Div. 2) A - DZY Loves Chessboard

    链接:http://codeforces.com/contest/445/problem/A 描述:一个n*m的棋盘,有一些格子不能放棋子.现在把黑白棋子往上放,要求放满且相邻格子的棋子颜色不同.输出 ...

  9. [题解] Codeforces Global Round 22 1738 A B C D E F 题解

    很久没rated打过cf的比赛了,这次打得还行,至少进前100了 点我看题 A. Glory Addicts 把类型0的数放进数组a里,类型1的数放进数组b里.如果\(|a|=|b|\),你可以把所有 ...

随机推荐

  1. 2016北京集训测试赛(八)Problem C: 直径

    Solution 一个定理: 把两棵树用一条边练成一棵树后, 树的直径在原来两棵树的四个直径端点中产生. 放到这一题, 我们通过DP先求出大树中以每个点为根的子树中的直径, 再取每棵小树中与其他树有连 ...

  2. CMake的应用(在vision studio2008中去掉 ALL_BUILD 和 ZERO_CHECK)

    from  http://blog.csdn.net/jtop0/article/details/6167432 一般由CMake是 跨平台软件开发和维护过程的 工程构建工具.“在每个系统构建你的工程 ...

  3. Android Retrofit使用教程

    Square公司开源了许多优秀的库,Retrofit就是其中之一. Retrofit是用来简化APP访问服务器API,如果你的服务器使用的使RESTAPI,那么赶紧使用Retrofit吧. 官方的文档 ...

  4. Oracle Forms Project For Students Free Download

    After getting to much requests for a free Oracle Forms and Reports project for students, I am sharin ...

  5. How to Log Users Login and Logout Details Through Oracle Forms

    Log user's login and logout details in to table through Oracle Forms using POST-LOGON and PRE-LOGOUT ...

  6. 对象第复制operator=

    类机制中有默认的对象复制操作符=,自定义对象复制需要注意一个问题,如果有遇到指针指向的资源是需要释放的,这时需要毫不留情释放,否则内存空间的泄露就不可避免.复制操作与拷贝构造函数的参数是一致的,只是在 ...

  7. linux系统故障分析与排查

    在处理Linux系统出现的各种故障时,故障的症状是最先发现的,而导致这以故障的原因才是最终排除故障的关键.熟悉Linux系统的日志管理,了解常见故障的分析与解决办法,将有助于管理员快速定位故障点.“对 ...

  8. vs2012搭建OpenGL环境

    1. 下载glut库 glut库地址为:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip glut全称为:OpenGL ...

  9. Zookeeper demo增删改查

    Zookeeper 的增删改查demo代码 public class SimpleZkClient { private static final String connectString = &quo ...

  10. 2016.6.21 将Eclipse中项目部署到tomcat下

    新建的web项目,各种都配置好,选择run on server之后,发现运行失败,并不能访问需要的网址.而脱离eclipse,将生成的war文件直接放到tomcat的webapp下时,可以正常访问.所 ...