[题解] [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$ 容斥入门题目 把取花 ...
随机推荐
- 适配方案(二)之PC端适配
PC端 特点 PC端的屏幕具备以下特点: 屏幕大小一般是大于 13.3英寸 用户会经常拖拉浏览器的大小 原因 正是因为 PC端上的浏览器大小会经常被改变,而且改变的范围还很大,用户会全屏浏览器,用户也 ...
- C++ STL用法总结(持续更新)
Vector 动态数组 https://www.cnblogs.com/zhonghuasong/p/5975979.html lower_bound&&upper_bound htt ...
- vue中修改数组,dom未更新的问题
vue中我们会频繁操作各种数据,但有时候发现修改完数据以后,dom并未更新? 比如有一个数组对象: obj = [{'name': 'joy'},{'name': 'bowen'}] 我要循坏插入某个 ...
- 帝国cms 从数据库删除端口
phome_enewsmoreport 这个表控制着帝国cms多端口.
- 阿里P8架构师谈:MySQL慢查询优化、索引优化、以及表等优化总结
更多内容:https://www.toutiao.com/i6599796228886626829/?tt_from=weixin&utm_campaign=client_share& ...
- 【转】bitbake 笔记
原文 http://blog.csdn.net/xiaofeng_yan/article/details/6757725 1 当你已经编完一个系统比如sato映像,在编一个meta-toolchain ...
- BLE 5协议栈-通用属性规范层(GATT)
文章转载自:http://www.sunyouqun.com/2017/04/page/2/ 通用属性规范GATT(Generic Attribute Profile)将ATT层定义的属性打包成不同的 ...
- python日志分割(转)
按文件大小分割:RotatingFileHandler 按时间分割:TimedRotatingFileHandler import logging from logging.handlers impo ...
- python 时间对应计算
import re import time def parse_time(date): if re.match('刚刚', date): date = time.strftime('%Y-%m-%d ...
- CF981F 二分+Hall定理
对于一个二分的答案 假设存在一个点集使得不满足Hall定理 题中给定的信息说明 左边每个点对应的右边点是一个区间 如果当前点集对应的右边区间是若干个不相交的区间组成的话说明我们还可以找到一个更小的点集 ...