結果

問題 No.187 中華風 (Hard)
ユーザー tubo28tubo28
提出日時 2015-04-20 01:25:58
言語 Ruby
(3.3.0)
結果
AC  
実行時間 135 ms / 3,000 ms
コード長 818 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 11,272 KB
実行使用メモリ 23,484 KB
最終ジャッジ日時 2023-09-17 22:31:22
合計ジャッジ時間 4,077 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
15,052 KB
testcase_01 AC 83 ms
15,148 KB
testcase_02 AC 114 ms
20,672 KB
testcase_03 AC 113 ms
20,292 KB
testcase_04 AC 132 ms
23,008 KB
testcase_05 AC 132 ms
22,932 KB
testcase_06 AC 133 ms
23,408 KB
testcase_07 AC 133 ms
23,196 KB
testcase_08 AC 134 ms
23,244 KB
testcase_09 AC 133 ms
23,328 KB
testcase_10 AC 135 ms
23,484 KB
testcase_11 AC 133 ms
23,004 KB
testcase_12 AC 131 ms
23,024 KB
testcase_13 AC 85 ms
15,232 KB
testcase_14 AC 83 ms
15,492 KB
testcase_15 AC 112 ms
20,288 KB
testcase_16 AC 113 ms
20,508 KB
testcase_17 AC 79 ms
15,108 KB
testcase_18 AC 86 ms
15,140 KB
testcase_19 AC 81 ms
15,148 KB
testcase_20 AC 121 ms
22,272 KB
testcase_21 AC 80 ms
14,972 KB
testcase_22 AC 134 ms
23,272 KB
testcase_23 AC 83 ms
15,204 KB
testcase_24 AC 81 ms
15,120 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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)
0