luckYrat's library.

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


:heavy_check_mark: cpp/z_test/yosupo-counting_primes.cpp

Depends on

Required by

Code

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

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

#include "../../cpp/math/meissel-lehhmer.cpp"


int main(){
  long long n;;cin>>n;
  MeisselLehmer P(n);
  cout << P.count() << endl;
}
#line 1 "cpp/z_test/yosupo-counting_primes.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/counting_primes"

#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-counting_primes.cpp"

#line 1 "cpp/data-structure/binary-indexed-tree.cpp"
template<typename T>
struct BIT{//1_Indexed
  int n;
  vector<T> bit;
  BIT(){}
  BIT(int n_):n(n_+1),bit(n,0){}

  void set(int n_){
    n = n_;
    bit.assign(n,0);
  }

  T sum(int a){
    T ret = 0;
    for(int i = a; i > 0; i -= i & -i) ret += bit[i];
    return ret;
  }

  void add(int a,T w){
    for(int i = a; i <= n; i += i & -i)bit[i] += w;
  }

  T query(int r,int l){
    return sum(l-1)-sum(r-1);
  }


  int lower_bound(T x){
    if(x <= 0){
      return 0;
    }
    x--;
    int t = 1;
    while(t < n)t <<= 1;
    int st = 0;
    int base = 0;
    for(; t; t/=2){
      if(st+t <= n && base+bit[st+t] <= x){
        base += bit[st+t];
        st += t;
      }
    }
    return st+1;
  }
};
#line 1 "cpp/math/sieve-of-eratosthenes.cpp"
struct SieveEratos{
  int N;
  vector<int> minp;
  vector<bool> t;
  vector<int> primes;
  map<int,int> invprimes;
  SieveEratos(){}
  SieveEratos(int n):N(n+1){
    generate();
  }
  void set(int n){
    N = n+1;
    generate();
  }
  void generate(){
    t.assign(N, true);
    t[0] = t[1] = false;
    for(int i = 2; N > i; i++){
      if(t[i]){
        primes.emplace_back(i);
        for(int j = i+i; N > j; j+=i){
          t[j] = false;
        }
      }
    } 
  }
  void generate_invprime(){
    for(size_t i = 0; primes.size() > i; i++){
      invprimes[primes[i]] = i+1;
    }

  }
  void generate_minfactor(){
    minp.assign(N, N+2);
    minp[0] = minp[1] = -1;
    for(int i = 2; N > i; i++){
      if(minp[i] == N+2){
        minp[i] = i;
        for(int j = i+i; N > j; j+=i){
          minp[j] = min(i, minp[j]);
        }
      }
    }
  }
  bool operator[](int x){return t[x] == x;}
};
#line 1 "cpp/math/exactsqrt.cpp"
// 整数型を取得することを想定
template <typename T>
T CeilExactSqrt(int c, T x){
  T mn = 0;
  T mx = x;
  while(mx-mn > 1){
    T ce = (mn+mx)/2;
    T nw = 1;
    for(int i = 0; c > i; i++){
      if(x/ce < nw){
        mx = ce;
        break;
      }
      nw *= ce;
      if(i+1 == c && x == nw){
        mx = ce;
      }
    }
    if(mx != ce)mn = ce;
  }
  return mx;
}

template <typename T>
T FloorExactSqrt(int c, T x){
  T mn = 0;
  T mx = x;
  while(mx-mn > 1){
    T ce = (mn+mx)/2;
    T nw = 1;
    for(int i = 0; c > i; i++){
      if(x/ce < nw){
        mx = ce;
        break;
      }
      nw *= ce;
    }
    if(mx != ce)mn = ce;
  }
  return mn;
}
#line 4 "cpp/math/meissel-lehhmer.cpp"

struct MeisselLehmer{
  long long N;
  long long alpha;
  SieveEratos sieve;
  BIT<int> varphiTable;
  struct d{
    long long m,b;
    int cof;
    bool operator<(const d x)const{
      if(m == x.m) return b < x.b;
      return m < x.m;
    }
  };
  vector<d> varphiQueue; 
  MeisselLehmer(long long n): N(n){
    alpha = ceil(pow(N, 0.34));
    sieve.set(N/alpha+10);
    sieve.generate_invprime();
    sieve.generate_minfactor();
  }
  long long varphiLoop(long long n, long long a, int cof = 1){
    if(a == 0){
      return n;
    }else{
      if(n/sieve.primes[a-1] <= N/alpha){
        if(n/sieve.primes[a-1] >= 2){
          varphiQueue.push_back({n/sieve.primes[a-1], a-1, cof*-1});
          return varphiLoop(n, a-1, cof);
        }else{
          return varphiLoop(n, a-1, cof) - varphiLoop(n/sieve.primes[a-1], a-1, cof*-1);
        } 
      }else{
        return varphiLoop(n, a-1, cof) - varphiLoop(n/sieve.primes[a-1], a-1, cof*-1);
      }
    }
  }

  long long varphi(long long n, long long a){
    varphiTable.set(N/alpha+1);
    long long val = varphiLoop(n,a);
    sort(varphiQueue.begin(), varphiQueue.end());
    int cur = 0;
    for(int i = 2; N/alpha >= i; i++){
      varphiTable.add(sieve.invprimes[sieve.minp[i]], 1);
      while(cur < varphiQueue.size() && varphiQueue[cur].m == i){
        val += varphiQueue[cur].cof * (varphiQueue[cur].m - varphiTable.query(1, varphiQueue[cur].b+1));
        cur++;
      }
    }
    varphiQueue.clear();
    return val;
  }

  long long P2(long long n, long long a){
    long long val = 0;
    int cur = sieve.primes.size()-1;
    for(int i = a; n/sieve.primes[a-1] > sieve.primes[i]; i++){
      while((sieve.primes[cur] > n / sieve.primes[i] || (n%sieve.primes[i] == 0 && sieve.primes[cur] == n/sieve.primes[i]))){
        cur--;
      }
      if(cur < i)break;
      val += cur-i+1;
    }
    return val;
  }


  long long count(long long n = -1){
    if(n == -1)n = N;
    long long prevN = N;
    N = n;
    alpha = ceil(pow(N, 0.34));
    if(N < 2)return 0;
    else if(N < 3)return 1;
    long long val = varphi(N, alpha) + alpha-1 - P2(N, alpha);
    N = prevN;
    return val;
  }
};

#line 6 "cpp/z_test/yosupo-counting_primes.cpp"


int main(){
  long long n;;cin>>n;
  MeisselLehmer P(n);
  cout << P.count() << endl;
}

Test cases

Env Name Status Elapsed Memory
g++ Grothendieck_prime_00 :heavy_check_mark: AC 6 ms 4 MB
g++ example_00 :heavy_check_mark: AC 5 ms 4 MB
g++ example_01 :heavy_check_mark: AC 5 ms 4 MB
g++ max_00 :heavy_check_mark: AC 2304 ms 226 MB
g++ max_01 :heavy_check_mark: AC 2412 ms 226 MB
g++ max_02 :heavy_check_mark: AC 2388 ms 226 MB
g++ max_03 :heavy_check_mark: AC 2215 ms 228 MB
g++ max_04 :heavy_check_mark: AC 2483 ms 226 MB
g++ random_00 :heavy_check_mark: AC 2491 ms 224 MB
g++ random_01 :heavy_check_mark: AC 1578 ms 153 MB
g++ random_02 :heavy_check_mark: AC 1536 ms 158 MB
g++ random_03 :heavy_check_mark: AC 1369 ms 136 MB
g++ random_04 :heavy_check_mark: AC 719 ms 84 MB
g++ small_00 :heavy_check_mark: AC 8 ms 4 MB
g++ small_01 :heavy_check_mark: AC 7 ms 4 MB
g++ small_02 :heavy_check_mark: AC 6 ms 4 MB
g++ small_03 :heavy_check_mark: AC 7 ms 4 MB
g++ small_04 :heavy_check_mark: AC 8 ms 4 MB
g++ small_prime_00 :heavy_check_mark: AC 5 ms 4 MB
g++ small_prime_01 :heavy_check_mark: AC 5 ms 4 MB
g++ very_small_00 :heavy_check_mark: AC 6 ms 4 MB
g++ very_small_01 :heavy_check_mark: AC 5 ms 4 MB
g++ very_small_02 :heavy_check_mark: AC 6 ms 4 MB
g++ very_small_03 :heavy_check_mark: AC 6 ms 4 MB
g++ very_small_04 :heavy_check_mark: AC 6 ms 4 MB
Back to top page