sss

<更新提示>

<第一次更新>


<正文>

容斥原理

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

[left | igcup_{i=1}^nS_i ight |=sum_{i=1}^n|S_i|-sum_{1leq i leq j leq n}|S_icap S_j|+...+(-1)^{n+1}left | igcap_{i=1}^nS_i ight | ]

多重集组合数

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

(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),则对于任意(rleq n),从(S)中取出(r)个元素组成的多重集方案数为

[C_{k-r-1}^{k-1}-sum_{i=1}^kC_{k+r-n_i-2}^{k-1}+sum_{1 leq ileq jleq 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_icap S_j)的方案数为(C_{k+r-n_i-n_j-3}^{k-1})

依次类推,用容斥原理可得至少有一种元素超过限制的方案数为:

[left| igcup_{i=1}^n S_i ight |=sum_{i=1}^kC_{k+r-n_i-2}^{k-1}+sum_{1 leq ileq jleq 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| igcup_{i=1}^n S_i ight | ]

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
",(ans+Mod)%Mod);
    return 0;
}

<后记>

原文地址:https://www.cnblogs.com/Parsnip/p/10742332.html