题解 ARC118E【Avoid Permutations】/ SS240928D【d】
题目描述
对于一个排列
生成一个
现在排列
solution
枚举最终路径,考虑有多少个排列能使这个路径走过去。这样的话可以 dp,考虑记录当前走到哪个格子,但是这样(其实是可以做的),所以考虑我们不考虑
code
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
template <unsigned umod>
struct modint {/*{{{*/
static constexpr int mod = umod;
unsigned v;
modint() = default;
template <class T> modint(const T& y) : v(y % mod + (y < 0 ? mod : 0)) {}
modint& operator+=(const modint& rhs) { v += rhs.v; if (v >= umod) v -= umod; return *this; }
modint& operator-=(const modint& rhs) { v -= rhs.v; if (v >= umod) v += umod; return *this; }
modint& operator*=(const modint& rhs) { v = (unsigned)(1ull * v * rhs.v % umod); return *this; }
friend modint operator+(modint lhs, const modint& rhs) { return lhs += rhs; }
friend modint operator-(modint lhs, const modint& rhs) { return lhs -= rhs; }
friend modint operator*(modint lhs, const modint& rhs) { return lhs *= rhs; }
friend int raw(const modint& self) { return self.v; }
friend ostream& operator<<(ostream& os, const modint& self) { return os << raw(self); }
};/*}}}*/
using mint = modint<998244353>;
int n, a[210];
bool vis[210];
mint f[210][210][210][2][2], fac[210];
mint ans = 0;
int main() {
#ifndef LOCAL
cin.tie(nullptr)->sync_with_stdio(false);
#endif
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int cnt = n;
vis[0] = vis[n + 1] = true;
for (int i = 1; i <= n; i++) if (~a[i]) vis[a[i]] = true, cnt -= 1;
for (int i = raw(fac[0] = 1); i <= n; i++) fac[i] = fac[i - 1] * i;
f[0][0][0][0][0] = 1;
for (int i = 0; i <= n + 1; i++) {
for (int j = 0; j <= n + 1; j++) if (i < 1 || i > n || a[i] != j) {
for (int k = 0; k <= cnt; k++) {
if (a[i] == -1 && !vis[j]) f[i][j][k + 1][1][1] += f[i][j][k][0][0];
for (int lf : {0, 1}) {
for (int cf : {0, 1}) {
if (raw(f[i][j][k][lf][cf])) debug("f[%d][%d][%d][%d][%d] = %d\n", i, j, k, lf, cf, raw(f[i][j][k][lf][cf]));
f[i + 1][j][k][0][cf] += f[i][j][k][lf][cf];
f[i][j + 1][k][lf][0] += f[i][j][k][lf][cf];
}
}
}
}
}
for (int k = 0; k <= cnt; k++) ans += (k & 1 ? -1 : 1) * fac[cnt - k] * f[n + 1][n + 1][k][0][0];
cout << ans << endl;
return 0;
}
本文来自博客园,作者:caijianhong,转载请注明原文链接:https://www.cnblogs.com/caijianhong/p/18438353/solution-ARC118E
分类:
solution
标签:
combinatorics
, dp
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现