結果

問題 No.148 試験監督(3)
ユーザー gew1fw
提出日時 2025-06-12 20:55:22
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,899 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 82,272 KB
実行使用メモリ 75,808 KB
最終ジャッジ日時 2025-06-12 20:59:05
合計ジャッジ時間 4,700 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other TLE * 1 -- * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

def add_one(s):
    s = list(s)
    i = len(s) - 1
    carry = 1
    while i >= 0 and carry:
        digit = int(s[i]) + carry
        if digit == 10:
            s[i] = '0'
            carry = 1
        else:
            s[i] = str(digit)
            carry = 0
        i -= 1
    if carry:
        s = ['1'] + s
    return ''.join(s)

def multiply_two(s):
    if s == '0':
        return '0'
    res = []
    carry = 0
    for c in reversed(s):
        d = int(c) * 2 + carry
        carry = d // 10
        res.append(str(d % 10))
    if carry:
        res.append(str(carry))
    return ''.join(reversed(res))

def compare_str(a, b):
    if len(a) > len(b):
        return True
    elif len(a) < len(b):
        return False
    else:
        return a > b

def is_p_ge_mod(p_str, mod_str="1000000007"):
    if len(p_str) > len(mod_str):
        return True
    elif len(p_str) < len(mod_str):
        return False
    else:
        return p_str >= mod_str

def mod_large_number(s, mod):
    res = 0
    for c in s:
        res = (res * 10 + int(c)) % mod
    return res

T = int(input())
for _ in range(T):
    C_str, P_str = input().split()
    # Check if 2*P > C+1
    P_times_2 = multiply_two(P_str)
    C_plus_1 = add_one(C_str)
    if compare_str(P_times_2, C_plus_1):
        print(0)
        continue
    # Check if P >= MOD
    if is_p_ge_mod(P_str):
        print(0)
        continue
    # Convert P to integer
    p = int(P_str)
    if p >= MOD:
        print(0)
        continue
    # Compute n mod MOD = (C - p + 1) mod MOD
    c_mod = mod_large_number(C_str, MOD)
    n_mod = (c_mod - p + 1) % MOD
    if n_mod < 0:
        n_mod += MOD
    a = n_mod
    # Check a >= p
    if a < p:
        print(0)
        continue
    # Compute product_{i=0}^{p-1} (a - i) mod MOD
    product = 1
    for i in range(p):
        product = product * (a - i) % MOD
    print(product)
0