結果

問題 No.1832 NAND Reversible
ユーザー rlangevin
提出日時 2024-02-09 12:39:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 1,159 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 84,992 KB
最終ジャッジ日時 2024-09-28 12:59:14
合計ジャッジ時間 2,842 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

N, K = map(int, input().split())
if K == 0:
    print(1)
    exit()
if K == 1:
    if N % 2 == 0:
        print(2)
    else:
        print(N - 2)
    exit()
mod = 998244353
N, K = N - 2, K - 2
if K == 0:
    print(pow((N+2)//2, 2, mod))
    exit()
if K == N:
    print(1)
    exit()

n = 505050
fact = [1] * (n + 1)
invfact = [1] * (n + 1)
for i in range(1, n):
    fact[i + 1] = ((i+1) * fact[i]) % mod
invfact[n] = pow(fact[n], mod - 2, mod)
for i in range(n - 1, -1, -1):
    invfact[i] = invfact[i + 1] * (i + 1) % mod

def comb(n, r):
    if n < 0 or r < 0 or n - r < 0:
        return 0
    return fact[n] * invfact[r] * invfact[n - r] % mod

ans = 0
for i in range(1, N):
    x = i + 1
    y = 1
    dx = N - x
    dy = K - y
    a = min(dx, dy)
    b = max(dx - a, dy - a)
    if a < 0 or b < 0:
        break
    if dx < dy:
        break
    ans += comb(a+b,a) * pow((i+2)//2, 2, mod)
    ans %= mod
    
for i in range(1, N):
    x = i + 1
    y = i
    dx = N - x
    dy = K - y
    a = min(dx, dy)
    b = max(dx - a, dy - a)
    if a < 0 or b < 0:
        break
    if dx < dy:
        break
    ans += comb(a+b,a)
    ans %= mod
    
print(ans)
0