CPLibrary

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

View the Project on GitHub o06660o/CPLibrary

:heavy_check_mark: test/math/millerrabin.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/primality_test
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;

#include "../../src/math/millerrabin.hpp"

void solve() {
  ll n;
  cin >> n;
  cout << (isprime(n) ? "Yes\n" : "No\n");
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int tt = 1;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}
#line 1 "test/math/millerrabin.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/primality_test
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;

#line 1 "src/math/millerrabin.hpp"
// 时间复杂度: $\mathcal{O}(k \log n)$,目前 $k = 9$ 足以判定 $64$ 位整数.
#line 1 "src/math/pow_mod_ll.hpp"
ll pow_mod_ll(ll a, ull n, ll p) {
  ll ret = 1;
  for (; n; n /= 2) {
    if (n & 1) ret = i128(ret) * a % p;
    a = i128(a) * a % p;
  }
  return ret;
}
#line 3 "src/math/millerrabin.hpp"
bool isprime(ll n) {
  if (n < 2) return 0;
  int s = __builtin_ctzll(n - 1);
  ll d = (n - 1) >> s;
  for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23}) {
    if (a == n) return 1;
    ll x = pow_mod_ll(a, d, n);
    if (x == 1 || x == n - 1) continue;
    bool ok = 0;
    for (int i = 0; i + 1 < s; i++) {
      x = i128(x) * x % n;
      if (x == n - 1) {
        ok = 1;
        break;
      }
    }
    if (!ok) return 0;
  }
  return 1;
}
#line 9 "test/math/millerrabin.test.cpp"

void solve() {
  ll n;
  cin >> n;
  cout << (isprime(n) ? "Yes\n" : "No\n");
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int tt = 1;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ all_prime_00 :heavy_check_mark: AC 468 ms 4 MB
g++ carmichael_00 :heavy_check_mark: AC 5 ms 3 MB
g++ example_00 :heavy_check_mark: AC 4 ms 4 MB
g++ hack_issue1325_00 :heavy_check_mark: AC 4 ms 4 MB
g++ hack_issue996_00 :heavy_check_mark: AC 4 ms 4 MB
g++ less_1000000000_00 :heavy_check_mark: AC 45 ms 4 MB
g++ prod_two_prime_00 :heavy_check_mark: AC 65 ms 4 MB
g++ pseudoprimes_00 :heavy_check_mark: AC 5 ms 4 MB
g++ random_00 :heavy_check_mark: AC 77 ms 4 MB
g++ random_01 :heavy_check_mark: AC 77 ms 4 MB
g++ random_02 :heavy_check_mark: AC 78 ms 4 MB
g++ small_00 :heavy_check_mark: AC 31 ms 4 MB
Back to top page