CF 题目集锦 PART 4 #258 div 2 E

来源:互联网 发布:exe反编译成c语言 编辑:程序博客网 时间:2024/05/11 16:42

【#258 div 2 E. Devu and Flowers】


【原题】

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 ≤ 200 ≤ 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 31 3
output
2
input
2 42 2
output
1
input
3 51 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个品种的花,要取出S种。每一种花没有区别,但最多只能取出Fi朵。求方案数。

【分析】S很大,但是N只有20。我们很容易想到用容斥跑2^20。

【预备知识】lucas定理:C(N,M)=C(N/P,M/P)*C(N%P,M%P)。缺点是当P很大的时候仍然没用。

本题中C(N,M)中的M很小可以暴力做,而过大的N可以用lucas优化。

(为什么现在想想直接暴力求好像也可以啊)

【题解】我们可以转化成数论公式:

设每种话取Ai朵,则就是求A1+A2+A3+...+AN=S的非负整数解的个数。(其中0<=Ai<=S)

正整数解可以用隔板法,即OOOOOO...OO=S(O的个数就是S),然后插入N-1个板,就是C(S-1,N-1)

非负整数解就先在每个A上都加1,S也加N。这样转化为正整数解个数,就是C(S+N-1,N-1)


但是其实这是不对的,因为有一种情况某个或几个Ai>fi了。

那么我们先减去有一个Ai>fi的个数,再加上两个Ai>fi的个数...这些可以暴力2^20跑出来。

还有一个问题是如何求有上面的公式中非负整数解的个数,其中存在K个Ai超过FI的。

我们先假设只有一个Ai不符合要求,即fi<Ai<=S。

如果我们把S赋成S-(fi+1),就把Ai化成了0<=Ai<=S的状态。

然后就是上面所说的非负整数解的方法。

同样,如果多个的话,就是把S累计减去那么多。

【代码】

#include<cstdio>#define N 21using namespace std;typedef long long LL;const LL P=1000000007ll;LL f[N],sum,all,sign,ans;int n,i,j;inline LL ni(LL a,LL b){  LL res=1;  for (;b;b>>=1,a=a*a%P) if (b&1) res=res*a%P;  return res%P;}inline LL C(LL n,LL m){  LL res=1,mo=1;  for (int i=1;i<=m;i++)  {    res=res*1ll*(n-m+i)%P;    mo=mo*1ll*i%P;  }  return res*ni(mo,P-2)%P;}inline LL Lucas(LL n,LL m){  if (!m) return 1ll;  return Lucas(n/P,m/P)*C(n%P,m%P)%P;}int main(){  scanf("%d%I64d",&n,&sum);  for (i=1;i<=n;i++) scanf("%I64d",&f[i]);  int cnt=0;  for (i=0;i<(1<<n);i++)  {    sign=1;all=sum;cnt++;    for (j=1;j<=n;j++)      if (i&(1<<(j-1))) sign*=-1,all-=f[j]+1;    if (all<0) continue;    LL temp=sign*Lucas(n+all-1,n-1);    ans+=temp;if (ans<0) ans=(ans%P+P)%P;else ans%=P;  }  printf("%I64d",ans);  return 0;}

0 0