結果

問題 No.186 中華風 (Easy)
ユーザー koyumeishikoyumeishi
提出日時 2015-04-20 02:42:14
言語 Ruby
(3.3.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 1,259 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 11,340 KB
実行使用メモリ 15,316 KB
最終ジャッジ日時 2023-09-27 00:44:46
合計ジャッジ時間 3,230 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
15,116 KB
testcase_01 AC 76 ms
15,180 KB
testcase_02 AC 78 ms
15,080 KB
testcase_03 AC 84 ms
14,988 KB
testcase_04 AC 79 ms
15,060 KB
testcase_05 AC 78 ms
15,124 KB
testcase_06 AC 77 ms
15,248 KB
testcase_07 AC 79 ms
15,084 KB
testcase_08 AC 81 ms
15,240 KB
testcase_09 AC 77 ms
15,020 KB
testcase_10 AC 77 ms
15,228 KB
testcase_11 AC 79 ms
15,316 KB
testcase_12 AC 78 ms
15,124 KB
testcase_13 AC 79 ms
15,124 KB
testcase_14 AC 76 ms
15,232 KB
testcase_15 AC 75 ms
15,120 KB
testcase_16 AC 73 ms
15,264 KB
testcase_17 AC 73 ms
15,232 KB
testcase_18 AC 76 ms
15,084 KB
testcase_19 AC 78 ms
15,252 KB
testcase_20 AC 77 ms
15,296 KB
testcase_21 AC 79 ms
15,020 KB
testcase_22 AC 79 ms
15,092 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 = 3

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