这题又是容斥原理,最近各种做容斥原理啊。当然,好像题解给的不是容斥原理的方法,而是用到Lucas定理好像。这里只讲容斥的做法。

题意:从n个容器中总共取s朵花出来,问有多少种情况。其中告诉你每个盒子中有多少朵花。

分析:其实就是求方程: x1+x2+...+xn = s 的整数解的个数,方程满足: 0<=x1<=a[1], 0<=x2<=a[2]...

设:A1 = {x1 >= a[1]+1} , A2 = {x2 >= a[2]+1} , .... , An = {xn >= a[n]+1}. 全集S = (n+s-1,s)

所以容斥原理可求得答案。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define Mod 1000000007
#define lll __int64
#define ll long long
using namespace std;
#define N 100007 ll a[];
ll inv[]; ll fastm(ll a,ll b)
{
ll res = 1LL;
while(b)
{
if(b&1LL)
res = (res*a)%Mod;
b >>= ;
a = (a*a)%Mod;
}
return res;
} ll comb(ll n,ll r)
{
if(r > n)
return ;
r = min(r,n-r);
n%=Mod;
ll ans = 1LL;
for(int i=;i<=r-;i++)
{
ans = ans*(n-i)%Mod;
ans = ans*inv[i+]%Mod;
}
return ans;
} int calc(int s)
{
int cnt = ;
while(s)
{
if(s&)
cnt++;
s >>= ;
}
return cnt;
} int main()
{
int n,i;
ll s;
for(i=;i<=;i++)
inv[i] = fastm(i,Mod-);
while(cin>>n>>s)
{
for(i=;i<n;i++)
cin>>a[i];
ll ans = ;
int S = (<<n)-;
for(int state=;state<=S;state++)
{
ll r = s;
int tmp = state;
int cnt = calc(state);
i=;
while(i<n)
{
if(tmp&)
r -= (a[i]+);
tmp >>= ;
i++;
}
if(r < )
continue;
ll cb = comb(n+r-,r);
if(cnt%)
cb = -cb;
ans = (ans+cb+Mod)%Mod;
}
printf("%I64d\n",ans);
}
return ;
}

Codeforces Round #258 E Devu and Flowers --容斥原理的更多相关文章

  1. Codeforces Round #258 (Div. 2)[ABCD]

    Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...

  2. Codeforces Round #258 (Div. 2) 小结

    A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...

  3. 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 ...

  4. Codeforces 451E Devu and Flowers(容斥原理)

    题目链接:Codeforces 451E Devu and Flowers 题目大意:有n个花坛.要选s支花,每一个花坛有f[i]支花.同一个花坛的花颜色同样,不同花坛的花颜色不同,问说能够有多少种组 ...

  5. Codeforces Round #258 (Div. 2) 容斥+Lucas

    题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...

  6. Codeforces Round #258 (Div. 2)

    A - Game With Sticks 题目的意思: n个水平条,m个竖直条,组成网格,每次删除交点所在的行和列,两个人轮流删除,直到最后没有交点为止,最后不能再删除的人将输掉 解题思路: 每次删除 ...

  7. Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

    http://blog.csdn.net/rowanhaoa/article/details/38116713 A:Game With Sticks 水题.. . 每次操作,都会拿走一个横行,一个竖行 ...

  8. Codeforces Round #258 (Div. 2)E - Devu and Flowers

    题意:n<20个箱子,每个里面有fi朵颜色相同的花,不同箱子里的花颜色不同,要求取出s朵花,问方案数 题解:假设不考虑箱子的数量限制,隔板法可得方案数是c(s+n-1,n-1),当某个箱子里的数 ...

  9. codeforces 451E. Devu and Flowers 容斥原理+lucas

    题目链接 给n个盒子, 每个盒子里面有f[i]个小球, 然后一共可以取sum个小球.问有多少种取法, 同一个盒子里的小球相同, 不同盒子的不同. 首先我们知道, n个盒子放sum个小球的方式一共有C( ...

随机推荐

  1. 实现跨域请求jsonp方式

    原理:http://madong.net.cn/index.php/2012/12/368/ 调用端: $.getJSON("http://192.168.220.85:8001/esb/a ...

  2. windows下使用makecert命令生成自签名证书

    1.makecert命令路径 C:\Program Files (x86)\Windows Kits\8.1\bin\x64 2.生成一个自签名证书 makecert -r -pe -n " ...

  3. linux下firefox手工安装flash插件

    1. 前往adobe官网,下载flash安装包.下载.tar.gz安装包即可.2. 解压安装包,得到libflashplayer.so文件3. 新建文件夹,~/.mozilla/plugins4. 拷 ...

  4. MVC Html.AntiForgeryToken() 防止CSRF攻击

    转自:http://blog.csdn.net/cpytiger/article/details/8781457 一.MVC中的Html.AntiForgeryToken()是用来防止跨站请求伪造(C ...

  5. inherit与auto

    大家是不是和我一样,在刚开始学习css的时候,在css文件开头是不是经常看到这样的代码: * {margin:0 px; padding:0 px;}  . 在接下来设置颜色字体时在body元素的cs ...

  6. ASP.NET页面动态添加js脚本

    有时我们需要生成自己的JavaScript代码并在运行时动态添加到页面,接下来我们来看一下如何将生成的JavaScript代码动态添加到ASP.NET页面. 为了添加脚本,要将自定义的脚本在一个字符串 ...

  7. Button 对 TreeView1 所有节点的全选

    protected void Button1_Click(object sender, EventArgs e)    {        for (int i = 0; i < this.Tre ...

  8. 关于ApplicationPoolIdentity

    一直以来IIS中的网站默认都是以network service在运行,但是从IIS7开始,默认会以ApplicationPoolIdentity运行. 这个账户比较特殊,是一种虚拟帐户,你无法在计算机 ...

  9. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q107-Q110)

    Question 107You are creating a custom workflow action that will be used in Microsoft SharePoint Desi ...

  10. IOS 杂笔-8(loadView、viewDidLoad、viewWillAppear、viewDidAppear等简介)

    loadView; This is where subclasses should create their custom view hierarchy if they aren't using a ...