結果

問題 No.109 N! mod M
ユーザー cielciel
提出日時 2015-11-01 00:42:43
言語 Ruby
(3.3.0)
結果
AC  
実行時間 641 ms / 5,000 ms
コード長 360 bytes
コンパイル時間 35 ms
コンパイル使用メモリ 11,580 KB
実行使用メモリ 15,304 KB
最終ジャッジ日時 2023-09-04 05:27:45
合計ジャッジ時間 2,533 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
15,288 KB
testcase_01 AC 134 ms
15,124 KB
testcase_02 AC 141 ms
15,132 KB
testcase_03 AC 97 ms
15,304 KB
testcase_04 AC 96 ms
15,260 KB
testcase_05 AC 641 ms
15,276 KB
testcase_06 AC 420 ms
15,152 KB
testcase_07 AC 114 ms
15,280 KB
testcase_08 AC 113 ms
15,276 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#!/usr/bin/ruby
gets.to_i.times{
	n,m=gets.split.map(&:to_i)
	a=0
	if (2..Math.sqrt(m)).each{|b|
		if m%b<1
			if n<b+m/b
				a=1
				n.downto(1){|n|
					a=a*n%m
				}
			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