結果

問題 No.981 一般冪乗根
ユーザー Kiri8128Kiri8128
提出日時 2020-09-04 01:42:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 167 ms / 6,000 ms
コード長 3,431 bytes
コンパイル時間 471 ms
コンパイル使用メモリ 82,112 KB
実行使用メモリ 146,404 KB
最終ジャッジ日時 2024-05-03 11:21:08
合計ジャッジ時間 71,859 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
146,404 KB
testcase_01 AC 67 ms
144,972 KB
testcase_02 AC 75 ms
145,436 KB
testcase_03 AC 67 ms
70,256 KB
testcase_04 AC 71 ms
69,284 KB
testcase_05 AC 69 ms
70,428 KB
testcase_06 AC 69 ms
70,552 KB
testcase_07 AC 65 ms
70,500 KB
testcase_08 AC 68 ms
70,924 KB
testcase_09 AC 66 ms
69,824 KB
testcase_10 AC 65 ms
70,064 KB
testcase_11 AC 64 ms
70,180 KB
testcase_12 AC 65 ms
69,688 KB
testcase_13 AC 64 ms
69,972 KB
testcase_14 AC 66 ms
71,148 KB
testcase_15 AC 65 ms
70,612 KB
testcase_16 AC 74 ms
70,620 KB
testcase_17 AC 65 ms
69,856 KB
testcase_18 AC 66 ms
70,020 KB
testcase_19 AC 72 ms
69,672 KB
testcase_20 AC 65 ms
70,804 KB
testcase_21 AC 64 ms
70,572 KB
testcase_22 AC 64 ms
70,556 KB
testcase_23 AC 64 ms
71,024 KB
testcase_24 AC 66 ms
69,952 KB
testcase_25 AC 68 ms
71,148 KB
testcase_26 AC 73 ms
71,196 KB
testcase_27 AC 64 ms
68,792 KB
testcase_28 AC 167 ms
76,336 KB
evil_60bit1.txt AC 72 ms
70,972 KB
evil_60bit2.txt AC 75 ms
71,112 KB
evil_60bit3.txt AC 73 ms
70,232 KB
evil_hack AC 34 ms
54,280 KB
evil_hard_random AC 72 ms
71,760 KB
evil_hard_safeprime.txt AC 78 ms
72,052 KB
evil_hard_tonelli0 AC 85 ms
73,660 KB
evil_hard_tonelli1 TLE -
evil_hard_tonelli2 TLE -
evil_hard_tonelli3 AC 772 ms
76,940 KB
evil_sefeprime1.txt AC 77 ms
71,504 KB
evil_sefeprime2.txt AC 78 ms
71,332 KB
evil_sefeprime3.txt AC 79 ms
72,884 KB
evil_tonelli1.txt TLE -
evil_tonelli2.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

# Reference: https://yukicoder.me/submissions/192539

def gcd(a, b):
    while b: a, b = b, a % b
    return a
def isPrimeMR(n):
    d = n - 1
    d = d // (d & -d)
    L = [2, 7, 61] if n < 1<<32 else [2, 3, 5, 7, 11, 13, 17] if n < 1<<48 else [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    for a in L:
        t = d
        y = pow(a, t, n)
        if y == 1: continue
        while y != n - 1:
            y = y * y % n
            if y == 1 or t == n - 1: return 0
            t <<= 1
    return 1
def findFactorRho(n):
    m = 1 << n.bit_length() // 8
    for c in range(1, 99):
        f = lambda x: (x * x + c) % n
        y, r, q, g = 2, 1, 1, 1
        while g == 1:
            x = y
            for i in range(r):
                y = f(y)
            k = 0
            while k < r and g == 1:
                ys = y
                for i in range(min(m, r - k)):
                    y = f(y)
                    q = q * abs(x - y) % n
                g = gcd(q, n)
                k += m
            r <<= 1
        if g == n:
            g = 1
            while g == 1:
                ys = f(ys)
                g = gcd(abs(x - ys), n)
        if g < n:
            if isPrimeMR(g): return g
            elif isPrimeMR(n // g): return n // g
            return findFactorRho(g)
def primeFactor(n):
    i = 2
    ret = {}
    rhoFlg = 0
    while i * i <= n:
        k = 0
        while n % i == 0:
            n //= i
            k += 1
        if k: ret[i] = k
        i += i % 2 + (3 if i % 3 == 1 else 1)
        if i == 101 and n >= 2 ** 20:
            while n > 1:
                if isPrimeMR(n):
                    ret[n], n = 1, 1
                else:
                    rhoFlg = 1
                    j = findFactorRho(n)
                    k = 0
                    while n % j == 0:
                        n //= j
                        k += 1
                    ret[j] = k

    if n > 1: ret[n] = 1
    if rhoFlg: ret = {x: ret[x] for x in sorted(ret)}
    return ret

def inv(a, mod):
    b = mod
    s, u = 1, 0
    while b:
        q = a // b
        a, b = b, a % b
        s, u = u, s - q * u
    assert a == 1
    return s % mod

def sqrt_mod_prime_power(a, p, e, mod): # solve x^(p^e) = a
    q = mod - 1
    s = 0
    while q % p == 0:
        q //= p
        s += 1
    pe = p ** e
    d = pow(-q, (p-1) * p ** (e-1) - 1, pe) * q
    r = pow(a, (d + 1) // pe, mod)
    t = pow(a, d, mod)
    if t == 1: return r
    ps = p ** (s - 1)
    c = -1
    z = 2
    while 1:
        c = pow(z, q, mod)
        if pow(c, ps, mod) != 1:
            break
        z += 1
    b = -1
    while t != 1:
        tmp = pow(t, p, mod)
        s2 = 1
        while tmp != 1:
            tmp = pow(tmp, p, mod)
            s2 += 1
        if s2 + e <= s:
            b = c
            for _ in range(s - s2 - e):
                b = pow(b, p, mod)
            c = pow(b, pe, mod)
            s = s2
        r = r * b % mod
        t = t * c % mod
    return r

def sqrt_mod(a, n, p): # solve x^n = a (mod p)
    assert n >= 1
    a %= p
    n %= p - 1
    if a <= 1: return a
    g = gcd(p - 1, n)
    if pow(a, (p-1) // g, p) != 1:
        return -1
    a = pow(a, inv(n // g, (p-1) // g), p)
    pf = primeFactor(g)
    for pp in pf:
        a = sqrt_mod_prime_power(a, pp, pf[pp], p)
    return a

T = int(input())
for _ in range(T):
    p, k, a = map(int, input().split())
    print(sqrt_mod(a, k, p))
0