CSP-S2025 T4 个人题解

· · 题解

::::info[code]

#include "bits/stdc++.h"
//#include "bits/extc++.h"
#define ll long long
#define int long long
#define pii std::pair<int, int> 
#define piii std::pair<int, std::pair<int, int> >
#define tiii tuple<int, int, int>
#define mkp std::make_pair
#define smin(a, b) (a = min(a, b))
#define smax(a, b) (a = max(a, b))
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define INF ((int)1e9)
#define oo (1e18)
#define debug() printf("Skmqwq")
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
using namespace std;
//using namespace __gnu_pbds;
mt19937_64 Skmqwq(time(0) ^ clock());

const int N = 5e2 + 7, P = 998244353;
int n, m, c[N], cnt[N], sum[N], f[2][N][N];
int fac[N], ifac[N], inv[N];
string s;

inline int qp(int a, int b) {
    int ret = 1;
    while (b) {
        if (b & 1) ret = ret * a % P;
        a = a * a % P, b >>= 1;
    }
    return ret;
}
inline void init() {
    fac[0] = 1;
    rep(i, 1, n) fac[i] = fac[i - 1] * i % P;
    ifac[n] = qp(fac[n], P - 2);
    per(i, n, 1) ifac[i - 1] = ifac[i] * i % P;
    rep(i, 1, n) inv[i] = ifac[i] * fac[i - 1] % P;
}
inline int C(int n, int m) { return fac[n] * ifac[m] % P * ifac[n - m] % P; }
inline int A(int n, int m) { return fac[n] * ifac[n - m] % P; }
inline void add(int &a, int b) { 
    a += b;
    if (a > P) a -= P;
}

signed main() {
    std::ios::sync_with_stdio(false);   
    std::cin.tie(0);
    std::cout.tie(0);

    cin >> n >> m >> s, s = ' ' + s;
    init();
    rep(i, 1, n) cin >> c[i], cnt[c[i]]++;
    sum[0] = cnt[0];
    rep(i, 1, n) sum[i] = sum[i - 1] + cnt[i];
    f[0][0][0] = 1;
    rep(cur, 0, n - 1) {
        int i = cur & 1, ii = i^1;
        rep(j, 0, cur) rep(k, 0, min(cur, sum[j])) f[ii][j][k] = 0;
        rep(j, 0, cur) rep(k, 0, min(cur, sum[j])) {
            if (s[cur + 1] == '1') {
                add(f[ii][j][k], (int)((n - sum[j]) > (cur - k)) * f[i][j][k]);
                rep(t, 0, min(cur - k, cnt[j + 1])) 
                    add(f[ii][j + 1][k + 1 + t], (sum[j] - k) * A(cur - k, t) % P * C(cnt[j + 1], t) % P * f[i][j][k] % P);
            } else {
                rep(t, 0, min(cur - k, cnt[j + 1])) {
                    add(f[ii][j + 1][k + t], (int)(n - sum[j + 1] > cur - k - t) * f[i][j][k] % P * A(cur - k, t) % P * C(cnt[j + 1], t) % P);
                    if (sum[j + 1] - k - t < 0) continue;
                    add(f[ii][j + 1][k + 1 + t], (sum[j + 1] - k - t) * f[i][j][k] % P * A(cur - k, t) % P * C(cnt[j + 1], t) % P);
                }       
            }
        }
        // if (cur == 1) cout << f[ii][]
    }
    int ans = 0;
    // rep(j, 0, n - m) cout << f[0][j][sum[j]] << ' ';
    rep(j, 0, n - m) add(ans, fac[n - sum[j]] * f[n & 1][j][sum[j]] % P);
    cout << ans << '\n';

    return 0;
}

::::