結果

問題 No.187 中華風 (Hard)
ユーザー kira924age
提出日時 2018-04-01 14:27:39
言語 Python2
(2.7.18)
結果
AC  
実行時間 828 ms / 3,000 ms
コード長 558 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 6,784 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-06-26 04:45:06
合計ジャッジ時間 9,565 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python2
# coding: utf-8

def extgcd(a, b):
    x,y, u,v = 0,1, 1,0
    while a != 0:
        q, r = b/a, b%a
        m, n = x-u*q, y-v*q
        b,a, x,y, u,v = a,r, u,v, m,n
        g = b
    return x, y, g


def solve():
    n = int(raw_input())
    a, b = 0, 1
    for i in xrange(n):
        na, nb = map(int, raw_input().split())
        x, y, g = extgcd(b, nb)
        if a%g != na%g:
            print -1; return
        l = b/g*nb
        a, b = ((a+(na-a)/g*x*b)%l, l)

    if a == 0:
        a = b

    print a % 1000000007

solve()
0