結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー GrayCoder
提出日時 2018-11-11 10:05:48
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 657 bytes
コンパイル時間 109 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 22,912 KB
最終ジャッジ日時 2024-11-18 16:32:12
合計ジャッジ時間 35,030 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

import random
from sys import stdin, stdout

def main():
    N = int(input())
    X = map(int, stdin.read().splitlines())

    for i in X:
        stdout.write(str(i) + ' ' + is_prime(i) + '\n')

def is_prime(n):
    if n == 2: return '1'
    if n == 1 or not n % 2: return '0'

    d = (n - 1) >> 1
    while not d % 2:
        d >>= 1

    for k in range(100):
        a = random.randint(1, n - 1)
        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 not t % 2:
            return '0'
    return '1'

input = lambda: stdin.readline()
main()
0