結果

問題 No.148 試験監督(3)
ユーザー lam6er
提出日時 2025-04-15 23:37:26
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,857 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 81,840 KB
実行使用メモリ 75,132 KB
最終ジャッジ日時 2025-04-15 23:38:20
合計ジャッジ時間 4,590 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other TLE * 1 -- * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 10**9 + 7

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

def increment_string(s):
    s_list = list(s[::-1])
    carry = 1
    for i in range(len(s_list)):
        num = int(s_list[i]) + carry
        if num >= 10:
            carry = 1
            num -= 10
        else:
            carry = 0
        s_list[i] = str(num)
    if carry:
        s_list.append('1')
    return ''.join(s_list[::-1])

def divide_by_two(s):
    if s == '0':
        return '0'
    result = []
    carry = 0
    for c in s:
        current = carry * 10 + int(c)
        quotient = current // 2
        carry = current % 2
        result.append(str(quotient))
    res_str = ''.join(result).lstrip('0')
    return res_str if res_str else '0'

def compare_strings(a, b):
    if len(a) < len(b):
        return True
    elif len(a) > len(b):
        return False
    else:
        for i in range(len(a)):
            a_digit = int(a[i])
            b_digit = int(b[i])
            if a_digit < b_digit:
                return True
            elif a_digit > b_digit:
                return False
        return True

T = int(input())
for _ in range(T):
    C_str, P_str = input().split()
    
    # Compute max_people = (C + 1) // 2
    C_plus_1 = increment_string(C_str)
    max_people_str = divide_by_two(C_plus_1)
    
    # Check if P exceeds max_people
    if not compare_strings(P_str, max_people_str):
        print(0)
        continue
    
    # Calculate mod values
    a = mod_string(C_str, mod)
    b = mod_string(P_str, mod)
    
    if b == 0:
        print(0)
        continue
    
    start = (a - b + 1) % mod
    if start < 0:
        start += mod
    
    result = 1
    for i in range(b):
        term = (start - i) % mod
        result = (result * term) % mod
    
    print(result)
0