1032 [JSOI2011]分特产 隔板法 容斥原理

#include<bits/stdc++.h>
using namespace std;

#define int long long 

const int N = 1e4 + 10, mod = 1e9 + 7;
int inv[N],fac[N] = {1};

int qmi(int a,int b) {
    int res = 1;
    while(b) {
        if(b & 1) res = a * res % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res % mod;
}

int c(int n,int m) {
    if(m == 0 || m == n ) return 1;
    if(m < 0 || m > n ) return 0;
    return fac[n] % mod * inv[m] % mod * inv[n - m] % mod;
}

void pre() {
    for(int i = 1;i< N;i++) {
        fac[i] = fac[i-1] * i % mod;
        inv[i] = qmi(fac[i],mod - 2);
    }
}
int a[N];
signed main() {
    pre();
    
    int n,m;cin>>n>>m;
    
    for(int i = 1;i<=m;i++) {cin>>a[i];}
    
    int ans = 1;
    for(int i = 1;i<=m;i++) {
        ans = (ans * c(a[i] + n - 1,n - 1)) % mod;
    }
    int o = -1;
    for(int i = 1;i<=n;i++) {
        int t = c(n,i) % mod;
        for(int j = 1;j<=m;j++) {
            t = t * c(a[j] + n - i - 1, n - i - 1) % mod;
        }
        ans = (ans % mod + o * t + mod) % mod;
        o *= -1;
    }
    cout<<ans<<endl;
}

 

分析

涉及:隔板法,容斥原理

 

posted @ 2022-07-30 01:34  er007  阅读(20)  评论(0编辑  收藏  举报