結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー iwkjoseciwkjosec
提出日時 2019-05-13 22:21:47
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 2,551 ms / 9,973 ms
コード長 1,126 bytes
コンパイル時間 1,196 ms
コンパイル使用メモリ 106,368 KB
実行使用メモリ 22,016 KB
最終ジャッジ日時 2024-04-28 09:22:28
合計ジャッジ時間 8,075 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
17,920 KB
testcase_01 AC 22 ms
18,048 KB
testcase_02 AC 22 ms
17,920 KB
testcase_03 AC 24 ms
17,792 KB
testcase_04 AC 1,348 ms
22,016 KB
testcase_05 AC 1,259 ms
21,888 KB
testcase_06 AC 368 ms
21,888 KB
testcase_07 AC 356 ms
22,016 KB
testcase_08 AC 359 ms
21,888 KB
testcase_09 AC 2,551 ms
22,016 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()
    {
        var n = long.Parse(ReadLine());

        for (int i = 0; i < n; i++)
        {
            var x = long.Parse(ReadLine());
            WriteLine(x.ToString() + " " + (MillerRabin(x) ? "1" : "0"));
        }
    }
    static int[] l = new int[] {2,3,5,7,11,13,17,19,23,29,31,37};
    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;
        }

        for (int i = 0; i < l.Length && l[i] < n; i++)
        {
            var a = l[i];
            var c = true;
            var x = System.Numerics.BigInteger.ModPow(a, 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;
    }
}
0