結果

問題 No.187 中華風 (Hard)
ユーザー 👑 rin204rin204
提出日時 2022-07-04 13:30:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 183 ms / 3,000 ms
コード長 2,186 bytes
コンパイル時間 1,038 ms
コンパイル使用メモリ 86,736 KB
実行使用メモリ 79,496 KB
最終ジャッジ日時 2023-08-20 20:14:52
合計ジャッジ時間 4,809 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 153 ms
78,436 KB
testcase_01 AC 145 ms
79,192 KB
testcase_02 AC 169 ms
78,696 KB
testcase_03 AC 183 ms
79,496 KB
testcase_04 AC 154 ms
77,784 KB
testcase_05 AC 149 ms
77,864 KB
testcase_06 AC 160 ms
78,600 KB
testcase_07 AC 158 ms
77,796 KB
testcase_08 AC 123 ms
77,260 KB
testcase_09 AC 120 ms
77,296 KB
testcase_10 AC 120 ms
77,380 KB
testcase_11 AC 160 ms
78,312 KB
testcase_12 AC 166 ms
78,504 KB
testcase_13 AC 91 ms
76,720 KB
testcase_14 AC 102 ms
78,136 KB
testcase_15 AC 173 ms
78,708 KB
testcase_16 AC 170 ms
79,496 KB
testcase_17 AC 59 ms
71,428 KB
testcase_18 AC 140 ms
79,016 KB
testcase_19 AC 62 ms
71,292 KB
testcase_20 AC 148 ms
77,920 KB
testcase_21 AC 59 ms
71,208 KB
testcase_22 AC 153 ms
78,216 KB
testcase_23 AC 58 ms
71,464 KB
testcase_24 AC 59 ms
71,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

def isprime(n):
    if n <= 1:
        return False
    elif n == 2:
        return True
    elif n % 2 == 0:
        return False
    
    A = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]
    s = 0
    d = n - 1
    while d % 2 == 0:
        s += 1
        d >>= 1
    
    for a in A:
        if a % n == 0:
            return True
        x = pow(a, d, n)
        if x != 1:
            for t in range(s):
                if x == n - 1:
                    break
                x = x * x % n
            else:
                return False
    return True
        
def pollard(n):
    if n % 2 == 0:
        return 2
    if isprime(n):
        return n
    
    f = lambda x:(x * x + 1) % n
    
    step = 0
    while 1:
        step += 1
        x = step
        y = f(x)
        while 1:
            p = gcd(y - x + n, n)
            if p == 0 or p == n:
                break
            if p != 1:
                return p
            x = f(x)
            y = f(f(y))

def primefact(n):
    if n == 1:
        return []
    p = pollard(n)
    if p == n:
        return [p]
    left = primefact(p)
    right = primefact(n // p)
    left += right
    return sorted(left)

def modinv(a, MOD):
    b = MOD
    u = 1
    v = 0
    while b:
        t = a // b
        a -= t * b
        u -= t * v
        a, b = b, a
        u, v = v, u
    u %= MOD
    return u

def Garner(M, R):
    m_prod = M[0]
    C = R[0]
    for m, r in zip(M[1:], R[1:]):
        t = (r - C) * modinv(m_prod, m) % m
        C += t * m_prod
        m_prod *= m
    if C == 0:
        C = m_prod
    return C

mr = {}
for _ in range(int(input())):
    x, y = map(int, input().split())
    divs = primefact(y)
    cnt = {}
    for p in divs:
        cnt[p] = cnt.get(p, 1) * p
    for p, v in cnt.items():
        if p not in mr:
            mr[p] = []
        mr[p].append((v, x % v))

M = []
R = []
for p, lst in mr.items():
    lst.sort()
    p, v = lst[0]
    for pp, vv in lst[1:]:
        if (vv - v) % p != 0:
            print(-1)
            exit()
        p = pp
        v = vv
    M.append(lst[-1][0])
    R.append(lst[-1][1])

MOD = 10 ** 9 + 7
print(Garner(M, R) % MOD)
0