結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー iwkjoseciwkjosec
提出日時 2021-01-07 14:35:36
言語 C#(csc)
(csc 3.9.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 4,999 bytes
コンパイル時間 1,120 ms
コンパイル使用メモリ 114,596 KB
実行使用メモリ 25,804 KB
最終ジャッジ日時 2024-11-18 19:15:30
合計ジャッジ時間 3,348 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 24 ms
25,660 KB
testcase_02 AC 25 ms
25,804 KB
testcase_03 AC 24 ms
25,664 KB
testcase_04 AC 296 ms
23,600 KB
testcase_05 AC 275 ms
23,612 KB
testcase_06 AC 125 ms
23,500 KB
testcase_07 AC 120 ms
23,496 KB
testcase_08 AC 125 ms
23,596 KB
testcase_09 AC 490 ms
25,548 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.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Math;

class Program
{
    static void Main()
    {
        SetOut(new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        var n = FastIO.Long();
        for (int i = 0; i < n; i++)
        {
            var x = FastIO.Long();
            WriteLine(x.ToString() + " " + (MillerRabin(x) ? "1" : "0"));
        }
        Out.Flush();
    }
    static uint[] l = new uint[] { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 };
    static bool MillerRabin(long n)
    {
        if (n < 2) return false;
        if (n % 2 == 0) return n == 2;
        var d = n - 1;
        var s = 0;
        while (d % 2 == 0)
        {
            s++;
            d >>= 1;
        }
        var dd = (ulong)d;
        var nn = (ulong)n;
        for (int i = 0; i < l.Length && l[i] < n; i++)
        {
            var a = l[i];
            var c = true;
            var x = ModPow(a, dd, nn);
            if (x == 1) c = false;
            for (int r = 0; r < s; r++)
            {
                if (x == nn - 1) c = false;
                x = ModMul(x, x, nn);
            }
            if (c) return false;
        }
        return true;
    }

    static ulong BigMul(ulong a, ulong b, out ulong low)
    {
        uint al = (uint)a;
        uint ah = (uint)(a >> 32);
        uint bl = (uint)b;
        uint bh = (uint)(b >> 32);

        ulong mull = ((ulong)al) * bl;
        ulong t = ((ulong)ah) * bl + (mull >> 32);
        ulong tl = ((ulong)al) * bh + (uint)t;

        low = tl << 32 | (uint)mull;

        return ((ulong)ah) * bh + (t >> 32) + (tl >> 32);
    }

    static ulong ModPow(ulong x, ulong n, ulong m)
    {
        var res = 1ul;
        while (n != 0)
        {
            if ((n & 1) == 1) res = ModMul(res, x, m);
            x = ModMul(x, x, m);
            n >>= 1;
        }
        return res;
    }

    static ulong ModMul(ulong l, ulong r, ulong mod)
    {
        unchecked
        {
            if (mod <= uint.MaxValue) return l % mod * (r % mod) % mod;
            var high = BigMul(l, r, out var low) % mod;
            if (high == 0ul) return low % mod;

            var lzc = 0;
            if (0 <= (long)mod)
            {
                lzc = LeadingZeroCount(mod);
                mod <<= lzc;
                high = (high << lzc) | (low >> (64 - lzc));
                low <<= lzc;
            }

            var mh = mod >> 32;
            var ml = (ulong)(uint)mod;

            var q = high / mh;
            var p = q * ml;
            high -= q * mh;
            high = (high << 32) | (low >> 32);
            if (high < p)
            {
                high += mod;
                if (high >= mod && high < p)
                {
                    high += mod;
                }
            }
            high -= p;

            q = high / mh;
            p = q * ml;
            high -= q * mh;
            high = (high << 32) | (uint)low;
            if (high < p)
            {
                high += mod;
                if (high >= mod && high < p)
                {
                    high += mod;
                }
            }
            return (high - p) >> lzc;
        }
    }

    public static int LeadingZeroCount(uint value)
    {
        if (value == 0)
        {
            return 32;
        }

        return 31 ^ Log2SoftwareFallback(value);
    }

    public static int LeadingZeroCount(ulong value)
    {
        uint hi = (uint)(value >> 32);

        if (hi == 0)
        {
            return 32 + LeadingZeroCount((uint)value);
        }

        return LeadingZeroCount(hi);
    }

    private static int Log2SoftwareFallback(uint value)
    {
        value |= value >> 01;
        value |= value >> 02;
        value |= value >> 04;
        value |= value >> 08;
        value |= value >> 16;

        return Log2DeBruijn[(int)((value * 0x07C4ACDDu) >> 27)];
    }

    private static ReadOnlySpan<byte> Log2DeBruijn => new byte[32]
    {
        00, 09, 01, 10, 13, 21, 02, 29,
        11, 14, 16, 18, 22, 25, 03, 30,
        08, 12, 20, 28, 15, 17, 24, 07,
        19, 27, 23, 06, 26, 05, 04, 31
    };
}

public static class FastIO
{
    static System.IO.Stream str = System.Console.OpenStandardInput();
    const int size = 1024;
    static byte[] buffer = new byte[size];
    static int ptr = size;

    static byte Read()
    {
        if (ptr == size)
        {
            str.Read(buffer, 0, size);
            ptr = 0;
        }
        return buffer[ptr++];
    }

    public static long Long()
    {
        var c = Read();
        while (c < 0x21)
        {
            c = Read();
        }
        var n = false;
        if (c == '-')
        {
            n = true;
            c = Read();
        }
        var ret = 0L;
        while (c > 0x20)
        {
            ret = ret * 10 + c - '0';
            c = Read();
        }
        return n ? -ret : ret;
    }
}
0