結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー akakimidoriakakimidori
提出日時 2019-04-24 02:33:49
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 1,221 bytes
コンパイル時間 443 ms
コンパイル使用メモリ 29,868 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-11 22:12:40
合計ジャッジ時間 11,997 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef uint32_t u32;
typedef uint64_t u64;

u64 mul (u64 a, u64 b, u64 mod) {
  u64 ans = 0;
  while (b > 0) {
    if (b & 1) {
      ans = (ans + a) % mod;
    }
    a = 2 * a % mod;
    b >>= 1;
  }
  return ans;
}

u64 mod_pow (u64 a, u64 n, u64 mod) {
  u64 t = 1;
  while (n > 0) {
    if (n & 1) t = mul (t, a, mod);
    a = mul (a, a, mod);
    n >>= 1;
  }
  return t;
}

u64 xor_shift (void) {
  static u64 x = (u64)88172645463325252;
  x ^= x << 7;
  return x ^= x >> 9;
}

u32 is_prime_miller (const u64 p, u32 iter) {
  if (p <= 1) return 0;
  if (p <= 3) return 1;
  if (p % 2 == 0) return 0;
  u64 d = p - 1;
  u32 s = 0;
  while (d % 2 == 0) {
    d /= 2;
    s++;
  }
  while (iter--) {
    u64 a = xor_shift() % (p - 1) + 1;
    a = mod_pow (a, d, p);
    if (a == 1) continue;
    u32 i = 0;
    for (; i < s && a != p - 1; ++i, a = mul (a, a, p));    
    if (i >= s) return 0;
  }
  return 1;
}

void run (void) {
  u32 n;
  scanf ("%" SCNu32, &n);
  while (n--) {
    u64 x;
    scanf ("%" SCNu64, &x);
    printf ("%" PRIu64 " %" PRIu32 "\n", x, is_prime_miller (x, 40));
  }
}

int main (void) {
  run();
  return 0;
}
0