跳到正文

题解

试试 冰冷昂贵入云涉水的轻身术

Loop1st约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年9月8日
收录
2026年10月1日
更新
2026年11月5日
篇幅
约 3 分钟

版本

  1. v1当前

ARC179B

乍一看明显是状压,但是怎么维护可行性?考虑如果放了一个 jj, 那么接下来所有满足 Xi=jX_i=j 的都是可以放的,但是 jj 不能放了(除非 Xj=jX_j=j),所以直接状压,dpi,sdp_{i,s} 表示考虑到前 ii 个数,能放的数的状态为 ss 的方案数。转移显然。

时间复杂度 O(nm2m)\mathcal{O}(nm2^m).

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e4 + 10, M = 10, mod = 998244353;
template<typename T>
void dbg(const T &t) { cout << t << endl; }
template<typename Type, typename... Types>
void dbg(const Type& arg, const Types&... args) {
    #ifdef ONLINE_JUDGE
        return ;
    #endif
    cout << arg << ' ';
    dbg(args...);
}   
void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
int n, m, mask[N], dp[N][1 << M]; // mask[i]: 满足 x_j = i 的 j 的集合 dp[i][s]: 考虑到前 i 位,能放的数集合为 j 的方案数
int main() {
	// freopen("data.in", "r", stdin);
	// freopen("data.out", "w", stdout);
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> m >> n;
	for (int i = 0, x; i < m; i++) {
		cin >> x; x--;
		mask[x] |= (1 << i);
	}
	dp[0][(1 << m) - 1] = 1;
	for (int i = 0; i < n; i++) {
		for (int s = 0; s < (1 << m); s++) {
			for (int j = 0; j < m; j++) if (s >> j & 1) {
				add(dp[i + 1][s ^ (1 << j) | mask[j]], dp[i][s]);
			}
		} 
	}
	int ans = 0;
	for (int s = 0; s < (1 << m); s++) add(ans, dp[n][s]);
	cout << ans << '\n';
    return 0;
}
原文