結果

問題 No.3146 RE: Parentheses Counting
ユーザー GOTKAKO
提出日時 2025-05-16 22:54:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 2,835 bytes
コンパイル時間 1,950 ms
コンパイル使用メモリ 198,580 KB
実行使用メモリ 34,636 KB
最終ジャッジ日時 2025-05-16 22:54:26
合計ジャッジ時間 9,698 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

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

long long mod = 998244353;
//入力が必ず-mod<a<modの時.
struct mint{
    long long v = 0;
    mint(){} mint(int a){v = a<0?a+mod:a;} mint(long long a){v = a<0?a+mod:a;}
    mint(unsigned long long a){v = a;}
    long long val(){return v;}
    void modu(){v %= mod;}
 
    mint &operator=(const mint &b) = default;
    mint operator-()const{return mint(0)-(*this);}
    mint operator+(const mint b)const{return mint(v)+=b;}
    mint operator-(const mint b)const{return mint(v)-=b;}
    mint operator*(const mint b)const{return mint(v)*=b;}
    mint operator/(const mint b)const{return mint(v)/=b;}
 
    mint operator+=(const mint b){
        v += b.v; if(v >= mod) v -= mod;
        return *this;
    }
    mint operator-=(const mint b){
        v -= b.v; if(v < 0) v += mod; 
        return *this;
    }   
    mint operator*=(const mint b){v = v*b.v%mod; return *this;}
    mint operator/=(mint b){
        if(b == 0) assert(false);
        int left = mod-2;
        while(left){if(left&1) *this *= b; b *= b; left >>= 1;}
        return *this;
    }
 
    mint operator++(){*this += 1; return *this;}
    mint operator--(){*this -= 1; return *this;}
    mint operator++(int){*this += 1; return *this;}
    mint operator--(int){*this -= 1; return *this;}
    bool operator==(const mint b)const{return v == b.v;}
    bool operator!=(const mint b)const{return v != b.v;}
    bool operator>(const mint b)const{return v > b.v;}
    bool operator>=(const mint b)const{return v >= b.v;}
    bool operator<(const mint b)const{return v < b.v;}
    bool operator<=(const mint b)const{return v <= b.v;}
 
    mint pow(long long n)const{
        mint ret = 1,p = v;
        if(n < 0) p = p.inv(),n = -n;
        while(n){
            if(n&1) ret *= p;
            p *= p; n >>= 1;
        }
        return ret;
    }
    mint inv()const{return mint(1)/v;}
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int Limit = 2002002; //Limit 必要なサイズ fac->x! facinv->1/x! inv->1/x.
    vector<mint> fac(Limit+1,1); for(int i=1; i<=Limit; i++) fac.at(i) = fac.at(i-1)*i;
    vector<mint> facinv(Limit+1); facinv.at(min((int)mod-1,Limit)) = fac.at(min((int)mod-1,Limit)).inv();
    for(int i=min((int)mod-2,Limit-1); i>=0; i--) facinv.at(i) = facinv.at(i+1)*(i+1);
    //vector<mint> inv(Limit+1); for(int i=1; i<=Limit; i++) inv.at(i) = fac.at(i-1)*facinv.at(i); //必要なら解放!.
    auto nCr = [&](int n, int r) -> mint {
        if(n < r || r < 0 || n < 0) return 0;
        return fac.at(n)*facinv.at(r)*facinv.at(n-r);
    };
    int T; cin >> T;
    while(T--){
        int N; cin >> N;
        if(N%2){cout << 0 << "\n"; continue;}
        N /= 2; N--;
        mint answer = mint(4).pow(N)-nCr(2*N+1,N);
        cout << answer.v << "\n";
    }
}
0