結果

問題 No.774 tatyamと素数大富豪
ユーザー AreTrashAreTrash
提出日時 2019-01-24 02:45:25
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,873 ms / 2,000 ms
コード長 5,289 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 106,760 KB
実行使用メモリ 54,928 KB
最終ジャッジ日時 2023-10-14 09:35:16
合計ジャッジ時間 10,627 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,873 ms
54,928 KB
testcase_01 AC 102 ms
23,944 KB
testcase_02 AC 95 ms
24,000 KB
testcase_03 AC 117 ms
24,048 KB
testcase_04 AC 103 ms
25,880 KB
testcase_05 AC 103 ms
25,976 KB
testcase_06 AC 73 ms
23,164 KB
testcase_07 AC 76 ms
25,332 KB
testcase_08 AC 74 ms
25,120 KB
testcase_09 AC 162 ms
26,680 KB
testcase_10 AC 103 ms
23,912 KB
testcase_11 AC 114 ms
26,116 KB
testcase_12 AC 917 ms
40,228 KB
testcase_13 AC 985 ms
42,900 KB
testcase_14 AC 108 ms
23,792 KB
testcase_15 AC 125 ms
28,084 KB
testcase_16 AC 109 ms
26,152 KB
testcase_17 AC 105 ms
25,904 KB
testcase_18 AC 113 ms
27,956 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 No774
{
    public class Program
    {
        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            var N = ss.Next(int.Parse);
            var A = ss.Next(String, N);

            var sos = new SortedSet<BigInteger>();
            do
            {
                var n = BigInteger.Parse(string.Join("", A));
                sos.Add(n);
            }
            while (MathX.NextPermutation(A));

            foreach (var n in sos.Reverse())
            {
                if (!MillerRabin.IsPrime(n)) continue;
                sw.WriteLine(n);
                return;
            }

            sw.WriteLine(-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();
        }

        static readonly Func<string, string> String = s => s;
    }

    public static partial class MathX
    {
        public static void Swap<T>(ref T left, ref T right)
        {
            var tmp = left;
            left = right;
            right = tmp;
        }

        //https://stackoverflow.com/questions/11208446/generating-permutations-of-a-set-most-efficiently
        //by Sani Singh Huttunen
        public static bool NextPermutation<T>(T[] items) where T : IComparable<T>
        {
            var x = -1;
            for (var i = items.Length - 2; i >= 0; i--)
            {
                if (items[i].CompareTo(items[i + 1]) >= 0) continue;
                x = i;
                break;
            }

            if (x < 0) return false;

            var y = -1;
            for (var i = items.Length - 1; i >= 0; i--)
            {
                if (items[x].CompareTo(items[i]) >= 0) continue;
                y = i;
                break;
            }

            Swap(ref items[x], ref items[y]);
            for (int i = x + 1, j = items.Length - 1; i < j; i++, j--)
            {
                Swap(ref items[i], ref items[j]);
            }

            return true;
        }
    }

    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 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