【题解】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. python画直线

    #!/usr/bin/env python import matplotlib.pyplot as plt import numpy as np #beita = 1 #gama = 0.5 #x:f ...

  2. POJ 3249 Test for Job (dfs + dp)

    题目链接:http://poj.org/problem?id=3249 题意: 给你一个DAG图,问你入度为0的点到出度为0的点的最长路是多少 思路: 记忆化搜索,注意v[i]可以是负的,所以初始值要 ...

  3. Java多线程总结之由synchronized说开去

    更新完毕,结贴,以后有新的想法再开新帖 这几天不断添加新内容,给个大概的提纲吧,方面朋友们阅读,各部分是用分割线隔开了的: synchronized与wait()/notify() JMM与synch ...

  4. xshell登录到CentOS7上时出现“The remote SSH server rejected X11 forwarding request.

    其原因是肯能对openssh版本进行了升级. 解决方法为:         yum install xorg-x11-font* xorg-x11-xauth        /etc/ssh/sshd ...

  5. JVM技术部分总结

    1.JVM内存模型 1.1 JVM内存模型图解 Java虚拟机在执行Java程序的过程中,会把它所管理的内存划分为若干个不同的数据区.这些区域有各自的用途,以及创建和销毁的时间,有的区域随着虚拟机进程 ...

  6. ichartjs 制作的图表

    ichartjs资源包下载:https://files.cnblogs.com/files/xiandedanteng/ichartjs-ichartjs1.2.zip 本例下载地址:https:// ...

  7. TP框架中多条件筛选

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

  8. HDU5294——Tricks Device(最短路 + 最大流)

    第一次做最大流的题目- 这题就是堆模板 #include <iostream> #include <algorithm> #include <cmath> #inc ...

  9. 在linux oracle 10g/11g x64bit环境中,goldengate随os启动而自己主动启动的脚本

    在linux.oracle 10g/11g x64bit环境中,goldengate随os启动而自己主动启动的脚本 背景描写叙述: goldengate安装于/u01/ggs文件夹下 rhel5.5 ...

  10. 【Excle数据透视】如何创建多条件汇总的数据透视表

    创建多条件的数据透视表,源数据如下: 插入数据透视表 根据需求拖拽字段(统计该地区下销售人员销售业绩) 数据透视结果 数据透视表布局的各个区域说明 区域 说明 数值 用于显示汇总值数据 行标签 用于将 ...