题目大意:求多重集合的组合数, \(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的更多相关文章

  1. 【Codeforces 258E】 Devu and Flowers

    [题目链接] http://codeforces.com/contest/451/problem/E [算法] 容斥原理 [代码] #include<bits/stdc++.h> usin ...

  2. 【HDU-4614】Vases and Flowers(线段树双查询)

    11946317 2014-10-23 09:08:28 Accepted 4614 437MS 2348K rid=11946317" target="_blank" ...

  3. CF451E Devu and Flowers 解题报告

    CF451E Devu and Flowers 题意: \(Devu\)有\(N\)个盒子,第\(i\)个盒子中有\(c_i\)枝花.同一个盒子内的花颜色相同,不同盒子的花颜色不同.\(Devu\)要 ...

  4. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  5. CF451E Devu and Flowers(容斥)

    CF451E Devu and Flowers(容斥) 题目大意 \(n\)种花每种\(f_i\)个,求选出\(s\)朵花的方案.不一定每种花都要选到. \(n\le 20\) 解法 利用可重组合的公 ...

  6. 【81.82%】【codeforces 740B】Alyona and flowers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

    Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...

  8. 【Henu ACM Round#20 D】 Devu and Partitioning of the Array

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 一开始所有的数字单独成一个集合. 然后用v[0]和v[1]记录集合的和为偶数和奇数的集合它们的根节点(并查集 然后先让v[0]的大小 ...

  9. BZOJ1101 [POI2007]Zap 和 CF451E Devu and Flowers

    Zap FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得到 ...

随机推荐

  1. 关于组播数据包“发不出去",c#无法接收

    问题一:发不出去 最近做一个小东西改进方案需要用到组播,简单来说就是我先作为服务器端组播发送设备编号,然后组播成员作为客户端接收消息后先确认对方是不是在呼叫我.是的话就返回一个消息,这样我服务器端就可 ...

  2. PJzhang:网络数据单位小比特

    猫宁!!! 参考链接:https://blog.csdn.net/qq_38880380/article/details/79887704 https://www.cnblogs.com/Flycho ...

  3. 论文阅读 | Generating Fluent Adversarial Examples for Natural Languages

    Generating Fluent Adversarial Examples for Natural Languages   ACL 2019 为自然语言生成流畅的对抗样本 摘要 有效地构建自然语言处 ...

  4. MessageBox显示位置

    假设存在2个窗口类CImDlg与CChatDlg,如果希望MessageBox跟随CChatDlg,方法是 CChatDlg *pDlg = xxx; pDlg->MessageBox();

  5. Momentum Contrast for Unsupervised Visual Representation Learning

    Momentum Contrast for Unsupervised Visual Representation Learning 一.Methods Previously Proposed 1. E ...

  6. [转载]汇编语言assume伪指令的作用

    原文:https://blog.csdn.net/u010234808/article/details/38366943 摘出关键部分: 编写程序,是写给编译软件的.由编译软件,编译成机器码,再去控制 ...

  7. kubernetes基本了解

    初识Kubernetes----k8s以及功能 kubernetes是由google公司开发的容器集群管理系统.采用go语言开发.也称为k8s,原因为k后面直到s这中间有8个字母,所以叫k8s.它主要 ...

  8. resulting in duplicate entry '1' for key 'primary'

    现在有一个标签表,里面已经填入了一些数据了,想把主键生成策略改成自增的: ALTER TABLE `tags` CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUT ...

  9. appium 自动化测试环境搭建

    最近再学习appium,把学习的过程记录下来,以防止到时候 换个电脑就不知道这么安装搭建appium环境了. 环境搭建: 0.JDK环境是必备的,这里大家自行百度,   1.安装 node 环境,前辈 ...

  10. QQ恶搞 - 让艾特你的人语无伦次

    效果图: 实现过程: 代码: ‮ 将上面的代码复制添加到你的群名片后面即可. 原理解析: 这个代码是一个Unicode控制字符 - RLO,它可以控制在它后面的所有文本都已倒序的方式显示.在qq群艾特 ...