結果

問題 No.1092 modular arithmetic
ユーザー 小野寺健
提出日時 2021-11-05 09:43:07
言語 Ruby
(3.4.1)
結果
AC  
実行時間 436 ms / 2,000 ms
コード長 560 bytes
コンパイル時間 582 ms
コンパイル使用メモリ 7,296 KB
実行使用メモリ 22,144 KB
最終ジャッジ日時 2024-10-15 18:16:43
合計ジャッジ時間 9,678 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
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