結果

問題 No.148 試験監督(3)
ユーザー gew1fw
提出日時 2025-06-12 21:00:18
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,153 bytes
コンパイル時間 234 ms
コンパイル使用メモリ 82,008 KB
実行使用メモリ 74,420 KB
最終ジャッジ日時 2025-06-12 21:03:46
合計ジャッジ時間 4,541 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other TLE * 1 -- * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7
MOD_STR = str(MOD)

def compare_str(a, b):
    if len(a) > len(b):
        return 1
    elif len(a) < len(b):
        return -1
    else:
        for i in range(len(a)):
            if a[i] > b[i]:
                return 1
            elif a[i] < b[i]:
                return -1
        return 0

def multiply_two_and_subtract_one(s):
    carry = 0
    result = []
    for c in reversed(s):
        digit = int(c)
        total = digit * 2 + carry
        carry = total // 10
        result.append(total % 10)
    if carry > 0:
        result.append(carry)
    result.reverse()
    two_times = ''.join(map(str, result))
    res = list(two_times)
    i = len(res) - 1
    while i >= 0 and res[i] == '0':
        res[i] = '9'
        i -= 1
    if i >= 0:
        res[i] = str(int(res[i]) - 1)
    else:
        return '0'
    s_res = ''.join(res).lstrip('0')
    return s_res if s_res else '0'

def is_ge_mod(s):
    if len(s) > len(MOD_STR):
        return True
    elif len(s) < len(MOD_STR):
        return False
    else:
        for i in range(len(s)):
            a = s[i]
            b = MOD_STR[i]
            if a > b:
                return True
            elif a < b:
                return False
        return True

def mod_str(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 C < P
    cmp_c_p = compare_str(C_str, P_str)
    if cmp_c_p < 0:
        print(0)
        continue
    
    # Check if 2P-1 > C
    two_p_minus_1_str = multiply_two_and_subtract_one(P_str)
    cmp_2p1_c = compare_str(two_p_minus_1_str, C_str)
    if cmp_2p1_c > 0:
        print(0)
        continue
    
    # Check if P >= MOD
    if is_ge_mod(P_str):
        print(0)
        continue
    
    # Now P is less than MOD
    P_mod = int(P_str)
    C_mod = mod_str(C_str, MOD)
    
    n = (C_mod - P_mod + 1) % MOD
    if n < 0:
        n += MOD
    
    if n < P_mod:
        print(0)
    else:
        result = 1
        for i in range(P_mod):
            result = result * (n - i) % MOD
        print(result)
0