luckYrat's library.

This documentation is automatically generated by competitive-verifier/competitive-verifier


:heavy_check_mark: cpp/z_test/yosupo-primality_test.test.cpp

Depends on

Required by

Code

#define PROBLEM "https://judge.yosupo.jp/problem/primality_test"

#include "../../cpp/template/template.cpp"

#include "../../cpp/math/miller-rabin.cpp"

int main(){
  int q;cin>>q;
  for(int i = 0; q > i; i++){
    long long n;cin>>n;
    cout << (MillerRabinCheck(n) ? "Yes" : "No") << endl;
  }
}
#line 1 "cpp/z_test/yosupo-primality_test.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/primality_test"

#line 2 "cpp/template/template.cpp"

//yukicoder@cpp17

#include <iostream>
#include <iomanip>

#include <algorithm>

#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>

#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>

#include <random>

#include <bitset>

#include <complex>

#include <utility>

#include <numeric>

#include <functional>


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


const ll MOD = 998244353;
const ll MODx = 1000000007;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62LL)-1;
const double EPS = (1e-10);


P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};


template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
 
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる

計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/

/* comment outed because can cause bugs
__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
*/
#line 4 "cpp/z_test/yosupo-primality_test.test.cpp"

#line 2 "cpp/math/binary-power-method.cpp"

template <typename T>
T uPow(T z,T n, T mod){
  T ans = 1;
  while(n != 0){
    if(n%2){
      ans*=z;
      if(mod)ans%=mod;
    }
    n >>= 1;
    z*=z;
    if(mod)z%=mod;
  }
  return ans;
}

#line 2 "cpp/math/miller-rabin.cpp"
/*
true: 素数
false: 合成数
*/
template<typename T>
bool MillerRabinCheck(T n){
  if(n == 1)return false;
  if(n%2 == 0){
    return n == 2;
  }
  __int128 d = n-1;
  __int128 s = 0;
  while(d%2 == 0){
    d/=2;
    s++;
  }
  vector<__int128> base = {2,3,5,7,11,13,17,19,23,29,31,37};
  for(int i = 0; base.size() > i; i++){
    if(base[i] >= n)break;
    long long current = (long long)uPow(base[i],d,(__int128)n);
    if(current == 1 || current == n-1)continue;
    bool ok = false;
    for(int j = 0; s > j; j++){
      current = ((__int128)current * (__int128)current) % n;
      if(current == n-1){
        ok = true;
        break;
      }
    }
    if(!ok)return false;
  }
  return true;
}
#line 6 "cpp/z_test/yosupo-primality_test.test.cpp"

int main(){
  int q;cin>>q;
  for(int i = 0; q > i; i++){
    long long n;cin>>n;
    cout << (MillerRabinCheck(n) ? "Yes" : "No") << endl;
  }
}

Test cases

Env Name Status Elapsed Memory
g++ all_prime_00 :heavy_check_mark: AC 886 ms 3 MB
g++ carmichael_00 :heavy_check_mark: AC 9 ms 3 MB
g++ example_00 :heavy_check_mark: AC 6 ms 3 MB
g++ hack_issue996_00 :heavy_check_mark: AC 6 ms 3 MB
g++ less_1000000000_00 :heavy_check_mark: AC 196 ms 3 MB
g++ prod_two_prime_00 :heavy_check_mark: AC 232 ms 3 MB
g++ random_00 :heavy_check_mark: AC 204 ms 3 MB
g++ random_01 :heavy_check_mark: AC 208 ms 3 MB
g++ random_02 :heavy_check_mark: AC 205 ms 3 MB
g++ small_00 :heavy_check_mark: AC 152 ms 3 MB
Back to top page