結果

問題 No.2303 Frog on Grid
ユーザー kyskys
提出日時 2023-05-13 18:16:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,024 ms / 2,000 ms
コード長 6,782 bytes
コンパイル時間 148 ms
コンパイル使用メモリ 82,564 KB
実行使用メモリ 160,092 KB
最終ジャッジ日時 2024-05-06 23:14:26
合計ジャッジ時間 13,343 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 234 ms
122,868 KB
testcase_01 AC 237 ms
123,400 KB
testcase_02 AC 629 ms
148,548 KB
testcase_03 AC 422 ms
132,188 KB
testcase_04 AC 627 ms
146,288 KB
testcase_05 AC 631 ms
148,568 KB
testcase_06 AC 430 ms
132,440 KB
testcase_07 AC 621 ms
142,932 KB
testcase_08 AC 618 ms
144,216 KB
testcase_09 AC 332 ms
126,812 KB
testcase_10 AC 428 ms
131,548 KB
testcase_11 AC 340 ms
126,684 KB
testcase_12 AC 427 ms
134,228 KB
testcase_13 AC 235 ms
122,872 KB
testcase_14 AC 232 ms
123,228 KB
testcase_15 AC 242 ms
123,092 KB
testcase_16 AC 236 ms
123,352 KB
testcase_17 AC 229 ms
123,476 KB
testcase_18 AC 1,015 ms
160,092 KB
testcase_19 AC 1,024 ms
159,696 KB
testcase_20 AC 1,020 ms
159,448 KB
testcase_21 AC 1,005 ms
159,704 KB
testcase_22 AC 999 ms
159,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    from sys import stdin, setrecursionlimit
    # setrecursionlimit(1000000)
    input = stdin.readline
    def iinput(): return int(input())
    def sinput(): return input().rstrip()
    def i0input(): return int(input()) - 1
    def linput(): return list(input().split())
    def liinput(): return list(map(int, input().split()))
    def miinput(): return map(int, input().split())
    def li0input(): return list(map(lambda x: int(x) - 1, input().split()))
    def mi0input(): return map(lambda x: int(x) - 1, input().split())
    INF = 1000000000000000000
    MOD = 998244353


    def modinv(a):
        b = MOD
        u, v = 1, 0
        while b > 0:
            t = a // b
            a -= t * b
            a, b = b, a
            u -= t * v
            u, v = v, u
        return u % MOD

    class Combination:
        def __init__(self, N, MOD):
            self.factorial = [1]
            self.inv_factorial = [1]
            self.mod = MOD
            for i in range(1, N+1):
                self.factorial.append(self.factorial[-1] * i % MOD)
                self.inv_factorial.append(self.inv_factorial[-1] * modinv(i) % MOD)
        def combi(self, n, k):
            return self.factorial[n] * self.inv_factorial[k] % self.mod * self.inv_factorial[n-k] % self.mod

    cmb = Combination(404040, MOD)

    def counter(N):
        ans = dict()
        for k in range((N+1)//2, N+1):
            ans[k] = cmb.combi(k, N-k)
        return ans

    H, W = miinput()
    

    def solve_naive(H, W):
        ans = 0
        for h in range((H+1)//2, H+1):
            for w in range((W+1)//2, W+1):
                ans += cmb.factorial[h+w] * cmb.inv_factorial[H-h] % MOD * cmb.inv_factorial[2*h-H] % MOD * cmb.inv_factorial[W-w] % MOD * cmb.inv_factorial[2*w-W] % MOD
                ans %= MOD
        return ans
    
    def solve(H, W):
        fft = FFT(MOD)
        ans = 0
        Wlist = []
        for w in range((W+1)//2, W+1):
            Wlist.append(cmb.inv_factorial[W-w] * cmb.inv_factorial[2*w-W] % MOD)
        HWlist = []
        for hw in range((H+1)//2 + (W+1)//2, H+W+1):
            HWlist.append(cmb.factorial[hw])
        Sum = fft.convolution(Wlist[::-1], HWlist)
        for i, h in enumerate(range((H+1)//2, H+1)):
            ans += Sum[i+len(Wlist)-1] % MOD * cmb.inv_factorial[H-h] % MOD * cmb.inv_factorial[2*h-H] % MOD
            ans %= MOD
        return ans
    
    # print(solve_naive(H, W))
    print(solve(H, W))
        

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
    butterfly_first=True
    butterfly_inv_first=True
    sum_e=[0]*30
    sum_ie=[0]*30
    def __init__(self,MOD):
        self.mod=MOD
        self.g=self.primitive_root_constexpr(self.mod)
    def butterfly(self,a):
        n=len(a)
        h=(n-1).bit_length()
        if self.butterfly_first:
            self.butterfly_first=False
            es=[0]*30
            ies=[0]*30
            cnt2=self.bsf(self.mod-1)
            e=pow(self.g,(self.mod-1)>>cnt2,self.mod)
            ie=pow(e,self.mod-2,self.mod)
            for i in range(cnt2,1,-1):
                es[i-2]=e
                ies[i-2]=ie
                e=(e*e)%self.mod
                ie=(ie*ie)%self.mod
            now=1
            for i in range(cnt2-2):
                self.sum_e[i]=((es[i]*now)%self.mod)
                now*=ies[i]
                now%=self.mod
        for ph in range(1,h+1):
            w=1<<(ph-1)
            p=1<<(h-ph)
            now=1
            for s in range(w):
                offset=s<<(h-ph+1)
                for i in range(p):
                    l=a[i+offset]
                    r=a[i+offset+p]*now
                    r%=self.mod
                    a[i+offset]=l+r
                    a[i+offset]%=self.mod
                    a[i+offset+p]=l-r
                    a[i+offset+p]%=self.mod
                now*=self.sum_e[(~s & -~s).bit_length()-1]
                now%=self.mod
    def butterfly_inv(self,a):
        n=len(a)
        h=(n-1).bit_length()
        if self.butterfly_inv_first:
            self.butterfly_inv_first=False
            es=[0]*30
            ies=[0]*30
            cnt2=self.bsf(self.mod-1)
            e=pow(self.g,(self.mod-1)>>cnt2,self.mod)
            ie=pow(e,self.mod-2,self.mod)
            for i in range(cnt2,1,-1):
                es[i-2]=e
                ies[i-2]=ie
                e=(e*e)%self.mod
                ie=(ie*ie)%self.mod
            now=1
            for i in range(cnt2-2):
                self.sum_ie[i]=((ies[i]*now)%self.mod)
                now*=es[i]
                now%=self.mod
        for ph in range(h,0,-1):
            w=1<<(ph-1)
            p=1<<(h-ph)
            inow=1
            for s in range(w):
                offset=s<<(h-ph+1)
                for i in range(p):
                    l=a[i+offset]
                    r=a[i+offset+p]
                    a[i+offset]=l+r
                    a[i+offset]%=self.mod
                    a[i+offset+p]=(l-r)*inow
                    a[i+offset+p]%=self.mod
                inow*=self.sum_ie[(~s & -~s).bit_length()-1]
                inow%=self.mod
    def convolution(self,a,b):
        n=len(a);m=len(b)
        if not(a) or not(b):
            return []
        if min(n,m)<=40:
            if n<m:
                n,m=m,n
                a,b=b,a
            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=[0]*z
        for i in range(z):
            c[i]=(a[i]*b[i])%self.mod
        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]

        

main()
0