結果
問題 | No.308 素数は通れません |
ユーザー |
![]() |
提出日時 | 2015-12-01 13:28:06 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 38 ms / 1,000 ms |
コード長 | 4,308 bytes |
コンパイル時間 | 1,265 ms |
コンパイル使用メモリ | 110,080 KB |
実行使用メモリ | 19,328 KB |
最終ジャッジ日時 | 2024-11-27 18:12:14 |
合計ジャッジ時間 | 5,543 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 107 |
コンパイルメッセージ
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);}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(0);private static readonly int MaxTest = 15;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 = (ulong)(((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;}}