[题解] [CF451E] Devu and Flowers
题面
题解
就是一个求\(\sum_{i= 1}^{n}x _ i = m\)的不重复多重集的个数, 我们可以由容斥原理得到:
\]
数据范围中\(1\leq N\leq 20\)告诉了我们什么?
我们考虑枚举\(x = 0 \sim 2 ^ n - 1\), 设\(x\)在二进制表示下共有\(p\)位为1, 分别是\(i_1, i_2, i_3, \cdots, i_p\), 则这个\(x\)对答案的贡献就是
\]
注意到\(x\)为0时它对答案的贡献为\(C_{n + m - 1}^{n - 1}\)
还是因为\(N\)比较小, 我们可以将\(C_{n+m-1}^{n-1}\)转化为\(P_{n+m-1}^{n-1}/(n-1)!\)
由于\(P_{n+m-1}^{n-1}=(n+m-1)*(n+m-2)*\cdots*((n+m-1)-(n-1)+1)\), 再乘上一个\((n-1)!\)的逆元就可以算出来了, 最后对于每个\(x\)求个和即可
Code
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#define itn int
#define reaD read
#define mod 1000000007
#define int long long
using namespace std;
int n, m, f[22], inv[22], ans;
inline int read()
{
int x = 0, w = 1; char c = getchar();
while(c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * w;
}
int fpow(int x, int y)
{
int res = 1;
while(y)
{
if(y & 1) res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
int lucas(int n, int m)
{
if(n < 0 || m < 0 || n < m) return 0;
n %= mod;
if(!n || !m) return 1;
int res = 1;
for(int i = n; i >= n - m + 1; i--) res = res * i % mod;
res = res * inv[m] % mod;
return res;
}
signed main()
{
n = read(); m = read(); inv[0] = 1;
for(int i = 1; i <= n; i++) f[i] = reaD();
for(int i = 1; i <= 20; i++) inv[i] = fpow(i, mod - 2);
for(int i = 1; i <= 20; i++) inv[i] = inv[i] * inv[i - 1] % mod;
for(int x = 0; x < (1 << n); x++)
{
if(!x) { ans = (ans + lucas(n + m - 1, n - 1)) % mod; continue; }
int num = n + m - 1, cnt = 0;
for(int i = 0; i < n; i++) if((x >> i) & 1) cnt++, num -= f[i + 1] + 1;
if(cnt % 2) ans = ((ans - lucas(num, n - 1)) % mod + mod) % mod;
else ans = (ans + lucas(num, n - 1)) % mod;
}
printf("%lld\n", ans);
return 0;
}
[题解] [CF451E] Devu and Flowers的更多相关文章
- CF451E Devu and Flowers 解题报告
CF451E Devu and Flowers 题意: \(Devu\)有\(N\)个盒子,第\(i\)个盒子中有\(c_i\)枝花.同一个盒子内的花颜色相同,不同盒子的花颜色不同.\(Devu\)要 ...
- CF451E Devu and Flowers(容斥)
CF451E Devu and Flowers(容斥) 题目大意 \(n\)种花每种\(f_i\)个,求选出\(s\)朵花的方案.不一定每种花都要选到. \(n\le 20\) 解法 利用可重组合的公 ...
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- BZOJ1101 [POI2007]Zap 和 CF451E Devu and Flowers
Zap FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得到 ...
- Luogu CF451E Devu and Flowers 题解报告
题目传送门 [题目大意] 有n种颜色的花,第i种颜色的花有a[i]朵,从这些花中选m朵出来,问有多少种方案?答案对109+7取模 [思路分析] 这是一个多重集的组合数问题,答案就是:$$C_{n+m- ...
- CF451E Devu and Flowers(组合数)
题目描述 Devu想用花去装饰他的花园,他已经购买了n个箱子,第i个箱子有fi朵花,在同一个的箱子里的所有花是同种颜色的(所以它们没有任何其他特征).另外,不存在两个箱子中的花是相同颜色的. 现在De ...
- CF451E Devu and Flowers 数论
正解:容斥+Lucas定理+组合数学 解题报告: 传送门! 先mk个我不会的母函数的做法,,, 首先这个题的母函数是不难想到的,,,就$\left ( 1+x_{1}^{1}+x_{1}^{2}+. ...
- CF451E Devu and Flowers
多重集求组合数,注意到\(n = 20\)所以可以用\(2 ^ n * n\)的容斥来写. 如果没有限制那么答案就是\(C(n + s - 1, n - 1)\).对每一个限制依次考虑,加上有一种选多 ...
- CF451E Devu and Flowers (组合数学+容斥)
题目大意:给你$n$个箱子,每个箱子里有$a_{i}$个花,你最多取$s$个花,求所有取花的方案,$n<=20$,$s<=1e14$,$a_{i}<=1e12$ 容斥入门题目 把取花 ...
随机推荐
- 二、maven学习
maven工程运行环境修改 例如maven自带Tomcat6,我们想使用Tomcat7 pom.xml #配置多一个Tomcat7<plugins> <plugin> < ...
- Laravel 查询数据按照时间分组
首先取消严格模式: // config/database.php // 'strict' => true, // 严谨模式注释掉 查询构造器代码: //查询构造器部分代码 })->with ...
- 搭建vue.js 的npm脚手架
1.在cmd中,找到nodeJs安装的路径下,运行 vue -V,查看当前vue版本,如下图所示,表明已经安装过了. 2.没有安装,进行安装.在cmd中,找到nodeJs安装的路径下,运命令行 npm ...
- shell 中执行Oracle查询和执行存储过程
[oracle@master2 test]$ more b.sh #!/bin/sh #数据库地址 ip=192.168.1.250 port sid=orcl username=c##scott p ...
- odoo 关系字段(关联关系)
Many-to-one关联 publisher_id = fields.Many2one(comodel_name= 'res.partner', domain='',context={},ondel ...
- DHCP显示
两种PXE启动芯片 开机显示:Inter® Boot Agent GE V1.2.45或者Intel UNDI PXE2.0 (Build 082):其中UNDI是Universal Network ...
- 注解【Annotation】、反射
注解:Annotation是从JDK5.0开始引入的新技术.Annotation的作用:如果没有注解信息处理流程,则注解毫无意义)- 不是程序本身,可以对程序作出解释.(这一点,跟注释没什么区别)- ...
- STM32WB HSE校准
通过改变RCC_HSECR寄存器中的HSETUNE[5:0]位域的值来校准HSE的输出频率 1.将HSE时钟配置为MCO模式输出到PA8引脚 HAL_RCC_MCOConfig(RCC_MCO1, R ...
- 身份证js正则
/* 根据[中华人民共和国国家标准 GB 11643-1999]中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地址码,八位 ...
- 6.Nginx的session一致性(共享)问题配置方案2
1.利用memcached配置session一致性的另外一种方案tengine的会话保持功能 1.1:Tengine会话保持:通过cookie来实现的 该模块是一个负载均衡模块,通过cookie实现客 ...