結果

問題 No.187 中華風 (Hard)
ユーザー r_dream0r_dream0
提出日時 2017-02-12 12:27:59
言語 Ruby
(3.3.0)
結果
AC  
実行時間 239 ms / 3,000 ms
コード長 660 bytes
コンパイル時間 62 ms
コンパイル使用メモリ 11,512 KB
実行使用メモリ 27,076 KB
最終ジャッジ日時 2023-08-28 14:51:15
合計ジャッジ時間 6,290 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,144 KB
testcase_01 AC 85 ms
15,264 KB
testcase_02 AC 139 ms
22,196 KB
testcase_03 AC 136 ms
21,964 KB
testcase_04 AC 237 ms
26,820 KB
testcase_05 AC 228 ms
26,892 KB
testcase_06 AC 228 ms
26,388 KB
testcase_07 AC 227 ms
26,400 KB
testcase_08 AC 238 ms
27,008 KB
testcase_09 AC 239 ms
27,076 KB
testcase_10 AC 236 ms
26,900 KB
testcase_11 AC 234 ms
26,744 KB
testcase_12 AC 236 ms
26,840 KB
testcase_13 AC 86 ms
15,376 KB
testcase_14 AC 87 ms
15,284 KB
testcase_15 AC 132 ms
20,316 KB
testcase_16 AC 133 ms
20,648 KB
testcase_17 AC 76 ms
15,244 KB
testcase_18 AC 78 ms
15,240 KB
testcase_19 AC 76 ms
15,132 KB
testcase_20 AC 191 ms
25,120 KB
testcase_21 AC 77 ms
15,132 KB
testcase_22 AC 226 ms
26,544 KB
testcase_23 AC 78 ms
15,024 KB
testcase_24 AC 77 ms
15,288 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
ans += mod if ans == 0
puts ans % 1000000007
0