题目大意:求多重集合的组合数, \(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. spark map和mapPartitions的区别

    package dayo1 import org.apache.spark.{SparkConf, SparkContext} import scala.collection.mutable.Arra ...

  2. Day02:基本IO操作

    IO流 基本IO与OS 按照流的方向主要分为输入流和输出流. 数据流按照数据单位的不同分为字节流和字符流. 按照功能可以划分节点流和处理流. 节点流与处理流 按照流是否直接与特定的地方(如磁盘.内存. ...

  3. 云计算共享组件--时间同步服务NTP(2)

    一.标准时间讲解 地球分为东西十二个区域,共计 24 个时区 格林威治作为全球标准时间即 (GMT 时间 ),东时区以格林威治时区进行加,而西时区则为减. 地球的轨道并非正圆,在加上自转速度逐年递减, ...

  4. LINQ查询表达式详解(1)——基本语法、使用扩展方法和Lambda表达式简化LINQ查询

    简介 使用线程的主要原因:应用程序中一些操作需要消耗一定的时间,比如对文件.数据库.网络的访问等等,而我们不希望用户一直等待到操作结束,而是在此同时可以进行一些其他的操作.  这就可以使用线程来实现. ...

  5. rabbitmq死信队列消息监听

    #邮件通知并发送队列消息#!/bin/bash maillog="/var/log/mq.maillog" message_file="/tmp/mq_message&q ...

  6. (4.16)mysql备份还原——物理备份之XtraBackup实践

    关键词:XtraBackup实践,物理备份,xtrabackup备份,innobackupex备份 [1]如何使用? [3]系列:innobackupex --help |more [4]系列:xtr ...

  7. python进程之间的通信——Queue

    我们知道进程之间的数据是互不影响的,但有时我们需要在进程之间通信,那怎么办呢? 认识Queue 可以使用multiprocessing模块的Queue实现多进程之间的数据传递,Queue本身是一个消息 ...

  8. python-redis缓存-pool

    #连接池 import redis pool=redis.ConnectionPool(host='192.168.71.140', port=6379) r = redis.Redis(connec ...

  9. CentOS 7 关闭Selinux

    临时关闭: [root@localhost ~]# getenforceEnforcing [root@localhost ~]# setenforce 0[root@localhost ~]# ge ...

  10. Robot Framework(三)项目实践出现的问题以及解决方法

    导航: 1.元素定位失败 2.系统自带的确认弹窗 3.ElementNotVisibleException: Message: element not visible 1.元素定位失败(使用frame ...