mmrz's library

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


Project maintained by mm-rz Hosted on GitHub Pages — Theme by mattgraham

:heavy_check_mark: verify/yosupo/counting_primes.test.cpp

Depends on

Code

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

#include "./../../template/template.hpp"
#include "./../../math/counting_primes.hpp"

using namespace mmrz;

void mmrz::solve(){
	ll n;
	cin >> n;
	cout << counting_primes(n) << '\n';
}
#line 1 "verify/yosupo/counting_primes.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/counting_primes"

#line 1 "template/template.hpp"
# include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const double pi = acos(-1);
template<class T>constexpr T inf() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T hinf() { return inf<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); }
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); }
template<class T> bool chmin(T& a,T b) { if(a > b){a = b; return true;} return false; }
template<class T> bool chmax(T& a,T b) { if(a < b){a = b; return true;} return false; }
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(ll n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(ll n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
ll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a%b); };
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g*b; };
ll MOD(ll x, ll m){return (x%m+m)%m; }
ll FLOOR(ll x, ll m) {ll r = (x%m+m)%m; return (x-r)/m; }
template<class T> using dijk = priority_queue<T, vector<T>, greater<T>>;
# define all(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        (wpwpw).erase(unique(all((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(all((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(all((rprpr)),(rprpr).begin(),TU<char>)
# define rep(i,upupu)         for(ll i = 0, i##_len = (upupu);(i) < (i##_len);(i)++)
# define reps(i,opopo)        for(ll i = 1, i##_len = (opopo);(i) <= (i##_len);(i)++)
# define len(x)                ((ll)(x).size())
# define bit(n)               (1LL << (n))
# define pb push_back
# define eb emplace_back
# define exists(c, e)         ((c).find(e) != (c).end())

struct INIT{
	INIT(){
		std::ios::sync_with_stdio(false);
		std::cin.tie(0);
		cout << fixed << setprecision(20);
	}
}INIT;

namespace mmrz {
	void solve();
}

int main(){
	mmrz::solve();
}
#line 2 "math/counting_primes.hpp"

#line 2 "math/iroot.hpp"

#line 5 "math/iroot.hpp"

unsigned long long iroot(unsigned long long n, int k=2){
	constexpr unsigned long long LIM = std::numeric_limits<unsigned long long>::max();
	if(n <= 1 || k == 1){
		return n;
	}
	if(k >= 64){
		return 1;
	}
	if(k == 2){
		return sqrtl(n);
	}

	if(n == LIM)n--;

	auto safe_mul = [&](unsigned long long &x, unsigned long long &y) -> void {
		if(x <= LIM / y){
			x *= y;
		}else{
			x = LIM;
		}
	};

	auto power = [&](unsigned long long a, int b) -> unsigned long long {
		unsigned long long ret = 1;
		while(b){
			if(b & 1)safe_mul(ret, a);
			safe_mul(a, a);
			b >>= 1;
		}
		return ret;
	};

	unsigned long long ret = (k == 3 ? cbrt(n)-1 : std::pow(n, std::nextafter(1.0/double(k), 0.0)));
	while(power(ret+1, k) <= n)ret++;
	return ret;
}
#line 4 "math/counting_primes.hpp"

#line 6 "math/counting_primes.hpp"

//https://judge.yosupo.jp/submission/61551
//https://rsk0315.hatenablog.com/entry/2021/05/18/015511
long long counting_primes(const long long N) {
	if (N <= 1) return 0;
	if (N == 2) return 1;
	const int v = iroot(N);
	int s = (v + 1) / 2;
	std::vector<int> smalls(s);
	for (int i = 1; i < s; i++) smalls[i] = i;
	std::vector<int> roughs(s);
	for (int i = 0; i < s; i++) roughs[i] = 2 * i + 1;
	std::vector<long long> larges(s);
	for (int i = 0; i < s; i++) larges[i] = (N / (2 * i + 1) - 1) / 2;
	std::vector<bool> skip(v + 1);
	const auto divide = [](long long n, long long d) -> int { return (double)n / d;};
	const auto half = [](int n) -> int { return (n - 1) >> 1;};
	int pc = 0;
	for (int p = 3; p <= v; p += 2) if (!skip[p]) {
		int q = p * p;
		if ((long long)q * q > N) break;
		skip[p] = true;
		for (int i = q; i <= v; i += 2 * p) skip[i] = true;
		int ns = 0;
		for (int k = 0; k < s; k++) {
			int i = roughs[k];
			if (skip[i]) continue;
			long long d = (long long)i * p;
			larges[ns] = larges[k] - (d <= v ? larges[smalls[d >> 1] - pc] : smalls[half(divide(N, d))]) + pc;
			roughs[ns++] = i;
		}
		s = ns;
		for (int i = half(v), j = ((v / p) - 1) | 1; j >= p; j -= 2) {
			int c = smalls[j >> 1] - pc;
			for (int e = (j * p) >> 1; i >= e; i--) smalls[i] -= c;
		}
		pc++;
	}
	larges[0] += (long long)(s + 2 * (pc - 1)) * (s - 1) / 2;
	for (int k = 1; k < s; k++) larges[0] -= larges[k];
	for (int l = 1; l < s; l++) {
		long long q = roughs[l];
		long long M = N / q;
		int e = smalls[half(M / q)] - pc;
		if (e < l + 1) break;
		long long t = 0;
		for (int k = l + 1; k <= e; k++)
			t += smalls[half(divide(M, roughs[k]))];
		larges[0] += t - (long long)(e - l) * (pc + l - 1);
	}
	return larges[0] + 1;
}
#line 5 "verify/yosupo/counting_primes.test.cpp"

using namespace mmrz;

void mmrz::solve(){
	ll n;
	cin >> n;
	cout << counting_primes(n) << '\n';
}
Back to top page