結果

問題 No.308 素数は通れません
ユーザー eitahoeitaho
提出日時 2015-12-01 13:28:06
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 74 ms / 1,000 ms
コード長 4,308 bytes
コンパイル時間 2,436 ms
コンパイル使用メモリ 108,072 KB
実行使用メモリ 25,112 KB
最終ジャッジ日時 2023-08-18 13:02:41
合計ジャッジ時間 12,717 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
21,060 KB
testcase_01 AC 63 ms
20,804 KB
testcase_02 AC 63 ms
20,912 KB
testcase_03 AC 62 ms
20,928 KB
testcase_04 AC 62 ms
20,956 KB
testcase_05 AC 63 ms
20,948 KB
testcase_06 AC 63 ms
22,872 KB
testcase_07 AC 63 ms
22,960 KB
testcase_08 AC 63 ms
22,940 KB
testcase_09 AC 62 ms
21,000 KB
testcase_10 AC 63 ms
20,904 KB
testcase_11 AC 63 ms
22,856 KB
testcase_12 AC 64 ms
22,956 KB
testcase_13 AC 63 ms
20,876 KB
testcase_14 AC 64 ms
22,864 KB
testcase_15 AC 63 ms
22,876 KB
testcase_16 AC 63 ms
22,932 KB
testcase_17 AC 64 ms
22,940 KB
testcase_18 AC 63 ms
20,828 KB
testcase_19 AC 61 ms
20,768 KB
testcase_20 AC 63 ms
22,820 KB
testcase_21 AC 63 ms
20,912 KB
testcase_22 AC 63 ms
20,992 KB
testcase_23 AC 63 ms
20,992 KB
testcase_24 AC 63 ms
20,852 KB
testcase_25 AC 64 ms
22,984 KB
testcase_26 AC 62 ms
20,956 KB
testcase_27 AC 66 ms
22,964 KB
testcase_28 AC 62 ms
18,908 KB
testcase_29 AC 64 ms
20,808 KB
testcase_30 AC 61 ms
18,728 KB
testcase_31 AC 62 ms
20,888 KB
testcase_32 AC 63 ms
22,900 KB
testcase_33 AC 65 ms
22,852 KB
testcase_34 AC 64 ms
24,884 KB
testcase_35 AC 63 ms
18,792 KB
testcase_36 AC 64 ms
20,904 KB
testcase_37 AC 63 ms
20,888 KB
testcase_38 AC 63 ms
22,800 KB
testcase_39 AC 63 ms
22,912 KB
testcase_40 AC 62 ms
20,916 KB
testcase_41 AC 63 ms
20,872 KB
testcase_42 AC 63 ms
22,948 KB
testcase_43 AC 62 ms
20,836 KB
testcase_44 AC 62 ms
20,792 KB
testcase_45 AC 62 ms
20,932 KB
testcase_46 AC 61 ms
20,932 KB
testcase_47 AC 62 ms
20,836 KB
testcase_48 AC 61 ms
21,008 KB
testcase_49 AC 62 ms
22,888 KB
testcase_50 AC 62 ms
20,960 KB
testcase_51 AC 63 ms
22,908 KB
testcase_52 AC 63 ms
20,920 KB
testcase_53 AC 62 ms
20,864 KB
testcase_54 AC 61 ms
20,808 KB
testcase_55 AC 62 ms
22,992 KB
testcase_56 AC 68 ms
22,848 KB
testcase_57 AC 62 ms
22,948 KB
testcase_58 AC 64 ms
20,792 KB
testcase_59 AC 63 ms
20,904 KB
testcase_60 AC 63 ms
20,860 KB
testcase_61 AC 64 ms
22,900 KB
testcase_62 AC 64 ms
22,964 KB
testcase_63 AC 73 ms
22,924 KB
testcase_64 AC 63 ms
20,936 KB
testcase_65 AC 64 ms
20,996 KB
testcase_66 AC 63 ms
20,832 KB
testcase_67 AC 64 ms
20,872 KB
testcase_68 AC 65 ms
20,960 KB
testcase_69 AC 65 ms
22,952 KB
testcase_70 AC 64 ms
22,912 KB
testcase_71 AC 63 ms
20,868 KB
testcase_72 AC 64 ms
22,872 KB
testcase_73 AC 64 ms
20,908 KB
testcase_74 AC 64 ms
20,940 KB
testcase_75 AC 62 ms
20,976 KB
testcase_76 AC 67 ms
20,992 KB
testcase_77 AC 62 ms
20,780 KB
testcase_78 AC 61 ms
20,940 KB
testcase_79 AC 61 ms
20,824 KB
testcase_80 AC 68 ms
22,936 KB
testcase_81 AC 70 ms
21,008 KB
testcase_82 AC 63 ms
18,772 KB
testcase_83 AC 62 ms
20,860 KB
testcase_84 AC 74 ms
25,112 KB
testcase_85 AC 64 ms
22,984 KB
testcase_86 AC 73 ms
25,104 KB
testcase_87 AC 73 ms
21,092 KB
testcase_88 AC 64 ms
20,844 KB
testcase_89 AC 64 ms
22,924 KB
testcase_90 AC 72 ms
22,908 KB
testcase_91 AC 64 ms
22,876 KB
testcase_92 AC 63 ms
20,804 KB
testcase_93 AC 63 ms
20,952 KB
testcase_94 AC 63 ms
20,788 KB
testcase_95 AC 63 ms
20,992 KB
testcase_96 AC 72 ms
25,012 KB
testcase_97 AC 64 ms
20,896 KB
testcase_98 AC 63 ms
20,824 KB
testcase_99 AC 73 ms
22,992 KB
testcase_100 AC 72 ms
25,008 KB
testcase_101 AC 72 ms
22,916 KB
testcase_102 AC 64 ms
22,848 KB
testcase_103 AC 64 ms
23,076 KB
testcase_104 AC 64 ms
22,856 KB
testcase_105 AC 63 ms
20,976 KB
testcase_106 AC 71 ms
21,160 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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;
    }
}
0