其他分享
首页 > 其他分享> > BZOJ4710 [Jsoi2011]分特产 容斥

BZOJ4710 [Jsoi2011]分特产 容斥

作者:互联网

题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4710

题解

本来想去找一个二项式反演的题的,结果被 https://www.cnblogs.com/GXZlegend/p/11407185.html 骗了,给的最后一道题是一个基础容斥的题。

(不过反演的本质就是容斥呢,如果二项式反演的 \(g(n)\) 的 \(n\) 是 \(0\) 的话也就跟最常见的容斥差不多了)

考虑如果钦定有 \(k\) 个同学没有拿到特产,那么特产中剩下的同学就可以随便分了。对于第 \(i\) 个特产,划分给 \(n-k\) 个同学的方案数显然就是插板法一下就没了,\(\binom {a_i+n-k-1}{n-k-1}\)。

然后容斥上一下就可以了。最后答案就是
\[ \sum_{i=0}^n(-1)^i\binom ni \prod_{j=1}^m \binom{a_j+n-i-1}{n-i-1} \]

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I>
inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

const int N = 2000 + 7;
const int P = 1e9 + 7;

int n, m, mxa;
int a[N];

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
    int ans = 1;
    for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
    return ans;
}

int fac[N], inv[N], ifac[N];
inline void ycl(const int &n = ::n) {
    fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = (ll)fac[i - 1] * i % P;
    inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = (ll)(P - P / i) * inv[P % i] % P;
    ifac[0] = 1; for (int i = 1; i <= n; ++i) ifac[i] = (ll)ifac[i - 1] * inv[i] % P;
}
inline int C(int x, int y) {
    if (x < y) return 0;
    return (ll)fac[x] * ifac[y] % P * ifac[x - y] % P;
}

inline void work() {
    ycl(n + mxa);
    int ans = 0;
    for (int i = 0; i <= n; ++i) {
        int f = C(n, i);
        if (i & 1) f = P - f;
        for (int j = 1; j <= m; ++j) f = (ll)f * C(a[j] + n - i - 1, n - i - 1) % P;
        sadd(ans, f);
    }
    printf("%d\n", ans);
}

inline void init() {
    read(n), read(m);
    for (int i = 1; i <= m; ++i) read(a[i]), smax(mxa, a[i]);
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    work();
    fclose(stdin), fclose(stdout);
    return 0;
}

标签:Jsoi2011,const,int,容斥,long,BZOJ4710,inline,define
来源: https://www.cnblogs.com/hankeke/p/BZOJ4710.html