結果

問題 No.577 Prime Powerful Numbers
ユーザー AreTrashAreTrash
提出日時 2019-02-07 10:47:26
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 8,259 bytes
コンパイル時間 3,134 ms
コンパイル使用メモリ 120,528 KB
実行使用メモリ 46,332 KB
最終ジャッジ日時 2024-06-24 07:45:58
合計ジャッジ時間 9,606 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 No577
{
    //ref: http://kmjp.hatenablog.jp/entry/2017/10/20/0930 by kmjpさん
    public class Program
    {
        static readonly IReadOnlyList<BigInteger> Primes = new Prime((int)1e6).List.Select(x => (BigInteger)x).ToArray();

        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            var Q = ss.Next(int.Parse);

            for (var i = 0; i < Q; i++)
            {
                var N = ss.Next(long.Parse);
                sw.WriteLine(SolveQuery(N));
            }
            //---------------------------------
        }

        string SolveQuery(long n)
        {
            for (var i = 2; i < n; i <<= 1)
            {
                if (IsPowerOfPrime(n - i)) return "Yes";
            }
            return "No";
        }

        bool IsPowerOfPrime(long x)
        {
            if (MillerRabin.IsPrime(x)) return true;

            var sqrt = (long)Math.Sqrt(x);
            if (sqrt * sqrt == x && MillerRabin.IsPrime(x)) return true;

            for (var a = 3; a < 64; a++)
            {
                var bs = Primes.ToBinarySearch().SetFunc(i => BigInteger.Pow(Primes[(int)i], a));
                if (bs.Contains(x)) return true; 
            }

            return false;
        }

        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 partial class BinarySearch
    {
        public static BinarySearch<T> Default<T>(Func<long, T> func, long left, long right) where T : IComparable<T>
        {
            return new BinarySearch<T>().SetFunc(func).SetInterval(left, right);
        }

        public static BinarySearch<T> ToBinarySearch<T>(this IReadOnlyList<T> source) where T : IComparable<T>
        {
            return new BinarySearch<T>().SetFunc(x => source[(int)x]).SetInterval(0, source.Count);
        }
    }

    public class BinarySearch<T> where T : IComparable<T>
    {
        Func<long, T> Func { get; set; } = null;
        long Left { get; set; } = 0;
        long Right { get; set; } = 0;
        bool IsOrderAscending { get; set; } = true;

        public BinarySearch<T> SetFunc(Func<long, T> func)
        {
            Func = func;
            return this;
        }

        public BinarySearch<T> SetInterval(long left, long right)
        {
            if (left > right) throw new ArgumentException($"{nameof(left)} <= {nameof(right)}");
            Left = left;
            Right = right;
            return this;
        }

        public BinarySearch<T> SetOrder(bool isOrderAscending)
        {
            IsOrderAscending = isOrderAscending;
            return this;
        }

        long BoundToTheRight(Func<long, bool> pred, long left, long right)
        {
            while (true)
            {
                if (left == right) return right;
                var mid = (left + right - 1) >> 1;
                if (pred(mid)) right = mid;
                else left = mid + 1;
            }
        }

        public long LowerBound(T value)
        {
            var sign = IsOrderAscending ? 1 : -1;
            return BoundToTheRight(x => sign * Func(x).CompareTo(value) >= 0, Left, Right);
        }

        public long UpperBound(T value)
        {
            var sign = IsOrderAscending ? 1 : -1;
            return BoundToTheRight(x => sign * Func(x).CompareTo(value) > 0, Left, Right);
        }

        public long Range(T vLeft, T vRight)
        {
            return Math.Max(0, UpperBound(vRight) - LowerBound(vLeft));
        }

        public long Count(T value)
        {
            return Range(value, value);
        }

        public bool Contains(T value)
        {
            var lb = LowerBound(value);
            return lb != Right && Func(lb).CompareTo(value) == 0;
        }
    }

    public class Prime
    {
        const int CoverIntRangeMax = 46349;
        readonly List<int> primes;
        public IReadOnlyList<int> List { get { return primes.AsReadOnly(); } }

        public Prime(int max = CoverIntRangeMax)
        {
            primes = new List<int> {2};
            var sieve = new bool[max + 1];
            for (var i = 3; i <= max; i += 2)
            {
                if (sieve[i]) continue;
                primes.Add(i);
                for (var j = i * 3; j <= max; j += i << 1) sieve[j] = true;
            }
        }

        public bool Is(long n)
        {
            return n > 1 && List.TakeWhile(p => p * p <= n).All(p => n % p != 0);
        }

        public IEnumerable<long> Factorize(long n)
        {
            foreach (var p in List.TakeWhile(p => p * p <= n))
            {
                while (n % p == 0)
                {
                    n /= p;
                    yield return p;
                }
            }
            if (n != 1) yield return n;
        }

        public long EulersTotient(long n)
        {
            foreach (var x in Factorize(n).Distinct()) n = n / x * (x - 1);
            return n;
        }
    }

    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