結果

問題 No.187 中華風 (Hard)
ユーザー roarisroaris
提出日時 2022-11-11 13:59:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 440 ms / 3,000 ms
コード長 730 bytes
コンパイル時間 383 ms
コンパイル使用メモリ 87,232 KB
実行使用メモリ 78,444 KB
最終ジャッジ日時 2023-10-11 11:00:40
合計ジャッジ時間 7,178 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,484 KB
testcase_01 AC 76 ms
71,348 KB
testcase_02 AC 128 ms
77,112 KB
testcase_03 AC 135 ms
77,972 KB
testcase_04 AC 416 ms
78,340 KB
testcase_05 AC 400 ms
77,200 KB
testcase_06 AC 389 ms
76,648 KB
testcase_07 AC 404 ms
77,244 KB
testcase_08 AC 440 ms
78,388 KB
testcase_09 AC 440 ms
78,432 KB
testcase_10 AC 440 ms
78,444 KB
testcase_11 AC 401 ms
77,200 KB
testcase_12 AC 389 ms
77,224 KB
testcase_13 AC 81 ms
75,044 KB
testcase_14 AC 79 ms
75,204 KB
testcase_15 AC 117 ms
77,736 KB
testcase_16 AC 119 ms
77,160 KB
testcase_17 AC 73 ms
71,768 KB
testcase_18 AC 77 ms
71,168 KB
testcase_19 AC 74 ms
71,484 KB
testcase_20 AC 306 ms
77,136 KB
testcase_21 AC 73 ms
71,768 KB
testcase_22 AC 398 ms
77,152 KB
testcase_23 AC 73 ms
71,216 KB
testcase_24 AC 70 ms
71,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# https://yukicoder.me/problems/no/187
import sys
input = sys.stdin.readline
from math import gcd

def ext_gcd(a, b):
    if b==0:
        return 1, 0
    
    s, t = ext_gcd(b, a%b)
    return t, s-a//b*t

def crt(rs, ms):
    r, m = 0, 1
    
    for ri, mi in zip(rs, ms):
        p, q = ext_gcd(m, mi)
        g = gcd(m, mi)
        
        if (ri-r)%g!=0:
            return -1, -1
        
        r += m*(ri-r)//g*p
        m *= mi//g
        r %= m
    
    return r, m

N = int(input())
rs, ms = [], []

for _ in range(N):
    r, m = map(int, input().split())
    rs.append(r)
    ms.append(m)

r, m = crt(rs, ms)

if r==-1:
    print(r)
else:
    if r==0:
        print(m%(10**9+7))
    else:
        print(r%(10**9+7))
0