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

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

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #define Mod 1000000007
  7. #define lll __int64
  8. #define ll long long
  9. using namespace std;
  10. #define N 100007
  11.  
  12. ll a[];
  13. ll inv[];
  14.  
  15. ll fastm(ll a,ll b)
  16. {
  17. ll res = 1LL;
  18. while(b)
  19. {
  20. if(b&1LL)
  21. res = (res*a)%Mod;
  22. b >>= ;
  23. a = (a*a)%Mod;
  24. }
  25. return res;
  26. }
  27.  
  28. ll comb(ll n,ll r)
  29. {
  30. if(r > n)
  31. return ;
  32. r = min(r,n-r);
  33. n%=Mod;
  34. ll ans = 1LL;
  35. for(int i=;i<=r-;i++)
  36. {
  37. ans = ans*(n-i)%Mod;
  38. ans = ans*inv[i+]%Mod;
  39. }
  40. return ans;
  41. }
  42.  
  43. int calc(int s)
  44. {
  45. int cnt = ;
  46. while(s)
  47. {
  48. if(s&)
  49. cnt++;
  50. s >>= ;
  51. }
  52. return cnt;
  53. }
  54.  
  55. int main()
  56. {
  57. int n,i;
  58. ll s;
  59. for(i=;i<=;i++)
  60. inv[i] = fastm(i,Mod-);
  61. while(cin>>n>>s)
  62. {
  63. for(i=;i<n;i++)
  64. cin>>a[i];
  65. ll ans = ;
  66. int S = (<<n)-;
  67. for(int state=;state<=S;state++)
  68. {
  69. ll r = s;
  70. int tmp = state;
  71. int cnt = calc(state);
  72. i=;
  73. while(i<n)
  74. {
  75. if(tmp&)
  76. r -= (a[i]+);
  77. tmp >>= ;
  78. i++;
  79. }
  80. if(r < )
  81. continue;
  82. ll cb = comb(n+r-,r);
  83. if(cnt%)
  84. cb = -cb;
  85. ans = (ans+cb+Mod)%Mod;
  86. }
  87. printf("%I64d\n",ans);
  88. }
  89. return ;
  90. }

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. nginx配合modsecurity实现WAF功能

    一.准备工作 系统:centos 7.2 64位.nginx1.10.2, modsecurity2.9.1 owasp3.0 1.nginx:http://nginx.org/download/ng ...

  2. php实现快速排序

    下午练习时候,把经典排序快速排序做了,以下是我的代码 <?php /** * Created by PhpStorm. * User: Administrator * Date: 16-8-29 ...

  3. tomcat下bin文件夹下shell文件分析

    在bin下面有9个sh文件,本文将逐步分析,今天就以version.sh为例 os400=false #uname取操作系统名称 如Linux 如果为OS400的操作系统 特殊处理 case &quo ...

  4. C# Winform 窗体美化

    using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using Sys ...

  5. Get a developer license for windows store app

     获取你的开发者许可证 开发人员许可证是免费.如果您通过使用微软账户获取一个或多个开发人员的许可证,您必须续订他们每隔 30 天 当您运行或调试应用程序第一次在远程机器上或直接连接到您的开发计算机的设 ...

  6. SarePoint Powershell Add user to Group

    $FromGroupnames = "001总经理","010101管理本部" $ToGroupname = "test" $SPWeb = ...

  7. 转:HTTP 1.1与HTTP 1.0的比较

    原文地址:http://blog.csdn.net/elifefly/article/details/3964766 HTTP 1.1与HTTP 1.0的比较 一个WEB站点每天可能要接收到上百万的用 ...

  8. OC数组常见操作

    // 数组存放的内容必须是NSObject对象,数组中存放的内容是有序的 // 数组打印输出在输出台是用()包括起来的 // NSArray 不可变数组 // 1.创建对象 // 初始化方法 NSAr ...

  9. iOS 抽象工厂模式

    iOS 抽象工厂模式 什么是抽象工厂模式 简单了解一下 按照惯例,我们先了解一下什么是抽象工厂模式.抽象工厂模式和工厂方法模式很相似,但是抽象工厂模式将抽象发挥的更加极致,是三种工厂模式中最抽象的一种 ...

  10. Win7下:编译器错误信息: CS0016: 未能写入输出文件

    错误如下: "/"应用程序中的服务器错误. 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS ...