ARC093F Dark Horse 容斥原理+DP

题目传送门

https://atcoder.jp/contests/arc093/tasks/arc093_d

题解

由于不论 \(1\) 在哪个位置,一轮轮下来,基本上过程都是相似的,所以不妨假设 \(1\) 在第 \(1\) 个位置。

那么,\(1\) 将以此遇到的对手是 \(p_2, \min\{p_3, p_4\}, \min\{p_5, p_6, p_7, p_8\}, \cdots\)

令这些数分别为 \(b_0, b_1, \cdots\),其中 \(b_i = \min \limits_{j=2^i + 1}^{2^{j+1}} p_j\),长度为 \(2^i\)。满足题意的 \(b\) 中的任何一个数均没有在 \(a\) 中出现过。

考虑容斥。对于集合 \(S\),需要求出 \(\forall i \in S\),有 \(b_i\)\(a\) 中元素的方案数。

可以使用 dp 来实现。为了方便计数,将 \(a\) 从大到小排序。令 \(dp[i][S]\) 表示集合 \(S\) 中的 \(b\) 值均出现在 \(a\) 中前 \(i\) 个数的方案数。(不考虑没有在 \(a\) 中出现的那些 \(b\) 中的元素)

第一种转移是不选第 \(i\) 个数,转移方法是 \(dp[i][S] \to dp[i+1][S]\)

第二种转移,枚举一个没有在 \(S\) 中出现的位置 \(j\),则可以转移 \(dp[i][S] \cdot \binom{les}{2^j-1} \cdot (2^j)! \to dp[i+1][S \cup \{j\}]\)

最后,计算集合 \(S\) 的实际方案数的时候,由于之前的 dp 没有考虑那些没有在 \(a\) 中出现的 \(b\),所以还需要乘上 \((2^n - 1 - S)!\)


下面是代码。状态数一共 \(O(mS)\),转移 \(O(n)\),总时间复杂度 \(O(nmS)\)

#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;
}

#define lowbit(x) ((x) & -(x))

const int N = 20;
const int M = (1 << 16) + 7;
const int P = 1e9 + 7;

int n, m, S;
int a[N], dp[N][M];
int inv[M], fac[M], ifac[M];

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, 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;
}

inline void ycl(int 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 DP() {
    S = (1 << n) - 1;
    dp[0][0] = 1;
    for (int i = 0; i < m; ++i) {
        for (int s = 0; s <= S; ++s) {
            int les = S + 1 - a[i + 1] - s;
            sadd(dp[i + 1][s], dp[i][s]);
            for (int j = 0; j < n; ++j) if (!((s >> j) & 1))
                sadd(dp[i + 1][s | (1 << j)], (ll)dp[i][s] * C(les, (1 << j) - 1) % P * fac[1 << j] % P);
//      dbg("dp[%d][%d] = %d\n", i, s, dp[i][s]);
        }
    }
}

inline void work() {
    ycl(1 << n);
    DP();
    int ans = 0;
    for (int i = 0; i <= S; ++i) {
        int cnt = 0, s = i;
        while (s) ++cnt, s -= lowbit(s);
        if (cnt & 1) sadd(ans, P - (ll)dp[m][i] * fac[S - i] % P);
        else sadd(ans, (ll)dp[m][i] * fac[S - i] % P);
//      dbg("dp[%d][%d] = %d, cnt = %d, %lld\n", m, i, dp[m][i], cnt, (ll)dp[m][i] * fac[S - i] % P);
    }
    ans = (ll)ans * (S + 1) % P;
    printf("%d\n", ans);
}

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

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

猜你喜欢

转载自www.cnblogs.com/hankeke/p/ARC093F.html