试试 冰冷昂贵入云涉水的轻身术
ARC179B
乍一看明显是状压,但是怎么维护可行性?考虑如果放了一个
时间复杂度
#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;
}