結果

問題 No.2605 Pickup Parentheses
ユーザー 👑 amentorimaruamentorimaru
提出日時 2023-10-30 02:58:44
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 7,545 bytes
コンパイル時間 495 ms
コンパイル使用メモリ 82,264 KB
実行使用メモリ 270,960 KB
最終ジャッジ日時 2024-09-27 01:09:58
合計ジャッジ時間 31,324 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
54,272 KB
testcase_01 AC 43 ms
59,904 KB
testcase_02 AC 44 ms
60,032 KB
testcase_03 AC 66 ms
71,296 KB
testcase_04 AC 62 ms
71,168 KB
testcase_05 AC 68 ms
76,032 KB
testcase_06 AC 70 ms
76,032 KB
testcase_07 AC 69 ms
76,288 KB
testcase_08 AC 71 ms
75,876 KB
testcase_09 AC 56 ms
67,712 KB
testcase_10 AC 70 ms
76,032 KB
testcase_11 AC 62 ms
71,424 KB
testcase_12 AC 55 ms
66,688 KB
testcase_13 AC 58 ms
72,320 KB
testcase_14 AC 62 ms
74,496 KB
testcase_15 AC 69 ms
76,160 KB
testcase_16 AC 55 ms
66,944 KB
testcase_17 AC 68 ms
75,904 KB
testcase_18 AC 1,079 ms
260,020 KB
testcase_19 AC 561 ms
141,772 KB
testcase_20 AC 152 ms
77,456 KB
testcase_21 AC 106 ms
76,652 KB
testcase_22 AC 79 ms
76,168 KB
testcase_23 AC 1,362 ms
203,900 KB
testcase_24 AC 92 ms
76,156 KB
testcase_25 TLE -
testcase_26 TLE -
testcase_27 AC 1,156 ms
225,684 KB
testcase_28 AC 61 ms
75,344 KB
testcase_29 AC 214 ms
78,712 KB
testcase_30 AC 553 ms
114,092 KB
testcase_31 AC 52 ms
68,608 KB
testcase_32 AC 42 ms
59,648 KB
testcase_33 AC 1,000 ms
132,764 KB
testcase_34 AC 61 ms
75,920 KB
testcase_35 AC 49 ms
64,384 KB
testcase_36 AC 59 ms
75,452 KB
testcase_37 AC 819 ms
189,032 KB
testcase_38 AC 170 ms
80,128 KB
testcase_39 AC 105 ms
77,152 KB
testcase_40 AC 255 ms
93,568 KB
testcase_41 AC 278 ms
84,480 KB
testcase_42 AC 311 ms
107,024 KB
testcase_43 AC 127 ms
78,080 KB
testcase_44 AC 152 ms
78,756 KB
testcase_45 AC 113 ms
77,056 KB
testcase_46 AC 101 ms
76,800 KB
testcase_47 AC 62 ms
75,884 KB
testcase_48 AC 213 ms
79,360 KB
testcase_49 AC 297 ms
99,072 KB
testcase_50 AC 171 ms
79,736 KB
testcase_51 AC 40 ms
53,760 KB
testcase_52 AC 209 ms
82,096 KB
testcase_53 AC 254 ms
86,544 KB
testcase_54 AC 181 ms
80,088 KB
testcase_55 AC 259 ms
83,904 KB
testcase_56 AC 111 ms
76,876 KB
testcase_57 AC 41 ms
54,016 KB
testcase_58 AC 945 ms
104,752 KB
testcase_59 AC 483 ms
189,176 KB
testcase_60 AC 857 ms
259,084 KB
testcase_61 TLE -
testcase_62 AC 1,614 ms
121,108 KB
testcase_63 AC 1,727 ms
149,020 KB
testcase_64 AC 1,227 ms
104,748 KB
testcase_65 AC 523 ms
204,800 KB
testcase_66 AC 1,012 ms
108,540 KB
testcase_67 AC 1,116 ms
260,376 KB
testcase_68 AC 262 ms
80,116 KB
testcase_69 AC 39 ms
53,668 KB
testcase_70 AC 53 ms
67,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
def read_values(): return tuple(map(int, input().split()))

class FFT():
    def primitive_root_constexpr(self,m):
        if m==2:return 1
        if m==167772161:return 3
        if m==469762049:return 3
        if m==754974721:return 11
        if m==998244353:return 3
        divs=[0]*20
        divs[0]=2
        cnt=1
        x=(m-1)//2
        while(x%2==0):x//=2
        i=3
        while(i*i<=x):
            if (x%i==0):
                divs[cnt]=i
                cnt+=1
                while(x%i==0):
                    x//=i
            i+=2
        if x>1:
            divs[cnt]=x
            cnt+=1
        g=2
        while(1):
            ok=True
            for i in range(cnt):
                if pow(g,(m-1)//divs[i],m)==1:
                    ok=False
                    break
            if ok:
                return g
            g+=1
    def bsf(self,x):
        res=0
        while(x%2==0):
            res+=1
            x//=2
        return res
    rank2=0
    root=[]
    iroot=[]
    rate2=[]
    irate2=[]
    rate3=[]
    irate3=[]
    
    def __init__(self,MOD):
        self.mod=MOD
        self.g=self.primitive_root_constexpr(self.mod)
        self.rank2=self.bsf(self.mod-1)
        self.root=[0 for i in range(self.rank2+1)]
        self.iroot=[0 for i in range(self.rank2+1)]
        self.rate2=[0 for i in range(self.rank2)]
        self.irate2=[0 for i in range(self.rank2)]
        self.rate3=[0 for i in range(self.rank2-1)]
        self.irate3=[0 for i in range(self.rank2-1)]
        self.root[self.rank2]=pow(self.g,(self.mod-1)>>self.rank2,self.mod)
        self.iroot[self.rank2]=pow(self.root[self.rank2],self.mod-2,self.mod)
        for i in range(self.rank2-1,-1,-1):
            self.root[i]=(self.root[i+1]**2)%self.mod
            self.iroot[i]=(self.iroot[i+1]**2)%self.mod
        prod=1;iprod=1
        for i in range(self.rank2-1):
            self.rate2[i]=(self.root[i+2]*prod)%self.mod
            self.irate2[i]=(self.iroot[i+2]*iprod)%self.mod
            prod=(prod*self.iroot[i+2])%self.mod
            iprod=(iprod*self.root[i+2])%self.mod
        prod=1;iprod=1
        for i in range(self.rank2-2):
            self.rate3[i]=(self.root[i+3]*prod)%self.mod
            self.irate3[i]=(self.iroot[i+3]*iprod)%self.mod
            prod=(prod*self.iroot[i+3])%self.mod
            iprod=(iprod*self.root[i+3])%self.mod
    def butterfly(self,a):
        n=len(a)
        h=(n-1).bit_length()
        
        LEN=0
        while(LEN<h):
            if (h-LEN==1):
                p=1<<(h-LEN-1)
                rot=1
                for s in range(1<<LEN):
                    offset=s<<(h-LEN)
                    for i in range(p):
                        l=a[i+offset]
                        r=a[i+offset+p]*rot
                        a[i+offset]=(l+r)%self.mod
                        a[i+offset+p]=(l-r)%self.mod
                    rot*=self.rate2[(~s & -~s).bit_length()-1]
                    rot%=self.mod
                LEN+=1
            else:
                p=1<<(h-LEN-2)
                rot=1
                imag=self.root[2]
                for s in range(1<<LEN):
                    rot2=(rot*rot)%self.mod
                    rot3=(rot2*rot)%self.mod
                    offset=s<<(h-LEN)
                    for i in range(p):
                        a0=a[i+offset]
                        a1=a[i+offset+p]*rot
                        a2=a[i+offset+2*p]*rot2
                        a3=a[i+offset+3*p]*rot3
                        a1na3imag=(a1-a3)%self.mod*imag
                        a[i+offset]=(a0+a2+a1+a3)%self.mod
                        a[i+offset+p]=(a0+a2-a1-a3)%self.mod
                        a[i+offset+2*p]=(a0-a2+a1na3imag)%self.mod
                        a[i+offset+3*p]=(a0-a2-a1na3imag)%self.mod
                    rot*=self.rate3[(~s & -~s).bit_length()-1]
                    rot%=self.mod
                LEN+=2
                
    def butterfly_inv(self,a):
        n=len(a)
        h=(n-1).bit_length()
        LEN=h
        while(LEN):
            if (LEN==1):
                p=1<<(h-LEN)
                irot=1
                for s in range(1<<(LEN-1)):
                    offset=s<<(h-LEN+1)
                    for i in range(p):
                        l=a[i+offset]
                        r=a[i+offset+p]
                        a[i+offset]=(l+r)%self.mod
                        a[i+offset+p]=(l-r)*irot%self.mod
                    irot*=self.irate2[(~s & -~s).bit_length()-1]
                    irot%=self.mod
                LEN-=1
            else:
                p=1<<(h-LEN)
                irot=1
                iimag=self.iroot[2]
                for s in range(1<<(LEN-2)):
                    irot2=(irot*irot)%self.mod
                    irot3=(irot*irot2)%self.mod
                    offset=s<<(h-LEN+2)
                    for i in range(p):
                        a0=a[i+offset]
                        a1=a[i+offset+p]
                        a2=a[i+offset+2*p]
                        a3=a[i+offset+3*p]
                        a2na3iimag=(a2-a3)*iimag%self.mod
                        a[i+offset]=(a0+a1+a2+a3)%self.mod
                        a[i+offset+p]=(a0-a1+a2na3iimag)*irot%self.mod
                        a[i+offset+2*p]=(a0+a1-a2-a3)*irot2%self.mod
                        a[i+offset+3*p]=(a0-a1-a2na3iimag)*irot3%self.mod
                    irot*=self.irate3[(~s & -~s).bit_length()-1]
                    irot%=self.mod
                LEN-=2
    def convolution(self,a,b):
        n=len(a);m=len(b)
        if not(a) or not(b):
            return []
        if min(n,m)<=40:
            res=[0]*(n+m-1)
            for i in range(n):
                for j in range(m):
                    res[i+j]+=a[i]*b[j]
                    res[i+j]%=self.mod
            return res
        z=1<<((n+m-2).bit_length())
        a=a+[0]*(z-n)
        b=b+[0]*(z-m)
        self.butterfly(a)
        self.butterfly(b)
        c=[(a[i]*b[i])%self.mod for i in range(z)]
        self.butterfly_inv(c)
        iz=pow(z,self.mod-2,self.mod)
        for i in range(n+m-1):
            c[i]=(c[i]*iz)%self.mod
        return c[:n+m-1]

mod=998244353
fft=FFT(mod)

def powfft(f,n):
    if n==1:
        return f
    ff=fft.convolution(f,f)
    res=powfft(ff,n//2)
    if n%2==1:
        res=fft.convolution(res,f)
    return res

def bin(a,b,f,rf):
    if a<0 or b<0 or a<b:
        return 0
    return f[a]*rf[b]%mod*rf[a-b]%mod

def main():  
    n,m=read_values()
    c={}
    for _ in range(m):
        l,r=read_values()
        c[r-l+1]=c.get(r-l+1,0)+1
    if n%2:
        print(0)
        return
    f=[1]*(n+1)
    rf=[1]*(n+1)
    r=[1]*(n+1)
    for i in range(n):
        f[i+1]=f[i]*(i+1)%mod
        rf[i+1]=pow(f[i+1],-1,mod)
        r[i+1]=pow(i+1,-1,mod)
    
    p=[1]
    for k,v in c.items():
        if k%2==1 or v==0:
            continue
        mul=[0]*(k//2+1)
        mul[0]=1
        mul[k//2]=-bin(k,k//2,f,rf)*r[k//2+1]%mod
        if v<=40:
            for _ in range(v):
                pp=[0]*(len(p)+k//2)
                for i in range(len(p)):
                    pp[i]=(pp[i]+p[i])%mod
                    pp[i+k//2]=(pp[i+k//2]+p[i]*mul[k//2])%mod
                p=pp
        else:
            res=powfft(mul,v)
            p=fft.convolution(p,res)
    ans=0
    for i in range(len(p)):
        ans+=p[i]*bin(n-i*2,n//2-i,f,rf)*r[n//2-i+1]%mod
        ans%=mod
    print(ans)
        
if __name__ == "__main__":
    main()
0