結果

問題 No.36 素数が嫌い!
コンテスト
ユーザー pekempey
提出日時 2015-09-25 14:07:04
言語 PyPy2
(7.3.15)
結果
WA  
実行時間 -
コード長 816 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 330 ms
コンパイル使用メモリ 77,364 KB
最終ジャッジ日時 2025-12-03 17:11:50
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 18 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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