結果

問題 No.500 階乗電卓
ユーザー gew1fw
提出日時 2025-06-12 19:25:41
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,274 bytes
コンパイル時間 458 ms
コンパイル使用メモリ 81,836 KB
実行使用メモリ 91,576 KB
最終ジャッジ日時 2025-06-12 19:25:48
合計ジャッジ時間 7,132 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

def compute_factorial_mod(N):
    mod_10e12 = 10 ** 12
    total_2 = 0
    total_5 = 0

    # Step 1: Count the number of 2s and 5s in N!
    for i in range(1, N + 1):
        x = i
        while x % 2 == 0:
            total_2 += 1
            x //= 2
        while x % 5 == 0:
            total_5 += 1
            x //= 5

    k = min(total_2, total_5)
    if k >= 12:
        return 0

    remaining_2 = total_2 - k
    remaining_5 = total_5 - k
    required_mod = 10 ** (12 - k)

    # Step 2: Compute the product of rest (without 2 and 5) modulo required_mod
    rest_product = 1
    for i in range(1, N + 1):
        x = i
        # Remove all factors of 2 and 5
        while x % 2 == 0:
            x //= 2
        while x % 5 == 0:
            x //= 5
        rest_product = (rest_product * x) % required_mod

    # Step 3: Multiply by remaining 2^remaining_2 and 5^remaining_5
    rest_product = (rest_product * pow(2, remaining_2, required_mod)) % required_mod
    rest_product = (rest_product * pow(5, remaining_5, required_mod)) % required_mod

    # Step 4: Multiply by 10^k and return mod 10^12
    result = (rest_product * (10 ** k)) % mod_10e12
    return result

# Read input and output the result
N = int(input())
print(f"{compute_factorial_mod(N):012d}")
0