結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー iwkjoseciwkjosec
提出日時 2019-10-11 00:24:41
言語 C#(csc)
(csc 3.9.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,132 bytes
コンパイル時間 985 ms
コンパイル使用メモリ 106,880 KB
実行使用メモリ 21,888 KB
最終ジャッジ日時 2024-04-29 13:31:53
合計ジャッジ時間 6,309 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 27 ms
17,792 KB
testcase_02 AC 26 ms
17,792 KB
testcase_03 AC 29 ms
17,536 KB
testcase_04 AC 910 ms
21,760 KB
testcase_05 AC 884 ms
21,760 KB
testcase_06 AC 364 ms
21,760 KB
testcase_07 AC 355 ms
21,632 KB
testcase_08 AC 356 ms
21,760 KB
testcase_09 AC 1,621 ms
21,888 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 System.Numerics;
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();
    }

    public static int[] b64 = new int[] { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 };// n < at least 2^64
    public static bool MillerRabin(long n)
    {
        if (n < 2) return false;
        if ((n & 1) == 0) return n == 2;
        var d = n - 1;
        var s = 0;
        while ((d & 1) == 0)
        {
            s++;
            d >>= 1;
        }
        for (int i = 0; i < b64.Length && b64[i] < n; i++)
        {
            var c = true;//このへんメソッド抽出すべきか したらアーリーリターンできるか
            var x = BigInteger.ModPow(b64[i], d, n);
            if (x == 1) c = false;
            for (int r = 0; r < s; r++)
            {
                if (x == n - 1) c = false;
                x = x * x % n;
            }
            if (c) return false;
        }
        return true;
    }
}

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