CF451E Devu and Flowers(容斥)

题目大意

\(n\)种花每种\(f_i\)个,求选出\(s\)朵花的方案。不一定每种花都要选到。

\(n\le 20\)

解法

利用可重组合的公式。

不考虑\(f_i\)的限制,直接可重组合的方案是,意思是从可以重复的\(n\)个元素中取出\(r\)个的个数。注意,根据定义,此时\(r\)种每个都要选。

\[f(s,r)={s+r-1 \choose r-1}
\]

考虑限制怎么办,我们先容斥。

我们可以钦定某些花选择了\(f_i+1\)次,代表这个花选出不合法的了。

那么为什么不是钦定\(f_i+0,2 \dots233666\dots \infin\) 呢?

是因为,我们钦定这种花选择了\(f_i+1\)后,就保证这种花超过限制了。

此时可重组合的公式仍然可以选择\(i\)号花,所以考虑到了\(i\)号花选择了\(\ge f_i+1\)的情况。

所以我们钦定\(f_i+1\)朵花就好了。

根据容斥原理,所有花不超过限制的方案数为

\[\Sigma_{t\subseteq S} (-1)^{|t|}f(s-\Sigma_{x\in t}(x_i+1)+r-1,r-1)
\]

//@winlere
#include<bits/stdc++.h>
#define int long long
using namespace std; typedef long long ll;
template < class ccf > inline ccf qr(ccf ret){ ret=0;
register char c=getchar();
while(not isdigit(c)) c=getchar();
while(isdigit(c)) ret=ret*10+c-48,c=getchar();
return ret;
}inline int qr(){return qr(1);}
const int maxn=25;
const ll mod=1e9+7;
inline ll Pow(ll base,ll p){
base%=mod;
register ll ret=1;
for(;p;p>>=1,base=base*base%mod)
if(p&1) ret=ret*base%mod;
return ret;
}
ll data[maxn],s,ans,inv[maxn]={1},jie[maxn]={1};
int n; inline ll C(const ll&n,const ll&m){
if(n<m||m<0||n<0)return 0;
if(n==m)return 1;
register ll ret=inv[m];
for(register ll t=n;t>=n-m+1ll;--t)
ret=t%mod*ret%mod;
return ret;
}
#undef int
int main(){
#define int long long
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
#endif
for(register int t=1;t<maxn;++t)
inv[t]=inv[t-1]*Pow(t,mod-2ll)%mod;
n=qr();s=qr(1ll);ans=C(s+n-1ll,n-1ll);
for(register int t=1;t<=n;++t)
data[t]=qr(1ll);
for(register int t=1,edd=1<<n,cnt=0;t<edd;++t){
ll f=cnt=0,delt;
for(register int i=1;i<=n;++i)
if(t<<1>>i&1)
f+=data[i]+1ll,++cnt;
delt=C(s-f+n-1ll,n-1ll);
if(cnt&1) ans=(ans-delt)%mod,ans=ans<0?ans+mod:ans;
else ans=(ans+delt)%mod;
}
cout<<ans<<endl;
return 0;
}

CF451E Devu and Flowers(容斥)的更多相关文章

  1. Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥

    E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...

  2. CodeForces - 451E Devu and Flowers (容斥+卢卡斯)

    题意:有N个盒子,每个盒子里有fi 朵花,求从这N个盒子中取s朵花的方案数.两种方法不同当且仅当两种方案里至少有一个盒子取出的花的数目不同. 分析:对 有k个盒子取出的数目超过了其中的花朵数,那么此时 ...

  3. CF451E Devu and Flowers 解题报告

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

  4. CF451E Devu and Flowers (组合数学+容斥)

    题目大意:给你$n$个箱子,每个箱子里有$a_{i}$个花,你最多取$s$个花,求所有取花的方案,$n<=20$,$s<=1e14$,$a_{i}<=1e12$ 容斥入门题目 把取花 ...

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

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

  6. CF451E Devu and Flowers

    多重集求组合数,注意到\(n = 20\)所以可以用\(2 ^ n * n\)的容斥来写. 如果没有限制那么答案就是\(C(n + s - 1, n - 1)\).对每一个限制依次考虑,加上有一种选多 ...

  7. CF451E Devu and Flowers 数论

    正解:容斥+Lucas定理+组合数学 解题报告: 传送门! 先mk个我不会的母函数的做法,,, 首先这个题的母函数是不难想到的,,,就$\left (  1+x_{1}^{1}+x_{1}^{2}+. ...

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

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

  9. CF451E Devu and Flowers(组合数)

    题目描述 Devu想用花去装饰他的花园,他已经购买了n个箱子,第i个箱子有fi朵花,在同一个的箱子里的所有花是同种颜色的(所以它们没有任何其他特征).另外,不存在两个箱子中的花是相同颜色的. 现在De ...

随机推荐

  1. [Functional Programming] Read and Transform Values from a State ADT’s State (get)

    Many times we need to access and transform state, either in part or in full, to be used when calcula ...

  2. 使用RAP搭建前端Mock Server

    转载自:<前后端分离--构建前端Mock Server--windows部署rap>http://www.cnblogs.com/dothin/p/5361883.html mock:模拟 ...

  3. 系统重装 Windows_VHD_辅助处理工具说明文档2

    创建一个原始安装的VHD文件 如果采用原始安装的方式,则在WIN7的原始光盘中将install.wim这个文件提取到一个文件夹中备用.如果采用Ghost安装的方式,则将Ghost备份文件提取出来(.G ...

  4. mysql常用命令和函数

    一.DROP IF EXISTS DROP FUNCTION IF EXISTS fun;DROP TABLE IF EXISTS table; 二.数据表1.建立表CREATE TABLE test ...

  5. 为什么要上大四???why

    毕业证        即将要上大四了.近期一直在思考,毕业证对于我有什么作用呢?我从来不忌讳表露自己的观点.哪怕这个观点是错误的. 如今这个观点,想必又要激起无数人对我的责骂吧?        但是毕 ...

  6. 51单片机 | 实现SMC1602液晶屏显示实例

    ———————————————————————————————————————————— LCD1602 - - - - - - - - - - - - - - - - - - - - - - - - ...

  7. 聚类分析算法及SAS实现

    聚类分析是用户细分里面最为重要的工具,而用户细分则是整个精准营销里面的基础. 聚类分析方法分为: 层次法:可分为凝聚式和分列式,适用于观测数比较少的情形 1.凝聚式:将每个观测都归为一类,然后每次都将 ...

  8. centos自动安装镜像脚本

    #!/bin/bash ######################################################################################## ...

  9. Windows下UEFI环境的搭建

    Windows下UEFI环境的搭建 一.环境准备 1. 安装2012及以上VS https://visualstudio.microsoft.com/ 2.下载NASM 2.13.03  http:/ ...

  10. Quartz.Net - Lesson 1: 使用Quartz

    Lesson 1: 使用Quartz 本系列文章是官方3.x文档的翻译,原文地址:https://www.quartz-scheduler.net/documentation/quartz-3.x/t ...