結果
問題 | No.308 素数は通れません |
ユーザー | Grenache |
提出日時 | 2015-12-01 20:33:20 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 81 ms / 1,000 ms |
コード長 | 3,863 bytes |
コンパイル時間 | 4,015 ms |
コンパイル使用メモリ | 80,112 KB |
実行使用メモリ | 37,432 KB |
最終ジャッジ日時 | 2024-11-27 18:16:57 |
合計ジャッジ時間 | 12,230 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 54 ms
36,752 KB |
testcase_01 | AC | 54 ms
36,752 KB |
testcase_02 | AC | 54 ms
37,028 KB |
testcase_03 | AC | 56 ms
37,020 KB |
testcase_04 | AC | 55 ms
37,020 KB |
testcase_05 | AC | 55 ms
37,100 KB |
testcase_06 | AC | 54 ms
37,076 KB |
testcase_07 | AC | 54 ms
37,184 KB |
testcase_08 | AC | 54 ms
37,072 KB |
testcase_09 | AC | 53 ms
36,620 KB |
testcase_10 | AC | 56 ms
36,892 KB |
testcase_11 | AC | 54 ms
36,820 KB |
testcase_12 | AC | 53 ms
37,100 KB |
testcase_13 | AC | 56 ms
36,884 KB |
testcase_14 | AC | 54 ms
36,620 KB |
testcase_15 | AC | 54 ms
37,008 KB |
testcase_16 | AC | 54 ms
37,072 KB |
testcase_17 | AC | 55 ms
37,020 KB |
testcase_18 | AC | 54 ms
36,820 KB |
testcase_19 | AC | 55 ms
36,576 KB |
testcase_20 | AC | 54 ms
36,820 KB |
testcase_21 | AC | 58 ms
36,620 KB |
testcase_22 | AC | 53 ms
36,876 KB |
testcase_23 | AC | 54 ms
36,892 KB |
testcase_24 | AC | 54 ms
36,752 KB |
testcase_25 | AC | 54 ms
36,940 KB |
testcase_26 | AC | 54 ms
37,008 KB |
testcase_27 | AC | 55 ms
36,708 KB |
testcase_28 | AC | 54 ms
36,980 KB |
testcase_29 | AC | 54 ms
36,620 KB |
testcase_30 | AC | 56 ms
36,980 KB |
testcase_31 | AC | 53 ms
36,940 KB |
testcase_32 | AC | 55 ms
37,052 KB |
testcase_33 | AC | 54 ms
37,120 KB |
testcase_34 | AC | 56 ms
37,120 KB |
testcase_35 | AC | 54 ms
37,152 KB |
testcase_36 | AC | 55 ms
36,964 KB |
testcase_37 | AC | 53 ms
36,620 KB |
testcase_38 | AC | 54 ms
37,120 KB |
testcase_39 | AC | 55 ms
37,184 KB |
testcase_40 | AC | 56 ms
36,844 KB |
testcase_41 | AC | 55 ms
36,820 KB |
testcase_42 | AC | 55 ms
37,020 KB |
testcase_43 | AC | 54 ms
36,952 KB |
testcase_44 | AC | 54 ms
36,756 KB |
testcase_45 | AC | 54 ms
36,952 KB |
testcase_46 | AC | 53 ms
37,072 KB |
testcase_47 | AC | 55 ms
36,620 KB |
testcase_48 | AC | 54 ms
37,084 KB |
testcase_49 | AC | 54 ms
37,184 KB |
testcase_50 | AC | 53 ms
37,068 KB |
testcase_51 | AC | 53 ms
36,972 KB |
testcase_52 | AC | 54 ms
37,120 KB |
testcase_53 | AC | 53 ms
36,784 KB |
testcase_54 | AC | 54 ms
36,952 KB |
testcase_55 | AC | 53 ms
37,108 KB |
testcase_56 | AC | 54 ms
37,172 KB |
testcase_57 | AC | 55 ms
37,184 KB |
testcase_58 | AC | 54 ms
36,844 KB |
testcase_59 | AC | 54 ms
37,048 KB |
testcase_60 | AC | 54 ms
36,748 KB |
testcase_61 | AC | 54 ms
36,608 KB |
testcase_62 | AC | 54 ms
36,620 KB |
testcase_63 | AC | 81 ms
37,432 KB |
testcase_64 | AC | 56 ms
37,120 KB |
testcase_65 | AC | 56 ms
37,236 KB |
testcase_66 | AC | 54 ms
36,960 KB |
testcase_67 | AC | 55 ms
37,008 KB |
testcase_68 | AC | 55 ms
36,620 KB |
testcase_69 | AC | 54 ms
36,584 KB |
testcase_70 | AC | 54 ms
36,932 KB |
testcase_71 | AC | 54 ms
37,008 KB |
testcase_72 | AC | 53 ms
36,924 KB |
testcase_73 | AC | 53 ms
36,968 KB |
testcase_74 | AC | 54 ms
36,976 KB |
testcase_75 | AC | 55 ms
36,808 KB |
testcase_76 | AC | 54 ms
37,020 KB |
testcase_77 | AC | 55 ms
36,608 KB |
testcase_78 | AC | 54 ms
36,976 KB |
testcase_79 | AC | 53 ms
36,620 KB |
testcase_80 | AC | 55 ms
36,940 KB |
testcase_81 | AC | 59 ms
37,072 KB |
testcase_82 | AC | 55 ms
36,852 KB |
testcase_83 | AC | 56 ms
36,820 KB |
testcase_84 | AC | 61 ms
37,008 KB |
testcase_85 | AC | 57 ms
36,892 KB |
testcase_86 | AC | 60 ms
36,876 KB |
testcase_87 | AC | 59 ms
37,344 KB |
testcase_88 | AC | 55 ms
36,820 KB |
testcase_89 | AC | 54 ms
36,940 KB |
testcase_90 | AC | 60 ms
37,148 KB |
testcase_91 | AC | 55 ms
36,752 KB |
testcase_92 | AC | 54 ms
37,028 KB |
testcase_93 | AC | 55 ms
36,724 KB |
testcase_94 | AC | 54 ms
37,020 KB |
testcase_95 | AC | 54 ms
36,976 KB |
testcase_96 | AC | 55 ms
37,104 KB |
testcase_97 | AC | 55 ms
36,892 KB |
testcase_98 | AC | 53 ms
36,932 KB |
testcase_99 | AC | 56 ms
37,248 KB |
testcase_100 | AC | 57 ms
37,180 KB |
testcase_101 | AC | 61 ms
37,040 KB |
testcase_102 | AC | 55 ms
36,916 KB |
testcase_103 | AC | 55 ms
36,580 KB |
testcase_104 | AC | 54 ms
36,892 KB |
testcase_105 | AC | 54 ms
36,976 KB |
testcase_106 | AC | 56 ms
37,008 KB |
ソースコード
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); } } }