結果

問題 No.2503 Typical Path Counting Problem on a Grid
ユーザー ニックネームニックネーム
提出日時 2023-10-13 23:56:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,234 ms / 2,000 ms
コード長 971 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 394,444 KB
最終ジャッジ日時 2023-10-13 23:56:43
合計ジャッジ時間 10,918 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 657 ms
389,000 KB
testcase_01 AC 619 ms
390,724 KB
testcase_02 AC 519 ms
390,228 KB
testcase_03 AC 855 ms
392,880 KB
testcase_04 AC 1,159 ms
392,280 KB
testcase_05 AC 769 ms
391,352 KB
testcase_06 AC 1,224 ms
394,356 KB
testcase_07 AC 1,234 ms
394,444 KB
testcase_08 AC 1,088 ms
393,472 KB
testcase_09 AC 1,174 ms
392,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def matrix_exponentiation(a,r,b):
    n = len(a); m = r.bit_length()
    res = [a]+[[[0]*n for _ in range(n)] for _ in range(m-1)]
    for h in range(m-1):
        for i in range(n):
            for j in range(n):
                for k in range(n): res[h+1][i][j] += res[h][i][k]*res[h][k][j]%mod
                res[h+1][i][j] %= mod
    for h in range(m):
        if not r>>h&1: continue
        tmp = [0]*n
        for i in range(n):
            for j in range(n): tmp[i] += res[h][i][j]*b[j]%mod
            tmp[i] %= mod
        b = tmp
    return b
mod = 998244353; k = 10**7
a = list(range(2,2*k+2,2))
b = list(range(1,k+1))
dp = [1]+[0]*k
for i in range(k):
    if i<=k-2: dp[i+2] = dp[i]*b[i]%mod
    dp[i+1] = (dp[i+1]+dp[i]*a[i])%mod
for _ in range(int(input())):
    n,m = map(int,input().split())
    if n>m: n,m = m,n
    if n==0: print(1); continue
    x,y = matrix_exponentiation([[2*n+1,n],[1,0]],m-n,[dp[n],dp[n-1]])
    print((x*dp[n]+y*n*dp[n-1])%mod)
0