結果

問題 No.308 素数は通れません
ユーザー ぴろずぴろず
提出日時 2015-12-01 10:42:48
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,465 bytes
コンパイル時間 3,541 ms
コンパイル使用メモリ 76,192 KB
実行使用メモリ 58,492 KB
最終ジャッジ日時 2023-10-12 07:39:56
合計ジャッジ時間 20,063 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,960 KB
testcase_01 AC 121 ms
55,872 KB
testcase_02 AC 120 ms
55,936 KB
testcase_03 AC 122 ms
55,860 KB
testcase_04 AC 120 ms
55,688 KB
testcase_05 AC 122 ms
55,720 KB
testcase_06 AC 120 ms
55,920 KB
testcase_07 AC 122 ms
55,968 KB
testcase_08 AC 121 ms
55,696 KB
testcase_09 AC 122 ms
55,980 KB
testcase_10 AC 121 ms
56,048 KB
testcase_11 WA -
testcase_12 AC 121 ms
55,884 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 121 ms
55,932 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 122 ms
55,452 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 122 ms
55,772 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 120 ms
55,484 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 122 ms
56,272 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 120 ms
55,692 KB
testcase_37 AC 121 ms
56,008 KB
testcase_38 AC 118 ms
55,436 KB
testcase_39 AC 121 ms
55,700 KB
testcase_40 AC 121 ms
56,216 KB
testcase_41 AC 120 ms
55,708 KB
testcase_42 AC 120 ms
55,744 KB
testcase_43 AC 119 ms
55,828 KB
testcase_44 AC 120 ms
55,732 KB
testcase_45 AC 121 ms
55,448 KB
testcase_46 AC 120 ms
55,988 KB
testcase_47 AC 120 ms
55,864 KB
testcase_48 AC 120 ms
55,788 KB
testcase_49 AC 119 ms
56,388 KB
testcase_50 AC 122 ms
56,176 KB
testcase_51 AC 121 ms
55,772 KB
testcase_52 AC 120 ms
55,848 KB
testcase_53 AC 122 ms
55,892 KB
testcase_54 AC 120 ms
55,848 KB
testcase_55 AC 120 ms
55,876 KB
testcase_56 AC 121 ms
55,892 KB
testcase_57 AC 122 ms
56,144 KB
testcase_58 AC 121 ms
55,948 KB
testcase_59 AC 120 ms
56,516 KB
testcase_60 AC 122 ms
56,360 KB
testcase_61 AC 120 ms
55,900 KB
testcase_62 AC 121 ms
55,956 KB
testcase_63 AC 124 ms
56,020 KB
testcase_64 AC 121 ms
55,648 KB
testcase_65 AC 122 ms
55,832 KB
testcase_66 AC 123 ms
55,996 KB
testcase_67 AC 122 ms
54,292 KB
testcase_68 RE -
testcase_69 RE -
testcase_70 RE -
testcase_71 RE -
testcase_72 RE -
testcase_73 RE -
testcase_74 RE -
testcase_75 RE -
testcase_76 AC 126 ms
55,748 KB
testcase_77 AC 121 ms
56,040 KB
testcase_78 AC 121 ms
56,088 KB
testcase_79 AC 121 ms
55,904 KB
testcase_80 AC 127 ms
56,016 KB
testcase_81 AC 129 ms
55,852 KB
testcase_82 AC 121 ms
55,992 KB
testcase_83 AC 120 ms
56,020 KB
testcase_84 AC 131 ms
56,096 KB
testcase_85 AC 123 ms
55,936 KB
testcase_86 AC 132 ms
55,972 KB
testcase_87 AC 133 ms
56,088 KB
testcase_88 AC 122 ms
55,616 KB
testcase_89 AC 120 ms
55,956 KB
testcase_90 AC 130 ms
55,952 KB
testcase_91 RE -
testcase_92 RE -
testcase_93 RE -
testcase_94 RE -
testcase_95 RE -
testcase_96 AC 118 ms
55,836 KB
testcase_97 RE -
testcase_98 RE -
testcase_99 RE -
testcase_100 RE -
testcase_101 RE -
testcase_102 RE -
testcase_103 RE -
testcase_104 RE -
testcase_105 RE -
testcase_106 RE -
権限があれば一括ダウンロードができます

ソースコード

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 void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		if (n < 60) {
			System.out.println(solveNaive((int) n));
			return;
		}
		if (n % 8 != 1 || !BigInteger.valueOf(n-8).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++) {
			boolean[] used = new boolean[n+1];
			Queue<Integer> q = new ArrayDeque<>();
			used[1] = true;
			q.offer(1);
			while(!q.isEmpty()) {
				int x = q.poll();
				check(x+1,n,isPrime,used,q);
				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