結果

問題 No.109 N! mod M
ユーザー cielciel
提出日時 2015-11-01 00:40:56
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 372 bytes
コンパイル時間 604 ms
コンパイル使用メモリ 11,212 KB
実行使用メモリ 15,384 KB
最終ジャッジ日時 2023-10-11 07:35:29
合計ジャッジ時間 3,804 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 117 ms
15,144 KB
testcase_04 WA -
testcase_05 AC 644 ms
15,360 KB
testcase_06 AC 425 ms
15,348 KB
testcase_07 WA -
testcase_08 AC 125 ms
15,032 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#!/usr/bin/ruby
gets.to_i.times{
	n,m=gets.split.map(&:to_i)
	a=0
	#b=1
	if (4..Math.sqrt(m)).each{|b|
		if m%b<1
			if n<b+m/b
				a=1
				while n>0
					a=a*n%m
					n-=1
				end
			end
			break
		end
	} then
		if n<m
			b=1
			n+=1
			while n<m
				b=b*n%m
				n+=1
			end
			a=m-1
			n-=2
			while n>0
				a=a*b%m if n%2>0
				b=b*b%m
				n/=2
			end
		end
	end
	p a
}
0