【题解】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. luogu P2949 [USACO09OPEN]工作调度Work Scheduling

    题目描述 Farmer John has so very many jobs to do! In order to run the farm efficiently, he must make mon ...

  2. Java NIO.2 使用Files类遍历文件夹

    在以前的Java版本中,如果要遍历某个文件夹下所有的子文件.子文件夹,需要我们自己写递归,很麻烦. 在Java7以后,我们可以NIO.2中的Files工具类来遍历某个文件夹(会自动递归). 大致用法: ...

  3. python在程序中通过Windows打开文件

    import os os.startfile(r'D:\test_input.html')

  4. html5扫面二维码逻辑

    写在前面 项目中有这样的需求,在android端嵌入的html5应用中,需要扫描二维码,而一般的浏览器是不允许你调用摄像头的.最后时限方式是由app的webview进行扫描,将扫描结果返回,也就是js ...

  5. IOS开发~开机启动&无限后台运行&监听进程

    非越狱情况下实现: 开机启动:App安装到IOS设备设备之后,无论App是否开启过,只要IOS设备重启,App就会随之启动: 无限后台运行:应用进入后台状态,可以无限后台运行,不被系统kill: 监听 ...

  6. 2016.7.12 Table configuration with catalog null, schema public, and table globalpage did not resolve to any tables(疑)

    在eclipse中运行mybatis的generator插件时,出现如下错误提示: Generation Warnings Occured:Table configuration with catal ...

  7. TP框架中多条件筛选

            $pid =I('pid');         $year = I('year');         $productType = I('productType');         ...

  8. Oracle中group by 的扩展函数rollup、cube、grouping sets

    Oracle的group by除了基本使用方法以外,还有3种扩展使用方法,各自是rollup.cube.grouping sets.分别介绍例如以下: 1.rollup 对数据库表emp.如果当中两个 ...

  9. [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15417   Accepted:  ...

  10. 【Python】分析文本split()

    分析单个文本 split()方法,是以空格为分隔符将字符串拆分成多个部分,并将这些部分存储到一个列表中 title = 'My name is oliver!' list = title.split( ...