結果

問題 No.187 中華風 (Hard)
ユーザー snukesnuke
提出日時 2015-04-20 15:57:38
言語 Python2
(2.7.18)
結果
WA  
実行時間 -
コード長 719 bytes
コンパイル時間 55 ms
コンパイル使用メモリ 6,532 KB
実行使用メモリ 6,152 KB
最終ジャッジ日時 2023-09-18 01:05:56
合計ジャッジ時間 15,395 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,888 KB
testcase_01 AC 11 ms
5,924 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 11 ms
5,884 KB
testcase_18 AC 11 ms
5,896 KB
testcase_19 AC 11 ms
5,916 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(raw_input())
a = 0
b = 1

def extgcd(x,y):
  if y == 0: return (x,1,0)
  g,a,b = extgcd(y,x%y);
  return (g,b,a-x//y*b)

def factor(x):
  r = []
  i = 2
  while i*i < x:
    while x%i == 0:
      r.append(i)
      x /= i
    i += 1
  if x != 1: r.append(x)
  return r

def f(x,p):
  r = 1
  while x%p == 0:
    x /= p
    r *= p
  return r

def fail():
  print -1
  exit(0)

for ti in xrange(n):
  na,nb = map(int,raw_input().split())
  g = extgcd(b,nb)[0]
  if a%g != na%g: fail()
  for p in factor(g):
    p1 = f(b,p)
    p2 = f(nb,p)
    if p1 < p2:
      b /= p1
      a %= b
    else:
      nb /= p2
      na %= nb
  x,y = extgcd(b,nb)[1:]
  a = x*b*na + y*nb*a
  b *= nb
  a %= b

if a == 0: a = b
print a
0