結果
問題 |
No.500 階乗電卓
|
ユーザー |
![]() |
提出日時 | 2025-05-14 13:03:17 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,355 bytes |
コンパイル時間 | 232 ms |
コンパイル使用メモリ | 83,008 KB |
実行使用メモリ | 54,136 KB |
最終ジャッジ日時 | 2025-05-14 13:04:52 |
合計ジャッジ時間 | 1,926 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 7 WA * 13 |
ソースコード
import sys # Set the modulus for the last 12 digits MOD = 1000000000000 def solve(): """ Reads the input N, calculates N! mod 10^12, and prints the result. """ # Read the input integer N n_str = sys.stdin.readline() # Strip potential whitespace and convert to integer try: n = int(n_str.strip()) except ValueError: # Handle potential errors if input is not a valid integer # Although constraints say N is a positive integer, adding a check is robust. print("Invalid input") return # Constraint check (though the problem guarantees 1 <= N) if n <= 0: # Factorial is not defined for non-positive integers in this context # Or handle as per specific requirements if needed. # Based on the problem, N is always a positive integer. # This branch might not be strictly necessary but adds safety. print("Input must be a positive integer.") return # Optimization: # If N is large enough, N! will have many trailing zeros. # The number of trailing zeros in N! is determined by the number of factors of 5. # Number of factors of 5 = floor(N/5) + floor(N/25) + floor(N/125) + ... # We need the result modulo 10^12 = (2*5)^12 = 2^12 * 5^12. # If N! has at least 12 factors of 5 and 12 factors of 2 (which it will if it has 12 factors of 5), # then N! is divisible by 10^12. # Let's find the smallest N for which the number of factors of 5 is >= 12. # N=5: 1 # N=10: 2 # N=15: 3 # N=20: 4 # N=25: 5 + 1 = 6 # N=30: 7 # N=35: 8 # N=40: 9 # N=45: 10 # N=50: 10 + 2 = 12 # So, for N >= 50, N! mod 10^12 is 0. if n >= 50: print(0) else: # For N < 50, calculate N! mod 10^12 iteratively. result = 1 for i in range(1, n + 1): result = (result * i) % MOD # Optimization: if result becomes 0, it will stay 0 # This happens if i >= 25 and the number of factors of 5 reaches 12 # and enough factors of 2 are accumulated. # However, with N < 50, this check might not save much time # and the main N>=50 check already handles the guaranteed zero case. # if result == 0: # break print(result) # Run the solve function solve()