結果

問題 No.2151 3 on Torus-Lohkous
ユーザー snukesnuke
提出日時 2022-10-30 21:57:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 529 ms / 2,000 ms
コード長 782 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 92,160 KB
最終ジャッジ日時 2024-04-22 03:50:54
合計ジャッジ時間 2,861 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
77,568 KB
testcase_01 AC 81 ms
75,904 KB
testcase_02 AC 81 ms
75,648 KB
testcase_03 AC 529 ms
92,032 KB
testcase_04 AC 473 ms
91,776 KB
testcase_05 AC 86 ms
76,032 KB
testcase_06 AC 187 ms
91,904 KB
testcase_07 AC 118 ms
88,064 KB
testcase_08 AC 369 ms
92,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

mod = 998244353

M = 10**6+5
fac,ifac = [1]*M,[1]*M
for i in range(1,M):
  fac[i] = fac[i-1]*i%mod
ifac[-1] = pow(fac[-1],mod-2,mod)
for i in range(M-1,1,-1):
  ifac[i-1] = ifac[i]*i%mod
def comb(n,k):
  return fac[n]*ifac[k]*ifac[n-k]%mod
def inv(x):
  return ifac[x]*fac[x-1]%mod

def solve():
  h,w = map(int,input().split())
  n = math.gcd(h,w)
  ans = h*w
  if 4 <= n:
    ans += n*2
  if 5 <= n:
    if h*w%3 == 0:
      ans += 12*n
  if n%4 == 0:
    ans += 16
  if n%2 == 0:
    for p in range(2):
      now = 0
      for i in range(2-p,n,2):
        m = n//2-i*2
        if m < i*3 or m%3 != 0: continue
        now = (now+comb(m//3-1,i-1)*inv(i))%mod
      now *= n
      ans += now**2
  ans %= mod
  print(ans)

T = int(input())
for ti in range(T):
  solve()
0