結果

問題 No.308 素数は通れません
ユーザー 37zigen37zigen
提出日時 2016-03-14 13:57:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 154 ms / 1,000 ms
コード長 1,637 bytes
コンパイル時間 5,784 ms
コンパイル使用メモリ 78,308 KB
実行使用メモリ 58,328 KB
最終ジャッジ日時 2023-08-18 13:27:46
合計ジャッジ時間 21,565 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
56,108 KB
testcase_01 AC 122 ms
55,824 KB
testcase_02 AC 123 ms
55,952 KB
testcase_03 AC 125 ms
56,024 KB
testcase_04 AC 124 ms
55,808 KB
testcase_05 AC 124 ms
55,868 KB
testcase_06 AC 122 ms
55,884 KB
testcase_07 AC 121 ms
56,020 KB
testcase_08 AC 124 ms
56,236 KB
testcase_09 AC 123 ms
56,004 KB
testcase_10 AC 123 ms
56,192 KB
testcase_11 AC 122 ms
56,536 KB
testcase_12 AC 123 ms
56,372 KB
testcase_13 AC 124 ms
55,996 KB
testcase_14 AC 123 ms
56,148 KB
testcase_15 AC 121 ms
55,776 KB
testcase_16 AC 125 ms
55,952 KB
testcase_17 AC 124 ms
55,880 KB
testcase_18 AC 121 ms
54,272 KB
testcase_19 AC 122 ms
55,800 KB
testcase_20 AC 122 ms
55,868 KB
testcase_21 AC 123 ms
54,092 KB
testcase_22 AC 122 ms
56,200 KB
testcase_23 AC 124 ms
55,968 KB
testcase_24 AC 120 ms
55,752 KB
testcase_25 AC 123 ms
56,044 KB
testcase_26 AC 122 ms
56,032 KB
testcase_27 AC 122 ms
55,976 KB
testcase_28 AC 122 ms
54,292 KB
testcase_29 AC 119 ms
56,124 KB
testcase_30 AC 124 ms
55,840 KB
testcase_31 AC 125 ms
55,880 KB
testcase_32 AC 126 ms
56,108 KB
testcase_33 AC 125 ms
55,800 KB
testcase_34 AC 125 ms
56,024 KB
testcase_35 AC 125 ms
55,872 KB
testcase_36 AC 141 ms
56,260 KB
testcase_37 AC 139 ms
56,172 KB
testcase_38 AC 138 ms
56,112 KB
testcase_39 AC 138 ms
56,120 KB
testcase_40 AC 138 ms
55,956 KB
testcase_41 AC 139 ms
55,720 KB
testcase_42 AC 139 ms
55,932 KB
testcase_43 AC 139 ms
55,936 KB
testcase_44 AC 140 ms
55,936 KB
testcase_45 AC 142 ms
56,816 KB
testcase_46 AC 140 ms
55,728 KB
testcase_47 AC 138 ms
55,884 KB
testcase_48 AC 138 ms
55,964 KB
testcase_49 AC 141 ms
55,948 KB
testcase_50 AC 139 ms
56,360 KB
testcase_51 AC 140 ms
56,124 KB
testcase_52 AC 139 ms
56,580 KB
testcase_53 AC 141 ms
55,916 KB
testcase_54 AC 138 ms
56,148 KB
testcase_55 AC 137 ms
55,932 KB
testcase_56 AC 138 ms
55,948 KB
testcase_57 AC 139 ms
56,260 KB
testcase_58 AC 141 ms
56,308 KB
testcase_59 AC 138 ms
56,068 KB
testcase_60 AC 139 ms
55,948 KB
testcase_61 AC 141 ms
55,940 KB
testcase_62 AC 139 ms
56,276 KB
testcase_63 AC 140 ms
55,736 KB
testcase_64 AC 140 ms
55,928 KB
testcase_65 AC 139 ms
56,420 KB
testcase_66 AC 142 ms
56,308 KB
testcase_67 AC 138 ms
56,092 KB
testcase_68 AC 138 ms
55,916 KB
testcase_69 AC 138 ms
55,900 KB
testcase_70 AC 137 ms
56,152 KB
testcase_71 AC 138 ms
55,984 KB
testcase_72 AC 139 ms
55,868 KB
testcase_73 AC 138 ms
55,936 KB
testcase_74 AC 137 ms
56,392 KB
testcase_75 AC 137 ms
56,044 KB
testcase_76 AC 142 ms
56,048 KB
testcase_77 AC 137 ms
56,208 KB
testcase_78 AC 139 ms
56,060 KB
testcase_79 AC 138 ms
55,724 KB
testcase_80 AC 144 ms
55,960 KB
testcase_81 AC 145 ms
56,372 KB
testcase_82 AC 137 ms
56,308 KB
testcase_83 AC 137 ms
55,964 KB
testcase_84 AC 148 ms
55,924 KB
testcase_85 AC 137 ms
56,072 KB
testcase_86 AC 144 ms
56,348 KB
testcase_87 AC 146 ms
55,896 KB
testcase_88 AC 138 ms
56,200 KB
testcase_89 AC 136 ms
55,932 KB
testcase_90 AC 145 ms
55,940 KB
testcase_91 AC 139 ms
55,952 KB
testcase_92 AC 138 ms
56,312 KB
testcase_93 AC 136 ms
54,200 KB
testcase_94 AC 139 ms
55,884 KB
testcase_95 AC 139 ms
55,668 KB
testcase_96 AC 140 ms
55,880 KB
testcase_97 AC 139 ms
55,972 KB
testcase_98 AC 137 ms
55,668 KB
testcase_99 AC 141 ms
56,196 KB
testcase_100 AC 141 ms
55,960 KB
testcase_101 AC 154 ms
55,748 KB
testcase_102 AC 139 ms
56,276 KB
testcase_103 AC 138 ms
56,072 KB
testcase_104 AC 139 ms
55,684 KB
testcase_105 AC 138 ms
55,980 KB
testcase_106 AC 142 ms
58,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Date;
import java.util.Queue;
import java.util.Scanner;
import java.math.BigInteger;
public class main{
	public static final BigInteger EIGHT=new BigInteger("8");
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		long exec_time=new Date().getTime();
		BigInteger n=sc.nextBigInteger();
		if(n.compareTo(BigInteger.valueOf(60))<0){
			System.out.println(solveNaive(n.intValue()));
			return;
		}
		if(!n.remainder(EIGHT).equals(BigInteger.ONE)||!n.subtract(EIGHT).isProbablePrime(20)){
			System.out.println("8");
		}else{
			System.out.println("14");
		}
		System.err.println(new Date().getTime()-exec_time+"ms");		
	}

	public static void check(int x,int n,boolean[] isPrime,boolean[] used,Queue<Integer> q){
		if(x<1||x>n||isPrime[x]||used[x]){
			return;
		}
		used[x]=true;
		q.offer(x);
	}

	public static int solveNaive(int n){
		boolean[] isPrime=isPrimeArray(n);
		for(int w=1;w<=n;w++){
			boolean[] used=new boolean[n+1];
			Queue<Integer> q=new ArrayDeque<>();
			used[1]=true;
			q.offer(1);
			while(!q.isEmpty()){
				int x=q.poll();
				if(x%w!=0){
					check(x+1,n,isPrime,used,q);
				}
				if(x%w!=1){
					check(x-1,n,isPrime,used,q);
				}
				check(x+w,n,isPrime,used,q);
				check(x-w,n,isPrime,used,q);
			}
			if (used[n]) {
				return w;
			}
		}
		return -1;
	}


	public static boolean[] isPrimeArray(int max){
		boolean[] isPrime=new boolean[max+1];
		Arrays.fill(isPrime, true);
		for(int i=2;i*i<=max;i++){
			int j=i*2;
			while(j<=max){
				isPrime[j]=false;
				j+=i;
			}
		}
		return isPrime;
	}
}


0