結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー iwkjoseciwkjosec
提出日時 2022-06-23 16:46:55
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 472 ms / 9,973 ms
コード長 3,635 bytes
コンパイル時間 8,606 ms
コンパイル使用メモリ 167,608 KB
実行使用メモリ 184,048 KB
最終ジャッジ日時 2024-04-28 09:51:20
合計ジャッジ時間 10,966 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
23,168 KB
testcase_01 AC 34 ms
22,656 KB
testcase_02 AC 35 ms
22,784 KB
testcase_03 AC 36 ms
23,040 KB
testcase_04 AC 259 ms
26,240 KB
testcase_05 AC 269 ms
26,240 KB
testcase_06 AC 130 ms
25,984 KB
testcase_07 AC 131 ms
25,984 KB
testcase_08 AC 133 ms
26,112 KB
testcase_09 AC 472 ms
184,048 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (81 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using static System.Console;

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 = (ulong)FastIO.Long();
            WriteLine(x.ToString() + " " + (MillerRabin(x) ? "1" : "0"));
        }
        Out.Flush();
    }

    public static uint[] b64 = new uint[] { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 };

    public static bool MillerRabin(ulong n)
    {
        if (n == 2 || n == 3 || n == 5 || n == 7) return true;
        if ((n & 1) == 0 || n % 3 == 0 || n % 5 == 0 || n % 7 == 0) return false;
        if (n < 121) return n > 1;

        var d = n - 1;
        var s = System.Numerics.BitOperations.TrailingZeroCount(d);
        d >>= s;

        foreach (var a in b64)
        {
            var k = a % n;
            if (k == 0) return true;

            var x = ModPow(k, d, n);
            if (x == 1) continue;
            for (int r = 0; r < s; r++)
            {
                if (x == n - 1) goto b;
                x = ModMul(x, x, n);
            }
            return false;
        b:;
        }
        return true;
    }

    static ulong ModPow(ulong x, ulong n, ulong mod)
    {
        var res = 1ul;
        while (n != 0)
        {
            if ((n & 1) == 1) res = ModMul(res, x, mod);
            x = ModMul(x, x, mod);
            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 = Math.BigMul(l, r, out var low) % mod;
            if (high == 0ul) return low % mod;

            var lzc = 0;
            if (0 <= (long)mod)
            {
                lzc = System.Numerics.BitOperations.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 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