結果

問題 No.187 中華風 (Hard)
ユーザー r_dream0r_dream0
提出日時 2017-02-12 12:22:18
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 633 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 11,252 KB
実行使用メモリ 26,944 KB
最終ジャッジ日時 2023-08-28 14:50:50
合計ジャッジ時間 5,090 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,228 KB
testcase_01 AC 79 ms
15,052 KB
testcase_02 AC 143 ms
22,284 KB
testcase_03 AC 138 ms
21,856 KB
testcase_04 AC 237 ms
26,856 KB
testcase_05 AC 228 ms
26,700 KB
testcase_06 AC 226 ms
26,628 KB
testcase_07 AC 227 ms
26,460 KB
testcase_08 AC 236 ms
26,928 KB
testcase_09 AC 238 ms
26,944 KB
testcase_10 AC 236 ms
26,732 KB
testcase_11 AC 229 ms
26,852 KB
testcase_12 AC 232 ms
26,616 KB
testcase_13 AC 83 ms
15,288 KB
testcase_14 AC 82 ms
15,364 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 78 ms
15,220 KB
testcase_18 AC 81 ms
15,148 KB
testcase_19 AC 79 ms
15,160 KB
testcase_20 AC 194 ms
25,304 KB
testcase_21 AC 78 ms
15,024 KB
testcase_22 AC 229 ms
26,396 KB
testcase_23 AC 77 ms
15,316 KB
testcase_24 AC 78 ms
15,024 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:9: warning: assigned but unused variable - y
Main.rb:14: warning: assigned but unused variable - u2
Main.rb:30: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

def extgcd(a,b)
  return [1,0] if b == 0
  y,x = extgcd(b, a % b)
  y -= (a/b)*x
  return [x,y]
end

def mod_inverse(a, mod)
  x,y = extgcd(a, mod)
  return x % mod
end

def chinese_remainder(m1,m2,a,b)
  u1, u2 = extgcd(m1, m2)
  return nil if (a - b) % m1.gcd(m2) != 0
  l = (a - b) / (m1.gcd(m2))
  return (a - m1 * u1 * l) % (m1 * m2 / m1.gcd(m2))
end

n = gets.to_i
x = Array.new(n)
y = Array.new(n)
n.times do |i|
  x[i], y[i] = gets.split.map(&:to_i)
end
ans, mod = 0, 1
x.zip(y).each do |x,y|
  ans = chinese_remainder(mod, y, ans, x)
  unless ans
    puts -1
    exit
  end
  mod = mod * y / mod.gcd(y)
end
p ans % (10**9+7)
0