結果
問題 | No.2031 Colored Brackets |
ユーザー |
![]() |
提出日時 | 2022-08-14 19:12:26 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 17 ms / 2,000 ms |
コード長 | 1,723 bytes |
コンパイル時間 | 363 ms |
コンパイル使用メモリ | 42,700 KB |
実行使用メモリ | 20,836 KB |
最終ジャッジ日時 | 2024-10-01 10:35:37 |
合計ジャッジ時間 | 1,848 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 30 |
ソースコード
/* -*- coding: utf-8 -*- * * 2031.cc: No.2031 Colored Brackets - yukicoder */ #include<cstdio> #include<algorithm> using namespace std; /* constant */ const int MAX_N = 3000; const int MAX_M = MAX_N / 2; const int MOD = 998244353; /* typedef */ template<const int MOD> struct MI { int v; MI(): v() {} MI(int _v): v(_v % MOD) {} MI(long long _v): v(_v % MOD) {} MI operator+(const MI m) const { return MI(v + m.v); } MI operator-(const MI m) const { return MI(v + MOD - m.v); } MI operator*(const MI m) const { return MI((long long)v * m.v); } MI &operator+=(const MI m) { return (*this = *this + m); } MI &operator-=(const MI m) { return (*this = *this - m); } MI &operator*=(const MI m) { return (*this = *this * m); } MI pow(int n) const { // a^n % MOD MI pm = 1, a = *this; while (n > 0) { if (n & 1) pm *= a; a *= a; n >>= 1; } return pm; } MI inv() const { return pow(MOD - 2); } MI operator/(const MI m) const { return *this * m.inv(); } MI &operator/=(const MI m) { return (*this = *this / m); } }; typedef MI<MOD> mi; /* global variables */ char s[MAX_N + 4]; mi dp[MAX_N + 1][MAX_M + 1]; /* subroutines */ /* main */ int main() { int n; scanf("%d%s", &n, s); int m = n / 2; dp[0][0] = 1; for (int i = 0, d = 0; i < n; i++) { if (s[i] == '(') { for (int j = 0; j <= m; j++) if (dp[i][j].v != 0) { dp[i + 1][j + 1] += dp[i][j]; dp[i + 1][j] += dp[i][j]; } d++; } else { for (int j = 0; j <= m; j++) if (dp[i][j].v != 0) { if (j > 0) dp[i + 1][j - 1] += dp[i][j]; if (d - j > 0) dp[i + 1][j] += dp[i][j]; } d--; } } printf("%d\n", dp[n][0].v); return 0; }