結果

問題 No.1140 EXPotentiaLLL!
ユーザー tktk_snsn
提出日時 2020-12-23 20:42:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 710 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 82,180 KB
実行使用メモリ 175,384 KB
最終ジャッジ日時 2024-09-21 16:31:10
合計ジャッジ時間 10,417 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 3 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
U = 5 * 10 ** 6


def prime_sieve(N):
    #sieve[i] : iの最小の素因数
    sieve = [0] * (N + 1)
    prime = []
    for i in range(2, N + 1):
        if sieve[i] == 0:
            sieve[i] = i
            prime.append(i)
        for p in prime:
            if p > sieve[i] or i * p > N:
                break
            sieve[i * p] = p
    return sieve


sieve = prime_sieve(U)
fact = [1] * (U + 1)


T = int(input())
for _ in range(T):
    a, p = map(int, input().split())
    if p == 1 or sieve[p] != p:
        print(-1)
    elif p == 2:
        print(a * a % p)
    else:
        n = 1 + p // 2
        print(pow(a, n, p))
0