結果

問題 No.187 中華風 (Hard)
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-03-03 14:19:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 465 ms / 3,000 ms
コード長 1,101 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 82,416 KB
実行使用メモリ 76,928 KB
最終ジャッジ日時 2024-04-14 07:02:45
合計ジャッジ時間 7,307 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,256 KB
testcase_01 AC 42 ms
55,344 KB
testcase_02 AC 103 ms
75,008 KB
testcase_03 AC 100 ms
75,008 KB
testcase_04 AC 430 ms
76,472 KB
testcase_05 AC 432 ms
76,264 KB
testcase_06 AC 428 ms
76,780 KB
testcase_07 AC 432 ms
76,600 KB
testcase_08 AC 465 ms
76,928 KB
testcase_09 AC 465 ms
76,660 KB
testcase_10 AC 457 ms
76,680 KB
testcase_11 AC 433 ms
76,772 KB
testcase_12 AC 432 ms
76,340 KB
testcase_13 AC 45 ms
56,904 KB
testcase_14 AC 45 ms
57,380 KB
testcase_15 AC 94 ms
74,928 KB
testcase_16 AC 96 ms
75,008 KB
testcase_17 AC 39 ms
52,868 KB
testcase_18 AC 42 ms
54,876 KB
testcase_19 AC 39 ms
52,688 KB
testcase_20 AC 327 ms
76,168 KB
testcase_21 AC 39 ms
53,884 KB
testcase_22 AC 434 ms
75,992 KB
testcase_23 AC 39 ms
52,928 KB
testcase_24 AC 39 ms
53,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

時刻tを求める

X <= t < X+Y (mod 2X+2Y)
P <= t (mod P+Q)

それぞれに関して中国剰余

"""

import sys
from sys import stdin

import math
def extGCD(a,b):
    g = math.gcd(a,b)
    x, y, u, v = 1, 0, 0, 1
    while b:
        k = a // b
        x -= k * u
        y -= k * v
        x, u = u, x
        y, v = v, y
        a, b = b, a % b
    return g ,x, y

def crt2(b1,m1,b2,m2):
    g,p,q = extGCD(m1,m2)
    if b1 % g != b2 % g:
        return 0,0
    return ( b1 + m1 * ((b2-b1)//g) * p ) % (m1*m2//g) , m1*m2//g

def crt(b,m):
    assert len(b) == len(m)
    
    nb,nm = 0,1
    for i in range(len(b)):
        nb,nm = crt2(nb,nm,b[i],m[i])
        if (nb,nm) == (0,0):
            return (0,0)
    return nb,nm


TT = 1
mod = 10**9+7
for loop in range(TT):

    N = int(stdin.readline())
    B = []
    M = []

    for i in range(N):
        x,y = map(int,stdin.readline().split())
        B.append(x)
        M.append(y)

    ans,lcm = crt(B,M)
    if (ans,lcm) == (0,0):
        print (-1)
    elif ans == 0:
        print (lcm % mod)
    else:
        print (ans % mod)
0