結果

問題 No.308 素数は通れません
ユーザー ぴろずぴろず
提出日時 2015-12-01 10:50:27
言語 Java19
(openjdk 21)
結果
AC  
実行時間 140 ms / 1,000 ms
コード長 1,722 bytes
コンパイル時間 4,644 ms
コンパイル使用メモリ 76,232 KB
実行使用メモリ 57,532 KB
最終ジャッジ日時 2023-08-18 13:01:32
合計ジャッジ時間 20,199 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,704 KB
testcase_01 AC 123 ms
55,360 KB
testcase_02 AC 125 ms
55,528 KB
testcase_03 AC 125 ms
55,848 KB
testcase_04 AC 125 ms
55,696 KB
testcase_05 AC 126 ms
55,360 KB
testcase_06 AC 125 ms
55,640 KB
testcase_07 AC 126 ms
53,956 KB
testcase_08 AC 123 ms
55,676 KB
testcase_09 AC 123 ms
55,936 KB
testcase_10 AC 122 ms
56,096 KB
testcase_11 AC 124 ms
55,796 KB
testcase_12 AC 123 ms
55,808 KB
testcase_13 AC 123 ms
56,004 KB
testcase_14 AC 123 ms
56,004 KB
testcase_15 AC 123 ms
55,632 KB
testcase_16 AC 124 ms
55,756 KB
testcase_17 AC 123 ms
55,776 KB
testcase_18 AC 123 ms
56,104 KB
testcase_19 AC 123 ms
55,892 KB
testcase_20 AC 123 ms
55,860 KB
testcase_21 AC 122 ms
56,120 KB
testcase_22 AC 122 ms
55,752 KB
testcase_23 AC 122 ms
56,248 KB
testcase_24 AC 122 ms
55,800 KB
testcase_25 AC 124 ms
55,980 KB
testcase_26 AC 124 ms
55,608 KB
testcase_27 AC 123 ms
55,908 KB
testcase_28 AC 123 ms
55,904 KB
testcase_29 AC 123 ms
55,812 KB
testcase_30 AC 122 ms
55,964 KB
testcase_31 AC 122 ms
55,856 KB
testcase_32 AC 126 ms
55,836 KB
testcase_33 AC 124 ms
55,508 KB
testcase_34 AC 124 ms
55,700 KB
testcase_35 AC 125 ms
56,012 KB
testcase_36 AC 125 ms
55,560 KB
testcase_37 AC 124 ms
55,500 KB
testcase_38 AC 124 ms
56,196 KB
testcase_39 AC 123 ms
56,104 KB
testcase_40 AC 123 ms
55,888 KB
testcase_41 AC 122 ms
56,044 KB
testcase_42 AC 122 ms
55,848 KB
testcase_43 AC 124 ms
57,532 KB
testcase_44 AC 125 ms
55,768 KB
testcase_45 AC 125 ms
55,616 KB
testcase_46 AC 125 ms
55,800 KB
testcase_47 AC 125 ms
55,516 KB
testcase_48 AC 126 ms
55,760 KB
testcase_49 AC 122 ms
55,548 KB
testcase_50 AC 124 ms
56,204 KB
testcase_51 AC 122 ms
55,404 KB
testcase_52 AC 123 ms
55,760 KB
testcase_53 AC 124 ms
55,572 KB
testcase_54 AC 123 ms
55,752 KB
testcase_55 AC 124 ms
55,484 KB
testcase_56 AC 124 ms
55,500 KB
testcase_57 AC 122 ms
55,860 KB
testcase_58 AC 122 ms
55,496 KB
testcase_59 AC 123 ms
55,776 KB
testcase_60 AC 125 ms
55,692 KB
testcase_61 AC 123 ms
55,492 KB
testcase_62 AC 122 ms
55,880 KB
testcase_63 AC 125 ms
55,892 KB
testcase_64 AC 121 ms
53,424 KB
testcase_65 AC 122 ms
55,960 KB
testcase_66 AC 122 ms
55,956 KB
testcase_67 AC 123 ms
55,720 KB
testcase_68 AC 123 ms
55,752 KB
testcase_69 AC 123 ms
55,704 KB
testcase_70 AC 124 ms
55,744 KB
testcase_71 AC 124 ms
56,508 KB
testcase_72 AC 125 ms
55,412 KB
testcase_73 AC 125 ms
55,736 KB
testcase_74 AC 123 ms
55,568 KB
testcase_75 AC 123 ms
55,768 KB
testcase_76 AC 126 ms
55,896 KB
testcase_77 AC 124 ms
55,720 KB
testcase_78 AC 121 ms
53,980 KB
testcase_79 AC 123 ms
55,764 KB
testcase_80 AC 128 ms
56,128 KB
testcase_81 AC 129 ms
56,108 KB
testcase_82 AC 122 ms
56,316 KB
testcase_83 AC 124 ms
55,732 KB
testcase_84 AC 132 ms
55,728 KB
testcase_85 AC 123 ms
55,704 KB
testcase_86 AC 132 ms
55,748 KB
testcase_87 AC 132 ms
55,700 KB
testcase_88 AC 126 ms
56,040 KB
testcase_89 AC 123 ms
55,956 KB
testcase_90 AC 133 ms
55,772 KB
testcase_91 AC 123 ms
56,016 KB
testcase_92 AC 125 ms
55,552 KB
testcase_93 AC 125 ms
55,620 KB
testcase_94 AC 123 ms
55,564 KB
testcase_95 AC 126 ms
56,360 KB
testcase_96 AC 127 ms
55,876 KB
testcase_97 AC 121 ms
55,960 KB
testcase_98 AC 123 ms
56,024 KB
testcase_99 AC 125 ms
55,860 KB
testcase_100 AC 127 ms
56,140 KB
testcase_101 AC 140 ms
56,200 KB
testcase_102 AC 123 ms
55,776 KB
testcase_103 AC 123 ms
56,040 KB
testcase_104 AC 125 ms
55,896 KB
testcase_105 AC 125 ms
55,964 KB
testcase_106 AC 127 ms
55,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no308;

import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	public static final BigInteger EIGHT = new BigInteger("8");
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		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);
		}
	}

	public static int solveNaive(int n) {
		boolean[] isPrime = isPrimeArray(n);
		for(int w=1;w<=n;w++) {
//			System.out.println(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();
//				System.out.println(x);
				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 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 boolean[] isPrimeArray(int max) {
		boolean[] isPrime = new boolean[max+1];
		Arrays.fill(isPrime, true);
		isPrime[0] = isPrime[1] = false;
		for(int i=2;i*i<=max;i++) {
			if (isPrime[i]) {
				int j = i * 2;
				while(j<=max) {
					isPrime[j] = false;
					j += i;
				}
			}
		}
		return isPrime;
	}

}
0