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

    keditor时一个免费的开源编辑器,很多公司在使用(百度编辑器也不错).最近为了做一个客户信息管理系统,在发送邮件模块用到这个编辑器,也算学习一下新的东西. 第一步:下载编辑器 到它的官网下载:ht ...

  2. $(document).ready()即$()方法和window.onload方法的比较

    以浏览器装载文档为例,我们都知道在页面完毕后,浏览器会通过JavaScript为DOM元素添加事件.在常规的JavaScript代码中,通常使用window.onload方法,而在jQuery中,使用 ...

  3. Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法

    Eclipse 出现Some sites could not be found.  See the error log for more detail.错误 解决方法 Some sites could ...

  4. UML类图相关实践

    最近看了下设计模式,其中无可避免会设计很多类图,UML类图对于学习设计模式很重要,关于设计模式,我也会在这里写上一写,这一篇关于UML类图的就先当个铺垫. 1.先上一个简单的类图来简单说明下: 1). ...

  5. 【GOF23设计模式】享元模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_享元模式.享元池.内部状态.外部状态.线程池.连接池 package com.test.flyweight; /** * ...

  6. Vue自定义过滤器

    gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson05 一 自定义过滤器(注册在Vue全局) 注意事项: (1)全局方 ...

  7. ruby 操作数据库语句

    1.多对多 user role u = User.first role = Role.first 插入 u.roles << role u.save 更新 u.roles = [] u.r ...

  8. 关于Activity销毁,而绘制UI的子线程未销毁出现的问题

    项目总结 ----------------------------------------------------------------------------------------------- ...

  9. 【原】visual studio添加现有文件夹的方法

    由于使用版本管理器协调工作,有时同事就直接上传文件夹了,但右键添加现有项时不能添加文件夹的. 在工具栏"项目"下面有个"显示所有文件",选择这个,整个工程的文件 ...

  10. 解决log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader)警告信息的问题

    spring项目经常在启动tomcat时报如下警告信息: log4j:WARN No appenders could be found for logger (org.springframework. ...