【CF451E】Devu and Flowers
题目大意:求多重集合的组合数, \(N \le 1e14,M \le 20\)。
题解:
考虑容斥原理,具体做法是枚举所有情况,即:枚举子集,第 i 位为 1 表示满足第 i 个条件,正负号采用 sign 进行判断。
对于本题的组合数来说,上指标过大,导致没办法预处理阶乘和逆元进行快速回答,不过下指标很小,可以按照定义进行枚举计算。
代码如下
#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef long long LL;
LL fpow(LL a,LL b,LL c){
LL ret=1%c;
for(;b;b>>=1,a=a*a%c)if(b&1)ret=ret*a%c;
return ret;
}
LL C(LL n,LL m){
if(n<m)return 0;
m=min(m,n-m);
LL ret=1,up=1,down=1;
for(int i=1;i<=m;i++){
up=up*(n-i+1)%mod;
down=down*i%mod;
}
down=fpow(down,mod-2,mod);
ret=ret*up%mod*down%mod;
return ret;
}
LL Lucas(LL n,LL m){
if(n<mod&&m<mod)return C(n,m);
return C(n%mod,m%mod)*Lucas(n/mod,m/mod)%mod;
}
LL f[21],s,ans;
int n;
int main(){
scanf("%d%lld",&n,&s);
for(int i=1;i<=n;i++){
scanf("%lld",&f[i]);
}
for(int i=0;i<1<<n;i++){
LL tot=s,sign=1;
for(int j=1;j<=n;j++)
if(i>>(j-1)&1){
tot-=(f[j]+1);
sign=-sign;
}
if(tot<0)continue;
ans=(ans+Lucas(tot+n-1,n-1)*sign)%mod;
}
printf("%lld\n",(ans+mod)%mod);
return 0;
}
【CF451E】Devu and Flowers的更多相关文章
- 【Codeforces 258E】 Devu and Flowers
[题目链接] http://codeforces.com/contest/451/problem/E [算法] 容斥原理 [代码] #include<bits/stdc++.h> usin ...
- 【HDU-4614】Vases and Flowers(线段树双查询)
11946317 2014-10-23 09:08:28 Accepted 4614 437MS 2348K rid=11946317" target="_blank" ...
- CF451E Devu and Flowers 解题报告
CF451E Devu and Flowers 题意: \(Devu\)有\(N\)个盒子,第\(i\)个盒子中有\(c_i\)枝花.同一个盒子内的花颜色相同,不同盒子的花颜色不同.\(Devu\)要 ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- CF451E Devu and Flowers(容斥)
CF451E Devu and Flowers(容斥) 题目大意 \(n\)种花每种\(f_i\)个,求选出\(s\)朵花的方案.不一定每种花都要选到. \(n\le 20\) 解法 利用可重组合的公 ...
- 【81.82%】【codeforces 740B】Alyona and flowers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- 【Henu ACM Round#20 D】 Devu and Partitioning of the Array
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 一开始所有的数字单独成一个集合. 然后用v[0]和v[1]记录集合的和为偶数和奇数的集合它们的根节点(并查集 然后先让v[0]的大小 ...
- BZOJ1101 [POI2007]Zap 和 CF451E Devu and Flowers
Zap FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得到 ...
随机推荐
- Python知识点面试题
一. DB类 索引相关: 1. mysql索引种类 2. 什么是覆盖索引? MySQL目前主要有以下几种索引类型:1.普通索引2.唯一索引3.主键索引4.组合索引:遵循最左前缀规则5.全文索引 其他: ...
- Opencv-python3.3版本安装
因为目前为止(2019.11.17)opencv最新版本为4.1,因此直接pip install opencv-python的话,无法安装想要的版本(老师推荐3.X) 上清华镜像查找opencv-py ...
- 将多个jar包重新打包成一个jar包
我介绍的方法是使用java命令来操作的,所以首先的安装jdk,这个就自己搞定吧. 提取jar包 为了将多个jar包打包成一个jar包,首先要将每个jar包的内容提取出来放到一个文件夹下,具体的操作命令 ...
- tourist's modular arithmetic class
#include <bits/stdc++.h> using namespace std; template <typename T> T inverse(T a, T m) ...
- redis配置文档细节问题
在window10环境下,redis的.conf配置文件在使用时,不可以有多余的空白符.比如为了对其在配置的前方添加两个空格. 这么做会导致redis-server使用这个配置文件的时候无法正常启动, ...
- Django中ORM操作提升性能
提升orm操作性能注意的点 优化一:尽量不查对象,能用values就是用values 直接使用对象查询的结果是5条sql语句 def youhua(request): # 使用对象查 obj_list ...
- idea 新建maven项目时,避免每次都需要指定自己的maven目录
01 .File->Other Settings -> Settings for New Project 02. 将Maven home directory目录修改成我们自己安装Maven ...
- RabbitMQ 示例-生产者-消费者-direct-topic-fanout
这是生产者和消费者2个项目, 包含 direct,topic,fanout模式下的消费,springboot + rabbitmq 代码地址:https://github.com/duende99/R ...
- 如何同步多个 git 远程仓库
请看 -> 如何同步多个 git 远程仓库
- MySQL索引详解(优缺点,何时需要/不需要创建索引,索引及sql语句的优化)
一.什么是索引? 索引是对数据库表中的一列或多列值进行排序的一种结构,使用索引可以快速访问数据库表中的特定信息. 二.索引的作用? 索引相当于图书上的目录,可以根据目录上的页码快速找到所需的内容,提 ...