結果
問題 | No.308 素数は通れません |
ユーザー | eitaho |
提出日時 | 2015-12-01 13:39:46 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 36 ms / 1,000 ms |
コード長 | 4,329 bytes |
コンパイル時間 | 1,153 ms |
コンパイル使用メモリ | 111,232 KB |
実行使用メモリ | 19,456 KB |
最終ジャッジ日時 | 2024-05-05 18:20:26 |
合計ジャッジ時間 | 6,269 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 26 ms
18,304 KB |
testcase_01 | AC | 26 ms
18,176 KB |
testcase_02 | AC | 27 ms
18,432 KB |
testcase_03 | AC | 26 ms
18,560 KB |
testcase_04 | AC | 27 ms
18,560 KB |
testcase_05 | AC | 26 ms
18,304 KB |
testcase_06 | AC | 27 ms
18,304 KB |
testcase_07 | AC | 27 ms
18,560 KB |
testcase_08 | AC | 27 ms
18,560 KB |
testcase_09 | AC | 26 ms
18,432 KB |
testcase_10 | AC | 26 ms
18,304 KB |
testcase_11 | AC | 26 ms
18,432 KB |
testcase_12 | AC | 26 ms
18,560 KB |
testcase_13 | AC | 26 ms
18,304 KB |
testcase_14 | AC | 27 ms
18,560 KB |
testcase_15 | AC | 28 ms
18,432 KB |
testcase_16 | AC | 27 ms
18,560 KB |
testcase_17 | AC | 26 ms
18,560 KB |
testcase_18 | AC | 28 ms
18,432 KB |
testcase_19 | AC | 28 ms
18,176 KB |
testcase_20 | AC | 27 ms
18,304 KB |
testcase_21 | AC | 27 ms
18,432 KB |
testcase_22 | AC | 27 ms
18,560 KB |
testcase_23 | AC | 26 ms
18,560 KB |
testcase_24 | AC | 27 ms
18,560 KB |
testcase_25 | AC | 26 ms
18,304 KB |
testcase_26 | AC | 29 ms
18,304 KB |
testcase_27 | AC | 28 ms
18,304 KB |
testcase_28 | AC | 26 ms
18,560 KB |
testcase_29 | AC | 25 ms
18,432 KB |
testcase_30 | AC | 26 ms
18,560 KB |
testcase_31 | AC | 26 ms
18,560 KB |
testcase_32 | AC | 26 ms
18,560 KB |
testcase_33 | AC | 26 ms
18,304 KB |
testcase_34 | AC | 26 ms
18,560 KB |
testcase_35 | AC | 25 ms
18,176 KB |
testcase_36 | AC | 25 ms
18,304 KB |
testcase_37 | AC | 25 ms
18,176 KB |
testcase_38 | AC | 26 ms
18,432 KB |
testcase_39 | AC | 26 ms
18,560 KB |
testcase_40 | AC | 26 ms
18,304 KB |
testcase_41 | AC | 27 ms
18,432 KB |
testcase_42 | AC | 26 ms
18,432 KB |
testcase_43 | AC | 26 ms
18,432 KB |
testcase_44 | AC | 25 ms
18,304 KB |
testcase_45 | AC | 26 ms
18,304 KB |
testcase_46 | AC | 26 ms
18,304 KB |
testcase_47 | AC | 26 ms
18,560 KB |
testcase_48 | AC | 26 ms
18,432 KB |
testcase_49 | AC | 25 ms
18,304 KB |
testcase_50 | AC | 27 ms
18,560 KB |
testcase_51 | AC | 26 ms
18,304 KB |
testcase_52 | AC | 27 ms
18,560 KB |
testcase_53 | AC | 27 ms
18,560 KB |
testcase_54 | AC | 26 ms
18,176 KB |
testcase_55 | AC | 26 ms
18,560 KB |
testcase_56 | AC | 29 ms
19,200 KB |
testcase_57 | AC | 26 ms
18,560 KB |
testcase_58 | AC | 26 ms
18,560 KB |
testcase_59 | AC | 25 ms
18,304 KB |
testcase_60 | AC | 27 ms
18,304 KB |
testcase_61 | AC | 26 ms
18,432 KB |
testcase_62 | AC | 26 ms
18,304 KB |
testcase_63 | AC | 32 ms
19,200 KB |
testcase_64 | AC | 26 ms
18,432 KB |
testcase_65 | AC | 26 ms
18,304 KB |
testcase_66 | AC | 26 ms
18,432 KB |
testcase_67 | AC | 28 ms
18,432 KB |
testcase_68 | AC | 27 ms
18,560 KB |
testcase_69 | AC | 26 ms
18,560 KB |
testcase_70 | AC | 27 ms
18,176 KB |
testcase_71 | AC | 26 ms
18,432 KB |
testcase_72 | AC | 27 ms
18,560 KB |
testcase_73 | AC | 27 ms
18,560 KB |
testcase_74 | AC | 27 ms
18,560 KB |
testcase_75 | AC | 26 ms
18,432 KB |
testcase_76 | AC | 30 ms
19,072 KB |
testcase_77 | AC | 26 ms
18,432 KB |
testcase_78 | AC | 26 ms
18,176 KB |
testcase_79 | AC | 25 ms
18,304 KB |
testcase_80 | AC | 29 ms
19,200 KB |
testcase_81 | AC | 33 ms
19,328 KB |
testcase_82 | AC | 27 ms
18,176 KB |
testcase_83 | AC | 27 ms
18,432 KB |
testcase_84 | AC | 35 ms
19,072 KB |
testcase_85 | AC | 29 ms
18,176 KB |
testcase_86 | AC | 36 ms
19,328 KB |
testcase_87 | AC | 36 ms
19,456 KB |
testcase_88 | AC | 28 ms
18,304 KB |
testcase_89 | AC | 28 ms
18,304 KB |
testcase_90 | AC | 35 ms
19,200 KB |
testcase_91 | AC | 27 ms
18,304 KB |
testcase_92 | AC | 26 ms
18,304 KB |
testcase_93 | AC | 26 ms
18,432 KB |
testcase_94 | AC | 26 ms
18,304 KB |
testcase_95 | AC | 26 ms
18,304 KB |
testcase_96 | AC | 33 ms
19,328 KB |
testcase_97 | AC | 26 ms
18,432 KB |
testcase_98 | AC | 25 ms
18,560 KB |
testcase_99 | AC | 34 ms
18,944 KB |
testcase_100 | AC | 31 ms
19,200 KB |
testcase_101 | AC | 35 ms
19,200 KB |
testcase_102 | AC | 28 ms
18,560 KB |
testcase_103 | AC | 27 ms
18,432 KB |
testcase_104 | AC | 27 ms
18,560 KB |
testcase_105 | AC | 26 ms
18,432 KB |
testcase_106 | AC | 32 ms
19,200 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; using System.IO; using System.Linq; using System.Text; using System.Collections.Generic; using System.Diagnostics; using System.Numerics; using Enu = System.Linq.Enumerable; class Program { public void Solve() { var N = BigInteger.Parse(Reader.String()); if (N <= 100) Console.WriteLine(BruteForce((int)N)); else if (N % 8 == 1 && MillerRabin.IsPrime(N - 8)) Console.WriteLine(14); else Console.WriteLine(8); Console.ReadLine(); } int BruteForce(int N) { for (int W = 3; W < N; W++) if (Can(N, W)) return W; return N; } private bool Can(int N, int W) { var seen = Sieve(N); var que = new Queue<int>(); que.Enqueue(1); Action<int> Add = x => { seen[x] = true; que.Enqueue(x); }; while (que.Count > 0) { int val = que.Dequeue(); if (val == N) return true; seen[val] = true; int c = (val - 1) % W; if (val > 1 && c - 1 >= 0 && !seen[val - 1]) Add(val - 1); if (c + 1 < W && !seen[val + 1]) Add(val + 1); if (val + W <= N && !seen[val + W]) Add(val + W); if (val >= W && !seen[val - W]) Add(val - W); } return false; } static bool[] Sieve(int n) { bool[] primes = new bool[n + 1]; bool[] composite = new bool[n + 1]; for (int i = 2; i <= n; i++) { if (composite[i]) continue; primes[i] = true; for (int j = i + i; j <= n; j += i) composite[j] = true; } return primes; } class MillerRabin { private static readonly Random random = new Random(); private static readonly int MaxTest = 100; public static bool IsPrime(BigInteger n) { if (n == 2) return true; if (n <= 1 || n % 2 == 0) return false; var d = n - 1; int s = 0; while (d % 2 == 0) { d /= 2; s++; } for (int t = 0; t < MaxTest; t++) { var a = (((long)random.Next() << 32) + random.Next()) % (n - 1) + 1; var x = BigInteger.ModPow(a, d, n); if (x == 1) continue; bool found = false; for (int r = 0; r < s; r++) { if (x == n - 1) { found = true; break; } x = x * x % n; } if (!found) return false; } return true; } } } class Entry { static void Main() { new Program().Solve(); } } class Reader { private static TextReader reader = Console.In; private static readonly char[] separator = { ' ' }; private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries; private static string[] A = new string[0]; private static int i; private static void Init() { A = new string[0]; } public static void Set(TextReader r) { reader = r; Init(); } public static void Set(string file) { reader = new StreamReader(file); Init(); } public static bool HasNext() { return CheckNext(); } public static string String() { return Next(); } public static int Int() { return int.Parse(Next()); } public static long Long() { return long.Parse(Next()); } public static double Double() { return double.Parse(Next()); } public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); } public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); } public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); } public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); } public static string Line() { return reader.ReadLine().Trim(); } private static string[] Split(string s) { return s.Split(separator, op); } private static string Next() { CheckNext(); return A[i++]; } private static bool CheckNext() { if (i < A.Length) return true; string line = reader.ReadLine(); if (line == null) return false; if (line == "") return CheckNext(); A = Split(line); i = 0; return true; } }