题解:P14990 马赛克

· · 题解

P14990 马赛克

已知卷积输入 \mathrm{in} 满足 \mathrm{in}_i \neq 0 \Rightarrow \operatorname{popcount}(i) = 2,求题解区使劲容斥、DP 转移的意义。

题目描述

给一个 01 矩阵 \{p_{n,m}\},定义四元组 (a, b, c, d) 是坏的,当且仅当:

定义 f(S, T) 为任意为 a_{i,j}(i \in S \lor j \in T) 重新赋值后坏四元组数量最小值,则给定参数 k,输出 \displaystyle\sum_{c}\left(\sum_{S,T}[f(S, T)=c]\right)^k \bmod 2^{64}。

## 解法说明 显然,$p_{a,c}\neq p_{a,d}$ 这个条件暗示了: - $a\leq b \land c\neq d$; - $a$ 这一行不全相同,其余类似。 也就是说我们计算 $f(S, T)$ 只需要让可能修改的 $a_{i,j}$ 同色即可。这就是说,$f(S, T) = \displaystyle\sum_{(a,b,c,d) 是坏的}[\{a,b\}\in\overline S][\{c,d\}\in\overline T]$。 我们不妨将 $S, T$ 全部取补,问题变成了一个典型的子集求和形式,直接做只能到 $\Theta(n2^m)$ 之类复杂度。 不过此时我们发现:需要做子集求和的那一维,来源总是一个二元组,问题变为: - 给定输入 $\{\mathrm{in}_{2^n}\}$,求 $\mathrm{out}_i = \displaystyle\sum_{j \operatorname{bitand} i = j}\mathrm{in}_i$,其中输入 $\mathrm{in}$ 满足 $\mathrm{in}_i \neq 0 \Rightarrow \operatorname{popcount}(i) = 2$。 上述问题是易于解决的。 具体来说,我们只需每次挑出集合的最小元素,仿照以下两个递推式: 1. $F(S) = \displaystyle\sum_{i,j\in S\land i< j}w(i,j) = F(S \backslash \{\min\{S\}\}) + \sum_{j\in S\land j \neq \min\{S\}}w(\min\{S\}, j)$; 2. $G(S) = \displaystyle\sum_{j\in S\land j \neq \min\{S\}}w(\min\{S\}, j) = G(S\backslash\operatorname{2ndmin}\{S\})+w(\min\{S\},\operatorname{2ndmin}\{S\})

不难使用 \Theta(2^n) 的复杂度递推输出。

因此,复杂度为 \Theta(2^{n+m}),可以通过本题。

代码实现

#include <bits/stdc++.h>

using std::cin;
using std::cout;

using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

constexpr int N = 12, M = 12, S = 4356 + 5;

u64 qkpow(u64 t, int p) {
  u64 r = 1;
  for (; p; t *= t, p >>= 1)
    if (p & 1) r *= t;
  return r;
}

template <int Siz>
void solve(u16 (&in)[Siz], int n, u16 (&out)[Siz]) {
  std::array<u16, Siz> tmp;
  std::fill(tmp.begin(), tmp.begin() + (1 << n), 0);
  for (u32 s = 0; s < (1u << n); ++s) {
    auto s0 = s & (s - 1), ls0 = s0 & -s0;
    tmp[s] = tmp[s ^ ls0] + in[ls0 | (s & -s)];
    out[s] = out[s0] + tmp[s];
  }
}

int T, n, m, k, w[S];
u16 in[1 << N][1 << M], xsum[1 << N][1 << M], tsum[1 << N], out[1 << N];
bool mp[N][M];

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  cin >> T;
  while (T--) {
    cin >> n >> m >> k;
    int sn = n * (n - 1) / 2, sm = m * (m - 1) / 2, siz = sn * sm;
    std::fill(w, w + siz + 1, 0);
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        char ch;
        cin >> ch;
        mp[i][j] = (ch == '1');
      }
    for (int a = 0; a < n; ++a)
      for (int b = a + 1; b < n; ++b) {
        int p = (1 << a) | (1 << b);
        for (int c = 0; c < m; ++c)
          for (int d = c + 1; d < m; ++d)
            in[p][(1 << c) | (1 << d)] = (mp[a][c] == mp[b][d]
                                       && mp[a][d] == mp[b][c]
                                       && mp[a][c] != mp[a][d]);
        solve(in[p], m, xsum[p]);
      }
    for (int q = 0; q < (1 << m); ++q) {
      for (int a = 0; a < n; ++a)
        for (int b = a + 1; b < n; ++b) {
          int p = (1 << a) | (1 << b);
          tsum[p] = xsum[p][q];
        }
      solve(tsum, n, out);
      for (int p = 0; p < (1 << n); ++p) ++w[out[p]];
    }
    u64 ans = 0;
    for (int c = 0; c <= siz; ++c) ans += qkpow(w[c], k);
    cout << ans << '\n';
  }
}

此外不难发现 #7 满足 p_{i,j}=(i+j)\bmod 2,原因:我交错代码了。