#line 1 "main.cpp" #include 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 "my-library\\library\\math\\meissel-lehmer.hpp" #line 4 "my-library\\library\\math\\meissel-lehmer.hpp" #include #line 7 "my-library\\library\\math\\meissel-lehmer.hpp" using namespace std; #line 2 "my-library\\library\\data-structure\\bit.hpp" #line 4 "my-library\\library\\data-structure\\bit.hpp" using namespace std; #line 2 "my-library\\library\\_internal\\types.hpp" #line 4 "my-library\\library\\_internal\\types.hpp" #include using namespace std; #line 2 "my-library\\library\\_internal\\modint-base.hpp" #line 5 "my-library\\library\\_internal\\modint-base.hpp" using namespace std; namespace asalib { namespace _internal { class modint_base {}; template concept is_modint = is_base_of_v; } } #line 8 "my-library\\library\\_internal\\types.hpp" namespace asalib { namespace _internal { template concept integral_like = integral || is_modint; template concept floating_like = floating_point; template concept numeric_like = integral_like || floating_like; template T plus(T a, T b) { return a + b; } template T minus(T a, T b) { return a - b; } template T zero() { return 0; } } } #line 7 "my-library\\library\\data-structure\\bit.hpp" namespace asalib { namespace ds { template<_internal::numeric_like T, T (*op)(T, T) = _internal::plus, T (*invop)(T, T) = _internal::minus, T (*e)() = _internal::zero> 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 data; T _sum(size_t i) const { T s = e(); while (i > 0) { s = op(s, data[i - 1]); i -= i & -i; } return s; } }; } } #line 10 "my-library\\library\\math\\meissel-lehmer.hpp" namespace asalib { namespace math { template constexpr T meissel_lehmer(T n) { 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 minim_prime(ny + 1, None); vector 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; 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; } vector> params(0); function 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 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; } } } #line 8 "main.cpp" int main() { int t; cin >> t; while (t--) { int n; cin >> n; cout << asalib::math::meissel_lehmer(n) << endl; } return 0; }