結果

問題 No.186 中華風 (Easy)
ユーザー 👑 rin204rin204
提出日時 2022-07-04 13:29:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 2,150 bytes
コンパイル時間 671 ms
コンパイル使用メモリ 86,864 KB
実行使用メモリ 71,608 KB
最終ジャッジ日時 2023-08-20 20:14:01
合計ジャッジ時間 2,924 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
71,580 KB
testcase_01 AC 61 ms
71,108 KB
testcase_02 AC 62 ms
71,416 KB
testcase_03 AC 59 ms
71,324 KB
testcase_04 AC 63 ms
71,252 KB
testcase_05 AC 61 ms
71,108 KB
testcase_06 AC 66 ms
71,608 KB
testcase_07 AC 63 ms
71,436 KB
testcase_08 AC 61 ms
71,284 KB
testcase_09 AC 60 ms
71,436 KB
testcase_10 AC 61 ms
71,256 KB
testcase_11 AC 63 ms
71,444 KB
testcase_12 AC 64 ms
71,328 KB
testcase_13 AC 62 ms
71,292 KB
testcase_14 AC 62 ms
71,212 KB
testcase_15 AC 60 ms
71,280 KB
testcase_16 AC 61 ms
71,328 KB
testcase_17 AC 63 ms
71,568 KB
testcase_18 AC 61 ms
71,296 KB
testcase_19 AC 60 ms
71,136 KB
testcase_20 AC 61 ms
71,244 KB
testcase_21 AC 60 ms
71,328 KB
testcase_22 AC 61 ms
71,248 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(3):
    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])
print(Garner(M, R))
0