結果
問題 | No.1677 mæx |
ユーザー | nawawan |
提出日時 | 2021-09-11 03:19:39 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 54 ms / 2,000 ms |
コード長 | 5,143 bytes |
コンパイル時間 | 3,279 ms |
コンパイル使用メモリ | 216,664 KB |
実行使用メモリ | 46,140 KB |
最終ジャッジ日時 | 2024-06-13 04:01:15 |
合計ジャッジ時間 | 4,628 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 5 ms
13,132 KB |
testcase_01 | AC | 6 ms
13,128 KB |
testcase_02 | AC | 5 ms
13,024 KB |
testcase_03 | AC | 5 ms
13,260 KB |
testcase_04 | AC | 49 ms
39,420 KB |
testcase_05 | AC | 48 ms
39,428 KB |
testcase_06 | AC | 48 ms
39,512 KB |
testcase_07 | AC | 50 ms
39,364 KB |
testcase_08 | AC | 48 ms
39,596 KB |
testcase_09 | AC | 48 ms
39,600 KB |
testcase_10 | AC | 47 ms
39,636 KB |
testcase_11 | AC | 48 ms
39,604 KB |
testcase_12 | AC | 47 ms
39,484 KB |
testcase_13 | AC | 48 ms
39,372 KB |
testcase_14 | AC | 50 ms
39,728 KB |
testcase_15 | AC | 48 ms
39,720 KB |
testcase_16 | AC | 49 ms
39,336 KB |
testcase_17 | AC | 50 ms
39,456 KB |
testcase_18 | AC | 49 ms
39,596 KB |
testcase_19 | AC | 5 ms
13,128 KB |
testcase_20 | AC | 6 ms
13,076 KB |
testcase_21 | AC | 54 ms
46,140 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; template<const int MOD> struct modint{ long long val; modint(): val(0) {} modint(long long x){ if(x < 0) val = x % MOD + MOD; else val = x % MOD; } modint(const modint &t){ val = t.val; } modint& operator =(const modint m){ val = m.val; return *this; } modint operator -(){ return modint(-val); } modint& operator-=(const modint &m){ val -= m.val; if(val < 0) val += MOD; return *this; } modint& operator+=(const modint &m){ val += m.val; if(val >= MOD) val -= MOD; return *this; } modint& operator*=(const modint &m){ val *= m.val; val %= MOD; return *this; } modint& operator/=(modint m){ *this *= m.inv(); return *this; } modint inv(){ long long x = 1, y = 0; long long a = val, b = MOD; while(b != 0){ long long t = a / b; a -= t * b; x -= t * y; swap(a, b); swap(x, y); } x %= MOD; if(x < 0) x += MOD; return modint(x); } modint pow(long long k){ long long res = 1; long long v = val; while(k > 0){ if(k & 1) res = res * v % MOD; v = v * v % MOD; k >>= 1; } return modint(res); } bool operator==(const modint &m){ return val == m.val; } modint operator+(const modint &m){ return modint(*this) += m; } modint operator-(const modint &m){ return modint(*this) -= m; } modint operator*(const modint &m){ return modint(*this) *= m; } modint operator/(const modint &m){ return modint(*this) /= m; } bool operator!=(const modint &m){ return modint(*this).val != m.val; } bool operator!=(const int &m){ return modint(*this).val != m; } }; const int MOD = 998244353; using mint = modint<MOD>; const int MAX = 410000; mint fac[MAX], finv[MAX], inv[MAX]; void COMinit(){ fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for(int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i; inv[i] = mint(MOD) - inv[MOD % i] * (MOD / i); finv[i] = finv[i - 1] * inv[i]; } } mint COM(int n, int k){ if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * finv[k] * finv[n - k]; } int main(){ string S; cin >> S; int K; cin >> K; int N = S.size(); vector<vector<mint>> dp(N + 1, vector<mint>(3)); stack<int> s; vector<int> p(N, -1), mid(N, -1), op(N, -1); for(int i = 0; i < N; i++){ if(S[i] == '(') { s.push(i); string T; for(int j = i - 3; j <= i - 1; j++){ T += S[j]; } if(T == "max") op[i] = 0; else if(T == "mex") op[i] = 1; else op[i] = 2; } else if(S[i] == ')'){ p[s.top()] = i; s.pop(); } else if(S[i] == ','){ mid[s.top()] = i; } } auto dfs = [&](auto dfs, int l, int r)->void{ if(r == l + 1){ if(S[l] == '?'){ for(int i = 0; i < 3; i++) dp[l][i] += 1; } else dp[l][S[l] - '0'] += 1; return; } if(S[l] == '('){ dfs(dfs, l + 1, mid[l]); dfs(dfs, mid[l] + 1, r - 1); if(op[l] == 0){ for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ dp[l][max(i, j)] += dp[l + 1][i] * dp[mid[l] + 1][j]; } } } else if(op[l] == 1){ for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ vector<bool> f(3, true); f[i] = false; f[j] = false; for(int k = 0; k < 3; k++){ if(f[k]){ dp[l][k] += dp[l + 1][i] * dp[mid[l] + 1][j]; break; } } } } } else{ for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ vector<bool> f(3, true); f[i] = false; f[j] = false; dp[l][max(i, j)] += dp[l + 1][i] * dp[mid[l] + 1][j]; for(int k = 0; k < 3; k++){ if(f[k]) { dp[l][k] += dp[l + 1][i] * dp[mid[l] + 1][j]; break; } } } } } } else{ dfs(dfs, l + 1, r); dp[l] = dp[l + 1]; } }; dfs(dfs, 0, N); cout << dp[0][K].val << endl; }