結果

問題 No.187 中華風 (Hard)
ユーザー Yuki IwatakeYuki Iwatake
提出日時 2021-03-07 13:46:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 89 ms / 3,000 ms
コード長 699 bytes
コンパイル時間 121 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-17 08:21:01
合計ジャッジ時間 2,457 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
11,008 KB
testcase_01 AC 45 ms
11,008 KB
testcase_02 AC 59 ms
10,880 KB
testcase_03 AC 55 ms
11,008 KB
testcase_04 AC 87 ms
11,008 KB
testcase_05 AC 89 ms
11,008 KB
testcase_06 AC 86 ms
10,880 KB
testcase_07 AC 89 ms
11,008 KB
testcase_08 AC 89 ms
11,136 KB
testcase_09 AC 89 ms
11,008 KB
testcase_10 AC 89 ms
11,008 KB
testcase_11 AC 86 ms
11,008 KB
testcase_12 AC 87 ms
10,880 KB
testcase_13 AC 33 ms
11,008 KB
testcase_14 AC 35 ms
10,752 KB
testcase_15 AC 57 ms
11,136 KB
testcase_16 AC 56 ms
11,008 KB
testcase_17 AC 29 ms
10,624 KB
testcase_18 AC 39 ms
10,880 KB
testcase_19 AC 29 ms
10,880 KB
testcase_20 AC 75 ms
11,008 KB
testcase_21 AC 29 ms
10,752 KB
testcase_22 AC 87 ms
10,880 KB
testcase_23 AC 29 ms
10,880 KB
testcase_24 AC 29 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def CRT(BMlist):
    # Extended Euclidean Algorithm
    def extgcd(a, b):
        if b:
            d, y, x = extgcd(b, a % b)
            y -= (a // b)*x
            return d, x, y
        return a, 1, 0
    r = 0; M = 1
    for b,m in BMlist:
        d,p,q = extgcd(M,m)
        if (b-r)%d != 0:
            return 0,-1
        r += M*((b-r)//d*p%(m//d))
        M *= m//d
    return r,M

import math
BMlist = []
ans = 1
sml = 10**9+7
N = int(input())
for i in range(N):
    x,y = map(int, input().split())
    BMlist.append((x,y))
    g = math.gcd(ans,y)
    ans = ((ans*y)//g)
r,m = CRT(BMlist)
if (r,m) == (0,-1):
    r = -1
elif r == 0 and m != -1:
    r = ans
print(r % sml if r != -1 else r)
0