『容斥原理和多重集组合数』


容斥原理

\(S_1,S_2,...,S_n\)\(n\)个有限集合,\(|S|\)代表集合\(S\)的大小,则有

\[\left | \bigcup_{i=1}^nS_i \right |=\sum_{i=1}^n|S_i|-\sum_{1\leq i \leq j \leq n}|S_i\cap S_j|+...+(-1)^{n+1}\left | \bigcap_{i=1}^nS_i \right |\]

多重集组合数

容斥原理的一个重要运用就是计算多重集组合数。

\(S=\{n_1*a_1,n_2*a_2,...,n_k*a_k\}\)是由\(n_1\)\(a_1\)\(n_2\)\(a_2\)\(...\)\(n_k\)\(a_k\)组成的多重集。设\(n=\sum_{i=1}^kn_k\),则对于任意\(r\leq n\),从\(S\)中取出\(r\)个元素组成的多重集方案数为
\[ C_{k-r-1}^{k-1}-\sum_{i=1}^kC_{k+r-n_i-2}^{k-1}+\sum_{1 \leq i\leq j\leq n}C_{k+r-n_i-n_j-3}^{k-1}-...+(-1)^kC_{k+r-\sum_{i=1}^k n_i-(k+1)}^{k-1} \]

证明:

不考虑\(n_i\)的限制,从\(S\)中任选\(r\)个元素,相当于从多重集\(S=\{\infty*a_1,\infty*a_2,...,\infty*a_k\}\)取出\(r\)个元素,则由组合数的知识可得方案数为\(C_{k+r-1}^{k-1}\)

\(S_i\)为包含至少\(n_i+1\)个元素\(a_i\)的多重集,那么我们先从\(S\)中选\(n_i+1\)\(a_i\),然后任选剩下\(r-n_i-1\)个元素,其方案数为\(C_{k+r-n_i-2}^{k-1}\)

那么先从\(S\)中先选\(n_i+1\)\(a_i\)\(n_j+1\)\(a_j\),再任选剩下\(r-n_i-n_j-2\)个元素,就可以得到\(S_i\cap S_j\)的方案数为\(C_{k+r-n_i-n_j-3}^{k-1}\)

依次类推,用容斥原理可得至少有一种元素超过限制的方案数为:
\[\left| \bigcup_{i=1}^n S_i\right |=\sum_{i=1}^kC_{k+r-n_i-2}^{k-1}+\sum_{1 \leq i\leq j\leq n}C_{k+r-n_i-n_j-3}^{k-1}-...+(-1)^kC_{k+r-\sum_{i=1}^k n_i-(k+1)}^{k-1}\]

那么合法的方案数就是
\[C_{k-r-1}^{k-1}-\left| \bigcup_{i=1}^n S_i\right |\]

Devu and Flowers

Description

Devu wants to decorate his garden with flowers. He has purchased nn boxes, where the ii -th box contains f_{i}fi​ flowers. All flowers in a single box are of the same color (hence they are indistinguishable). Also, no two boxes have flowers of the same color.

Now Devu wants to select exactly ss flowers from the boxes to decorate his garden. Devu would like to know, in how many different ways can he select the flowers from each box? Since this number may be very large, he asks you to find the number modulo (10^{9}+7)(109+7) .

Devu considers two ways different if there is at least one box from which different number of flowers are selected in these two ways.

Input Format

The first line of input contains two space-separated integers nn and ss ( 1<=n<=201<=n<=20 , 0<=s<=10^{14}0<=s<=1014 ).

The second line contains nn space-separated integers f_{1},f_{2},... f_{n}f1​,f2​,... fn​ ( 0<=f_{i}<=10^{12}0<=fi​<=1012 ).

Output Format

Output a single integer — the number of ways in which Devu can select the flowers modulo (10^{9}+7)(109+7) .

Sample Input

3 5
1 3 2

Sample Output

3

解析

多重集组合数模板题,直接利用容斥原理的结论即可。

在实现代码时,可以枚举\(x=1\)\(2^n-1\),若该数字在二进制表示下第\(p_1,p_2,...,p_k\)位为\(1\),则表示上式中的\[(-1)^kC_{k+r-n_{p_1}-n_{p_2}-...-n_{p_k}-(p+1) }^{k-1}\]这一项,由于\(n\)的范围很大,\(m\)的范围很小,所以直接计算组合数,利用费马小定理乘上逆元即可。

\(Code:\)


#include <bits/stdc++.h>
using namespace std;
const long long N=22,Mod=1e9+7;
long long n,s,a[N],inv[N],ans;
inline void input(void)
{
    scanf("%lld%lld",&n,&s);
    for (int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
}
inline long long power(long long a,long long b)
{
    long long res = 1;
    while (b)
    {
        if (1&b)res = res * a % Mod;
        b >>= 1;
        a = a * a % Mod;
    }
    return res;
}
inline void init(void)
{
    for (int i=1;i<=20;i++)
        inv[i] = power(i,Mod-2);
}
inline long long C(long long u,long long d)
{
    if ( u>d || u<0 || d<0 )return 0LL;
    d %= Mod;
    long long res=1;
    for (int i=0;i<u;i++)
        res = res * (d-i) % Mod;
    for (int i=1;i<=u;i++)
        res = res * inv[i] % Mod;
    return res;
}
inline long long Lucas(long long u,long long d)
{
    if (u<=Mod&&d<=Mod)return C(u,d) % Mod;
    else return Lucas(u/Mod,d/Mod) * C(u%Mod,d%Mod) % Mod;
}
inline void solve(void)
{
    for (int i=0;i< 1<<n ;i++)
    {
        long long d = n + s , cnt = 0;
        for (int j=0;j<n;j++)
            if ( i >> j & 1 )
                cnt++ , d -= a[j+1];
        d -= cnt+1;
        if ( 1 & cnt )
            ans = (ans - Lucas(n-1,d)) % Mod;
        else ans = (ans + Lucas(n-1,d)) % Mod;
    }
}
int main(void)
{
    input();
    init();
    solve();
    printf("%lld\n",(ans+Mod)%Mod);
    return 0;
}


转载于:https://www.cnblogs.com/Parsnip/p/10742332.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值