結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー aajisakaaajisaka
提出日時 2020-04-29 01:52:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,740 bytes
コンパイル時間 2,331 ms
コンパイル使用メモリ 203,640 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 13:54:32
合計ジャッジ時間 4,650 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 327 ms
5,376 KB
testcase_06 AC 168 ms
5,376 KB
testcase_07 AC 165 ms
5,376 KB
testcase_08 AC 167 ms
5,376 KB
testcase_09 AC 555 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
  The implementation of Miller-Rabin Test with C++
  varified with ALDS1_1_C

  http://joisino.hatenablog.com/entry/2017/08/03/210000

  Copyright (c) 2017 joisino
  Released under the MIT license
  http://opensource.org/licenses/mit-license.php
 */

// 64bit support
// Verify: https://yukicoder.me/submissions/472283
struct Miller64 {
    const vector<__uint128_t> v = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
    __uint128_t modpow(__uint128_t x, __uint128_t k, __uint128_t m){
      __uint128_t res = 1;
      while( k ){
        if( k & 1 ){
          res = res * x % m;
        }
        k /= 2;
        x = x * x % m;
      }
      return res;
    }
    // check if n is prime
    bool check(__uint128_t n){
      if( n < 2 ){
        return false;
      }
      __uint128_t d = n - 1;
      __uint128_t s = 0;
      while( d % 2 == 0 ){
        d /= 2;
        s++;
      }
      for(__uint128_t a : v ){
        if( a == n ){
          return true;
        }
        if( modpow( a , d , n ) != 1 ){
          bool ok = true;
          for(__uint128_t r = 0; r < s; r++ ){
            if( modpow( a, d * (1LL << r), n ) == n-1 ){
              ok = false;
              break;
            }
          }
          if( ok ){
            return false;
          }
        }
      }
      return true;
    }
};


class TestMillerRabin {
public:
    void solve(istream& cin, ostream& cout) {
      SPEED;
      int n; cin >> n;
      Miller64 miller64;
      while(n--) {
        ll p; cin >> p;
        cout << p << ' ';
        cout << (miller64.check(p) ? 1 : 0) << '\n';
      }

    }
};

signed main() {
  TestMillerRabin solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
0