結果

問題 No.3146 RE: Parentheses Counting
ユーザー Nauclhlt🪷
提出日時 2025-03-13 18:32:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 1,455 bytes
コンパイル時間 1,866 ms
コンパイル使用メモリ 198,428 KB
実行使用メモリ 35,016 KB
最終ジャッジ日時 2025-05-16 20:52:40
合計ジャッジ時間 7,893 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define MOD 998244353LL

using ll = long long;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;

    vector<ll> inv = {1, 1};
    vector<ll> fact = {1};
    vector<ll> invfact = {1};

    vector<ll> ans = {0};
    ll sum = 0;
    ll catalan = 0;

    while (T--)
    {
        int N;
        cin >> N;
        if (N % 2 == 1) cout << "0\n";
        else
        {
            N /= 2;
            while (fact.size() <= 2 * N)
            {
                ll x = fact.size();
                if (inv.size() == fact.size())
                {
                    inv.push_back((-(MOD / x) * inv[MOD % x]) % MOD + MOD);
                    inv[x] %= MOD;
                }
                fact.push_back(fact[fact.size() - 1] * x % MOD);
                invfact.push_back(invfact[invfact.size() - 1] * inv[x] % MOD);
            }
            
            while (ans.size() <= N)
            {
                ll next = 3LL * sum % MOD + catalan;
                
                next %= MOD;
                ans.push_back(next);
                sum += next;
                sum %= MOD;
                ll x = ans.size() - 1;
                ll c = fact[2 * x] * invfact[x] % MOD * invfact[x] % MOD * inv[x + 1] % MOD;
                
                catalan += c;
                catalan %= MOD;
            }

            cout << ans[N] << "\n";
        }
    }
}
0