結果

問題 No.308 素数は通れません
ユーザー GrenacheGrenache
提出日時 2015-12-01 20:33:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 62 ms / 1,000 ms
コード長 3,863 bytes
コンパイル時間 4,365 ms
コンパイル使用メモリ 79,888 KB
実行使用メモリ 51,844 KB
最終ジャッジ日時 2023-08-18 13:05:07
合計ジャッジ時間 12,037 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
49,368 KB
testcase_01 AC 46 ms
49,300 KB
testcase_02 AC 46 ms
49,356 KB
testcase_03 AC 45 ms
49,440 KB
testcase_04 AC 46 ms
49,384 KB
testcase_05 AC 47 ms
49,320 KB
testcase_06 AC 46 ms
49,688 KB
testcase_07 AC 46 ms
49,644 KB
testcase_08 AC 46 ms
49,344 KB
testcase_09 AC 47 ms
49,416 KB
testcase_10 AC 46 ms
49,780 KB
testcase_11 AC 46 ms
49,384 KB
testcase_12 AC 47 ms
49,536 KB
testcase_13 AC 46 ms
49,348 KB
testcase_14 AC 46 ms
49,596 KB
testcase_15 AC 48 ms
49,480 KB
testcase_16 AC 47 ms
49,444 KB
testcase_17 AC 46 ms
49,440 KB
testcase_18 AC 46 ms
49,688 KB
testcase_19 AC 46 ms
49,352 KB
testcase_20 AC 45 ms
49,424 KB
testcase_21 AC 46 ms
49,428 KB
testcase_22 AC 45 ms
49,372 KB
testcase_23 AC 46 ms
49,296 KB
testcase_24 AC 48 ms
47,416 KB
testcase_25 AC 46 ms
49,316 KB
testcase_26 AC 46 ms
49,404 KB
testcase_27 AC 45 ms
49,312 KB
testcase_28 AC 45 ms
49,648 KB
testcase_29 AC 46 ms
49,300 KB
testcase_30 AC 46 ms
49,500 KB
testcase_31 AC 45 ms
49,424 KB
testcase_32 AC 47 ms
49,360 KB
testcase_33 AC 46 ms
49,316 KB
testcase_34 AC 45 ms
49,412 KB
testcase_35 AC 45 ms
49,276 KB
testcase_36 AC 45 ms
49,488 KB
testcase_37 AC 45 ms
49,772 KB
testcase_38 AC 45 ms
49,308 KB
testcase_39 AC 45 ms
49,344 KB
testcase_40 AC 46 ms
49,412 KB
testcase_41 AC 45 ms
49,508 KB
testcase_42 AC 44 ms
49,440 KB
testcase_43 AC 46 ms
49,448 KB
testcase_44 AC 46 ms
49,376 KB
testcase_45 AC 48 ms
49,796 KB
testcase_46 AC 46 ms
49,596 KB
testcase_47 AC 46 ms
49,400 KB
testcase_48 AC 46 ms
47,520 KB
testcase_49 AC 45 ms
49,788 KB
testcase_50 AC 45 ms
47,440 KB
testcase_51 AC 46 ms
49,796 KB
testcase_52 AC 46 ms
49,368 KB
testcase_53 AC 45 ms
49,344 KB
testcase_54 AC 45 ms
49,424 KB
testcase_55 AC 45 ms
49,488 KB
testcase_56 AC 46 ms
49,344 KB
testcase_57 AC 46 ms
49,460 KB
testcase_58 AC 47 ms
49,288 KB
testcase_59 AC 46 ms
49,364 KB
testcase_60 AC 46 ms
49,408 KB
testcase_61 AC 46 ms
49,448 KB
testcase_62 AC 44 ms
49,468 KB
testcase_63 AC 48 ms
49,716 KB
testcase_64 AC 47 ms
49,420 KB
testcase_65 AC 46 ms
49,292 KB
testcase_66 AC 45 ms
49,776 KB
testcase_67 AC 44 ms
49,364 KB
testcase_68 AC 45 ms
49,328 KB
testcase_69 AC 45 ms
49,424 KB
testcase_70 AC 45 ms
49,416 KB
testcase_71 AC 45 ms
49,540 KB
testcase_72 AC 45 ms
49,352 KB
testcase_73 AC 45 ms
49,408 KB
testcase_74 AC 46 ms
49,440 KB
testcase_75 AC 45 ms
49,464 KB
testcase_76 AC 46 ms
49,324 KB
testcase_77 AC 45 ms
49,348 KB
testcase_78 AC 45 ms
49,688 KB
testcase_79 AC 45 ms
49,320 KB
testcase_80 AC 47 ms
49,596 KB
testcase_81 AC 49 ms
49,676 KB
testcase_82 AC 46 ms
49,432 KB
testcase_83 AC 45 ms
49,376 KB
testcase_84 AC 51 ms
50,424 KB
testcase_85 AC 45 ms
49,368 KB
testcase_86 AC 51 ms
50,440 KB
testcase_87 AC 52 ms
50,416 KB
testcase_88 AC 45 ms
49,372 KB
testcase_89 AC 47 ms
49,500 KB
testcase_90 AC 55 ms
50,468 KB
testcase_91 AC 46 ms
49,508 KB
testcase_92 AC 46 ms
49,328 KB
testcase_93 AC 46 ms
49,428 KB
testcase_94 AC 46 ms
49,600 KB
testcase_95 AC 45 ms
49,348 KB
testcase_96 AC 47 ms
50,156 KB
testcase_97 AC 46 ms
49,356 KB
testcase_98 AC 46 ms
49,452 KB
testcase_99 AC 49 ms
50,444 KB
testcase_100 AC 48 ms
50,576 KB
testcase_101 AC 62 ms
51,844 KB
testcase_102 AC 46 ms
49,780 KB
testcase_103 AC 46 ms
49,336 KB
testcase_104 AC 45 ms
49,328 KB
testcase_105 AC 46 ms
49,452 KB
testcase_106 AC 50 ms
50,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;


public class Main_yukicoder308_1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        String N = sc.next();

        if (N.length() <= 2) {
        	int n = Integer.parseInt(N);

        	boolean[] notprime = new boolean[n + 1];
        	notprime[1] = true;
        	for (int i = 2; i * i <= n; i++) {
        		if (!notprime[i]) {
        			for (int j = 2; i * j <= n; j++) {
        				notprime[i * j] = true;
        			}
        		}
        	}

        	for (int w = 2; w <= n; w++) {
        		if (dfs(w, n, notprime, pr)) {
        			pr.println(w);
        			break;
        		}
        	}
        } else {
        	int mod = 0;
        	for (int i = 0; i < N.length(); i++) {
        		mod = (10 * mod + (N.charAt(i) - '0')) % 8;
        	}

        	if (mod != 1) {
        		pr.println(8);
        	} else {
        		if (isPrime(N, 8)) {
        			pr.println(14);
        		} else {
            		pr.println(8);
        		}
        	}
        }

        pr.close();
        sc.close();
    }

	private static boolean isPrime(String N, int sub) {
		if (N.length() < 12) {
			long n = Long.parseLong(N);
			n -= sub;
			boolean prime = (n != 1);
			for (long i = 2; i * i <= n; i++) {
				if (n % i == 0) {
					prime = false;
					break;
				}
			}

			return prime;
		} else {
			BigInteger n = new BigInteger(N);
			n = n.subtract(new BigInteger(Integer.toString(sub)));

			return n.isProbablePrime(20);
		}
	}

	private static boolean dfs(int w, int n, boolean[] notprime, Printer pr) {
		int[] d = {w, 1, -1, -w};
		boolean[] used = new boolean[n + 1];

		Deque<Integer> st = new ArrayDeque<Integer>();
		Deque<List<Integer>> rt = new ArrayDeque<List<Integer>>();
		st.push(1);
		rt.push(new ArrayList<Integer>());
		used[1] = true;
		while (!st.isEmpty()) {
			int e = st.pop();
			List<Integer> tmprt = rt.pop();

			for (int i = 0; i < d.length; i++) {
				int tmp = e + d[i];

				if (tmp < 1 || tmp > n) {
					continue;
				}
				if (e % w == 0 && d[i] == 1) {
					continue;
				}
				if (e % w == 1 && d[i] == -1) {
					continue;
				}

				if (tmp == n) {
					tmprt.add(tmp);
//					pr.println(tmprt.toString());

					return true;
				}

				if (!used[tmp] && notprime[tmp]) {
					used[tmp] = true;
					st.push(tmp);
					List<Integer> tt = new ArrayList<Integer>(tmprt);
					tt.add(tmp);
					rt.push(tt);
				}
			}
		}

		return false;
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0