結果

問題 No.187 中華風 (Hard)
ユーザー koyumeishikoyumeishi
提出日時 2015-04-20 02:43:02
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,267 bytes
コンパイル時間 54 ms
コンパイル使用メモリ 11,592 KB
実行使用メモリ 20,724 KB
最終ジャッジ日時 2023-09-17 22:43:49
合計ジャッジ時間 48,397 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,132 ms
18,080 KB
testcase_01 AC 2,124 ms
18,020 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 84 ms
15,024 KB
testcase_18 AC 1,556 ms
17,204 KB
testcase_19 AC 82 ms
15,160 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:31: warning: assigned but unused variable - d
Main.rb:31: warning: assigned but unused variable - y
Syntax OK

ソースコード

diff #

def gcd(a,b)
	if(b == 0) then
		return a
	end
	return gcd(b, a%b)
end

def lcm(a,b)
	if a<b then
		a,b = b,a
	end
	if(b==1) then
		return a
	end
	return a*(b/gcd(a,b))
end

def extgcd(a,b,x,y)
	d = a
	if(b!=0) then
		d,y,x = extgcd(b, a%b, y,x)
		y -= (a/b) * x
	else
		x = 1
		y = 0
	end
	return d,x,y
end

def mod_inv(a,m)
	d,x,y = extgcd(a,m,-1,-1)
	return (m+x%m)%m
end

N = gets.to_i

x = Array.new(N)
y = Array.new(N)

for i in 0...N do
	x[i],y[i] = gets.split.map{|v| v.to_i}
end

MOD = 10**9 + 7

valid = true

for i in 0...N do
	for j in 0...N do
		if i!=j then
			g = gcd(y[i], y[j])

			if(x[i]%g != x[j]%g) then
				valid = false
			end

			if g != 1 then
				y[i] /= g
				y[j] /= g
				g_ = gcd(y[i], g)
				while g_ != 1 do
					y[i] *= g_
					g /= g_
					g_ = gcd(y[i], g)
				end
				y[j] *= g
				x[i] %= y[i]
				x[j] %= y[j]
			end
		end
	end
end

ans = 0


z = Array.new(N)
for i in 0...N do
	z[i] = x[i]
	for j in 0...i do
		z[i] = mod_inv(y[j], y[i]) * (z[i] - z[j])
		z[i] = (z[i] + y[i]) % y[i]
	end
end

tmp = 1
for i in 0...N do
	ans = (ans + z[i] * tmp) #%MOD
	tmp = (tmp * y[i]) #%MOD
end

if ans == 0 then
	lcm_ = 1
	for i in 0...N do
		lcm_ = lcm(lcm_, y[i]) #%MOD
	end
	ans = lcm_
end

if valid == false then
	ans = -1
end

puts ans
0