結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
15,044 KB
testcase_01 AC 84 ms
15,276 KB
testcase_02 AC 119 ms
20,412 KB
testcase_03 AC 116 ms
20,380 KB
testcase_04 AC 136 ms
24,720 KB
testcase_05 AC 135 ms
24,824 KB
testcase_06 AC 136 ms
25,084 KB
testcase_07 AC 133 ms
24,872 KB
testcase_08 AC 135 ms
24,980 KB
testcase_09 AC 138 ms
24,960 KB
testcase_10 AC 137 ms
25,144 KB
testcase_11 AC 135 ms
24,696 KB
testcase_12 AC 139 ms
24,764 KB
testcase_13 AC 86 ms
15,316 KB
testcase_14 AC 88 ms
15,420 KB
testcase_15 AC 115 ms
20,668 KB
testcase_16 AC 114 ms
20,576 KB
testcase_17 AC 80 ms
15,280 KB
testcase_18 AC 84 ms
15,076 KB
testcase_19 AC 81 ms
15,288 KB
testcase_20 AC 125 ms
22,632 KB
testcase_21 AC 82 ms
15,080 KB
testcase_22 AC 137 ms
24,904 KB
testcase_23 AC 82 ms
15,156 KB
testcase_24 AC 81 ms
15,036 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 = gets.to_i

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])) % 1000000007
0