結果

問題 No.36 素数が嫌い!
ユーザー pekempeypekempey
提出日時 2015-09-25 14:07:04
言語 Python2
(2.7.18)
結果
WA  
実行時間 -
コード長 816 bytes
コンパイル時間 71 ms
コンパイル使用メモリ 6,672 KB
実行使用メモリ 9,400 KB
最終ジャッジ日時 2023-09-26 15:03:24
合計ジャッジ時間 1,880 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
9,360 KB
testcase_01 AC 17 ms
9,144 KB
testcase_02 AC 16 ms
9,156 KB
testcase_03 WA -
testcase_04 AC 16 ms
9,364 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 17 ms
9,288 KB
testcase_08 AC 17 ms
9,400 KB
testcase_09 AC 17 ms
9,244 KB
testcase_10 AC 17 ms
9,156 KB
testcase_11 AC 16 ms
9,188 KB
testcase_12 AC 17 ms
9,128 KB
testcase_13 WA -
testcase_14 AC 19 ms
9,260 KB
testcase_15 AC 17 ms
9,104 KB
testcase_16 AC 17 ms
9,184 KB
testcase_17 AC 16 ms
9,180 KB
testcase_18 AC 17 ms
9,236 KB
testcase_19 AC 17 ms
9,256 KB
testcase_20 AC 17 ms
9,188 KB
testcase_21 AC 17 ms
9,068 KB
testcase_22 AC 17 ms
9,296 KB
testcase_23 AC 17 ms
9,204 KB
testcase_24 AC 17 ms
9,240 KB
testcase_25 AC 17 ms
9,180 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import math, itertools, random, collections, time

def suspect(a, e, k, n):
	a = pow(a, e, n)
	if a == 1: return True
	for _ in xrange(k):
		if a == n - 1: return True
		a = a * a % n
	return False

def isprime(n):
	if n == 1: return False
	e = n - 1
	k = 0
	while e % 2 == 0:
		e /= 2
		k += 1
	if n < 2 ** 31:
		bases = [2, 3, 5, 7]
	else:
		bases = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]
	for i in bases:
		if n != i and not suspect(i, e, k, n): return False
	return True

def gcd(a, b):
	if b == 0: return a
	return gcd(b, a % b)

def rho(n):
	if n == 1: return -1
	x, y, d = 2, 2, 1
	f = lambda x: (x * x + 1) % n
	while d == 1:
		x, y = f(x), f(f(y))
		d = gcd(abs(x - y), n)
	if d == n: return -1
	return d

n = input()
if isprime(n):
	print "NO"
elif rho(n) != -1:
	print "YES"
else:
	print "NO"
0