Codeforces Round #258 E Devu and Flowers --容斥原理
这题又是容斥原理,最近各种做容斥原理啊。当然,好像题解给的不是容斥原理的方法,而是用到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 --容斥原理的更多相关文章
- Codeforces Round #258 (Div. 2)[ABCD]
Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...
- Codeforces Round #258 (Div. 2) 小结
A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...
- 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 ...
- Codeforces 451E Devu and Flowers(容斥原理)
题目链接:Codeforces 451E Devu and Flowers 题目大意:有n个花坛.要选s支花,每一个花坛有f[i]支花.同一个花坛的花颜色同样,不同花坛的花颜色不同,问说能够有多少种组 ...
- Codeforces Round #258 (Div. 2) 容斥+Lucas
题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...
- Codeforces Round #258 (Div. 2)
A - Game With Sticks 题目的意思: n个水平条,m个竖直条,组成网格,每次删除交点所在的行和列,两个人轮流删除,直到最后没有交点为止,最后不能再删除的人将输掉 解题思路: 每次删除 ...
- Codeforces Round #258 (Div. 2)-(A,B,C,D,E)
http://blog.csdn.net/rowanhaoa/article/details/38116713 A:Game With Sticks 水题.. . 每次操作,都会拿走一个横行,一个竖行 ...
- Codeforces Round #258 (Div. 2)E - Devu and Flowers
题意:n<20个箱子,每个里面有fi朵颜色相同的花,不同箱子里的花颜色不同,要求取出s朵花,问方案数 题解:假设不考虑箱子的数量限制,隔板法可得方案数是c(s+n-1,n-1),当某个箱子里的数 ...
- codeforces 451E. Devu and Flowers 容斥原理+lucas
题目链接 给n个盒子, 每个盒子里面有f[i]个小球, 然后一共可以取sum个小球.问有多少种取法, 同一个盒子里的小球相同, 不同盒子的不同. 首先我们知道, n个盒子放sum个小球的方式一共有C( ...
随机推荐
- Asp.Net MVC开源论坛中文版
支持多国语言 支持多种数据库,开盖即饮(因为EF支持),无需安装. 积分 等级 权限 角色 标签 Rss 表情 附件 审核 问答 投票 收藏 日志 排行榜与热点 主题,默认Bootstrap响应式 最 ...
- 【GOF23设计模式】解释器模式 & 访问者模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_解释器模式.访问者模式.数学表达式动态解析库式 1.解释器模式Interpreter 2.访问者模式Visitor
- JavaScript 学习—— js获取行间样式和非行间样式
1. 问题引入 <head> <style> #div1{ width:150px; height:200px; position:absolute; left:-150px; ...
- JavaScript基础15——js的DOM对象
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- APP落地页开发中的一些小经验~
在开发日常落地页的时候,每当碰到一些很酷炫的宣传图用css实现很复杂且耗时的时候,一般采取切图然后将其放在页面中,在这个过程中发现<img/>标签中图片下方会有一行小空白,影响了与后一部分 ...
- 一个H5的3D滑动组件实现(兼容2D模式)
起由 原始需求来源于一个项目的某个功能,要求实现3D图片轮播效果,而已有的组件大多是普通的2D图片轮播,于是重新造了一个轮子,实现了一个既支持2D,又支持3D的滑动.轮播组件. 实现思路 刚一开始肯定 ...
- mongodb driver c#语法
Definitions and BuildersThe driver has introduced a number of types related to the specification of ...
- Powershell Remove "Limited Access" - 金大昊(jindahao)
对于有多级web获取getlist会报错:Exception calling “GetList” with “1” argument $SPWeb = Get-SPWeb -Identity http ...
- android 类ios actionsheet效果
1.http://blog.csdn.net/zhaoxy_thu/article/details/17733389 2. https://github.com/ojhariddhish/action ...
- Android 购物车功能的实现
首先,众所周知,ListView是Android最常用的控件,可以说是最简单的控件,也可以说是最复杂的控件. 作为一个Android初级开发者,可能会简单的ListView展示图文信息. 作为一个有一 ...