Hashiryo's Library

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

View the Project on GitHub hashiryo/Library

:heavy_check_mark: test/loj/6686.Dirich.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://loj.ac/p/6686
// competitive-verifier: TLE 1.0
// competitive-verifier: MLE 64

#include <iostream>
#include "src/Math/ModInt.hpp"
#include "src/NumberTheory/ArrayOnDivisors.hpp"
#include "src/NumberTheory/DirichletSeries.hpp"
using namespace std;
template <class Tp> void read(Tp &x) {
 static char ch;
 static bool neg;
 for (ch= neg= 0; ch < '0' || ch > '9'; neg|= (ch == '-'), ch= getchar());
 for (x= 0; ch >= '0' && ch <= '9'; (x*= 10)+= (ch ^ 48), ch= getchar());
 neg && (x= -x);
}
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 using Mint= ModInt<998244353>;
 using u128= __uint128_t;
 using u64= uint64_t;
 u128 n= 0;
 read(n);
 u64 l= 0, h= 1e10 + 10;
 while (h - l > 1) {
  u64 x= (h + l) / 2;
  u128 t= u128(x) * x * x;
  if (t <= n) l= x;
  else h= x;
 }
 u64 r= l - 1;
 u128 m= u128(l) * l * l;
 Mint ans= 0;
 ArrayOnDivisors<u64, u64> totient(l);
 totient.set_totient();
 for (auto [d, phi]: totient) ans+= Mint(n / d - (m - 1) / d) * phi;
 auto zeta= get_1<Mint>(r), id= get_Id<Mint>(r), id2= get_Id2<Mint>(r);
 ans+= (id2.square() / id).sum() * 3;
 ans+= (id.square() / zeta).sum() * 3;
 ans+= id.sum();
 cout << ans << '\n';
 return 0;
}
#line 1 "test/loj/6686.Dirich.test.cpp"
// competitive-verifier: PROBLEM https://loj.ac/p/6686
// competitive-verifier: TLE 1.0
// competitive-verifier: MLE 64

#include <iostream>
#line 2 "src/Math/mod_inv.hpp"
#include <utility>
#include <type_traits>
#include <cassert>
template <class Uint> constexpr inline Uint mod_inv(Uint a, Uint mod) {
 std::make_signed_t<Uint> x= 1, y= 0, z= 0;
 for (Uint q= 0, b= mod, c= 0; b;) z= x, x= y, y= z - y * (q= a / b), c= a, a= b, b= c - b * q;
 return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod;
}
#line 2 "src/Internal/Remainder.hpp"
namespace math_internal {
using namespace std;
using u8= unsigned char;
using u32= unsigned;
using i64= long long;
using u64= unsigned long long;
using u128= __uint128_t;
struct MP_Na {  // mod < 2^32
 u32 mod;
 constexpr MP_Na(): mod(0) {}
 constexpr MP_Na(u32 m): mod(m) {}
 constexpr inline u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; }
 constexpr inline u32 set(u32 n) const { return n; }
 constexpr inline u32 get(u32 n) const { return n; }
 constexpr inline u32 norm(u32 n) const { return n; }
 constexpr inline u32 plus(u64 l, u32 r) const { return l+= r, l < mod ? l : l - mod; }
 constexpr inline u32 diff(u64 l, u32 r) const { return l-= r, l >> 63 ? l + mod : l; }
};
template <class u_t, class du_t, u8 B> struct MP_Mo {  // mod < 2^32, mod < 2^62
 u_t mod;
 constexpr MP_Mo(): mod(0), iv(0), r2(0) {}
 constexpr MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {}
 constexpr inline u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); }
 constexpr inline u_t set(u_t n) const { return mul(n, r2); }
 constexpr inline u_t get(u_t n) const { return n= reduce(n), n >= mod ? n - mod : n; }
 constexpr inline u_t norm(u_t n) const { return n >= mod ? n - mod : n; }
 constexpr inline u_t plus(u_t l, u_t r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
 constexpr inline u_t diff(u_t l, u_t r) const { return l-= r, l >> (B - 1) ? l + (mod << 1) : l; }
private:
 u_t iv, r2;
 static constexpr u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; }
 constexpr inline u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); }
};
using MP_Mo32= MP_Mo<u32, u64, 32>;
using MP_Mo64= MP_Mo<u64, u128, 64>;
struct MP_Br {  // 2^20 < mod <= 2^41
 u64 mod;
 constexpr MP_Br(): mod(0), x(0) {}
 constexpr MP_Br(u64 m): mod(m), x((u128(1) << 84) / m) {}
 constexpr inline u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); }
 static constexpr inline u64 set(u64 n) { return n; }
 constexpr inline u64 get(u64 n) const { return n >= mod ? n - mod : n; }
 constexpr inline u64 norm(u64 n) const { return n >= mod ? n - mod : n; }
 constexpr inline u64 plus(u64 l, u64 r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
 constexpr inline u64 diff(u64 l, u64 r) const { return l-= r, l >> 63 ? l + (mod << 1) : l; }
private:
 u64 x;
 constexpr inline u128 quo(const u128 &n) const { return (n * x) >> 84; }
 constexpr inline u64 rem(const u128 &n) const { return n - quo(n) * mod; }
};
template <class du_t, u8 B> struct MP_D2B1 {  // mod < 2^63, mod < 2^64
 u64 mod;
 constexpr MP_D2B1(): mod(0), s(0), d(0), v(0) {}
 constexpr MP_D2B1(u64 m): mod(m), s(__builtin_clzll(m)), d(m << s), v(u128(-1) / d) {}
 constexpr inline u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; }
 constexpr inline u64 set(u64 n) const { return n; }
 constexpr inline u64 get(u64 n) const { return n; }
 constexpr inline u64 norm(u64 n) const { return n; }
 constexpr inline u64 plus(du_t l, u64 r) const { return l+= r, l < mod ? l : l - mod; }
 constexpr inline u64 diff(du_t l, u64 r) const { return l-= r, l >> B ? l + mod : l; }
private:
 u8 s;
 u64 d, v;
 constexpr inline u64 rem(const u128 &u) const {
  u128 q= (u >> 64) * v + u;
  u64 r= u64(u) - (q >> 64) * d - d;
  if (r > u64(q)) r+= d;
  if (r >= d) r-= d;
  return r;
 }
};
using MP_D2B1_1= MP_D2B1<u64, 63>;
using MP_D2B1_2= MP_D2B1<u128, 127>;
template <class u_t, class MP> constexpr u_t pow(u_t x, u64 k, const MP &md) {
 for (u_t ret= md.set(1);; x= md.mul(x, x))
  if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret;
}
}
#line 3 "src/Internal/modint_traits.hpp"
namespace math_internal {
struct m_b {};
struct s_b: m_b {};
}
template <class mod_t> constexpr bool is_modint_v= std::is_base_of_v<math_internal::m_b, mod_t>;
template <class mod_t> constexpr bool is_staticmodint_v= std::is_base_of_v<math_internal::s_b, mod_t>;
#line 6 "src/Math/ModInt.hpp"
namespace math_internal {
template <class MP, u64 MOD> struct SB: s_b {
protected:
 static constexpr MP md= MP(MOD);
};
template <class U, class B> struct MInt: public B {
 using Uint= U;
 static constexpr inline auto mod() { return B::md.mod; }
 constexpr MInt(): x(0) {}
 template <class T, typename= enable_if_t<is_modint_v<T> && !is_same_v<T, MInt>>> constexpr MInt(T v): x(B::md.set(v.val() % B::md.mod)) {}
 constexpr MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {}
 constexpr MInt operator-() const { return MInt() - *this; }
#define FUNC(name, op) \
 constexpr MInt name const { \
  MInt ret; \
  return ret.x= op, ret; \
 }
 FUNC(operator+(const MInt & r), B::md.plus(x, r.x))
 FUNC(operator-(const MInt & r), B::md.diff(x, r.x))
 FUNC(operator*(const MInt & r), B::md.mul(x, r.x))
 FUNC(pow(u64 k), math_internal::pow(x, k, B::md))
#undef FUNC
 constexpr MInt operator/(const MInt &r) const { return *this * r.inv(); }
 constexpr MInt &operator+=(const MInt &r) { return *this= *this + r; }
 constexpr MInt &operator-=(const MInt &r) { return *this= *this - r; }
 constexpr MInt &operator*=(const MInt &r) { return *this= *this * r; }
 constexpr MInt &operator/=(const MInt &r) { return *this= *this / r; }
 constexpr bool operator==(const MInt &r) const { return B::md.norm(x) == B::md.norm(r.x); }
 constexpr bool operator!=(const MInt &r) const { return !(*this == r); }
 constexpr bool operator<(const MInt &r) const { return B::md.norm(x) < B::md.norm(r.x); }
 constexpr inline MInt inv() const { return mod_inv<U>(val(), B::md.mod); }
 constexpr inline Uint val() const { return B::md.get(x); }
 friend ostream &operator<<(ostream &os, const MInt &r) { return os << r.val(); }
 friend istream &operator>>(istream &is, MInt &r) {
  i64 v;
  return is >> v, r= MInt(v), is;
 }
private:
 Uint x;
};
template <u64 MOD> using MP_B= conditional_t < (MOD < (1 << 30)) & MOD, MP_Mo32, conditional_t < MOD < (1ull << 32), MP_Na, conditional_t<(MOD < (1ull << 62)) & MOD, MP_Mo64, conditional_t<MOD<(1ull << 41), MP_Br, conditional_t<MOD<(1ull << 63), MP_D2B1_1, MP_D2B1_2>>>>>;
template <u64 MOD> using ModInt= MInt < conditional_t<MOD<(1 << 30), u32, u64>, SB<MP_B<MOD>, MOD>>;
}
using math_internal::ModInt;
#line 2 "src/NumberTheory/Factors.hpp"
#include <numeric>
#line 5 "src/NumberTheory/Factors.hpp"
#include <algorithm>
#include <vector>
#line 3 "src/NumberTheory/is_prime.hpp"
namespace math_internal {
template <class Uint, class MP, u32... args> constexpr bool miller_rabin(Uint n) {
 const MP md(n);
 const Uint s= __builtin_ctzll(n - 1), d= n >> s, one= md.set(1), n1= md.norm(md.set(n - 1));
 for (u32 a: (u32[]){args...})
  if (Uint b= a % n; b)
   if (Uint p= md.norm(pow(md.set(b), d, md)); p != one)
    for (int i= s; p != n1; p= md.norm(md.mul(p, p)))
     if (!(--i)) return 0;
 return 1;
}
}
constexpr bool is_prime(unsigned long long n) {
 if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3;
 if (n < (1 << 30)) return math_internal::miller_rabin<unsigned, math_internal::MP_Mo32, 2, 7, 61>(n);
 if (n < (1ull << 62)) return math_internal::miller_rabin<unsigned long long, math_internal::MP_Mo64, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
 if (n < (1ull << 63)) return math_internal::miller_rabin<unsigned long long, math_internal::MP_D2B1_1, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
 return math_internal::miller_rabin<unsigned long long, math_internal::MP_D2B1_2, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
}
#line 4 "src/Math/binary_gcd.hpp"
#include <cstdint>
template <class Int> constexpr int bsf(Int a) {
 if constexpr (sizeof(Int) == 16) {
  uint64_t lo= a & uint64_t(-1);
  return lo ? __builtin_ctzll(lo) : 64 + __builtin_ctzll(a >> 64);
 } else if constexpr (sizeof(Int) == 8) return __builtin_ctzll(a);
 else return __builtin_ctz(a);
}
template <class Int> constexpr Int binary_gcd(Int a, Int b) {
 if (a == 0 || b == 0) return a + b;
 int n= bsf(a), m= bsf(b), s= 0;
 for (a>>= n, b>>= m; a != b;) {
  Int d= a - b;
  bool f= a > b;
  s= bsf(d), b= f ? b : a, a= (f ? d : -d) >> s;
 }
 return a << std::min(n, m);
}
#line 9 "src/NumberTheory/Factors.hpp"
namespace math_internal {
template <class T> constexpr void bubble_sort(T *bg, T *ed) {
 for (int sz= ed - bg, i= 0; i < sz; i++)
  for (int j= sz; --j > i;)
   if (auto tmp= bg[j - 1]; bg[j - 1] > bg[j]) bg[j - 1]= bg[j], bg[j]= tmp;
}
template <class T, size_t _Nm> struct ConstexprArray {
 constexpr size_t size() const { return sz; }
 constexpr auto &operator[](int i) const { return dat[i]; }
 constexpr auto *begin() const { return dat; }
 constexpr auto *end() const { return dat + sz; }
protected:
 T dat[_Nm]= {};
 size_t sz= 0;
 friend ostream &operator<<(ostream &os, const ConstexprArray &r) {
  os << "[";
  for (size_t i= 0; i < r.sz; ++i) os << r[i] << ",]"[i == r.sz - 1];
  return os;
 }
};
class Factors: public ConstexprArray<pair<u64, uint16_t>, 16> {
 template <class Uint, class MP> static constexpr Uint rho(Uint n, Uint c) {
  const MP md(n);
  auto f= [&md, c](Uint x) { return md.plus(md.mul(x, x), c); };
  const Uint m= 1LL << (__lg(n) / 5);
  Uint x= 1, y= md.set(2), z= 1, q= md.set(1), g= 1;
  for (Uint r= 1, i= 0; g == 1; r<<= 1) {
   for (x= y, i= r; i--;) y= f(y);
   for (Uint k= 0; k < r && g == 1; g= binary_gcd<Uint>(md.get(q), n), k+= m)
    for (z= y, i= min(m, r - k); i--;) y= f(y), q= md.mul(q, md.diff(y, x));
  }
  if (g == n) do {
    z= f(z), g= binary_gcd<Uint>(md.get(md.diff(z, x)), n);
   } while (g == 1);
  return g;
 }
 static constexpr u64 find_prime_factor(u64 n) {
  if (is_prime(n)) return n;
  for (u64 i= 100; i--;)
   if (n= n < (1 << 30) ? rho<u32, MP_Mo32>(n, i + 1) : n < (1ull << 62) ? rho<u64, MP_Mo64>(n, i + 1) : n < (1ull << 62) ? rho<u64, MP_D2B1_1>(n, i + 1) : rho<u64, MP_D2B1_2>(n, i + 1); is_prime(n)) return n;
  return 0;
 }
 constexpr void init(u64 n) {
  for (u64 p= 2; p < 98 && p * p <= n; ++p)
   if (n % p == 0)
    for (dat[sz++].first= p; n % p == 0;) n/= p, ++dat[sz - 1].second;
  for (u64 p= 0; n > 1; dat[sz++].first= p)
   for (p= find_prime_factor(n); n % p == 0;) n/= p, ++dat[sz].second;
 }
public:
 constexpr Factors()= default;
 constexpr Factors(u64 n) { init(n), bubble_sort(dat, dat + sz); }
};
}
using math_internal::Factors;
constexpr uint64_t totient(const Factors &f) {
 uint64_t ret= 1, i= 0;
 for (auto [p, e]: f)
  for (ret*= p - 1, i= e; --i;) ret*= p;
 return ret;
}
constexpr auto totient(uint64_t n) { return totient(Factors(n)); }
template <class Uint= uint64_t> std::vector<Uint> enumerate_divisors(const Factors &f) {
 int k= 1;
 for (auto [p, e]: f) k*= e + 1;
 std::vector<Uint> ret(k, 1);
 k= 1;
 for (auto [p, e]: f) {
  int sz= k;
  for (Uint pw= 1; pw*= p, e--;)
   for (int j= 0; j < sz;) ret[k++]= ret[j++] * pw;
 }
 return ret;
}
template <class Uint> std::vector<Uint> enumerate_divisors(Uint n) { return enumerate_divisors<Uint>(Factors(n)); }
#line 3 "src/NumberTheory/ArrayOnDivisors.hpp"
template <class Int, class T> struct ArrayOnDivisors {
 using Hint= std::conditional_t<sizeof(Int) == 8, unsigned, uint16_t>;
 Int n;
 uint8_t shift;
 std::vector<Hint> os, id;
 std::vector<std::pair<Int, T>> dat;
 Hint hash(uint64_t i) const { return (i * 11995408973635179863ULL) >> shift; }
#define _UP for (int j= k; j < a; ++j)
#define _DWN for (int j= a; j-- > k;)
#define _OP(J, K, op) dat[i + J].second op##= dat[i + K].second
#define _FUN(J, K, name) name(dat[i + J].second, dat[i + K].second)
#define _ZETA(op) \
 int k= 1; \
 for (auto [p, e]: factors) { \
  int a= k * (e + 1); \
  for (int i= 0, d= dat.size(); i < d; i+= a) op; \
  k= a; \
 }
public:
 Factors factors;
 ArrayOnDivisors() {}
 template <class Uint> ArrayOnDivisors(Int N, const Factors &factors, const std::vector<Uint> &divisors): n(N), shift(__builtin_clzll(divisors.size()) - 1), os((1 << (64 - shift)) + 1), id(divisors.size()), dat(divisors.size()), factors(factors) {
  static_assert(std::is_integral_v<Uint>, "Uint must be integral");
  int m= divisors.size(), i= 0;
  for (; i < m; ++i) ++os[hash(dat[i].first= divisors[i])];
  for (std::partial_sum(os.begin(), os.end(), os.begin()); i--;) id[--os[hash(divisors[i])]]= i;
 }
 ArrayOnDivisors(Int N, const Factors &factors): ArrayOnDivisors(N, factors, enumerate_divisors(factors)) {}
 ArrayOnDivisors(Int N): ArrayOnDivisors(N, Factors(N)) {}
 T &operator[](Int i) {
  assert(i && n % i == 0);
  for (unsigned a= hash(i), j= os[a]; j < os[a + 1]; ++j)
   if (auto &[d, v]= dat[id[j]]; d == i) return v;
  assert(0);
 }
 const T &operator[](Int i) const {
  assert(i && n % i == 0);
  for (unsigned a= hash(i), j= os[a]; j < os[a + 1]; ++j)
   if (auto &[d, v]= dat[id[j]]; d == i) return v;
  assert(0);
 }
 size_t size() const { return dat.size(); }
 auto begin() { return dat.begin(); }
 auto begin() const { return dat.begin(); }
 auto end() { return dat.begin() + os.back(); }
 auto end() const { return dat.begin() + os.back(); }
 /* f -> g s.t. g(n) = sum_{m|n} f(m) */
 void divisor_zeta() { _ZETA(_UP _OP(j, j - k, +)) }
 /* f -> h s.t. f(n) = sum_{m|n} h(m) */
 void divisor_mobius() { _ZETA(_DWN _OP(j, j - k, -)) }
 /* f -> g s.t. g(n) = sum_{n|m} f(m) */
 void multiple_zeta() { _ZETA(_DWN _OP(j - k, j, +)) }
 /* f -> h s.t. f(n) = sum_{n|m} h(m) */
 void multiple_mobius() { _ZETA(_UP _OP(j - k, j, -)) }
 /* f -> g s.t. g(n) = sum_{m|n} f(m), add(T& a, T b): a+=b */
 template <class F> void divisor_zeta(const F &add) { _ZETA(_UP _FUN(j, j - k, add)) }
 /* f -> h s.t. f(n) = sum_{m|n} h(m), sub(T& a, T b): a-=b */
 template <class F> void divisor_mobius(const F &sub) { _ZETA(_UP _FUN(j, j - k, sub)) }
 /* f -> g s.t. g(n) = sum_{n|m} f(m), add(T& a, T b): a+=b */
 template <class F> void multiple_zeta(const F &add) { _ZETA(_UP _FUN(j - k, j, add)) }
 /* f -> h s.t. f(n) = sum_{n|m} h(m), sub(T& a, T b): a-=b */
 template <class F> void multiple_mobius(const F &sub) { _ZETA(_UP _FUN(j - k, j, sub)) }
#undef _UP
#undef _DWN
#undef _OP
#undef _ZETA
 // f(p,e): multiplicative function of p^e
 template <typename F> void set_multiplicative(const F &f) {
  int k= 1;
  dat[0].second= 1;
  for (auto [p, e]: factors)
   for (int m= k, d= 1; d <= e; ++d)
    for (int i= 0; i < m;) dat[k++].second= dat[i++].second * f(p, d);
 }
 void set_totient() {
  int k= 1;
  dat[0].second= 1;
  for (auto [p, e]: factors) {
   Int b= p - 1;
   for (int m= k; e--; b*= p)
    for (int i= 0; i < m;) dat[k++].second= dat[i++].second * b;
  }
 }
 void set_mobius() {
  set_multiplicative([](auto, auto e) { return e == 1 ? -1 : 0; });
 }
};
#line 2 "src/NumberTheory/DirichletSeries.hpp"
#include <valarray>
#include <iterator>
#line 5 "src/NumberTheory/DirichletSeries.hpp"
#include <cmath>
#line 9 "src/NumberTheory/DirichletSeries.hpp"
template <class T> struct DirichletSeries {
 using Self= DirichletSeries;
 uint64_t N;  // <= K * L
 size_t K, L;
 std::valarray<T> x, X;
 DirichletSeries(uint64_t N, bool unit= false): N(N), K(N > 1 ? std::max(std::ceil(std::pow((double)N / std::log2(N), 2. / 3)), std::sqrt(N) + 1) : 1), L((N - 1 + K) / K), x(K + 1), X(K + L + 1) {
  if (assert(N > 0); unit) x[1]= 1, X= 1;
 }
 template <class F, typename= std::enable_if_t<std::is_invocable_r_v<T, F, uint64_t>>> DirichletSeries(uint64_t N, const F &sum): DirichletSeries(N) {
  for (size_t i= 1; i <= K; ++i) X[i]= sum(i);
  for (size_t i= 1; i <= L; ++i) X[K + i]= sum(uint64_t((double)N / i));
  for (size_t i= K; i; --i) x[i]= X[i] - X[i - 1];
 }
 Self operator-() const {
  Self ret(N);
  return ret.x= -x, ret.X= -X, ret;
 }
 Self &operator+=(T r) { return x[1]+= r, X+= r, *this; }
 Self &operator-=(T r) { return x[1]-= r, X-= r, *this; }
 Self &operator*=(T r) { return x*= r, X*= r, *this; }
 Self &operator/=(T r) {
  if (T iv= T(1) / r; iv == 0) x/= r, X/= r;
  else x*= iv, X*= iv;
  return *this;
 }
 Self &operator+=(const Self &r) { return assert(N == r.N), assert(K == r.K), assert(L == r.L), x+= r.x, X+= r.X, *this; }
 Self &operator-=(const Self &r) { return assert(N == r.N), assert(K == r.K), assert(L == r.L), x-= r.x, X-= r.X, *this; }
 Self operator+(T r) const { return Self(*this)+= r; }
 Self operator-(T r) const { return Self(*this)-= r; }
 Self operator*(T r) const { return Self(*this)*= r; }
 Self operator/(T r) const { return Self(*this)/= r; }
 Self operator+(const Self &r) const { return Self(*this)+= r; }
 Self operator-(const Self &r) const { return Self(*this)-= r; }
 friend Self operator+(T l, Self r) { return r+= l; }
 friend Self operator-(T l, Self r) { return r.x[1]-= l, r.X-= l, r.x= -r.x, r.X= -r.X, r; }
 friend Self operator*(T l, const Self &r) { return r * l; }
 friend Self operator/(T l, const Self &r) { return (Self(r.N, true)/= r)*= l; }
 Self operator*(const Self &r) const {
  assert(N == r.N), assert(K == r.K), assert(L == r.L);
  Self ret(N);
  uint64_t n;
  for (size_t i= K, j; i; --i)
   for (j= K / i; j; --j) ret.x[i * j]+= x[i] * r.x[j];
  for (size_t l= L, m, i; l; ret.X[K + l--]-= sum(m) * r.sum(m))
   for (i= m= std::sqrt(n= (double)N / l); i; --i) ret.X[K + l]+= x[i] * r.sum((double)n / i) + r.x[i] * sum((double)n / i);
  for (size_t i= 1; i <= K; ++i) ret.X[i]= ret.X[i - 1] + ret.x[i];
  return ret;
 }
 Self operator/(const Self &r) const { return Self(*this)/= r; }
 Self &operator*=(const Self &r) { return *this= *this * r; }
 Self &operator/=(const Self &r) {
  assert(N == r.N), assert(K == r.K), assert(L == r.L);
  for (size_t i= 1, j, ed; i <= K; i++)
   for (x[i]/= r.x[1], j= 2, ed= K / i; j <= ed; j++) x[i * j]-= x[i] * r.x[j];
  X[1]= x[1];
  for (size_t i= 2; i <= K; ++i) X[i]= X[i - 1] + x[i];
  uint64_t n;
  for (size_t l= L, m; l; X[K + l--]/= r.x[1])
   for (m= std::sqrt(n= (double)N / l), X[K + l]+= r.sum(m) * sum(m) - x[1] * r.sum(n); m > 1;) X[K + l]-= r.x[m] * sum((double)n / m) + x[m] * r.sum((double)n / m), --m;
  return *this;
 }
 Self square() const {
  Self ret(N);
  size_t i, j, l= std::sqrt(K);
  uint64_t n;
  T tmp;
  for (i= l; i; --i)
   for (j= K / i; j > i; --j) ret.x[i * j]+= x[i] * x[j];
  ret.x+= ret.x;
  for (i= l; i; --i) ret.x[i * i]+= x[i] * x[i];
  for (l= L; l; ret.X[K + l]+= ret.X[K + l], ret.X[K + l--]-= tmp * tmp)
   for (tmp= sum(i= std::sqrt(n= (double)N / l)); i; --i) ret.X[K + l]+= x[i] * sum((double)n / i);
  for (size_t i= 1; i <= K; ++i) ret.X[i]= ret.X[i - 1] + ret.x[i];
  return ret;
 }
 Self pow(uint64_t M) const {
  if (N / M > M)
   for (auto ret= Self(N, true), b= *this;; b= b.square()) {
    if (M & 1) ret*= b;
    if (!(M>>= 1)) return ret;
   }
  size_t n= 0, m, i, l, p= 2;
  uint64_t e, j;
  while (n <= M && (1ULL << n) <= N) ++n;
  T pw[65]= {1}, b= x[1], tmp;
  for (e= M - n + 1;; b*= b)
   if (e & 1 ? pw[0]*= b : T(); !(e>>= 1)) break;
  for (m= 1; m < n; ++m) pw[m]= pw[m - 1] * x[1];
  Self ret(*this);
  std::valarray<T> D= (ret.X-= x[1]), E(std::begin(D), K + 1), Y(std::begin(D) + K, L + 1), y= x, z(K + 1), Z(L + 1);
  auto A= [&](uint64_t n) { return n > K ? D[K + (double)N / n] : D[n]; };
  auto B= [&](uint64_t n) { return n > K ? Y[(double)N / n] : E[n]; };
  for (tmp= pw[n - 2] * M, l= L; l; l--) ret.X[K + l]*= tmp;
  for (i= 2; i <= K; ++i) ret.x[i]*= tmp;
  for (ret.x[1]= pw[n - 1], l= L; l; l--) ret.X[K + l]+= ret.x[1];
  for (m= 1, b= M, l= std::min<uint64_t>(L, uint64_t((double)N / p) / 2); m + 1 < n;) {
   for (b*= M - m, b/= ++m, tmp= b * pw[n - 1 - m]; l; ret.X[K + l--]+= Z[l] * tmp) {
    for (i= j= std::sqrt(e= (double)N / l); i >= p; --i) Z[l]+= y[i] * A((double)e / i);
    for (i= std::min(j, e / p); i >= 2; --i) Z[l]+= x[i] * B((double)e / i);
    if (j >= p) Z[l]-= A(j) * B(j);
   }
   for (i= K; i >= p; --i)
    for (l= K / i; l >= 2; l--) z[i * l]+= y[i] * x[l];
   for (i= p= 1 << m; i <= K; ++i) ret.x[i]+= z[i] * tmp;
   if (m + 1 == n) break;
   if (l= std::min<uint64_t>(L, uint64_t((double)N / p) / 2), y.swap(z), Y.swap(Z), std::fill_n(std::begin(Z) + 1, l, 0); p * 2 <= K) std::fill(std::begin(z) + p * 2, std::end(z), 0);
   if (p <= K)
    for (E[p]= y[p], i= p + 1; i <= K; ++i) E[i]= E[i - 1] + y[i];
  }
  for (size_t i= 1; i <= K; ++i) ret.X[i]= ret.X[i - 1] + ret.x[i];
  return ret;
 }
 inline T sum() const { return X[K + 1]; }
 inline T sum(uint64_t n) const { return n > K ? X[K + (double)N / n] : X[n]; }
 inline T operator()(uint64_t n) const { return n > K ? x[K + (double)N / n] : x[n]; }
};
// 1, zeta(s), O(K+L)
template <class T> DirichletSeries<T> get_1(uint64_t N) {
 DirichletSeries<T> ret(N);
 for (size_t i= ret.L; i; --i) ret.X[ret.K + i]= uint64_t((double)N / i);
 return std::fill(std::begin(ret.x) + 1, std::end(ret.x), T(1)), std::iota(std::begin(ret.X), std::begin(ret.X) + ret.K + 1, 0), ret;
}
// Mobius, 1/zeta(s), O(N^(2/3)log^(1/3)N))
template <class T> DirichletSeries<T> get_mu(uint64_t N) { return DirichletSeries<T>(N, true)/= get_1<T>(N); }
// n, zeta(s-1)
template <class T> DirichletSeries<T> get_Id(uint64_t N) {
 DirichletSeries<T> ret(N);
 __uint128_t a;
 for (size_t l= ret.L; l; --l) a= (double)N / l, ret.X[ret.K + l]= (a * (a + 1)) >> 1;
 std::iota(std::begin(ret.x), std::end(ret.x), 0);
 for (size_t i= 1; i <= ret.K; ++i) ret.X[i]= ret.X[i - 1] + ret.x[i];
 return ret;
}
// n^2, zeta(s-2), O(K+L)
template <class T> DirichletSeries<T> get_Id2(uint64_t N) {
 DirichletSeries<T> ret(N);
 __uint128_t a, b, c;
 for (size_t l= ret.L; l; --l) a= (double)N / l, b= (a * (a + 1)) >> 1, c= (a + a + 1), ret.X[ret.K + l]= c % 3 == 0 ? T(c / 3) * b : T(b / 3) * c;
 for (uint64_t i= ret.K; i; --i) ret.x[i]= i * i;
 for (size_t i= 1; i <= ret.K; ++i) ret.X[i]= ret.X[i - 1] + ret.x[i];
 return ret;
}
// number-of-divisors, zeta(s)zeta(s-1), O(N^(2/3)log^(1/3)N))
template <class T> DirichletSeries<T> get_d(uint64_t N) { return get_1<T>(N).square(); }
// sum-of-divisors, zeta(s)zeta(s-2), function, O(N^(2/3)log^(1/3)N))
template <class T> DirichletSeries<T> get_sigma(uint64_t N) { return get_1<T>(N) * get_Id<T>(N); }
// Euler's totient, zeta(s-1)/zeta(s), O(N^(2/3)log^(1/3)N))
template <class T> DirichletSeries<T> get_phi(uint64_t N) { return get_Id<T>(N)/= get_1<T>(N); }
template <class T>  // zeta(2s), O(K+L)
DirichletSeries<T> get_1sq(uint64_t N) {
 DirichletSeries<T> ret(N);
 for (size_t i= 1, e= ret.x.size(); i * i <= e; ++i) ret.x[i * i]= 1;
 for (size_t i= 1; i <= ret.K; ++i) ret.X[i]= ret.X[i - 1] + ret.x[i];
 for (size_t l= ret.L; l; --l) ret.X[ret.K + l]= uint64_t(std::sqrt((double)N / l));
 return ret;
}
// Liouville, zeta(2s)/zeta(s), O(N^(2/3)log^(1/3)N))
template <class T> DirichletSeries<T> get_lambda(uint64_t N) { return get_1sq<T>(N)/= get_1<T>(N); }
// square-free, zeta(s)/zeta(2s), O(N^(2/3)log^(1/3)N))
template <class T> DirichletSeries<T> get_absmu(uint64_t N) { return get_1<T>(N)/= get_1sq<T>(N); }
#line 9 "test/loj/6686.Dirich.test.cpp"
using namespace std;
template <class Tp> void read(Tp &x) {
 static char ch;
 static bool neg;
 for (ch= neg= 0; ch < '0' || ch > '9'; neg|= (ch == '-'), ch= getchar());
 for (x= 0; ch >= '0' && ch <= '9'; (x*= 10)+= (ch ^ 48), ch= getchar());
 neg && (x= -x);
}
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 using Mint= ModInt<998244353>;
 using u128= __uint128_t;
 using u64= uint64_t;
 u128 n= 0;
 read(n);
 u64 l= 0, h= 1e10 + 10;
 while (h - l > 1) {
  u64 x= (h + l) / 2;
  u128 t= u128(x) * x * x;
  if (t <= n) l= x;
  else h= x;
 }
 u64 r= l - 1;
 u128 m= u128(l) * l * l;
 Mint ans= 0;
 ArrayOnDivisors<u64, u64> totient(l);
 totient.set_totient();
 for (auto [d, phi]: totient) ans+= Mint(n / d - (m - 1) / d) * phi;
 auto zeta= get_1<Mint>(r), id= get_Id<Mint>(r), id2= get_Id2<Mint>(r);
 ans+= (id2.square() / id).sum() * 3;
 ans+= (id.square() / zeta).sum() * 3;
 ans+= id.sum();
 cout << ans << '\n';
 return 0;
}

Test cases

Env Name Status Elapsed Memory
g++-13 gcd1 :heavy_check_mark: AC 5 ms 4 MB
g++-13 gcd10 :heavy_check_mark: AC 6 ms 4 MB
g++-13 gcd11 :heavy_check_mark: AC 10 ms 4 MB
g++-13 gcd12 :heavy_check_mark: AC 10 ms 4 MB
g++-13 gcd13 :heavy_check_mark: AC 10 ms 4 MB
g++-13 gcd14 :heavy_check_mark: AC 10 ms 4 MB
g++-13 gcd15 :heavy_check_mark: AC 11 ms 4 MB
g++-13 gcd16 :heavy_check_mark: AC 551 ms 25 MB
g++-13 gcd17 :heavy_check_mark: AC 553 ms 25 MB
g++-13 gcd18 :heavy_check_mark: AC 548 ms 25 MB
g++-13 gcd19 :heavy_check_mark: AC 550 ms 25 MB
g++-13 gcd2 :heavy_check_mark: AC 6 ms 4 MB
g++-13 gcd20 :heavy_check_mark: AC 413 ms 20 MB
g++-13 gcd3 :heavy_check_mark: AC 7 ms 4 MB
g++-13 gcd4 :heavy_check_mark: AC 23 ms 4 MB
g++-13 gcd5 :heavy_check_mark: AC 6 ms 4 MB
g++-13 gcd6 :heavy_check_mark: AC 7 ms 4 MB
g++-13 gcd7 :heavy_check_mark: AC 8 ms 4 MB
g++-13 gcd8 :heavy_check_mark: AC 6 ms 4 MB
g++-13 gcd9 :heavy_check_mark: AC 6 ms 4 MB
clang++-18 gcd1 :heavy_check_mark: AC 5 ms 4 MB
clang++-18 gcd10 :heavy_check_mark: AC 6 ms 4 MB
clang++-18 gcd11 :heavy_check_mark: AC 12 ms 4 MB
clang++-18 gcd12 :heavy_check_mark: AC 11 ms 4 MB
clang++-18 gcd13 :heavy_check_mark: AC 12 ms 4 MB
clang++-18 gcd14 :heavy_check_mark: AC 12 ms 4 MB
clang++-18 gcd15 :heavy_check_mark: AC 11 ms 4 MB
clang++-18 gcd16 :heavy_check_mark: AC 751 ms 25 MB
clang++-18 gcd17 :heavy_check_mark: AC 747 ms 25 MB
clang++-18 gcd18 :heavy_check_mark: AC 749 ms 25 MB
clang++-18 gcd19 :heavy_check_mark: AC 745 ms 25 MB
clang++-18 gcd2 :heavy_check_mark: AC 6 ms 4 MB
clang++-18 gcd20 :heavy_check_mark: AC 590 ms 20 MB
clang++-18 gcd3 :heavy_check_mark: AC 6 ms 4 MB
clang++-18 gcd4 :heavy_check_mark: AC 6 ms 4 MB
clang++-18 gcd5 :heavy_check_mark: AC 38 ms 4 MB
clang++-18 gcd6 :heavy_check_mark: AC 9 ms 4 MB
clang++-18 gcd7 :heavy_check_mark: AC 9 ms 4 MB
clang++-18 gcd8 :heavy_check_mark: AC 60 ms 4 MB
clang++-18 gcd9 :heavy_check_mark: AC 9 ms 4 MB
Back to top page