結果

問題 No.981 一般冪乗根
ユーザー Kiri8128Kiri8128
提出日時 2020-02-09 00:38:30
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,467 bytes
コンパイル時間 501 ms
コンパイル使用メモリ 82,448 KB
実行使用メモリ 662,140 KB
最終ジャッジ日時 2024-10-09 15:34:31
合計ジャッジ時間 176,829 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 MLE -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 3,766 ms
194,172 KB
evil_60bit1.txt TLE -
evil_60bit2.txt TLE -
evil_60bit3.txt TLE -
evil_hack AC 45 ms
60,728 KB
evil_hard_random TLE -
evil_hard_safeprime.txt TLE -
evil_hard_tonelli0 TLE -
evil_hard_tonelli1 TLE -
evil_hard_tonelli2 TLE -
evil_hard_tonelli3 TLE -
evil_sefeprime1.txt TLE -
evil_sefeprime2.txt TLE -
evil_sefeprime3.txt TLE -
evil_tonelli1.txt TLE -
evil_tonelli2.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def primeFactor(N):
    i = 2
    ret = {}
    n = N
    mrFlg = 0
    while i*i <= n:
        k = 0
        while n % i == 0:
            n //= i
            k += 1
        if k: ret[i] = k
        i += 1 + i%2
        if i == 101 and n >= 2**20:
            def findFactorRho(N):
                def gcd(a, b):
                    while b: a, b = b, a % b
                    return a
                def f(x, c):
                    return (x * x + c) % N
                for c in range(1, 99):
                    X, d, j = [2], 1, 0
                    while d == 1:
                        j += 1
                        X.append(f(X[-1], c))
                        X.append(f(X[-1], c))
                        d = gcd(abs(X[2*j]-X[j]), N)
                    if d != N:
                        if isPrimeMR(d):
                            return d
                        elif isPrimeMR(N//d): return N//d
            while n > 1:
                if isPrimeMR(n):
                    ret[n], n = 1, 1
                else:
                    mrFlg = 1
                    j = findFactorRho(n)
                    k = 0
                    while n % j == 0:
                        n //= j
                        k += 1
                    ret[j] = k

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

def isPrimeMR(n):
    if n == 2:
        return True
    if n == 1 or n & 1 == 0:
        return False
    d = (n - 1) >> 1
    while d & 1 == 0:
        d >>= 1
    
    L = [2, 7, 61] if n < 1<<32 else [2, 13, 23, 1662803] if n < 1<<40 else [2, 3, 5, 7, 11, 13, 17] if n < 1<<48 else [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
    for a in L:
        t = d
        y = pow(a, t, n)
        while t != n - 1 and y != 1 and y != n - 1:
            y = (y * y) % n
            t <<= 1

        if y != n - 1 and t & 1 == 0:
            return False
    return True

def discrete_logarithm(a, b, mod):
    a %= mod
    b %= mod
    m = int(mod**0.5+0.9) + 1
    s = 1
    for i in range(m):
        if s == b:
            return i
        s = s * a % mod
        
    inva = pow(a, mod-2, mod)
    am = pow(a, m, mod)
    D = {}
    k = b
    for i in range(m):
        if k not in D:
            D[k] = i
        k = k * inva % mod
    k = 1
    for i in range(m):
        if k in D:
            return D[k] + i * m
        k = k * am % mod
    return -1

def primitive_root(mod, pf):
    for i in range(1, mod):
        for q in pf:
            if pow(i, (mod-1) // q, mod) == 1:
                break
        else:
            return i

def gcd(a, b):
    while b: a, b = b, a % b
    return abs(a)

def inv(x, nn):
    def invv(a, n):
        if a == 1: return (1, 0)
        if a == 0: return (-1, -1)
        b, m = a, n
        while m != 0: b,m = m,b%m
        if b > 1: return (-1, -1)
        k = n // a
        y, yy = invv(n % a, a)
        if y < 0: return (-1, -1)
        x = yy - k * y
        while x < 0:
            x += n
            y -= a
        return (x, y)
    return invv(x, nn)[0]

T = int(input())
for _ in range(T):
    p, k, a = map(int, input().split())
    orgk = k
    pf = primeFactor(p-1)
    h = primitive_root(p, pf)
    b = discrete_logarithm(h, a, p)
    while True:
        g = gcd(k, p-1)
        if g == 1: break
        if b % g:
            print(-1)
            break
        b //= g
        k //= g
    if g > 1: continue
    print(pow(h, b * inv(k, p-1) % (p-1), p))
0