Devu and Flowers lucas定理+容斥原理
Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contains fi flowers. All flowers in a single box are of the same color (hence they are indistinguishable). Also, no two boxes have flowers of the same color.
Now Devu wants to select exactly s flowers from the boxes to decorate his garden. Devu would like to know, in how many different ways can he select the flowers from each box? Since this number may be very large, he asks you to find the number modulo (109 + 7).
Devu considers two ways different if there is at least one box from which different number of flowers are selected in these two ways.
Input
The first line of input contains two space-separated integers n and s (1 ≤ n ≤ 20, 0 ≤ s ≤ 1014).
The second line contains n space-separated integers f1, f2, ... fn (0 ≤ fi ≤ 1012).
Output
Output a single integer — the number of ways in which Devu can select the flowers modulo (109 + 7).
Example
2 3
1 3
2
2 4
2 2
1
3 5
1 3 2
3
Note
Sample 1. There are two ways of selecting 3 flowers: {1, 2} and {0, 3}.
Sample 2. There is only one way of selecting 4 flowers: {2, 2}.
Sample 3. There are three ways of selecting 5 flowers: {1, 2, 2}, {0, 3, 2}, and {1, 3, 1}.
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
#define mod 1000000007
using namespace std;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
ll qpow(ll a,ll b)
{
ll ans=;
while(b)
{
if(b&)ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=;
}
return ans;
}
ll getc(ll a,ll b)
{
if(a<b)return ;
if(b>a-b)b=a-b;
ll s1=,s2=;
for(ll i=;i<b;i++)
{
s1=s1*(a-i)%mod;
s2=s2*(i+)%mod;
}
return s1*qpow(s2,mod-)%mod;
}
ll lucas(ll n,ll k)
{
if(k==)return ;
return getc(n%mod,k%mod)*lucas(n/mod,k/mod)%mod;
}
int n;
ll s,f[];
ll solve()
{
ll ans=;
for(int i=;i<(<<n);i++)
{
ll sign=,sum=s;
for(int j=;j<n;j++)
{
if(i&(<<j))
{
sum-=f[j]+;
sign*=-;
}
}
if(sum<)continue;
ans+=sign*lucas(sum+n-,n-);
ans%=mod;
}
return (ans+mod)%mod;
}
int main()
{
cin>>n>>s;
for(int i=;i<n;i++)
cin>>f[i];
printf("%lld\n",solve());
return ;
}
Devu and Flowers lucas定理+容斥原理的更多相关文章
- 2018.10.31 bzoj4737: 组合数问题(lucas定理+容斥原理+数位dp)
传送门 这是一道让我重新认识lucaslucaslucas的题. 考虑到lucaslucaslucas定理: (nm)≡(n%pm%p)∗(npmp)\binom n m \equiv \binom ...
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- codeforces 451E. Devu and Flowers 容斥原理+lucas
题目链接 给n个盒子, 每个盒子里面有f[i]个小球, 然后一共可以取sum个小球.问有多少种取法, 同一个盒子里的小球相同, 不同盒子的不同. 首先我们知道, n个盒子放sum个小球的方式一共有C( ...
- Codeforces 451E Devu and Flowers(容斥原理)
题目链接:Codeforces 451E Devu and Flowers 题目大意:有n个花坛.要选s支花,每一个花坛有f[i]支花.同一个花坛的花颜色同样,不同花坛的花颜色不同,问说能够有多少种组 ...
- Codeforces Round #258 E Devu and Flowers --容斥原理
这题又是容斥原理,最近各种做容斥原理啊.当然,好像题解给的不是容斥原理的方法,而是用到Lucas定理好像.这里只讲容斥的做法. 题意:从n个容器中总共取s朵花出来,问有多少种情况.其中告诉你每个盒子中 ...
- BZOJ3129/洛谷P3301方程(SDOI2013)容斥原理+扩展Lucas定理
题意:给定方程x1+x2+....xn=m,每个x是正整数.但是对前n1个数做了限制x1<=a1,x2<=a2...xn1<=an1,同时对第n1+1到n1+n2个数也做了限制xn1 ...
- 【bzoj3782】上学路线 dp+容斥原理+Lucas定理+中国剩余定理
题目描述 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不能通过这些路口.小C喜欢走最短的路径到达目的 ...
- Codeforces 451 E. Devu and Flowers(组合数学,数论,容斥原理)
传送门 解题思路: 假如只有 s 束花束并且不考虑 f ,那么根据隔板法的可重复的情况时,这里的答案就是 假如说只有一个 f 受到限制,其不合法时一定是取了超过 f 的花束 那么根据组合数,我们仍然可 ...
- Lucas定理及其应用
Lucas定理这里有详细的证明. 其实就是针对n, m很大时,要求组合数C(n, m) % p, 一般来说如果p <= 10^5,那么就能很方便的将n,m转化为10^5以下这样就可以按照乘法逆元 ...
随机推荐
- java IO流 复制图片
(一)使用字节流复制图片 //字节流方法 public static void copyFile()throws IOException { //1.获取目标路径 //(1)可以通过字符串 // St ...
- spring实现模板文件下载
前台 <form id="batchModel0" method="post" action="/common/download-file&qu ...
- 调用wsdl接口,参数是xml格式
1.最近太累了,好困.闲话少许直奔主题吧.上代码 try{ String wsurl = "http://172.16.16.236:9999/xxx/ws/WSService?wsdl&q ...
- 手写一套迷你版HTTP服务器
本文主要介绍如何通过netty来手写一套简单版的HTTP服务器,同时将关于netty的许多细小知识点进行了串联,用于巩固和提升对于netty框架的掌握程度. 服务器运行效果 服务器支持对静态文件css ...
- Spark学习之基于MLlib的机器学习
Spark学习之基于MLlib的机器学习 1. 机器学习算法尝试根据训练数据(training data)使得表示算法行为的数学目标最大化,并以此来进行预测或作出决定. 2. MLlib完成文本分类任 ...
- RSA js加密 java解密
1. 首先你要拥有一对公钥.私钥: ``` pubKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1gr+rIfYlaNUNLiFsK/Kn ...
- Python3.0 操作MySQL数据库执行SQL语句
py3不支持MySQLdb,需要导入pymysql模块 # coding: utf-8 # Team : Quality Management Center # Author:Carson # Dat ...
- java网络编程_IP地址
InetAddress类,此类表示Internet协议(IP)地址.具体使用方法查看文档:https://docs.oracle.com/en/java/javase/11/docs/api/java ...
- win下配置qt creator 能够执行c/c++
首先需要相关包共四个: qt-win-opensource-4.8.5-mingw.exe qt-creator-windows-opensource-2.8.1.exe MinGW-gcc440_1 ...
- 散列的键值对没初始化时不要用print打印此值,不要用 . 操作符去连接打印 这个值。
31 delete $vertical_alignment{$anonymous}; 32 print $vertical_alignment{$anonymous}."\n&quo ...