跳到正文

题解

Flower's land 3

pystraf11约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年10月28日
收录
2026年11月7日
更新
2026年11月16日
篇幅
约 6 分钟
获赞
1
题目
P17125

版本

  1. v1当前

Description

给定 nn 个 mm 位二进制数 sis_i,对每个 2≤i≤n2\le i\le n 选择一个 1≤pi<i1\le p_i<i,使得 popcount(si⊕spi)≤k\text{popcount}(s_i \oplus s_{p_i})\le k,问所有可能序列 pp 的个数模 998244353998244353。sis_i 用 m/4m/4 位十六进制给出,n≤5000,m≤15000,k≤3n\le 5000,m\le 15000,k\le 3。

sis_i 先压位存下,显然只需计算 j<ij<i 且 popcount(si⊕sj)≤k\text{popcount}(s_i \oplus s_j)\le k 的数量。直接暴力是 O(n2m/w)O(n^2m/w) 过不了。对每个 ii 显然从一个合法 pip_i 推来只需考虑 O(k)O(k) 个位置。

考虑逐个 ii 递推,维护一组合法 pip_i 形成一棵树,对于当前建好的部分,由 d(spv,si)d(s_{p_v},s_i) 容易 O(k)O(k) 遍历差异位集合 DvD_v 推得每个节点的 d(sv,si)d(s_v,s_i)。于是只需算出 d(s1,si)d(s_1,s_i) 即可递推出所有 d(s∗,si)d(s_*,s_i),然后暴力比较计算 DiD_i 即可。复杂度 O(n2k+nm/w)O(n^2k+nm/w)。

code
C++
#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;
}
原文