結果

問題 No.148 試験監督(3)
ユーザー lam6er
提出日時 2025-03-20 20:37:19
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,314 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 73,728 KB
最終ジャッジ日時 2025-03-20 20:38:17
合計ジャッジ時間 4,704 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other TLE * 1 -- * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

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

def subtract_one(s):
    s_list = list(s)
    i = len(s_list) - 1
    while i >= 0 and s_list[i] == '0':
        s_list[i] = '9'
        i -= 1
    if i >= 0:
        s_list[i] = str(int(s_list[i]) - 1)
    if s_list[0] == '0' and len(s_list) > 1:
        return ''.join(s_list[1:])
    return ''.join(s_list)

def is_greater_or_equal(a_str, b_str):
    if len(a_str) > len(b_str):
        return True
    if len(a_str) < len(b_str):
        return False
    return a_str >= b_str

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

def main():
    import sys
    input = sys.stdin.read().split()
    T = int(input[0])
    ptr = 1
    for _ in range(T):
        C = input[ptr]
        P = input[ptr+1]
        ptr +=2
        
        # Compute 2*P -1
        twoP = multiply_two(P)
        twoP_minus1 = subtract_one(twoP)
        
        # Check C >= twoP_minus1
        if not is_greater_or_equal(C, twoP_minus1):
            print(0)
            continue
        
        # Calculate mod values
        c_mod = mod_of_string(C, MOD)
        p_mod = mod_of_string(P, MOD)
        term_base = (c_mod - p_mod + 1) % MOD
        term_base = (term_base + MOD) % MOD  # Ensure non-negative
        
        # Check P's length to determine if P >= MOD
        if len(P) > 10:
            print(0)
            continue
        elif len(P) == 10:
            if P > '1000000007':
                print(0)
                continue
            else:
                P_val = int(P)
                if P_val >= MOD:
                    print(0)
                    continue
        else:
            P_val = int(P)
            if P_val >= MOD:
                print(0)
                continue
        
        if term_base < P_val:
            print(0)
            continue
        
        # Compute permutation(term_base, P_val)
        perm = 1
        for i in range(P_val):
            perm = (perm * (term_base - i)) % MOD
        print(perm)

if __name__ == '__main__':
    main()
0