結果

問題 No.186 中華風 (Easy)
ユーザー tubo28tubo28
提出日時 2015-04-20 00:22:56
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 705 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 11,372 KB
実行使用メモリ 15,372 KB
最終ジャッジ日時 2023-09-17 22:14:44
合計ジャッジ時間 3,117 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,136 KB
testcase_01 AC 79 ms
15,136 KB
testcase_02 AC 79 ms
15,032 KB
testcase_03 AC 82 ms
15,112 KB
testcase_04 AC 80 ms
15,108 KB
testcase_05 AC 82 ms
15,244 KB
testcase_06 AC 80 ms
15,136 KB
testcase_07 AC 78 ms
15,032 KB
testcase_08 AC 79 ms
15,260 KB
testcase_09 AC 79 ms
15,080 KB
testcase_10 AC 79 ms
15,032 KB
testcase_11 AC 79 ms
15,160 KB
testcase_12 AC 79 ms
15,108 KB
testcase_13 AC 79 ms
15,228 KB
testcase_14 AC 81 ms
15,252 KB
testcase_15 AC 79 ms
15,064 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 82 ms
15,320 KB
testcase_19 AC 81 ms
15,028 KB
testcase_20 AC 79 ms
15,292 KB
testcase_21 AC 79 ms
15,200 KB
testcase_22 AC 78 ms
15,224 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
  nn = a.size

  nn.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