結果

問題 No.36 素数が嫌い!
ユーザー pekempeypekempey
提出日時 2015-09-25 14:22:41
言語 Python2
(2.7.18)
結果
AC  
実行時間 61 ms / 5,000 ms
コード長 966 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 6,608 KB
実行使用メモリ 9,384 KB
最終ジャッジ日時 2023-09-09 07:21:25
合計ジャッジ時間 2,097 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
9,164 KB
testcase_01 AC 17 ms
9,268 KB
testcase_02 AC 17 ms
9,248 KB
testcase_03 AC 17 ms
9,228 KB
testcase_04 AC 17 ms
9,256 KB
testcase_05 AC 17 ms
9,184 KB
testcase_06 AC 17 ms
9,188 KB
testcase_07 AC 17 ms
9,284 KB
testcase_08 AC 17 ms
9,292 KB
testcase_09 AC 16 ms
9,096 KB
testcase_10 AC 17 ms
9,232 KB
testcase_11 AC 17 ms
9,284 KB
testcase_12 AC 17 ms
9,304 KB
testcase_13 AC 61 ms
9,128 KB
testcase_14 AC 19 ms
9,276 KB
testcase_15 AC 17 ms
9,196 KB
testcase_16 AC 16 ms
9,340 KB
testcase_17 AC 17 ms
9,320 KB
testcase_18 AC 17 ms
9,260 KB
testcase_19 AC 16 ms
9,180 KB
testcase_20 AC 17 ms
9,196 KB
testcase_21 AC 17 ms
9,168 KB
testcase_22 AC 17 ms
9,272 KB
testcase_23 AC 17 ms
9,384 KB
testcase_24 AC 16 ms
9,376 KB
testcase_25 AC 17 ms
9,308 KB
testcase_26 AC 18 ms
9,252 KB
testcase_27 AC 18 ms
9,308 KB
testcase_28 AC 17 ms
9,372 KB
testcase_29 AC 17 ms
9,112 KB
権限があれば一括ダウンロードができます

ソースコード

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):
	for c in xrange(1, n):
		if c == n - 2: continue
		x, y, d = 2, 2, 1
		f = lambda x: (x * x + c) % n
		while d == 1:
			x, y = f(x), f(f(y))
			d = gcd(abs(x - y), n)
		if d != n: return d
	return -1

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