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).
类似XDOJ卡尔的技能II,首先所有的情况是C(n+m-1,n)然后减去花坛中超过f[i]的,加上两个超过,减去三个的。。。
#include <iostream> using namespace std; typedef long long LL; #define MOD 1000000007 LL f[35],n,s; inline LL read() { LL x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } LL Fast_Mod(LL a,LL b,LL p) { LL res = 1,base = a; while (b){ if (b & 1) res = (res * base) % p; base = base * base % p; b >>= 1; } return res; } LL C(LL n,LL m,LL p){ if (n < m) return 0; if (m > n - m) m = n - m; LL s1 = 1,s2 = 1; for (LL i=0;i<m;i++){ s1 = s1 * (n - i) % p; s2 = s2 * (i + 1) % p; } return s1 * Fast_Mod(s2,p-2,p) % p; } LL Lucas(LL n,LL m,LL p){ if (m == 0) return 1; return C(n % p,m % p,p) * Lucas(n / p,m / p,p); } LL solve(){ LL ans = 0; for (int i=0;i<(1<<n);i++){ // cout << i << ans << endl; LL sign = 1,sum = s; for (int j=0;j<n;j++){ if (i & (1 << j)){ sum -= (f[j] + 1); sign *= -1; } } if (sum < 0){ continue; } ans += sign * Lucas(sum + n - 1, n - 1, MOD); ans %= MOD; } return (ans + MOD) % MOD; } int main(){ // freopen("test.in","r",stdin); n=read();s=read(); for(int i=0;i<n;i++) f[i]=read(); printf("%lld ", solve()); }