結果

問題 No.187 中華風 (Hard)
ユーザー koyumeishikoyumeishi
提出日時 2015-04-20 03:12:01
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,382 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 11,304 KB
実行使用メモリ 15,456 KB
最終ジャッジ日時 2023-09-17 23:21:48
合計ジャッジ時間 40,218 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,719 ms
15,388 KB
testcase_01 AC 1,712 ms
15,432 KB
testcase_02 AC 1,745 ms
15,284 KB
testcase_03 AC 1,716 ms
15,280 KB
testcase_04 AC 2,404 ms
15,276 KB
testcase_05 AC 2,405 ms
15,304 KB
testcase_06 AC 2,415 ms
15,376 KB
testcase_07 AC 2,405 ms
15,456 KB
testcase_08 AC 1,483 ms
15,180 KB
testcase_09 AC 1,480 ms
15,272 KB
testcase_10 AC 1,485 ms
15,396 KB
testcase_11 AC 2,399 ms
15,236 KB
testcase_12 AC 2,405 ms
15,284 KB
testcase_13 AC 524 ms
15,196 KB
testcase_14 AC 534 ms
15,152 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 78 ms
15,120 KB
testcase_18 AC 1,251 ms
15,212 KB
testcase_19 AC 81 ms
15,340 KB
testcase_20 AC 1,851 ms
15,360 KB
testcase_21 AC 81 ms
15,300 KB
testcase_22 AC 2,401 ms
15,396 KB
testcase_23 WA -
testcase_24 AC 80 ms
15,140 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

#start = Time.now()

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


#STDERR.puts Time.now - start

MOD = 10**9 + 7

valid = true

for i in 0...N do
	for j in i+1...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


#STDERR.puts Time.now - start

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

#STDERR.puts Time.now - start

tmp = 1
for i in 0...N do
	ans = (ans + x[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

#STDERR.puts Time.now - start
0