BZOJ4710 [Jsoi2011] specialty inclusion and exclusion points

Topic Portal

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

answer

Had wanted to find a binomial inversion problem, the results are https://www.cnblogs.com/GXZlegend/p/11407185.html cheated, to the last question is a basic question of inclusion and exclusion.

(However, the essence of inclusion and exclusion is the inversion it, if binomial inversion \ (g (n) \) The \ (n \) is \ (0 \) , then have the makings of the most common inclusion and exclusion almost)

Consider if there are Imperial \ (k \) classmates did not get the specialty, the specialty of the rest of the students can be easily divided. For the first \ (I \) a specialty, allocated to \ (NK \) several classmates scheme is clearly look no card method, \ (\ {a_i + NK-Binom. 1. 1-NK} {} \) .

Then on inclusion and exclusion click on it. The last answer is
\ [\ sum_ {i = 0 } ^ n (-1) ^ i \ binom ni \ prod_ {j = 1} ^ m \ binom {a_j + ni-1} {ni-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;
}

Guess you like

Origin www.cnblogs.com/hankeke/p/BZOJ4710.html