結果

問題 No.186 中華風 (Easy)
ユーザー tubo28tubo28
提出日時 2015-04-20 00:27:24
言語 Ruby
(3.2.2)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 717 bytes
コンパイル時間 478 ms
コンパイル使用メモリ 11,216 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2023-09-27 00:43:44
合計ジャッジ時間 3,025 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,036 KB
testcase_01 AC 84 ms
15,128 KB
testcase_02 AC 77 ms
15,132 KB
testcase_03 AC 74 ms
15,156 KB
testcase_04 AC 76 ms
15,012 KB
testcase_05 AC 81 ms
15,252 KB
testcase_06 AC 75 ms
15,360 KB
testcase_07 AC 78 ms
15,308 KB
testcase_08 AC 77 ms
15,124 KB
testcase_09 AC 78 ms
15,128 KB
testcase_10 AC 79 ms
15,308 KB
testcase_11 AC 75 ms
15,012 KB
testcase_12 AC 77 ms
15,164 KB
testcase_13 AC 75 ms
15,184 KB
testcase_14 AC 77 ms
15,040 KB
testcase_15 AC 78 ms
15,308 KB
testcase_16 AC 80 ms
15,264 KB
testcase_17 AC 78 ms
15,024 KB
testcase_18 AC 79 ms
15,088 KB
testcase_19 AC 80 ms
15,016 KB
testcase_20 AC 79 ms
15,044 KB
testcase_21 AC 79 ms
15,324 KB
testcase_22 AC 78 ms
15,152 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 lc(a,b,m)
  xx,mm = 0,1
  a.size.times do |i|
    aa = a[i]*mm
    bb = b[i] - a[i] * xx
    dd = m[i].gcd(aa)
    return if bb % dd != 0
    tt = bb/dd * mod_inv(aa/dd, m[i]/dd) % (m[i] / dd)
    xx = xx + mm*tt
    mm *= m[i]/dd
  end
  return [xx%mm,mm]
end

n = 3

xs, ys = [], []
n.times do
  x,y = gets.chomp.split.map(&:to_i)
  xs << x
  ys << y
end

as = [1] * n

ans = lc(as,xs,ys)
puts ans.nil? ? -1 : (ans[0] == 0 ? ans[1] : ans[0])
0