結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 113 ms
20,176 KB
testcase_03 AC 113 ms
20,156 KB
testcase_04 AC 136 ms
24,776 KB
testcase_05 AC 133 ms
24,600 KB
testcase_06 AC 134 ms
25,092 KB
testcase_07 AC 132 ms
24,976 KB
testcase_08 AC 134 ms
25,084 KB
testcase_09 AC 134 ms
25,164 KB
testcase_10 AC 136 ms
25,068 KB
testcase_11 AC 131 ms
24,852 KB
testcase_12 AC 133 ms
24,852 KB
testcase_13 AC 85 ms
15,300 KB
testcase_14 AC 84 ms
15,420 KB
testcase_15 AC 113 ms
20,640 KB
testcase_16 AC 114 ms
20,572 KB
testcase_17 AC 81 ms
15,156 KB
testcase_18 WA -
testcase_19 AC 82 ms
15,160 KB
testcase_20 AC 125 ms
22,548 KB
testcase_21 AC 81 ms
15,252 KB
testcase_22 AC 137 ms
25,032 KB
testcase_23 AC 82 ms
15,212 KB
testcase_24 AC 81 ms
15,124 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