cpp-library

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

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: nt/primes.hpp

Depends on

Required by

Verified with

Code

#pragma once
#include "arith/sat.hpp"
#include "prelude.hpp"

vector<bool> sieve(int upto) {
  vector<bool> prime(upto + 1, true);
  prime[0] = prime[1] = false;
  for (int p = 2; p * p <= upto; p++)
    if (prime[p])
      for (int q = p * p; q <= upto; q += p) prime[q] = false;
  return prime;
}

vector<int> primes(int upto) {
  vector<int> ans;
  vector<bool> sieved = sieve(upto);
  for (int n = 2; n <= upto; n++)
    if (sieved[n]) ans.push_back(n);
  return ans;
}

bool is_prime(int n) {
  if (n == 2) return true;
  if (n % 2 == 0) return false;
  for (int d = 3; sat_mul(d, d) <= n; d += 2)
    if (n % d == 0) return false;
  return true;
}
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "arith/sat.hpp"

template <class T, class U>
auto sat_add(T a, U b) {
  using V = common_type_t<T, U>;
  V res;
  return __builtin_add_overflow((V)a, (V)b, &res)
             ? (a < 0 ? numeric_limits<V>::min() : numeric_limits<V>::max())
             : res;
}
template <class T, class U>
auto sat_sub(T a, U b) {
  using V = common_type_t<T, U>;
  V res;
  return __builtin_sub_overflow((V)a, (V)b, &res)
             ? (a < 0 ? numeric_limits<V>::min() : numeric_limits<V>::max())
             : res;
}
template <class T, class U>
auto sat_mul(T a, U b) {
  using V = common_type_t<T, U>;
  V res;
  return __builtin_mul_overflow((V)a, (V)b, &res)
             ? ((a < 0) == (b < 0) ? numeric_limits<V>::max()
                                   : numeric_limits<V>::min())
             : res;
}
#line 4 "nt/primes.hpp"

vector<bool> sieve(int upto) {
  vector<bool> prime(upto + 1, true);
  prime[0] = prime[1] = false;
  for (int p = 2; p * p <= upto; p++)
    if (prime[p])
      for (int q = p * p; q <= upto; q += p) prime[q] = false;
  return prime;
}

vector<int> primes(int upto) {
  vector<int> ans;
  vector<bool> sieved = sieve(upto);
  for (int n = 2; n <= upto; n++)
    if (sieved[n]) ans.push_back(n);
  return ans;
}

bool is_prime(int n) {
  if (n == 2) return true;
  if (n % 2 == 0) return false;
  for (int d = 3; sat_mul(d, d) <= n; d += 2)
    if (n % d == 0) return false;
  return true;
}
Back to top page