Asa's CP Library

a01sa01to's competitive programming library.

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub a01sa01to/cp-library

:heavy_check_mark: tests/math/meissel-lehmer/libchecker.test.cpp

Depends on

Code

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

#include "../../../cpp/library/math/meissel-lehmer.hpp"
#define PROBLEM "https://judge.yosupo.jp/problem/counting_primes"

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  ll n;
  cin >> n;
  cout << asalib::math::meissel_lehmer(n) << endl;
  return 0;
}
#line 1 "tests/math/meissel-lehmer/libchecker.test.cpp"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

#line 2 "library/math/meissel-lehmer.hpp"

#line 4 "library/math/meissel-lehmer.hpp"
#include <concepts>
#line 7 "library/math/meissel-lehmer.hpp"
using namespace std;

#line 2 "library/data-structure/bit.hpp"

#line 4 "library/data-structure/bit.hpp"
using namespace std;

#line 2 "library/_internal/types.hpp"

#line 4 "library/_internal/types.hpp"
#include <type_traits>
using namespace std;

#line 2 "library/_internal/modint-base.hpp"

#line 5 "library/_internal/modint-base.hpp"
using namespace std;

namespace asalib {
  namespace _internal {
    class modint_base {};

    template<typename T>
    concept is_modint = is_base_of_v<modint_base, T>;
  }  // namespace _internal
}  // namespace asalib
#line 8 "library/_internal/types.hpp"

namespace asalib {
  namespace _internal {
    // ---------- concept definition ---------- //
    template<class T>
    concept integral_like = integral<T> || is_modint<T>;

    template<class T>
    concept floating_like = floating_point<T>;

    template<class T>
    concept numeric_like = integral_like<T> || floating_like<T>;

    // ---------- function definition ---------- //
    template<class T>
    T plus(T a, T b) { return a + b; }

    template<class T>
    T minus(T a, T b) { return a - b; }

    // ---------- constant definition ---------- //
    template<class T>
    T zero() { return 0; }
  }  // namespace _internal
}  // namespace asalib
#line 7 "library/data-structure/bit.hpp"

namespace asalib {
  namespace ds {
    template<_internal::numeric_like T, T (*op)(T, T) = _internal::plus<T>, T (*invop)(T, T) = _internal::minus<T>, T (*e)() = _internal::zero<T>>
    class BIT {
      public:
      BIT(): _n(0) {}
      explicit BIT(size_t n) {
        _n = n;
        data.reserve(n);
        data.resize(n, 0);
      }

      void add(size_t i, T x) {
        assert(0 <= i && i < _n);
        ++i;
        while (i <= _n) {
          data[i - 1] = op(data[i - 1], x);
          i += i & -i;
        }
      }

      T sum(size_t l, size_t r) const {
        assert(0 <= l && l <= r && r <= _n);
        return invop(_sum(r), _sum(l));
      }

      private:
      size_t _n;
      vector<T> data;
      T _sum(size_t i) const {
        T s = e();
        while (i > 0) {
          s = op(s, data[i - 1]);
          i -= i & -i;
        }
        return s;
      }
    };
  }  // namespace ds
}  // namespace asalib
#line 10 "library/math/meissel-lehmer.hpp"

namespace asalib {
  namespace math {
    template<integral T>
    constexpr T meissel_lehmer(T n) {
      // n <= 2 だと y = 0 になってしまうのでここで返しておく
      if (n <= 2) [[unlikely]]
        return n == 2;
      T y = powl(n, 1.0 / 3.0) * powl(logl(n), 2.0 / 3.0);

      // 前計算
      T ny = n / y;
      if (n < 200) [[unlikely]]  // この場合は普通に求めちゃったほうが早い
        ny = n;

      constexpr T None = -1;
      vector<T> minim_prime(ny + 1, None);
      vector<T> prime(0);
      for (T i = 2; i <= ny; ++i) {
        if (minim_prime[i] == None) {
          minim_prime[i] = prime.size();
          prime.push_back(i);
        }
        for (T j = 0; j < (T) prime.size(); ++j) {
          if (i * prime[j] > ny || prime[j] > prime[minim_prime[i]]) break;
          minim_prime[i * prime[j]] = j;
        }
      }
      if (n < 200) [[unlikely]]
        return prime.size();

      T pi_ny = prime.size();
      T pi_y = 0;
      for (T p : prime) {
        if (p > y) break;
        ++pi_y;
      }
      T pi_n = pi_y - 1;

      // P_2 (n, a) を計算し引いていく
      for (T b = pi_y + 1, c = pi_ny; b <= pi_ny; ++b) {
        while (c >= b && prime[b - 1] * prime[c - 1] > n) --c;
        if (c < b) break;
        pi_n -= c - b + 1;
      }

      // phi(n, a) の計算
      vector<tuple<T, T, bool>> params(0);
      function<void(T, T, bool)> phi = [&](T x, T a, bool isMinus) {
        if (x == 0) return;
        else if (x <= ny)
          params.push_back({ x, a, isMinus });
        else if (a == 0)
          pi_n += x * (isMinus ? -1 : 1);
        else
          phi(x, a - 1, isMinus), phi(x / prime[a - 1], a - 1, !isMinus);
      };
      phi(n, pi_y, false);
      sort(params.begin(), params.end());

      asalib::ds::BIT<T> bit(pi_ny);
      T idx = 2;
      for (auto&& [n, a, isMinus] : params) {
        while (idx <= n) bit.add(minim_prime[idx++], 1);
        pi_n += (n - bit.sum(0, a)) * (isMinus ? -1 : 1);
      }

      return pi_n;
    }
  }  // namespace math
}  // namespace asalib
#line 8 "tests/math/meissel-lehmer/libchecker.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/counting_primes"

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  ll n;
  cin >> n;
  cout << asalib::math::meissel_lehmer(n) << endl;
  return 0;
}
Back to top page