这题又是容斥原理,最近各种做容斥原理啊。当然,好像题解给的不是容斥原理的方法,而是用到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. AngularJs Cookie 的使用

    最新在学习 AngularJs ,发现网上很难搜到 AngularJs.Cookie 教程, 就自己写篇博客,希望能帮到刚学的人. 废话不多说上代码 首先要引用 angular-cookies.js ...

  2. eclipse优化与标准化记录

    1.文件使用UTF-8格式: 2.取消js验证: 3.设置java文件模板

  3. C#中List<T>对象的深度拷贝问题

    一.List<T>对象中的T是值类型的情况(int 类型等) 对于值类型的List直接用以下方法就可以复制: List<T> oldList = new List<T&g ...

  4. Play 内置模板标签(1.2.3版本)http://www.anool.net/?p=617

    a标签: 用来插入一个连接到控制器方法的html link.如下: #{a @Application.logout()}Disconnect#{/a}模板内容被解析后变成: <a href=&q ...

  5. CSS通过边框border-style来写小三角

    <!DOCTYPE html> /*直接复制代码即可在浏览器验证*/ <html> <head lang="en"> <meta char ...

  6. 在 SharePoint 2013 中配置 Office Web Apps

    原文发布于 2012 年 7 月 23 日(星期一) 如您所知或您即将知道,SharePoint 2013 中的 Office Web Apps 不再是 SharePoint 场中的服务应用程序.相反 ...

  7. C安全编码--整数理解

    建议和规则 建议: 理解编译器所使用的数据模型 使用rsize_t或size_t类型表示所有表示对象长度的整数值 理解整数转换规则 使用安全的整数库 对来自不信任来源的整数值实行限制 如果输入函数无法 ...

  8. mod_slotmem mod_manager mod_proxy_cluster mod_advertise Permission denied

    restorecon /etc/httpd/modules/mod_slotmem.so

  9. iOS多线程-02-GCD

    简介 GCD(Grand Center Dispatch)是Apple为多核的并行运算提出的解决方案,纯C语言 更加适配多核处理器,且自动管理线程的生命周期,使用起来较为方便 GCD通过任务和队列实现 ...

  10. 这些git技能够你用一年了

    用git有一年了,下面是我这一年来的git使用总结,覆盖了日常使用中绝大多数的场景.嗯,至少是够用一年了,整理出来分享给大家,不明白的地方可以回复交流. git设置关闭自动换行 git config ...