結果

問題 No.187 中華風 (Hard)
ユーザー tubo28tubo28
提出日時 2015-04-20 01:15:37
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 728 bytes
コンパイル時間 60 ms
コンパイル使用メモリ 11,512 KB
実行使用メモリ 23,404 KB
最終ジャッジ日時 2023-09-17 22:29:19
合計ジャッジ時間 3,983 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
15,200 KB
testcase_01 AC 85 ms
15,080 KB
testcase_02 AC 116 ms
20,828 KB
testcase_03 AC 115 ms
20,316 KB
testcase_04 AC 138 ms
22,956 KB
testcase_05 AC 133 ms
23,036 KB
testcase_06 AC 135 ms
23,384 KB
testcase_07 AC 135 ms
23,396 KB
testcase_08 AC 136 ms
23,024 KB
testcase_09 AC 135 ms
23,116 KB
testcase_10 AC 136 ms
23,404 KB
testcase_11 AC 136 ms
22,948 KB
testcase_12 AC 137 ms
22,968 KB
testcase_13 AC 87 ms
15,368 KB
testcase_14 AC 88 ms
15,412 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 83 ms
15,016 KB
testcase_18 AC 85 ms
15,280 KB
testcase_19 AC 83 ms
15,236 KB
testcase_20 AC 125 ms
22,464 KB
testcase_21 AC 83 ms
15,076 KB
testcase_22 AC 136 ms
23,364 KB
testcase_23 AC 82 ms
15,104 KB
testcase_24 AC 81 ms
15,076 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]
  (m + x%m) % m
end

def lc(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
    t = b/d * mod_inv(a/d, ms[i]/d) % (ms[i]/d)
    x = x + m*t
    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

ans = lc(as,xs,ys)
puts -> (x){
  if x.nil?
    -1
  else
    x[0] == 0 ? x[0] : x[0] % 1000000007
  end
}.call(ans)
0