214. Devu and flowers (combinatorial mathematics + tolerance + mod details)

https://www.acwing.com/problem/content/description/216/


Ideas:

First transform it into the number of non-negative integer solutions to solve the indeterminate equation. The solution is the same as that of the m balls of the winter vacation cow guest taking n non-repeated n balls to form a decreasing composition. After adding one to each of them, it becomes a problem of the number of schemes of m+n-1 inserting n-1 baffles in the air. Then take the inverse for each situation, and finally use state pressure to represent the state of inclusion and exclusion.

detail:

When calculating the combination mod alone, the *i above may still blow up LL, so *(i%mod)%mod. Or when passing parameters below, pass m%mod before passing it.

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=30;
typedef long long LL;
const LL mod=1e9+7;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn];
LL chu=1;
LL ksm(LL a,LL k){
    LL res=1;
    while(k>0){
        if(k&1) res=res*a%mod;
        a=a*a%mod;
        k>>=1;
    }
    return res%mod;
}
LL C(LL n,LL m){
    if(n>m) return 0;
    LL res=1;
    for(LL i=m;i>=(m-n+1);i--) res=(res%mod*(i%mod)%mod)%mod;
    ///res%后1e9,i>=1e10,直接炸,所以i要mod,如果这里注意不到,那么单纯51行的cnt%mod后再算也对
    return (res%mod*chu%mod)%mod;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<=n;i++) cin>>a[i];
  for(LL i=1;i<=n-1;i++) chu=(chu%mod*i%mod)%mod;
  chu=ksm(chu,mod-2)%mod;
  LL ans=0;
  for(LL i=0;i<(1LL<<n);i++){
      LL num=1;LL cnt=m+n-1;
      for(LL j=1;j<=n;j++){
         if( (1LL<<(j-1))&i ){
            num*=-1;
            cnt-=(a[j]+1);
         }
      }
      ans=(ans%mod+num*C(n-1,cnt)%mod+mod)%mod;
  }
  cout<<(ans+mod)%mod<<"\n";
return 0;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/115168710