結果

問題 No.1140 EXPotentiaLLL!
ユーザー tyawanmusityawanmusi
提出日時 2020-07-31 21:33:08
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 802 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 83,860 KB
最終ジャッジ日時 2023-09-20 21:56:42
合計ジャッジ時間 4,486 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 323 ms
79,496 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 88 ms
72,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline

import random

def is_prime(q,k=50):
    q = abs(q)
    #計算するまでもなく判定できるものははじく
    if q == 2: return True
    if q < 2 or q&1 == 0: return False

    #n-1=2^s*dとし(但しaは整数、dは奇数)、dを求める
    d = (q-1)>>1
    while d&1 == 0:
        d >>= 1
    
    #判定をk回繰り返す
    for i in xrange(k):
        a = random.randint(1,q-1)
        t = d
        y = pow(a,t,q)
        #[0,s-1]の範囲すべてをチェック
        while t != q-1 and y != 1 and y != q-1: 
            y = pow(y,2,q)
            t <<= 1
        if y != q-1 and t&1 == 0:
            return False
    return True


for _ in range(int(input())):
  a,p=map(int,input().split())
  if is_prime(p):print(1)
  else:print(-1)
0