結果

問題 No.308 素数は通れません
ユーザー AreTrashAreTrash
提出日時 2019-01-24 00:55:06
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 74 ms / 1,000 ms
コード長 5,311 bytes
コンパイル時間 4,406 ms
コンパイル使用メモリ 113,076 KB
実行使用メモリ 23,624 KB
最終ジャッジ日時 2023-10-14 09:33:56
合計ジャッジ時間 14,185 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
21,412 KB
testcase_01 AC 66 ms
21,400 KB
testcase_02 AC 66 ms
21,588 KB
testcase_03 AC 66 ms
21,560 KB
testcase_04 AC 67 ms
21,700 KB
testcase_05 AC 66 ms
19,464 KB
testcase_06 AC 67 ms
21,592 KB
testcase_07 AC 67 ms
21,428 KB
testcase_08 AC 66 ms
21,552 KB
testcase_09 AC 66 ms
21,528 KB
testcase_10 AC 67 ms
21,436 KB
testcase_11 AC 66 ms
21,480 KB
testcase_12 AC 67 ms
21,688 KB
testcase_13 AC 67 ms
19,468 KB
testcase_14 AC 66 ms
19,564 KB
testcase_15 AC 66 ms
21,460 KB
testcase_16 AC 66 ms
21,500 KB
testcase_17 AC 66 ms
23,484 KB
testcase_18 AC 67 ms
21,528 KB
testcase_19 AC 67 ms
21,516 KB
testcase_20 AC 67 ms
21,592 KB
testcase_21 AC 67 ms
23,580 KB
testcase_22 AC 67 ms
21,540 KB
testcase_23 AC 68 ms
21,556 KB
testcase_24 AC 67 ms
21,436 KB
testcase_25 AC 67 ms
23,464 KB
testcase_26 AC 66 ms
21,704 KB
testcase_27 AC 68 ms
23,552 KB
testcase_28 AC 71 ms
23,600 KB
testcase_29 AC 70 ms
21,520 KB
testcase_30 AC 71 ms
23,624 KB
testcase_31 AC 70 ms
21,752 KB
testcase_32 AC 70 ms
21,508 KB
testcase_33 AC 71 ms
21,696 KB
testcase_34 AC 70 ms
19,544 KB
testcase_35 AC 70 ms
21,404 KB
testcase_36 AC 61 ms
18,784 KB
testcase_37 AC 62 ms
20,720 KB
testcase_38 AC 62 ms
20,748 KB
testcase_39 AC 62 ms
22,800 KB
testcase_40 AC 61 ms
20,828 KB
testcase_41 AC 62 ms
18,824 KB
testcase_42 AC 63 ms
20,848 KB
testcase_43 AC 62 ms
18,684 KB
testcase_44 AC 62 ms
22,908 KB
testcase_45 AC 62 ms
22,860 KB
testcase_46 AC 63 ms
20,816 KB
testcase_47 AC 63 ms
20,712 KB
testcase_48 AC 61 ms
20,832 KB
testcase_49 AC 61 ms
20,872 KB
testcase_50 AC 62 ms
20,996 KB
testcase_51 AC 61 ms
20,992 KB
testcase_52 AC 62 ms
20,888 KB
testcase_53 AC 62 ms
22,860 KB
testcase_54 AC 62 ms
20,940 KB
testcase_55 AC 62 ms
20,876 KB
testcase_56 AC 67 ms
21,452 KB
testcase_57 AC 62 ms
20,812 KB
testcase_58 AC 62 ms
22,848 KB
testcase_59 AC 61 ms
20,824 KB
testcase_60 AC 62 ms
18,992 KB
testcase_61 AC 63 ms
22,856 KB
testcase_62 AC 63 ms
20,768 KB
testcase_63 AC 66 ms
19,360 KB
testcase_64 AC 64 ms
22,744 KB
testcase_65 AC 64 ms
22,852 KB
testcase_66 AC 64 ms
20,820 KB
testcase_67 AC 64 ms
20,696 KB
testcase_68 AC 64 ms
20,812 KB
testcase_69 AC 62 ms
20,956 KB
testcase_70 AC 64 ms
22,852 KB
testcase_71 AC 63 ms
22,796 KB
testcase_72 AC 63 ms
20,804 KB
testcase_73 AC 63 ms
20,956 KB
testcase_74 AC 63 ms
20,864 KB
testcase_75 AC 64 ms
20,996 KB
testcase_76 AC 70 ms
21,432 KB
testcase_77 AC 62 ms
20,868 KB
testcase_78 AC 62 ms
20,928 KB
testcase_79 AC 62 ms
20,872 KB
testcase_80 AC 69 ms
23,552 KB
testcase_81 AC 72 ms
21,600 KB
testcase_82 AC 62 ms
20,808 KB
testcase_83 AC 62 ms
22,860 KB
testcase_84 AC 73 ms
21,588 KB
testcase_85 AC 62 ms
18,704 KB
testcase_86 AC 74 ms
21,592 KB
testcase_87 AC 73 ms
21,544 KB
testcase_88 AC 62 ms
20,768 KB
testcase_89 AC 62 ms
20,688 KB
testcase_90 AC 72 ms
21,772 KB
testcase_91 AC 63 ms
22,876 KB
testcase_92 AC 62 ms
18,780 KB
testcase_93 AC 63 ms
20,816 KB
testcase_94 AC 63 ms
20,912 KB
testcase_95 AC 63 ms
22,892 KB
testcase_96 AC 68 ms
21,500 KB
testcase_97 AC 63 ms
20,848 KB
testcase_98 AC 62 ms
20,852 KB
testcase_99 AC 72 ms
19,500 KB
testcase_100 AC 72 ms
21,704 KB
testcase_101 AC 73 ms
23,564 KB
testcase_102 AC 63 ms
22,816 KB
testcase_103 AC 63 ms
18,940 KB
testcase_104 AC 63 ms
20,872 KB
testcase_105 AC 63 ms
22,920 KB
testcase_106 AC 67 ms
21,432 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.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace No308
{
    public class Program
    {
        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            var N = ss.Next(BigInteger.Parse);

            if (N < 100)
            {
                sw.WriteLine(BruteForce((int)N));
                return;
            }

            if (N % 8 != 1)
            {
                sw.WriteLine(8);
                return;
            }

            sw.WriteLine(MillerRabin.IsPrime(N - 8) ? 14 : 8);
            //---------------------------------
        }

        int BruteForce(int n)
        {
            for (var w = 1; w <= n; w++)
            {
                var uf = new UnionFind(n + 1);
                for (var i = 1; i <= n; i++)
                {
                    if (MillerRabin.IsPrime(i)) continue;
                    if (i + 1 <= n && i % w != 0 && !MillerRabin.IsPrime(i + 1)) uf.Unite(i, i + 1);
                    if (i + w <= n && !MillerRabin.IsPrime(i + w)) uf.Unite(i, i + w);
                }
                if (uf.IsSameSet(1, n)) return w;
            }
            return -1;
        }

        static void Main()
        {
            var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput()));
            var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
            new Program().Solve(ss, sw);
            sw.Flush();
        }
    }

    public static class MillerRabin
    {
        const int RandomTestCount = 30;

        static readonly BigInteger[] BasePrimes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,};
        static readonly BigInteger[] WitnessRanges =
        {
            2047,
            1373653,
            25326001,
            118670087467, //3215031751が例外
            2152302898747,
            3474749660383,
            341550071728321,
            341550071728321,
            3825123056546413051,
            3825123056546413051,
            3825123056546413051,
            BigInteger.Parse("318665857834031151167461"),
            BigInteger.Parse("3317044064679887385961981"),
        };

        public static bool IsPrime(BigInteger n)
        {
            if (n < 2 || n == 3215031751) return false;
            if (n < 42) return BasePrimes.Contains(n);
            if (BasePrimes.Any(p => n % p == 0)) return false;

            var d = n - 1;
            while ((d & 1) == 0) d >>= 1;

            for (var i = 0; i < BasePrimes.Length; i++)
            {
                if (!MillerRabinTest(n, d, BasePrimes[i])) return false;
                if (n < WitnessRanges[i]) return true;
            }

            //n > 3.3e24
            var random = new Random();
            for (var i = 0; i < RandomTestCount; i++)
            {
                if (!MillerRabinTest(n, d, random.Next(1, int.MaxValue))) return false;
            }
            return true;
        }

        static bool MillerRabinTest(BigInteger n, BigInteger d, BigInteger a)
        {
            a = BigInteger.ModPow(a, d, n);
            if (a == 1 || a == n - 1) return true;

            while ((d <<= 1) < n - 1)
            {
                a = a * a % n;
                if (a == n - 1) return true;
            }

            return false;
        }
    }

    public class UnionFind
    {
        readonly int[] p;

        public UnionFind(int n)
        {
            p = new int[n];
            for (var i = 0; i < n; i++) p[i] = -1;
        }

        public int Root(int x)
        {
            return p[x] < 0 ? x : (p[x] = Root(p[x]));
        }

        public bool IsSameSet(int x, int y)
        {
            return Root(x) == Root(y);
        }

        public bool Unite(int x, int y)
        {
            x = Root(x);
            y = Root(y);
            if (x == y) return false;

            if (p[x] > p[y])
            {
                var t = x;
                x = y;
                y = t;
            }

            p[x] += p[y];
            p[y] = x;
            return true;
        }

        public int Size(int x)
        {
            return -p[Root(x)];
        }
    }

    public class StreamScanner
    {
        static readonly char[] Sep = {' '};
        readonly Queue<string> buffer = new Queue<string>();
        readonly TextReader textReader;

        public StreamScanner(TextReader textReader)
        {
            this.textReader = textReader;
        }

        public T Next<T>(Func<string, T> parser)
        {
            if (buffer.Count != 0) return parser(buffer.Dequeue());
            var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries);
            foreach (var nextString in nextStrings) buffer.Enqueue(nextString);
            return Next(parser);
        }

        public T[] Next<T>(Func<string, T> parser, int x)
        {
            var ret = new T[x];
            for (var i = 0; i < x; ++i) ret[i] = Next(parser);
            return ret;
        }

        public T[][] Next<T>(Func<string, T> parser, int x, int y)
        {
            var ret = new T[y][];
            for (var i = 0; i < y; ++i) ret[i] = Next(parser, x);
            return ret;
        }
    }
}
0