結果
問題 | No.3098 Linear Reversi |
ユーザー |
|
提出日時 | 2025-04-15 01:49:57 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 410 ms / 4,000 ms |
コード長 | 13,753 bytes |
コンパイル時間 | 4,645 ms |
コンパイル使用メモリ | 283,452 KB |
実行使用メモリ | 7,844 KB |
最終ジャッジ日時 | 2025-04-15 01:50:07 |
合計ジャッジ時間 | 9,076 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 37 |
ソースコード
#ifndef HIDDEN_IN_VS // 折りたたみ用 // 警告の抑制 #define _CRT_SECURE_NO_WARNINGS // ライブラリの読み込み #include <bits/stdc++.h> using namespace std; // 型名の短縮 using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9e18(int は -2^31 ~ 2^31 = 2e9) using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; using Graph = vvi; // 定数の定義 const double PI = acos(-1); int DX[4] = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左) int DY[4] = { 0, 1, 0, -1 }; int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF; // 入出力高速化 struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp; // 汎用マクロの定義 #define all(a) (a).begin(), (a).end() #define sz(x) ((int)(x).size()) #define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x))) #define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x))) #define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");} #define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順 #define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順 #define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順 #define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能) #define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能) #define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順) #define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順) #define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順) #define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去 #define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了 #define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定 // 汎用関数の定義 template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; } template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す) template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す) template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); } template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod // 演算子オーバーロード template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; } template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; } template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; } #endif // 折りたたみ用 #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #ifdef _MSC_VER #include "localACL.hpp" #endif using mint = modint998244353; //using mint = static_modint<(int)1e9 + 7>; //using mint = modint; // mint::set_mod(m); namespace atcoder { inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; } inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; } } using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>; #endif #ifdef _MSC_VER // 手元環境(Visual Studio) #include "local.hpp" #else // 提出用(gcc) inline int popcount(int n) { return __builtin_popcount(n); } inline int popcount(ll n) { return __builtin_popcountll(n); } inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; } inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; } inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; } inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; } #define dump(...) #define dumpel(v) #define dump_math(v) #define input_from_file(f) #define output_to_file(f) #define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す #endif //【幅優先探索(動的)】O(n + m)(遅い) /* * st から到達可能な頂点 t のリストを返す.nxt(s) は s の次に訪れることのできる頂点のリストを返す. * 探索は lim [ms] だけ続ける. */ template <class T> set<T> get_reachable_set(T st, const function<vector<T>(T)>& nxt, int lim = (int)1e9) { // verify : https://atcoder.jp/contests/agc045/tasks/agc045_c auto start = chrono::system_clock::now(); set<T> vs; // st から到達可能な頂点のリスト vs.insert(st); queue<T> que; // 次に探索する頂点を入れておくキュー que.push(st); while (!que.empty()) { // 未探索の頂点 s を得る. auto s = que.front(); que.pop(); repe(t, nxt(s)) { // t が発見済みの頂点なら何もしない. if (vs.count(t)) continue; // t に到達したことを記録する. vs.insert(t); // 未探索の頂点として t を追加する. que.push(t); } auto now = chrono::system_clock::now(); auto msec = chrono::duration_cast<chrono::milliseconds>(now - start).count(); if (msec >= lim) break; } return vs; /* nxt の定義の雛形 using T = ll; function<vector<T>(T)> nxt = [&](T s) { vector<T> res; return res; }; */ } //【ランレングス符号(文字列)】O(n) /* * 文字列 s[0..n) をランレングス符号化し,結果を格納したリスト cls を返す. * cls[i] = {c, l} は前から i 番目の連が l 個の文字 c からなることを表す. */ vector<pair<char, int>> run_length_encoding(const string& s) { // verify : https://atcoder.jp/contests/abc124/tasks/abc124_d int n = sz(s); vector<pair<char, int>> cls; if (n == 0) return cls; cls.emplace_back(s[0], 1); // いま読んでいる文字の種類を記憶する. char c = s[0]; repi(i, 1, n - 1) { // 記憶している文字と同じ文字の場合 if (s[i] == c) { // 列の長さを増やす. cls.back().second++; } // 記憶している文字と異なる文字の場合 else { // 新しい文字を記憶しておく. c = s[i]; // 新たな列を追加する. cls.emplace_back(c, 1); } } return cls; } void zikken() { set<vi> b0s, b1s, b2s; int N = 14; repi(n, 1, N) { using T = string; function<vector<T>(T)> nxt = [&](T s) { vector<T> res; rep(i, n) { if (s[i] == '.') { { auto t(s); t[i] = 'o'; int i2 = i - 1; while (i2 >= 0 && s[i2] == 'x') i2--; if (i2 != -1 && s[i2] != '.') { repi(i3, i2, i) t[i3] = 'o'; } i2 = i + 1; while (i2 < n && s[i2] == 'x') i2++; if (i2 != n && s[i2] != '.') { repi(i3, i, i2) t[i3] = 'o'; } res.push_back(t); } { auto t(s); t[i] = 'x'; int i2 = i - 1; while (i2 >= 0 && s[i2] == 'o') i2--; if (i2 != -1 && s[i2] != '.') { repi(i3, i2, i) t[i3] = 'x'; } i2 = i + 1; while (i2 < n && s[i2] == 'o') i2++; if (i2 != n && s[i2] != '.') { repi(i3, i, i2) t[i3] = 'x'; } res.push_back(t); } } } return res; }; string ST; rep(i, n) ST += '.'; auto res = get_reachable_set(ST, nxt); // dumpel(res); repe(s, res) { bool ok = true; rep(i, n) if (s[i] == '.') ok = false; if (!ok) continue; auto rle = run_length_encoding(s); vi a; for (auto [c, l] : rle) a.push_back(min(l, 3)); vi b; int tp = 0; repe(x, a) { if (x == 3) { b.push_back(3); if (tp == 0) { b1s.insert(b); tp = 1; } else { b2s.insert(b); } b = vi{ 3 }; } else { b.push_back(x); } } if (tp == 0) b0s.insert(b); } } dump("b0s:"); dumpel(b0s); dump("b1s:"); dumpel(b1s); dump("b2s:"); dumpel(b2s); exit(0); } /* b0s: 0: 1 1: 1 1 2: 1 1 1 3: 1 1 2 4: 1 1 2 1 5: 1 1 2 2 6: 1 1 2 2 1 7: 1 1 2 2 2 8: 1 1 2 2 2 1 9: 1 1 2 2 2 2 10: 1 1 2 2 2 2 1 11: 1 1 2 2 2 2 2 12: 1 1 2 2 2 2 2 1 13: 1 1 2 2 2 2 2 2 14: 1 2 15: 1 2 1 16: 1 2 1 1 17: 1 2 1 2 18: 1 2 1 2 1 19: 1 2 1 2 2 20: 1 2 1 2 2 1 21: 1 2 1 2 2 2 22: 1 2 1 2 2 2 1 23: 1 2 1 2 2 2 2 24: 1 2 1 2 2 2 2 1 25: 1 2 1 2 2 2 2 2 26: 1 2 2 27: 1 2 2 1 28: 1 2 2 1 1 29: 1 2 2 1 2 30: 1 2 2 1 2 1 31: 1 2 2 1 2 2 32: 1 2 2 1 2 2 1 33: 1 2 2 1 2 2 2 34: 1 2 2 1 2 2 2 1 35: 1 2 2 1 2 2 2 2 36: 1 2 2 2 37: 1 2 2 2 1 38: 1 2 2 2 1 1 39: 1 2 2 2 1 2 40: 1 2 2 2 1 2 1 41: 1 2 2 2 1 2 2 42: 1 2 2 2 1 2 2 1 43: 1 2 2 2 1 2 2 2 44: 1 2 2 2 2 45: 1 2 2 2 2 1 46: 1 2 2 2 2 1 1 47: 1 2 2 2 2 1 2 48: 1 2 2 2 2 1 2 1 49: 1 2 2 2 2 1 2 2 50: 1 2 2 2 2 2 51: 1 2 2 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 3 17: 1 2 2 2 3 18: 1 2 2 3 19: 1 2 3 20: 1 3 21: 2 1 2 2 2 2 3 22: 2 1 2 2 2 3 23: 2 1 2 2 3 24: 2 1 2 3 25: 2 1 3 26: 2 2 1 2 2 2 3 27: 2 2 1 2 2 3 28: 2 2 1 2 3 29: 2 2 1 3 30: 2 2 2 1 2 2 3 31: 2 2 2 1 2 3 32: 2 2 2 1 3 33: 2 2 2 2 1 2 3 34: 2 2 2 2 1 3 35: 2 2 2 2 2 1 3 36: 2 2 2 2 2 3 37: 2 2 2 2 3 38: 2 2 2 3 39: 2 2 3 40: 2 3 41: 3 b2s: 0: 3 1 2 2 2 3 1: 3 1 2 2 3 2: 3 1 2 3 3: 3 1 3 4: 3 2 1 2 2 3 5: 3 2 1 2 3 6: 3 2 1 3 7: 3 2 2 1 2 3 8: 3 2 2 1 3 9: 3 2 2 2 1 3 10: 3 2 2 2 2 3 11: 3 2 2 2 3 12: 3 2 2 3 13: 3 2 3 14: 3 3 気合入れればオートマトン DP で解けそう. */ mint naive(int n, string s) { using T = string; function<vector<T>(T)> nxt = [&](T s) { vector<T> res; rep(i, n) { if (s[i] == '.') { { auto t(s); t[i] = 'o'; int i2 = i - 1; while (i2 >= 0 && s[i2] == 'x') i2--; if (i2 != -1 && s[i2] != '.') { repi(i3, i2, i) t[i3] = 'o'; } i2 = i + 1; while (i2 < n && s[i2] == 'x') i2++; if (i2 != n && s[i2] != '.') { repi(i3, i, i2) t[i3] = 'o'; } res.push_back(t); } { auto t(s); t[i] = 'x'; int i2 = i - 1; while (i2 >= 0 && s[i2] == 'o') i2--; if (i2 != -1 && s[i2] != '.') { repi(i3, i2, i) t[i3] = 'x'; } i2 = i + 1; while (i2 < n && s[i2] == 'o') i2++; if (i2 != n && s[i2] != '.') { repi(i3, i, i2) t[i3] = 'x'; } res.push_back(t); } } } return res; }; string ST; rep(i, n) ST += '.'; auto ss = get_reachable_set(ST, nxt); mint res = 0; repe(t, ss) { bool ok = true; rep(i, n) { if (t[i] == '.') ok = false; if (t[i] == 'o' && s[i] == 'x') ok = false; if (t[i] == 'x' && s[i] == 'o') ok = false; } if (!ok) continue; res++; } return res; } mint solve(int n, string s) { // dp_i[j][k][b] : コマ j が k 個連続で並んでいて,b:1が禁止されているか vvvm dp(2, vvm(4, vm(2))); dp[0][0][0] = 1; string c = "ox"; rep(i, n) { dump("-------------- i:", i, "---------------"); vvvm ndp(2, vvm(4, vm(2))); rep(j, 2) rep(k, 4) rep(b, 2) { // 同じ if (s[i] != c[j ^ 1]) { int nj = j; int nk = min(k + 1, 3); int nb = b; if (nk == 3) nb = 0; ndp[nj][nk][nb] += dp[j][k][b]; } // 異なる if (s[i] != c[j]) { if (k == 1 && b == 1) continue; int nj = j ^ 1; int nk = 1; int nb = b; if (k == 1 && i != 1) nb = 1; ndp[nj][nk][nb] += dp[j][k][b]; } } dp = move(ndp); dumpel(dp); } mint res = 0; rep(j, 2) rep(k, 4) rep(b, 2) { res += dp[j][k][b]; } return res; } int main() { input_from_file("input.txt"); // output_to_file("output.txt"); // zikken(); int n; string s; cin >> n >> s; dump(naive(n, s)); dump("======"); EXIT(solve(n, s)); }