結果

問題 No.308 素数は通れません
ユーザー eitahoeitaho
提出日時 2015-12-01 13:39:46
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 38 ms / 1,000 ms
コード長 4,329 bytes
コンパイル時間 1,088 ms
コンパイル使用メモリ 118,256 KB
実行使用メモリ 27,384 KB
最終ジャッジ日時 2024-11-27 18:15:59
合計ジャッジ時間 6,722 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
26,740 KB
testcase_01 AC 30 ms
26,484 KB
testcase_02 AC 29 ms
24,188 KB
testcase_03 AC 29 ms
26,488 KB
testcase_04 AC 29 ms
24,640 KB
testcase_05 AC 28 ms
24,448 KB
testcase_06 AC 28 ms
22,452 KB
testcase_07 AC 28 ms
24,512 KB
testcase_08 AC 29 ms
24,624 KB
testcase_09 AC 28 ms
24,312 KB
testcase_10 AC 28 ms
24,316 KB
testcase_11 AC 29 ms
26,488 KB
testcase_12 AC 31 ms
26,428 KB
testcase_13 AC 30 ms
24,512 KB
testcase_14 AC 29 ms
24,448 KB
testcase_15 AC 28 ms
24,440 KB
testcase_16 AC 30 ms
24,828 KB
testcase_17 AC 30 ms
24,448 KB
testcase_18 AC 30 ms
24,444 KB
testcase_19 AC 30 ms
24,640 KB
testcase_20 AC 29 ms
26,488 KB
testcase_21 AC 30 ms
26,560 KB
testcase_22 AC 28 ms
24,648 KB
testcase_23 AC 28 ms
24,576 KB
testcase_24 AC 29 ms
24,704 KB
testcase_25 AC 29 ms
24,380 KB
testcase_26 AC 31 ms
26,420 KB
testcase_27 AC 29 ms
24,572 KB
testcase_28 AC 30 ms
26,428 KB
testcase_29 AC 30 ms
26,484 KB
testcase_30 AC 30 ms
26,356 KB
testcase_31 AC 29 ms
24,572 KB
testcase_32 AC 30 ms
24,500 KB
testcase_33 AC 31 ms
26,488 KB
testcase_34 AC 31 ms
24,452 KB
testcase_35 AC 31 ms
26,684 KB
testcase_36 AC 28 ms
24,572 KB
testcase_37 AC 27 ms
24,756 KB
testcase_38 AC 27 ms
24,344 KB
testcase_39 AC 27 ms
24,572 KB
testcase_40 AC 27 ms
24,492 KB
testcase_41 AC 29 ms
24,504 KB
testcase_42 AC 27 ms
24,448 KB
testcase_43 AC 28 ms
24,320 KB
testcase_44 AC 28 ms
24,376 KB
testcase_45 AC 28 ms
26,556 KB
testcase_46 AC 29 ms
24,516 KB
testcase_47 AC 29 ms
26,556 KB
testcase_48 AC 28 ms
24,444 KB
testcase_49 AC 29 ms
22,576 KB
testcase_50 AC 27 ms
24,448 KB
testcase_51 AC 28 ms
24,576 KB
testcase_52 AC 28 ms
26,676 KB
testcase_53 AC 30 ms
24,320 KB
testcase_54 AC 29 ms
24,640 KB
testcase_55 AC 28 ms
26,488 KB
testcase_56 AC 34 ms
25,344 KB
testcase_57 AC 31 ms
24,320 KB
testcase_58 AC 28 ms
24,576 KB
testcase_59 AC 29 ms
24,568 KB
testcase_60 AC 29 ms
26,424 KB
testcase_61 AC 28 ms
26,684 KB
testcase_62 AC 28 ms
26,612 KB
testcase_63 AC 34 ms
25,516 KB
testcase_64 AC 28 ms
24,620 KB
testcase_65 AC 28 ms
24,572 KB
testcase_66 AC 28 ms
26,356 KB
testcase_67 AC 28 ms
24,576 KB
testcase_68 AC 28 ms
26,560 KB
testcase_69 AC 29 ms
24,900 KB
testcase_70 AC 30 ms
24,628 KB
testcase_71 AC 29 ms
24,452 KB
testcase_72 AC 28 ms
24,324 KB
testcase_73 AC 29 ms
26,556 KB
testcase_74 AC 29 ms
24,392 KB
testcase_75 AC 31 ms
24,628 KB
testcase_76 AC 32 ms
25,340 KB
testcase_77 AC 29 ms
22,324 KB
testcase_78 AC 29 ms
22,456 KB
testcase_79 AC 28 ms
24,384 KB
testcase_80 AC 33 ms
25,152 KB
testcase_81 AC 35 ms
25,216 KB
testcase_82 AC 29 ms
22,452 KB
testcase_83 AC 28 ms
26,868 KB
testcase_84 AC 34 ms
25,340 KB
testcase_85 AC 28 ms
26,552 KB
testcase_86 AC 37 ms
25,288 KB
testcase_87 AC 34 ms
25,288 KB
testcase_88 AC 28 ms
24,324 KB
testcase_89 AC 28 ms
24,700 KB
testcase_90 AC 37 ms
25,412 KB
testcase_91 AC 30 ms
24,380 KB
testcase_92 AC 34 ms
26,676 KB
testcase_93 AC 31 ms
24,368 KB
testcase_94 AC 31 ms
24,448 KB
testcase_95 AC 32 ms
24,528 KB
testcase_96 AC 37 ms
25,088 KB
testcase_97 AC 29 ms
24,448 KB
testcase_98 AC 29 ms
24,448 KB
testcase_99 AC 33 ms
25,396 KB
testcase_100 AC 34 ms
25,340 KB
testcase_101 AC 38 ms
25,392 KB
testcase_102 AC 30 ms
24,516 KB
testcase_103 AC 30 ms
26,484 KB
testcase_104 AC 30 ms
24,576 KB
testcase_105 AC 35 ms
26,560 KB
testcase_106 AC 36 ms
27,384 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);
        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;
    }
}
0