Flower's land 3

· · 题解

Description

给定 n 个 m 位二进制数 s_i,对每个 2\le i\le n 选择一个 1\le p_i<i,使得 \text{popcount}(s_i \oplus s_{p_i})\le k,问所有可能序列 p 的个数模 998244353。s_i 用 m/4 位十六进制给出,n\le 5000,m\le 15000,k\le 3。

考虑逐个 $i$ 递推,维护一组合法 $p_i$ 形成一棵树,对于当前建好的部分,由 $d(s_{p_v},s_i)$ 容易 $O(k)$ 遍历差异位集合 $D_v$ 推得每个节点的 $d(s_v,s_i)$。于是只需算出 $d(s_1,s_i)$ 即可递推出所有 $d(s_*,s_i)$,然后暴力比较计算 $D_i$ 即可。复杂度 $O(n^2k+nm/w)$。 ::::info[code] ```cpp #include <bits/stdc++.h> using namespace std; using i64 = long long; using ui64 = unsigned long long; using i128 = __int128; using ui128 = unsigned __int128; using f4 = float; using f8 = double; using f16 = long double; template<class T> bool chmax(T &a, const T &b){ if(a < b){ a = b; return true; } return false; } template<class T> bool chmin(T &a, const T &b){ if(a > b){ a = b; return true; } return false; } constexpr int MOD = 998244353; signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n, m, k; cin >> n >> m >> k; int W = (m + 63) >> 6; vector dp(n, vector<ui64>(W)); auto Hex = [&](char c) { if ('0' <= c && c <= '9') return c - '0'; return c - 'A' + 10; }; auto Bit = [&](int i, int p) { return (dp[i][p >> 6] >> (p & 63)) & 1ULL; }; for (int i = 0; i < m; i++) { string s; cin >> s; for (int j = 0; j < s.size(); j++) { int x = Hex(s[j]), p = 4 * j; dp[i][p >> 6] |= (ui64)x << (p & 63); } } int ans = 1; vector<int> par(n), dis(n); par[0] = -1; vector dlt(n, vector<unsigned short>{}); for (int i = 1; i < n; i++) { int rd = 0; for (int w = 0; w < W; w++) rd += __builtin_popcountll(dp[i][w] ^ dp[0][w]); dis[0] = rd; int ways = 0, cp = -1; if (dis[0] <= k) ways++, cp = 0; for (int v = 1; v < i; v++) { int u = par[v], d = dis[u]; for (int z = 0; z < dlt[v].size(); z++) { unsigned short c = dlt[v][z]; int pos = c >> 1, pb = c & 1; int xb = Bit(i, pos); if (xb == pb) d++; else d--; } dis[v] = d; if (d <= k) { ways++; if (cp == -1) cp = v; } } if (ways == 0) { cout << 0 << '\n'; return 0; } ans = 1ll * ans * ways % MOD, par[i] = cp, dlt[i].clear(); for (int w = 0; w < W; w++) { ui64 x = dp[i][w] ^ dp[cp][w]; while (x) { int b = __builtin_ctzll(x); int p = (w << 6) + b, pb = (dp[cp][w] >> b) & 1ULL; dlt[i].push_back((unsigned short)((p << 1) | pb)); x &= x - 1; } } } cout << ans; return 0; } ``` ::::