結果

問題 No.1092 modular arithmetic
ユーザー 小野寺健小野寺健
提出日時 2021-11-05 09:43:07
言語 Ruby
(3.3.0)
結果
AC  
実行時間 378 ms / 2,000 ms
コード長 560 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 22,272 KB
最終ジャッジ日時 2024-04-23 17:40:31
合計ジャッジ時間 7,390 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
12,288 KB
testcase_01 AC 148 ms
22,272 KB
testcase_02 AC 74 ms
12,288 KB
testcase_03 AC 210 ms
21,120 KB
testcase_04 AC 191 ms
18,176 KB
testcase_05 AC 190 ms
18,176 KB
testcase_06 AC 160 ms
15,616 KB
testcase_07 AC 183 ms
18,432 KB
testcase_08 AC 194 ms
20,480 KB
testcase_09 AC 186 ms
18,304 KB
testcase_10 AC 168 ms
18,048 KB
testcase_11 AC 174 ms
18,176 KB
testcase_12 AC 153 ms
15,616 KB
testcase_13 AC 126 ms
18,304 KB
testcase_14 AC 135 ms
20,224 KB
testcase_15 AC 89 ms
13,440 KB
testcase_16 AC 123 ms
18,048 KB
testcase_17 AC 138 ms
20,096 KB
testcase_18 AC 209 ms
20,992 KB
testcase_19 AC 154 ms
15,488 KB
testcase_20 AC 215 ms
21,248 KB
testcase_21 AC 181 ms
18,176 KB
testcase_22 AC 206 ms
20,736 KB
testcase_23 AC 334 ms
18,176 KB
testcase_24 AC 157 ms
13,696 KB
testcase_25 AC 285 ms
15,616 KB
testcase_26 AC 320 ms
18,176 KB
testcase_27 AC 110 ms
12,672 KB
testcase_28 AC 273 ms
15,616 KB
testcase_29 AC 378 ms
20,480 KB
testcase_30 AC 270 ms
15,744 KB
testcase_31 AC 101 ms
12,544 KB
testcase_32 AC 254 ms
15,488 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Mod
	def initialize(m)
		@MOD = m
	end
	def modPow(x, n)
		res = 1
		while n > 0 do
			res = res * x % @MOD if n & 1 != 0
			x = x * x % @MOD
			n >>= 1
		end
		res
	end
	def modInv(x)
		modPow(x, @MOD - 2)
	end
end

P, N = gets.split(" ").map{|s| s.to_i}
A = gets.split(" ").map{|s| s.to_i}
S = gets.strip

ans = A[0]

mod = Mod.new(P)

0.upto(N-2) {|i|
	x = A[i+1]
	case S[i]
	when "+" then
		ans = (ans + x) % P
	when "-" then
		ans = (ans - x) % P
	when "*" then
		ans = (ans * x) % P
	when "/" then
		ans = (ans * mod.modInv(x)) % P
	end
}

puts ans
0