『容斥原理和多重集組合數』


<更新提示>

<第一次更新>


<正文>

容斥原理

\(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;
}

<后記>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM