CodeForces-451E: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
题意:给定N种花,每种花有Fi朵,现在要取M朵花,问有多少种方案。
思路:母函数或者容斥定理,当然,学过数学竞赛的应该知道结论,怎么用容斥定理去做。我更倾向于用母函数去做,虽然方程是一样的,但是感觉后者好理解一些。
母函数 : (1+X^1...+X^f1)*(1+X^1...+Xf2)*...(1+X^1...+X^fn)
=(1-X^(f1+1))*(1-X^(f2+1))...(1-X^(fn+1)) / ((1-X)^N)
=(1-X^(f1+1))*(1-X^(f2+1))...(1-X^(fn+1)) * ((1+X^1+X^2+...)^N)。
对于前面部分((1-X^(f1+1))*(1-X^(f2+1))...(1-X^(fn+1)) )可以枚举O(2^N),然后剩余部分,就是组合问题了,假设前面的和为S,那么就要在后面部分(((1+X^1+X^2+...)^N))拿M-S个,然后根据组合公式,C(M-S+N-1,N-1)。
组合公式那里利用Lucas可以降低复杂度,但是我还是觉得复杂度过不去,O(N * 2^N *Mod *LogMod )可能是数据水了,然后只花了900ms。
容斥定理:假设没有限制F,则得到ans=C(M+N-1,N-1)。但是有限制后有的会超过Fi,枚举超过的那几个,blabla,大概是这个方向啦,具体的我也没有细究。
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int Mod=1e9+;
int N; ll M,f[],ans;
ll qpow(ll a,ll x)
{
ll res=; a%=Mod;
while(x){
if(x&1LL) res=res*a%Mod;
a=a*a%Mod;
x>>=;
} return res;
}
ll Com(ll a,ll b)
{
ll res=,fm=,fz=;
if(a-b<b) b=a-b;
for(int i=;i<=b;i++){
fm=fm*i%Mod;
fz=fz*(a-i+)%Mod;
}
res=fz*qpow(fm,Mod-)%Mod;
return res;
}
ll Lucas(ll a,ll b)
{
if(b==) return ;
return Lucas(a/Mod,b/Mod)*Com(a%Mod,b%Mod)%Mod;
}
void solve()
{
for(int i=;i<(<<N);i++){
ll sig=,sum=M;
for(int j=;j<N;j++){
if((<<j)&i) sum-=(f[j]+),sig*=-;
}
if(sum<) continue;
ans=(ans+sig*Lucas(N+sum-,N-))%Mod;
}
}
int main()
{
scanf("%d%lld",&N,&M);
for(int i=;i<N;i++)
scanf("%lld",&f[i]);//,sum+=f[i];
solve();
printf("%lld\n",(ans%Mod+Mod)%Mod);
return ;
}
CodeForces-451E:Devu and Flowers (母函数+组合数+Lucas定理)的更多相关文章
- Codeforces 451E Devu and Flowers【容斥原理+卢卡斯定理】
题意:每个箱子里有\( f[i] \)种颜色相同的花,现在要取出\( s \)朵花,问一共有多少种颜色组合 首先枚举\( 2^n \)种不满足条件的情况,对于一个不被满足的盒子,我们至少拿出\( f[ ...
- Codeforces 451E Devu and Flowers(容斥原理)
题目链接:Codeforces 451E Devu and Flowers 题目大意:有n个花坛.要选s支花,每一个花坛有f[i]支花.同一个花坛的花颜色同样,不同花坛的花颜色不同,问说能够有多少种组 ...
- codeforces 451E. Devu and Flowers 容斥原理+lucas
题目链接 给n个盒子, 每个盒子里面有f[i]个小球, 然后一共可以取sum个小球.问有多少种取法, 同一个盒子里的小球相同, 不同盒子的不同. 首先我们知道, n个盒子放sum个小球的方式一共有C( ...
- CodeForces - 451E Devu and Flowers (容斥+卢卡斯)
题意:有N个盒子,每个盒子里有fi 朵花,求从这N个盒子中取s朵花的方案数.两种方法不同当且仅当两种方案里至少有一个盒子取出的花的数目不同. 分析:对 有k个盒子取出的数目超过了其中的花朵数,那么此时 ...
- Codeforces 451E Devu and Flowers(组合计数)
题目地址 在WFU(不是大学简称)第二次比赛中做到了这道题.高中阶段参加过数竞的同学手算这样的题简直不能更轻松,只是套一个容斥原理公式就可以.而其实这个过程放到编程语言中来实现也没有那么的复杂,不过为 ...
- codeforces 451E Devu and Flowers
题意:有n个瓶子每个瓶子有 f[i] 支相同的颜色的花(不同瓶子颜色不同,相同瓶子花视为相同) 问要取出s支花有多少种不同方案. 思路: 如果每个瓶子的花有无穷多.那么这个问题可以转化为 s支花分到 ...
- uoj86 mx的组合数 (lucas定理+数位dp+原根与指标+NTT)
uoj86 mx的组合数 (lucas定理+数位dp+原根与指标+NTT) uoj 题目描述自己看去吧( 题解时间 首先看到 $ p $ 这么小还是质数,第一时间想到 $ lucas $ 定理. 注意 ...
- 【BZOJ-4591】超能粒子炮·改 数论 + 组合数 + Lucas定理
4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 95 Solved: 33[Submit][Statu ...
- [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)
题目链接:http://acm.swust.edu.cn/problem/0247/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
随机推荐
- mongodb客户端连接mongodb server
import pymongo import sys import os sys.path.append(os.path.split(os.path.realpath(__file__))[0]+&qu ...
- [Istioc]Istio部署sock-shop时rabbitmq出现CrashLoopBackOff
因Istio官网自带的bookinfo服务依赖关系较少,因此想部署sock-shop进行进一步的实验. kubectl apply -f <(istioctl kube-inject -f so ...
- [POJ1984]Navigation Nightmare
[POJ1984]Navigation Nightmare 试题描述 Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
- poj1091:跳蚤【容斥原理】
题目大意:中文题就不翻译了 思路:假设跳蚤选择X1个第一张卡片,X2个第二张卡片...Xn个第n张卡片,Xn+1张写着m的卡片,那么就可以列出方程:a1*X1+a2*X2+…+an*Xn+m*X(n+ ...
- 使用 Apache Lucene 和 Solr 4 实现下一代搜索和分析
使用 Apache Lucene 和 Solr 4 实现下一代搜索和分析 使用搜索引擎计数构建快速.高效和可扩展的数据驱动应用程序 Apache Lucene™ 和 Solr™ 是强大的开源搜索技术, ...
- new String()理解
public static void main(String[] args){ String a=new String("ddy"); String b=new String(&q ...
- openstack DVR的AIO 问题
问题描述 : 创建public 网络,创建路由器,并且把路由器的gateway 设置指向网络后有下面几种错误 路由器对应的linux network namespace 建立起来了,但是里面并没有对应 ...
- 【转】c++内存管理学习纲要
http://blog.csdn.net/zhanghefu/article/details/5003407 转自:http://blog.csdn.net/wdzxl198/article/deta ...
- linux动态库的种种要点
linux下使用动态库,基本用起来还是非常easy.但假设我们的程序中大量使用动态库来实现各种框架/插件,那么就会遇到一些坑,掌握这些坑才有利于程序更稳健地执行. 本篇先谈谈动态库符号方面的问题. 測 ...
- Visual Studio Visual assistant注释也做拼写检查怎么办
1 打开Visual Assistant 2 在Advanced中找到Underlines,取消勾选"Underline spelling errors in comments and ...