BZOJ3622は二項反転+ DP恐れることは何もありません

トピックポータル

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

問題の解決策

まず、明らか場合\は(N - \ K)が奇数であるが、その解決策ではありません。そうでない場合、グループの多数の"ピル"よりも"キャンディ"がなければならない(\ \ + K FRAC {N-2} \)

考慮すると、そのちょうど\(K \)非常に良いではありませんが、あなたが選択した場合、グループシーク\(K \)グループは偉大「薬」よりも「キャンディ」にする必要があり、このプログラムは、要件のかなりの数です。

まず、選択\を(K \)のグループは、「キャンディ」のDPを解決するために使用することができる「薬」、より多数のプログラムでなければなりません。私たちはお菓子を並べ替え、その後、前処理します\(S_Iを\)表現\(S_I \)キャンディよりも小さいタブレットを。セット\(DP [I] [J ] \) 前方を表す\を(私は\)キャンディ、選択した\(J \)キャンディ組成物が錠剤よりも大きくなっています。場合は、現在の列挙この選挙の転送は、それを選択しないでください。
\ [Dpが[i]は[Jは、
- 1] [J] + DP [I - 1] [J - - 1] \ CDOT(S_I J + 1)\] DP [I =] 次に、我々はせ\(F(kは)\)インペリアル表し\(I)\よりも「キャンディ」に大きな未来番組の数「の丸薬を。」その後、明らかに存在する
\ [F(K)= DP
[n]が[K]の\ CDOT(NK)!\] 同時に求めることができ、もしそうであれば\(G(k)は\)正確である\(iは\) "キャンディ"の大きな"ピル"よりプログラムナンバーが、そこである
\ [F(K)= \
sum_ {i = K} ^ n個の\ binomのIKのG(I)\] そう直接反転は二項式であることができます。


以下のコード、時間複雑度\(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;
}

おすすめ

転載: www.cnblogs.com/hankeke/p/BZOJ3622.html