結果
| 問題 |
No.187 中華風 (Hard)
|
| コンテスト | |
| ユーザー |
tubo28
|
| 提出日時 | 2015-04-20 01:25:58 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 145 ms / 3,000 ms |
| コード長 | 818 bytes |
| コンパイル時間 | 40 ms |
| コンパイル使用メモリ | 7,552 KB |
| 実行使用メモリ | 23,040 KB |
| 最終ジャッジ日時 | 2024-07-04 16:10:24 |
| 合計ジャッジ時間 | 3,806 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 25 |
コンパイルメッセージ
Syntax OK
ソースコード
def extgcd(a, b)
if b.zero?
{x: 1, y: 0, gcd: a}
else
prev = extgcd(b, a % b)
{
x: prev[:y],
y: prev[:x] - (a / b) * prev[:y],
gcd: prev[:gcd]
}
end
end
def mod_inv(a,m)
res = extgcd(a,m)
x = res[:x]
return (m+x%m)%m
end
def linear_congruence(as,bs,ms)
x,m = 0,1
as.size.times do |i|
a = as[i]*m
b = bs[i] - as[i] * x
d = ms[i].gcd(a)
return if b % d != 0
tt = b/d * mod_inv(a/d, ms[i]/d) % (ms[i]/d)
x = x + m*tt
m *= ms[i]/d
end
return [x%m, m]
end
n = gets.to_i
t = []
n.times do
x,y = gets.chomp.split.map(&:to_i)
t << [x,y]
end
xs,ys = t.transpose
as = [1] * n
mod = 1000000007
ans = linear_congruence(as,xs,ys)
puts ->(x){
if x.nil?
-1
elsif ans[0] == 0
ans[1] % mod
else
ans[0] % mod
end
}.call(ans)
tubo28