結果

問題 No.2474 Empty Quartz
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2023-07-22 03:39:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,525 bytes
コンパイル時間 598 ms
コンパイル使用メモリ 87,168 KB
実行使用メモリ 94,688 KB
最終ジャッジ日時 2023-09-02 16:16:08
合計ジャッジ時間 2,578 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
92,260 KB
testcase_01 AC 112 ms
92,536 KB
testcase_02 AC 172 ms
94,688 KB
testcase_03 AC 159 ms
94,508 KB
testcase_04 AC 190 ms
93,704 KB
testcase_05 AC 217 ms
93,260 KB
testcase_06 AC 199 ms
93,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

Empty Quartz (7) 想定解

"""

import sys
from sys import stdin

mod = 998244353

from collections import deque

def modfac(n, MOD):
 
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
    return factorials, invs

def modnCr(n,r):
    if n < 0 or r < 0 or n < r:
        return 0
    return fac[n] * inv[n-r] * inv[r] % mod

fac,inv = modfac(200000,mod)

T = int(stdin.readline())

assert 1 <= T <= 10**5

ANS = []

for loop in range(T):

    N,K = map(int,stdin.readline().split())
    
    assert 1 <= N <= 10**5
    assert 0 <= K <= N*(N+1)//2

    # x^2 - (N+1)x + K = 0 を解く
    in_root = (N+1)**2 - 4 * K

    if in_root < 0:
        ANS.append(0)
        continue

    # 10^6 > root の時、pythonでこの処理で大丈夫なことはチェック済(下部)
    root = int(in_root**0.5)
    if root**2 != in_root:
        ANS.append(0)
        continue

    topa = (N+1) + root
    topb = (N+1) - root

    ans = 0
    if topa % 2 == 0 and 1 <= topa // 2 <= N+1:
        ans += modnCr(N,topa//2-1)
    if topb % 2 == 0 and topa != topb and 1 <= topb <= N+1:
        ans += modnCr(N,topb//2-1)
    ANS.append(ans % mod)

print (*ANS,sep="\n")

"""    
# 精度チェック用
for i in range(1,10**6):
	
	x = i**2
	
	y = int(x**0.5)
	
	if y**2 != x:
		print (x,y)
"""
0