結果

問題 No.308 素数は通れません
ユーザー 👑 kmjpkmjp
提出日時 2015-12-01 00:53:31
言語 Python2
(2.7.18)
結果
AC  
実行時間 19 ms / 1,000 ms
コード長 1,058 bytes
コンパイル時間 643 ms
コンパイル使用メモリ 7,052 KB
実行使用メモリ 8,976 KB
最終ジャッジ日時 2023-08-18 12:54:36
合計ジャッジ時間 5,945 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,864 KB
testcase_01 AC 16 ms
8,776 KB
testcase_02 AC 16 ms
8,716 KB
testcase_03 AC 15 ms
8,752 KB
testcase_04 AC 15 ms
8,852 KB
testcase_05 AC 15 ms
8,716 KB
testcase_06 AC 15 ms
8,904 KB
testcase_07 AC 15 ms
8,864 KB
testcase_08 AC 15 ms
8,812 KB
testcase_09 AC 15 ms
8,916 KB
testcase_10 AC 15 ms
8,920 KB
testcase_11 AC 15 ms
8,780 KB
testcase_12 AC 14 ms
8,888 KB
testcase_13 AC 15 ms
8,776 KB
testcase_14 AC 15 ms
8,836 KB
testcase_15 AC 15 ms
8,904 KB
testcase_16 AC 15 ms
8,920 KB
testcase_17 AC 15 ms
8,916 KB
testcase_18 AC 15 ms
8,748 KB
testcase_19 AC 16 ms
8,916 KB
testcase_20 AC 15 ms
8,944 KB
testcase_21 AC 15 ms
8,944 KB
testcase_22 AC 16 ms
8,748 KB
testcase_23 AC 16 ms
8,848 KB
testcase_24 AC 15 ms
8,784 KB
testcase_25 AC 15 ms
8,912 KB
testcase_26 AC 15 ms
8,772 KB
testcase_27 AC 15 ms
8,804 KB
testcase_28 AC 15 ms
8,924 KB
testcase_29 AC 15 ms
8,824 KB
testcase_30 AC 15 ms
8,928 KB
testcase_31 AC 15 ms
8,776 KB
testcase_32 AC 16 ms
8,808 KB
testcase_33 AC 15 ms
8,784 KB
testcase_34 AC 15 ms
8,752 KB
testcase_35 AC 16 ms
8,944 KB
testcase_36 AC 16 ms
8,824 KB
testcase_37 AC 16 ms
8,908 KB
testcase_38 AC 16 ms
8,820 KB
testcase_39 AC 16 ms
8,940 KB
testcase_40 AC 15 ms
8,844 KB
testcase_41 AC 15 ms
8,892 KB
testcase_42 AC 16 ms
8,792 KB
testcase_43 AC 16 ms
8,776 KB
testcase_44 AC 16 ms
8,860 KB
testcase_45 AC 16 ms
8,860 KB
testcase_46 AC 16 ms
8,860 KB
testcase_47 AC 16 ms
8,724 KB
testcase_48 AC 15 ms
8,820 KB
testcase_49 AC 16 ms
8,724 KB
testcase_50 AC 15 ms
8,956 KB
testcase_51 AC 16 ms
8,860 KB
testcase_52 AC 15 ms
8,924 KB
testcase_53 AC 15 ms
8,924 KB
testcase_54 AC 16 ms
8,796 KB
testcase_55 AC 15 ms
8,764 KB
testcase_56 AC 16 ms
8,756 KB
testcase_57 AC 16 ms
8,820 KB
testcase_58 AC 16 ms
8,928 KB
testcase_59 AC 15 ms
8,904 KB
testcase_60 AC 16 ms
8,956 KB
testcase_61 AC 16 ms
8,792 KB
testcase_62 AC 15 ms
8,832 KB
testcase_63 AC 16 ms
8,840 KB
testcase_64 AC 16 ms
8,820 KB
testcase_65 AC 15 ms
8,860 KB
testcase_66 AC 15 ms
8,800 KB
testcase_67 AC 16 ms
8,880 KB
testcase_68 AC 16 ms
8,860 KB
testcase_69 AC 16 ms
8,824 KB
testcase_70 AC 16 ms
8,904 KB
testcase_71 AC 16 ms
8,796 KB
testcase_72 AC 16 ms
8,908 KB
testcase_73 AC 16 ms
8,840 KB
testcase_74 AC 16 ms
8,764 KB
testcase_75 AC 16 ms
8,920 KB
testcase_76 AC 16 ms
8,760 KB
testcase_77 AC 16 ms
8,796 KB
testcase_78 AC 16 ms
8,760 KB
testcase_79 AC 16 ms
8,768 KB
testcase_80 AC 16 ms
8,788 KB
testcase_81 AC 18 ms
8,936 KB
testcase_82 AC 16 ms
8,940 KB
testcase_83 AC 16 ms
8,800 KB
testcase_84 AC 18 ms
8,900 KB
testcase_85 AC 16 ms
8,764 KB
testcase_86 AC 19 ms
8,912 KB
testcase_87 AC 18 ms
8,836 KB
testcase_88 AC 16 ms
8,796 KB
testcase_89 AC 16 ms
8,800 KB
testcase_90 AC 19 ms
8,796 KB
testcase_91 AC 16 ms
8,908 KB
testcase_92 AC 16 ms
8,828 KB
testcase_93 AC 15 ms
8,864 KB
testcase_94 AC 15 ms
8,936 KB
testcase_95 AC 16 ms
8,728 KB
testcase_96 AC 16 ms
8,904 KB
testcase_97 AC 15 ms
8,828 KB
testcase_98 AC 15 ms
8,764 KB
testcase_99 AC 16 ms
8,824 KB
testcase_100 AC 16 ms
8,976 KB
testcase_101 AC 18 ms
8,764 KB
testcase_102 AC 16 ms
8,800 KB
testcase_103 AC 16 ms
8,912 KB
testcase_104 AC 15 ms
8,824 KB
testcase_105 AC 16 ms
8,900 KB
testcase_106 AC 15 ms
8,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
import random

N=input()

def create_erato(n):
	div = [0]*(n+2)
	primes = []
	for i in range(2,n+1):
		if div[i] == 0:
			primes.append(i)
			for j in range(i,n+1,i):
				div[j] = i
	return (div,primes)

div = create_erato(105)[0]
vis=[0]*105

def ok(N,M):
	q=[1]
	while len(q) > 0:
		v = q[-1]
		q.pop()
		
		for t in (v-1,v+1,v-M,v+M):
			if v%M==1 and t==v-1:
				continue
			if v%M==0 and t==v+1:
				continue
			if t<=0 or t>N:
				continue
			if div[t]!=t and vis[t]<M:
				vis[t]=M
				q.append(t)
	
	if vis[N]==M:
		return 1
	return 0

def MillarRabin(v,loop):
	d = v-1
	s = 0
	while d%2==0:
		s += 1
		d /= 2
	
	for i in range(loop):
		a = random.randrange(0,v-2)+1
		r = pow(a,d,v)
		if r==1 or r==v-1:
			continue
		ng = 0
		for j in range(s):
			r = pow(r,2,v)
			if r == v-1:
				ng = 1
		if ng==0:
			return 0
	
	return 1


if N>=100:
	random.seed()
	for v in range(8,100,2):
		if div[v+1]!=v+1 and (N%v!=1 or MillarRabin(N-v,100)==0):
			print v
			break
else:
	for M in range(2,105):
		if ok(N,M):
			print M
			break

0