結果
| 問題 |
No.109 N! mod M
|
| ユーザー |
|
| 提出日時 | 2021-06-14 23:40:24 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,225 bytes |
| コンパイル時間 | 280 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 60,288 KB |
| 最終ジャッジ日時 | 2024-12-25 10:11:26 |
| 合計ジャッジ時間 | 5,792 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 6 WA * 3 |
ソースコード
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))
def factor(n, m=None):
# mを与えると、高々その素因数まで見て、残りは分解せずにそのまま出力する
f = {}
tmp = n
M = int(-(-n**0.5//1))+1
if m is not None:
M = min(m+1, M)
for i in range(2, M+1):
if tmp<i:
break
if tmp%i==0:
cnt=0
while tmp%i==0:
cnt+=1
tmp //= i
f[i] = cnt
if tmp!=1:
f[tmp] = 1
if not f:
f[n] = 1
return f
def sub(n,m):
if n<2*10**6:
ans = 1
for i in range(1,n+1):
ans *= i
ans %= m
else:
f = factor(m)
if len(f)>=2:
ans = 0
elif n>=m:
ans = 0
else:
ans = -1
for i in range(n+1,m-1)[::-1]:
ans *= pow(i,m-2,m)
ans %= m
return ans%m
t = int(input())
for i in range(t):
n,m = list(map(int, input().split()))
print(sub(n,m))