結果

問題 No.187 中華風 (Hard)
ユーザー convexineq
提出日時 2020-12-29 11:49:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 111 ms / 3,000 ms
コード長 827 bytes
コンパイル時間 326 ms
コンパイル使用メモリ 81,936 KB
実行使用メモリ 75,740 KB
最終ジャッジ日時 2024-10-05 08:11:31
合計ジャッジ時間 3,158 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

def inv_gcd(x,y):
    if y==0: return 1,0
    r0,r1,s0,s1 = x,y,1,0
    while r1 != 0:
        r0,r1, s0,s1 = r1,r0%r1, s1,s0-r0//r1*s1
    return s0%y,r0 # s0*x + ??*y = r0 = gcd(x,y)

def Chinese_remainder_theorem(r,m):
    assert len(r)==len(m)
    r0, M0 = 0,1
    for r1, M1 in zip(r,m):
        if r0 < r1:
            r0,r1 = r1,r0
            M0,M1 = M1,M0
        if M0%M1==0:
            if r0%M1 != r1: return (0,0)
            continue
        minv,g = inv_gcd(M0,M1)
        if (r1-r0)%g: return (0,0)
        x = (r1-r0)//g*minv%(M1//g)
        r0 += x*M0
        M0 *= M1//g
    return r0,M0

MOD = 10**9+7
n = int(input())
x = [0]*n
y = [0]*n
for i in range(n):
    xi,yi = map(int,input().split())
    x[i] = xi
    y[i] = yi

r,m = Chinese_remainder_theorem(x,y)
print(-1 if m == 0 else r%MOD if r else m%MOD)
0