容斥原理--------Devu和鲜花

Devu有N个盒子,第i个盒子中有AiAi枝花。
同一个盒子内的花颜色相同,不同盒子内的花颜色不同。
Devu要从这些盒子中选出M枝花组成一束,求共有多少种方案。
若两束花每种颜色的花的数量都相同,则认为这两束花是相同的方案。
结果需对109+7109+7取模之后方可输出。
输入格式
第一行包含两个整数N和M。
第二行包含N个空格隔开的整数,表示A1,A2,…,ANA1,A2,…,AN。
输出格式
输出一个整数,表示方案数量对109+7109+7取模后的结果。
数据范围
1≤N≤201≤N≤20,

0≤M≤10140≤M≤1014,

0≤Ai≤10120≤Ai≤1012
输入样例:
3 5
1 3 2

输出样例:
3


```#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 20, mod = 1e9 + 7;
LL A[N];
int down = 1;
int qmi(int a, int k, int p){
 int res = 1;
 while(k){
  if (k & 1)   res = (LL)res * a % p;
  a = (LL)a * a % p;
  k >>= 1;
 }
 return res;
}
int C(LL a, LL b){
 if (a < b)  return 0;
 int up = 1;
 for (LL i = a; i > a - b; i --)  up = i % mod * up % mod;
 return (LL)up * down % mod;
}
int main(){
    LL n, m;
 cin >> n >> m;
 for (int i = 0; i < n; i ++)   cin >> A[i];
 for (int j = 1; j <= n - 1; j ++)   down = (LL)j * down % mod;
 down = qmi(down, mod - 2, mod);
  int res = 0;
 for (int i = 0; i < 1 << n; i ++){
  LL a = m + n - 1, b = n - 1;
  int sign = 1;
  for (int j = 0; j < n; j ++)
    if (i >> j & 1){
     sign *= -1;
     a -= A[j] + 1;
    }
    res = (res + C(a, b) * sign) % mod;
 }
 cout << (res + mod) % mod << endl;
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值