BZOJ3622 have nothing to be afraid of the binomial inversion + DP

Topic Portal

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

answer

First, obviously if \ (n - k \) is odd then that is no solution. Otherwise, "candy" than "pills" of a large number of groups should be \ (\ + K FRAC {n-2} \) .

Taking into consideration that just \ (k \) group seeking is not very good, but if you chose \ (k \) group must be "candy" than "pills" Great, this program is a good number of requirements.

First, choose the \ (k \) group must be "candy" large number of programs than the "pills", which can be used to solve a dp. We will sort the candy, and then pretreated \ (s_i \) expressed \ (s_i \) a tablet smaller than the candy. Set \ (dp [i] [j ] \) representing the forward \ (I \) candy chose \ (J \) candy composition is larger than the tablet. When the transfer of the current enumeration this election do not choose it.
\ [Dp [i] [j
] = dp [i - 1] [j] + dp [i - 1] [j - 1] \ cdot (s_i - j + 1) \] Then we let \ (f (k ) \) represents the Imperial \ (i) \ number of large future programs on the "candy" than "pills." Then obviously there is
\ [f (k) = dp
[n] [k] \ cdot (nk)! \] At the same time can be found, if so \ (g (k) \) is exactly \ (i \) for "candy" program number than large "pills", then there is
\ [f (k) = \
sum_ {i = k} ^ n \ binom ik g (i) \] so a direct inversion can be a binomial.


Code below, the time complexity \ (O (^ n-2) \) .

#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 + 9;

int n, k;
int a[N], b[N], s[N], dp[N][N], f[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() {
    if ((n - k) & 1) {
        puts("-1");
        return;
    } else k = (n + k) / 2;
    
    std::sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j) if (b[j] < a[i]) ++s[i];
    dp[0][0] = 1;
    for (int i = 1; i <= n; ++i)
        for (int j = 0; j <= i; ++j) {
            dp[i][j] = dp[i - 1][j];
            if (j) sadd(dp[i][j], (ll)dp[i - 1][j - 1] * (s[i] - j + 1) % P);
        }
        
    ycl();
    for (int i = 0; i <= n; ++i) f[i] = (ll)dp[n][i] * fac[n - i] % P;
    int ans = 0;
    for (int i = k; i <= n; ++i)
        if ((i - k) & 1) sadd(ans, P - (ll)C(i, k) * f[i] % P);
        else sadd(ans, (ll)C(i, k) * f[i] % P);
    printf("%d\n", ans);
}

inline void init() {
    read(n), read(k);
    for (int i = 1; i <= n; ++i) read(a[i]);
    for (int i = 1; i <= n; ++i) read(b[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/BZOJ3622.html