結果
問題 | No.109 N! mod M |
ユーザー | 👑 cologne |
提出日時 | 2022-02-02 17:12:30 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 215 ms / 5,000 ms |
コード長 | 884 bytes |
コンパイル時間 | 293 ms |
コンパイル使用メモリ | 87,088 KB |
実行使用メモリ | 76,244 KB |
最終ジャッジ日時 | 2023-09-02 02:51:53 |
合計ジャッジ時間 | 2,111 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 73 ms
75,228 KB |
testcase_01 | AC | 165 ms
75,396 KB |
testcase_02 | AC | 182 ms
75,252 KB |
testcase_03 | AC | 71 ms
71,212 KB |
testcase_04 | AC | 76 ms
75,524 KB |
testcase_05 | AC | 215 ms
76,244 KB |
testcase_06 | AC | 95 ms
76,128 KB |
testcase_07 | AC | 81 ms
75,336 KB |
testcase_08 | AC | 73 ms
75,520 KB |
ソースコード
import itertools import sys def input(): return sys.stdin.readline().rstrip() def isPrime(N): for i in itertools.count(2): if i*i > N: return True if N % i == 0: return False def fact(N, M): # M - N <= LIM LIM = 10**5 if N >= M: # trivial case return 0 if M <= 2*LIM: # small case a = 1 % M for i in range(1, N+1): a = a * i % M return a elif isPrime(M): # prime case, (M-1)! == -1 (mod M) a = M-1 for i in range(N+1, M): a = a * i % M return pow(a, -1, M) else: # M = pq, q <= M/p <= M/2 <= M/2 + (M/2 - LIM) <= N return 0 def main(): T = int(input()) for i in range(T): N, M = map(int, input().split()) print(fact(N, M)) if __name__ == '__main__': main()