結果

問題 No.187 中華風 (Hard)
ユーザー convexineqconvexineq
提出日時 2020-12-29 21:39:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 106 ms / 3,000 ms
コード長 786 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 75,776 KB
最終ジャッジ日時 2024-04-15 21:59:39
合計ジャッジ時間 2,859 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
61,696 KB
testcase_01 AC 59 ms
61,568 KB
testcase_02 AC 78 ms
71,424 KB
testcase_03 AC 78 ms
70,656 KB
testcase_04 AC 100 ms
75,392 KB
testcase_05 AC 101 ms
75,392 KB
testcase_06 AC 106 ms
75,392 KB
testcase_07 AC 104 ms
75,520 KB
testcase_08 AC 104 ms
75,264 KB
testcase_09 AC 103 ms
75,776 KB
testcase_10 AC 102 ms
75,392 KB
testcase_11 AC 102 ms
75,776 KB
testcase_12 AC 101 ms
75,264 KB
testcase_13 AC 61 ms
62,720 KB
testcase_14 AC 61 ms
62,336 KB
testcase_15 AC 77 ms
70,784 KB
testcase_16 AC 78 ms
70,400 KB
testcase_17 AC 40 ms
52,096 KB
testcase_18 AC 56 ms
60,672 KB
testcase_19 AC 40 ms
51,840 KB
testcase_20 AC 93 ms
75,264 KB
testcase_21 AC 40 ms
52,352 KB
testcase_22 AC 99 ms
75,776 KB
testcase_23 AC 39 ms
52,352 KB
testcase_24 AC 40 ms
52,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def inv_gcd(x,y):
    if y==0: return 1,0
    r0,r1,s0,s1 = x,y,1,0
    while r1 != 0:
        a,b = divmod(r0,r1)
        r0, r1, s0, s1 = r1, b, s1, s0-a*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 M0 < M1:
            r0,r1 = r1,r0
            M0,M1 = M1,M0
        minv,g = inv_gcd(M0,M1)
        Q,R = divmod(r1-r0,g)
        if R%g: return (0,0)
        M11 = M1//g
        r0 += Q%M11*minv%M11*M0
        M0 *= M11
    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