結果
問題 | No.109 N! mod M |
ユーザー | ayaoni |
提出日時 | 2021-08-31 20:39:27 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 213 ms / 5,000 ms |
コード長 | 950 bytes |
コンパイル時間 | 271 ms |
コンパイル使用メモリ | 87,228 KB |
実行使用メモリ | 76,592 KB |
最終ジャッジ日時 | 2023-08-17 08:16:52 |
合計ジャッジ時間 | 2,638 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge15 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 76 ms
75,840 KB |
testcase_01 | AC | 169 ms
75,888 KB |
testcase_02 | AC | 200 ms
76,064 KB |
testcase_03 | AC | 74 ms
71,328 KB |
testcase_04 | AC | 86 ms
76,592 KB |
testcase_05 | AC | 213 ms
76,480 KB |
testcase_06 | AC | 92 ms
75,864 KB |
testcase_07 | AC | 85 ms
76,368 KB |
testcase_08 | AC | 76 ms
75,800 KB |
ソースコード
import sys def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) def is_prime(n): for i in range(2,int(n**.5)+1): if n % i == 0: return False return True T = I() for _ in range(T): N,M = MI() if M == 1 or N >= M: ans = 0 elif M <= 2*10**5: ans = 1 for i in range(1,N+1): ans *= i ans %= M else: if is_prime(M): a = -1 for i in range(N+1,M): a *= i a %= M ans = pow(a,M-2,M) else: ans = 0 print(ans)