結果

問題 No.109 N! mod M
ユーザー cielciel
提出日時 2015-10-31 23:15:02
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 229 ms / 5,000 ms
コード長 300 bytes
コンパイル時間 518 ms
コンパイル使用メモリ 77,664 KB
実行使用メモリ 78,732 KB
最終ジャッジ日時 2023-09-04 05:27:19
合計ジャッジ時間 2,260 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
78,508 KB
testcase_01 AC 102 ms
78,572 KB
testcase_02 AC 105 ms
78,332 KB
testcase_03 AC 89 ms
78,416 KB
testcase_04 AC 92 ms
78,732 KB
testcase_05 AC 229 ms
78,332 KB
testcase_06 AC 123 ms
78,600 KB
testcase_07 AC 99 ms
78,684 KB
testcase_08 AC 88 ms
78,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python
for _ in range(input()):
	n,m=map(int,raw_input().split())
	a=0
	b=1
	while b*-~b<=m:
		b+=1
		if m%b<1:
			if n<b+m/b:
				a=1
				while n:
					a=a*n%m
					n-=1
			break
	else:
		if n<m:
			b=1
			n+=1
			while n<m:
				b=b*n%m
				n+=1
			a=(m-1)*pow(b,max(n-2,0),m)%m
	print(a)
0