結果

問題 No.187 中華風 (Hard)
ユーザー koyumeishikoyumeishi
提出日時 2015-04-20 02:45:05
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 1,264 bytes
コンパイル時間 442 ms
コンパイル使用メモリ 11,224 KB
実行使用メモリ 15,480 KB
最終ジャッジ日時 2023-09-17 23:00:29
合計ジャッジ時間 49,278 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,143 ms
15,168 KB
testcase_01 AC 2,103 ms
15,440 KB
testcase_02 AC 2,154 ms
15,336 KB
testcase_03 AC 2,130 ms
15,156 KB
testcase_04 AC 2,968 ms
15,264 KB
testcase_05 AC 2,978 ms
15,436 KB
testcase_06 TLE -
testcase_07 AC 2,959 ms
15,152 KB
testcase_08 AC 1,854 ms
15,348 KB
testcase_09 AC 1,852 ms
15,412 KB
testcase_10 AC 1,854 ms
15,472 KB
testcase_11 AC 2,979 ms
15,408 KB
testcase_12 AC 2,946 ms
15,172 KB
testcase_13 AC 653 ms
15,208 KB
testcase_14 AC 664 ms
15,232 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 83 ms
15,128 KB
testcase_18 AC 1,556 ms
15,376 KB
testcase_19 AC 81 ms
15,260 KB
testcase_20 AC 2,263 ms
15,204 KB
testcase_21 AC 83 ms
15,196 KB
testcase_22 AC 2,969 ms
15,352 KB
testcase_23 WA -
testcase_24 AC 84 ms
15,024 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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