結果

問題 No.3043 括弧列の数え上げ
ユーザー titia
提出日時 2025-03-03 03:10:04
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 949 bytes
コンパイル時間 437 ms
コンパイル使用メモリ 82,536 KB
実行使用メモリ 106,928 KB
最終ジャッジ日時 2025-03-03 03:10:11
合計ジャッジ時間 6,365 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41 RE * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N=int(input())
mod=998244353

FACT=[1]
for i in range(1,2*10**5+1):
    FACT.append(FACT[-1]*i%mod)

FACT_INV=[pow(FACT[-1],mod-2,mod)]
for i in range(2*10**5,0,-1):
    FACT_INV.append(FACT_INV[-1]*i%mod)

FACT_INV.reverse()

def Combi(a,b):
    if 0<=b<=a:
        return FACT[a]*FACT_INV[b]%mod*FACT_INV[a-b]%mod
    else:
        return 0

if N%2==1:
    print(0)
    exit()

C=[0]*(N+1)

for i in range(N+1):
    C[i]=Combi(2*i,i)*pow(i+1,mod-2,mod)%mod

from functools import lru_cache
@lru_cache(maxsize=None)
def calc(x):
    if x==2:
        return 0
    if x==4:
        return 1
    ANS=0

    for i in range(2,x,2):
        ANS+=calc2(i)*C[(x-i)//2]+calc(x-i)*C[(i-2)//2]
        ANS%=mod

        #print(i,ANS)

    ANS+=calc2(x)
    ANS%=mod

    return ANS

@lru_cache(maxsize=None)
def calc2(x):
    if x<=2:
        return 0
    return (calc(x-2)+(x-2)//2*C[(x-2)//2])%mod

print(calc(N)%mod)
0