結果

問題 No.308 素数は通れません
ユーザー 👑 kmjpkmjp
提出日時 2015-12-01 00:46:08
言語 Python2
(2.7.18)
結果
WA  
実行時間 -
コード長 961 bytes
コンパイル時間 674 ms
コンパイル使用メモリ 6,600 KB
実行使用メモリ 8,936 KB
最終ジャッジ日時 2023-10-12 06:09:39
合計ジャッジ時間 5,571 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import sys
import math
import random

N=input()

isok=[0]*105
vis=[0]*105
for i in range(2,105):
	for j in range(2,i):
		if i%j==0:
			isok[i]=1

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 isok[t] and vis[t]<M:
				vis[t]=M
				q.append(t)
	
	if vis[N]==M:
		return 1
	return 0

def isprime(v):
	d = v-1
	s = 0
	while d%2==0:
		s += 1
		d /= 2
	
	for i in range(100):
		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 isok[v+1]==0:
			continue
		if N%v!=1:
			print v
			break
		if isprime(N-v)==0:
			print v
			break
else:
	for M in range(2,105):
		print M
		if ok(N,M):
			print M
			break

0