CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

时间:2022-12-19 00:21:18

Codeforces Round #258 (Div. 2)

 

E. Devu and Flowers
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contains 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 s 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 (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

The first line of input contains two space-separated integers n and s (1 ≤ n ≤ 20, 0 ≤ s ≤ 1014).

The second line contains n space-separated integers f1, f2, ... fn (0 ≤ fi ≤ 1012).

Output

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

Sample test(s)
Input
2 3
1 3
Output
2
Input
2 4
2 2
Output
1
Input
3 5
1 3 2
Output
3
Note

Sample 1. There are two ways of selecting 3 flowers: {1, 2} and {0, 3}.

Sample 2. There is only one way of selecting 4 flowers: {2, 2}.

Sample 3. There are three ways of selecting 5 flowers: {1, 2, 2}, {0, 3, 2}, and {1, 3, 1}.

题意:有N种花,每种最多f[i]枝,从中选s枝花,问有多少种选法。

题解:隔板法+容斥原理+Lucas定理算大组合数+求逆元

如此难的题,我不懂!我是看 http://hzwer.com/3810.html 学会的。

首先看没有f[i]枝数限制的话,可以用隔板法,N种花选s枝,相当于s个相同的球放到N个箱子有多少种放法。隔板法要求每个箱子至少放1个球,所以我们先增加N个假球来搞隔板法(相当于隔完板把每个盒子去掉一个球,就能算到有空的的情况了),N+s个球有N+s-1个空隙,分N个箱子需要N-1个隔板,种类数有C(N+s-1 , N-1)种。

然后观察有f[i]限制的情况。我们可以假装取超了,假装已经在第i个盒子取了f[i]+1个球,把总球数减去(f[i]+1),用这个总球数可以用C(N'+s-1,N'-1)算出取超了的情况的种类数。

然后可能有0个盒子取超、1个盒子盒子取超、2个盒子取超……等等好多情况,这些情况还有互相重复的,这就要用到容斥原理。

ans=0个超的情况数 - 各种1个超的情况数 +各种2个超的情况数 - 各种3个超的情况数……

 

知道要算什么了,接下来看怎么算。

N<=20,s<=10^14,各种盒子取超的情况可以2^20枚举。算C(N+s-1 - ...    , N-1)就比较难,不可能直接算。

用到Lucas定理:C(n,m)%p=C(n/p,m/p)*C(n%p,m%p),左边继续递归Lucas,右边用逆元来算。

怎么用逆元:C(n,m)不是先算出分子和分母,然后分子除以分母嘛,我们可以当做用分子乘以分母的逆元。分母的逆元可以用超碉的一个定理:

费马小定理a^(p-1)=1(mod p),a为质数

a^(p-2)=a^(-1)(mod p),那么a^(p-2)就是a在modp意义下的逆元。

我们就用快速幂求出分母^(p-2),就是逆元了。

 

用这么多知识才能解E题,我都怕,是时候变碉了。

 

代码:

CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
 1 //#pragma comment(linker, "/STACK:102400000,102400000")
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<map>
 9 #include<set>
10 #include<stack>
11 #include<queue>
12 using namespace std;
13 #define ll long long
14 #define usint unsigned int
15 #define mz(array) memset(array, 0, sizeof(array))
16 #define minf(array) memset(array, 0x3f, sizeof(array))
17 #define REP(i,n) for(i=0;i<(n);i++)
18 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
19 #define RD(x) scanf("%d",&x)
20 #define RD2(x,y) scanf("%d%d",&x,&y)
21 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
22 #define WN(x) printf("%d\n",x);
23 #define RE  freopen("D.in","r",stdin)
24 #define WE  freopen("1biao.out","w",stdout)
25 
26 const ll maxs=1e14;
27 const ll MOD=1e9+7;
28 ll a[22];
29 ll n,s,ans;
30 
31 
32 ll PowerMod(ll a, ll b) {
33     ll tmp = a, ret = 1;
34     while (b) {
35         if (b & 1) ret = ret * tmp % MOD;
36         tmp = tmp * tmp % MOD;
37         b >>= 1;
38     }
39     return ret;
40 }
41 
42 ll calC(ll n,ll m){
43     m=n-m>m?m:n-m;
44     ll up=1,down=1;
45     int i;
46     for(i=1;i<=m;i++){
47         down*=i;
48         down%=MOD;
49         up*=(n-i+1);
50         up%=MOD;
51     }
52     return (up*PowerMod(down,MOD-2))%MOD;
53 }
54 
55 ll Lucas(ll n, ll m) {
56     if(m==0)return 1;
57     return (Lucas(n/MOD, m/MOD)*calC(n%MOD, m%MOD))%MOD;
58 }
59 
60 
61 void attack(ll now,ll sum,ll flag){
62     if(sum<n)return;
63     if(now==n){
64         //printf("%I64d C(%I64d,%I64d)=",flag,sum-1 , n-1);
65         //printf("%I64d\n",Lucas(sum-1,n-1));
66         ans+=flag*Lucas(sum-1 , n-1);
67         ans%=MOD;
68         return;
69     }
70     attack(now+1,sum,flag);
71     attack(now+1,sum-a[now]-1,-flag);
72 }
73 
74 int main() {
75     int i;
76     scanf("%I64d%I64d",&n,&s);
77     REP(i,n) scanf("%I64d",&a[i]);
78     ans=0;
79     attack(0,n+s,1);
80     printf("%I64d\n",((ans%MOD)+MOD)%MOD);
81     return 0;
82 }
View Code