answer
- We first consider the case where there is no limit how demand, i.e. remove the n box number s balls embodiment, each box is a separator may not take Method C (n + s-1, n-1)
- Consider then subtract each illegal scheme, which is the number of i boxes take over the program
- Consider the inclusion-exclusion principle, it is easy to think of inclusion and exclusion factor (-1) ^ i, then took the total number of programs - 1 + 2 box box super super ...
- Then find or use Lucas theorem, n and m are as much
Code
1 #include <cstdio> 2 #include <iostream> 3 #define ll long long 4 using namespace std; 5 const ll mo=1e9+7,N=30; 6 int n; 7 ll s,r,f[N]; 8 ll ksm(ll a,ll b) { for (r=1;b;b>>=1,a=a*a%mo) if (b&1) r=r*a%mo; return r; } 9 ll C(ll n,ll m) 10 { 11 if (m>n) return 0; 12 if (m>n-m) m=n-m; 13 ll ans=1ll,fac=1ll; 14 for (int i=1;i<=m;i++) ans=(ans*(n-i+1))%mo,fac=fac*i%mo; 15 fac=ksm(fac,mo-2),ans=ans*fac%mo; 16 return ans; 17 } 18 ll lucas(ll n,ll m) { return !m?1:C(n%mo,m%mo)*lucas(n/mo,m/mo)%mo; } 19 int main() 20 { 21 while (scanf("%d%lld",&n,&s)!=EOF) 22 { 23 ll ans=0; 24 for (int i=0;i<n;i++) scanf("%lld",&f[i]); 25 for (int i=0;i<(1<<n);i++) 26 { 27 int k=1; ll p=s; 28 for (int j=0;j<n;j++) if ((1<<j)&i) k*=-1,p-=f[j]+1; 29 if (p<0) continue; 30 (ans+=k*lucas(p+n-1,n-1)%mo)%=mo; 31 } 32 ans=(ans+mo)%mo,printf("%lld\n",ans); 33 } 34 }