結果
| 問題 |
No.3043 括弧列の数え上げ
|
| コンテスト | |
| ユーザー |
Nauclhlt🪷
|
| 提出日時 | 2025-01-10 21:55:07 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 760 ms / 2,000 ms |
| コード長 | 5,697 bytes |
| コンパイル時間 | 1,980 ms |
| コンパイル使用メモリ | 199,348 KB |
| 実行使用メモリ | 222,336 KB |
| 最終ジャッジ日時 | 2025-01-11 17:13:49 |
| 合計ジャッジ時間 | 12,554 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 45 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll CONST_MOD = 998244353LL;
struct ModInt
{
long long Value;
public:
ModInt()
{
Value = 0L;
}
ModInt(long long value)
{
Value = value;
}
ModInt Power(long long exp) const
{
if (exp <= -1L)
{
return ModInt(1L) / Power(-exp);
}
if (exp == 0L)
return 1L;
if (exp == 1L)
return *this;
ModInt m = Power(exp / 2L);
m = m * m;
if (exp % 2L == 1L)
{
m = m * (*this);
}
return m;
}
ModInt Inv() const
{
return this->Power(CONST_MOD - 2L);
}
ModInt operator+() const
{
return *this;
}
ModInt operator-() const
{
return ModInt(-Value);
}
friend ModInt operator+(const ModInt& left, const ModInt& right)
{
return ModInt(SafeMod(left.Value + right.Value));
}
friend ModInt operator+(const ModInt& left, const long long& right)
{
return ModInt(SafeMod(left.Value + right));
}
friend ModInt operator+(const long long& left, const ModInt& right)
{
return ModInt(SafeMod(left + right.Value));
}
ModInt& operator+=(const ModInt& x)
{
Value += x.Value;
Value = SafeMod(Value);
return *this;
}
ModInt& operator+=(const long long& x)
{
Value += x;
Value = SafeMod(Value);
return *this;
}
friend ModInt operator-(const ModInt& left, const ModInt& right)
{
return ModInt(SafeMod(left.Value - right.Value));
}
friend ModInt operator-(const ModInt& left, const long long& right)
{
return ModInt(SafeMod(left.Value - right));
}
friend ModInt operator-(const long long& left, const ModInt& right)
{
return ModInt(SafeMod(left - right.Value));
}
ModInt& operator-=(const ModInt& x)
{
Value -= x.Value;
Value = SafeMod(Value);
return *this;
}
ModInt& operator-=(const long long& x)
{
Value -= x;
Value = SafeMod(Value);
return *this;
}
friend ModInt operator*(const ModInt& left, const ModInt& right)
{
return ModInt(SafeMod(left.Value * right.Value));
}
friend ModInt operator*(const ModInt& left, const long long& right)
{
return ModInt(SafeMod(left.Value * right));
}
friend ModInt operator*(const long long& left, const ModInt& right)
{
return ModInt(SafeMod(left * right.Value));
}
ModInt& operator*=(const ModInt& x)
{
Value *= x.Value;
Value = SafeMod(Value);
return *this;
}
ModInt& operator*=(const long long& x)
{
Value *= x;
Value = SafeMod(Value);
return *this;
}
friend ModInt operator /(const ModInt& left, const ModInt& right)
{
ModInt inv = right.Inv();
return ModInt(SafeMod(left.Value * inv.Value));
}
friend ModInt operator/(const ModInt& left, const long long& right)
{
return ModInt(SafeMod(left.Value * ModInt(right).Inv().Value));
}
friend ModInt operator/(const long long& left, const ModInt& right)
{
return ModInt(SafeMod(left * right.Inv().Value));
}
ModInt& operator/=(const ModInt& x)
{
Value *= x.Inv().Value;
Value = SafeMod(Value);
return *this;
}
ModInt& operator/=(const long long& x)
{
Value *= ModInt(x).Inv().Value;
Value = SafeMod(Value);
return *this;
}
ModInt& operator++()
{
++Value;
Value = SafeMod(Value);
return *this;
}
ModInt operator++(int)
{
ModInt temp = *this;
Value++;
Value = SafeMod(Value);
return temp;
}
ModInt& operator--()
{
--Value;
Value = SafeMod(Value);
return *this;
}
ModInt operator--(int)
{
ModInt temp = *this;
Value--;
Value = SafeMod(Value);
return temp;
}
inline static ModInt One()
{
return ModInt(1L);
}
static ModInt Combination(long long n, long long r)
{
ModInt c = 1L;
for (ModInt i = 1; i.Value <= r; i++)
{
c = c * (ModInt(n) - i + ModInt::One()) / i;
}
return c;
}
private:
inline static long long SafeMod(long long a)
{
a %= CONST_MOD;
if (a < 0)
{
a += CONST_MOD;
}
return a;
}
};
int main()
{
int N;
cin >> N;
const int OPEN = 0;
const int CLOSE = 1;
if ((N & 1) == 1)
{
cout << 0 << endl;
return 0;
}
vector<vector<vector<ModInt>>> dp(N + 1, vector<vector<ModInt>>(N + 1, vector<ModInt>(2, 0LL)));
dp[1][1][OPEN] = 1LL;
for (int i = 2; i <= N; i++)
{
for (int j = 0; j <= N; j++)
{
// ')'を追加
if (j <= N - 1)
{
dp[i][j][CLOSE] += dp[i - 1][j + 1][OPEN] + dp[i - 1][j + 1][CLOSE];
}
// '('を追加
if (j >= 1)
{
dp[i][j][OPEN] += dp[i - 1][j - 1][OPEN] + dp[i - 1][j - 1][CLOSE];
}
}
}
ModInt ans = 0LL;
for (int i = 1; i <= N; i++)
{
// i文字目の')'の寄与
for (int j = 1; j <= N; j++)
{
int right = N - i;
ans += dp[i][j][CLOSE] * (dp[right][j][OPEN] + dp[right][j][CLOSE]) * ModInt(j);
}
}
cout << ans.Value << endl;
}
Nauclhlt🪷