Tifa's CP Library

:heavy_check_mark: conv_lcm (src/code/conv/conv_lcm.hpp)

Depends on

Verified with

Code

#ifndef TIFALIBS_CONV_CONV_LCM
#define TIFALIBS_CONV_CONV_LCM

#include "zmt_divisor.hpp"

namespace tifa_libs::math {

template <class T>
CEXP vec<T> conv_lcm(vec<T> l, vec<T> r) {
  assert(l.size() == r.size());
  const auto pf = prime_seq((u32)l.size() - 1);
  zt_divisor(l, pf), zt_divisor(r, pf);
  flt_ (u32, i, 0, (u32)l.size()) l[i] *= r[i];
  mt_divisor(l, pf);
  return l;
}

}  // namespace tifa_libs::math

#endif
#line 1 "src/code/conv/conv_lcm.hpp"



#line 1 "src/code/conv/zmt_divisor.hpp"



#line 1 "src/code/nt/prime_seq.hpp"



#line 1 "src/code/math/isqrt.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/extc++.h>

#define CEXP constexpr
#define CEXPE constexpr explicit
#define TPN typename
#define CR const&

#define cT_(...) std::conditional_t<sizeof(__VA_ARGS__) <= sizeof(size_t), __VA_ARGS__, __VA_ARGS__ CR>
#define fle_(T, i, l, r, ...) for (T i = (l), i##e = (r)__VA_OPT__(, ) __VA_ARGS__; i <= i##e; ++i)
#define flt_(T, i, l, r, ...) for (T i = (l), i##e = (r)__VA_OPT__(, ) __VA_ARGS__; i < i##e; ++i)

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;
using f32 = float;
using f64 = double;
using f128 = long double;
using strn = std::string;
using strnv = std::string_view;

// clang-format off
template <class T, T v> using ic = std::integral_constant<T, v>;
template <class T> using ptt = std::pair<T, T>;
template <class T> struct edge_t {
  T w; u32 u, v;
  CEXP auto operator<=>(edge_t CR) const = default;
};
template <class T> struct pt3 {
  T _0, _1, _2;
  CEXP auto operator<=>(pt3 CR) const = default;
};
template <class T> struct pt4 {
  T _0, _1, _2, _3;
  CEXP auto operator<=>(pt4 CR) const = default;
};
template <class E> using itl = std::initializer_list<E>;
template <class T> using vec = std::vector<T>;
template <class T> using vvec = vec<vec<T>>;
template <class T> using v3ec = vec<vvec<T>>;
template <class T> using vecpt = vec<ptt<T>>;
template <class T> using vvecpt = vvec<ptt<T>>;
template <class T> using ptvec = ptt<vec<T>>;
template <class T> using ptvvec = ptt<vvec<T>>;

template <class T, usz ext = std::dynamic_extent> using spn = std::span<T const, ext>;
template <class T, usz N> using arr = std::array<T, N>;
template <class U, class T> using vecp = vec<std::pair<U, T>>;
template <class U, class T> using vvecp = vvec<std::pair<U, T>>;

template <class T, class C = std::less<T>> using pq = std::priority_queue<T, vec<T>, C>;
template <class T> using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;
// clang-format on

#define mk_(V, A, T) using V##A = V<T>;
#define mk(A, T) mk_(edge_t, A, T) mk_(ptt, A, T) mk_(pt3, A, T) mk_(pt4, A, T) mk_(vec, A, T) mk_(vvec, A, T) mk_(v3ec, A, T) mk_(vecpt, A, T) mk_(vvecpt, A, T) mk_(ptvec, A, T) mk_(ptvvec, A, T) mk_(spn, A, T) mk_(itl, A, T)
mk(b, bool) mk(i, i32) mk(u, u32) mk(ii, i64) mk(uu, u64);
#undef mk
#undef mk_

using namespace std::literals;
CEXP i8 operator""_i8(unsigned long long x) { return (i8)x; }
CEXP i16 operator""_i16(unsigned long long x) { return (i16)x; }
CEXP i32 operator""_i32(unsigned long long x) { return (i32)x; }
CEXP i64 operator""_i64(unsigned long long x) { return (i64)x; }
CEXP isz operator""_iz(unsigned long long x) { return (isz)x; }
CEXP u8 operator""_u8(unsigned long long x) { return (u8)x; }
CEXP u16 operator""_u16(unsigned long long x) { return (u16)x; }
CEXP u32 operator""_u32(unsigned long long x) { return (u32)x; }
CEXP u64 operator""_u64(unsigned long long x) { return (u64)x; }
CEXP usz operator""_uz(unsigned long long x) { return (usz)x; }

using std::numbers::pi_v;
template <std::floating_point FP>
inline FP eps_v = std::sqrt(std::numeric_limits<FP>::epsilon());
template <std::floating_point FP>
CEXP void set_eps(FP v) { eps_v<FP> = v; }

inline const auto fn_0 = [](auto&&...) {};
inline const auto fn_is0 = [](auto x) { return x == 0; };

namespace tifa_libs {
using std::min, std::max, std::swap;
template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }
}  // namespace tifa_libs


#line 5 "src/code/math/isqrt.hpp"

namespace tifa_libs::math {

CEXP u32 isqrt(u64 x) {
  if (!x) return 0;
  const int sh = 31 - i32(std::bit_width(x) - 1) / 2;
  u32 u = [](u64 x) {
    CEXP u8 TAB[192] = {128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 144, 145, 146, 147, 148, 149, 150, 151, 151, 152, 153, 154, 155, 156, 156, 157, 158, 159, 160, 160, 161, 162, 163, 164, 164, 165, 166, 167, 167, 168, 169, 170, 170, 171, 172, 173, 173, 174, 175, 176, 176, 177, 178, 179, 179, 180, 181, 181, 182, 183, 183, 184, 185, 186, 186, 187, 188, 188, 189, 190, 190, 191, 192, 192, 193, 194, 194, 195, 196, 196, 197, 198, 198, 199, 200, 200, 201, 201, 202, 203, 203, 204, 205, 205, 206, 206, 207, 208, 208, 209, 210, 210, 211, 211, 212, 213, 213, 214, 214, 215, 216, 216, 217, 217, 218, 219, 219, 220, 220, 221, 221, 222, 223, 223, 224, 224, 225, 225, 226, 227, 227, 228, 228, 229, 229, 230, 230, 231, 232, 232, 233, 233, 234, 234, 235, 235, 236, 237, 237, 238, 238, 239, 239, 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245, 246, 246, 247, 247, 248, 248, 249, 249, 250, 250, 251, 251, 252, 252, 253, 253, 254, 254, 255, 255, 255};
    u32 u = TAB[(x >> 56) - 64];
    return u = (u << 7) + (u32)(x >> 41) / u, (u << 15) + (u32)((x >> 17) / u);
  }(x << 2 * sh);
  return u >>= sh, u -= (u64)u * u > x, u;
}

}  // namespace tifa_libs::math


#line 5 "src/code/nt/prime_seq.hpp"

namespace tifa_libs::math {

CEXP vecu prime_seq(u32 n) {
  vecb sieve(n / 3 + 1, 1);
  for (u32 p = 5, d = 4, i = 1, sqn = isqrt(n); p <= sqn; p += d = 6 - d, ++i) {
    if (!sieve[i]) continue;
    for (u64 q = p * p / 3, r = d * p / 3 + (d * p % 3 == 2), s = 2 * p, qe = sieve.size(); q < qe; q += r = s - r) sieve[q] = 0;
  }
  vecu ret{2, 3};
  for (u32 p = 5, d = 4, i = 1; p <= n; p += d = 6 - d, ++i)
    if (sieve[i]) ret.push_back(p);
  while (!ret.empty() && ret.back() > n) ret.pop_back();
  return ret;
}

}  // namespace tifa_libs::math


#line 5 "src/code/conv/zmt_divisor.hpp"

namespace tifa_libs::math {

template <class T>
CEXP void zt_divisor(vec<T>& a, spnu pf) {
  for (u32 p : pf)
    for (u64 k = 1; k * p < a.size(); ++k) a[k * p] += a[k];
}
template <class T>
CEXP void zt_divisor(vec<T>& a) { zt_divisor(a, prime_seq(a.size() - 1)); }
template <class T>
CEXP void mt_divisor(vec<T>& a, spnu pf) {
  for (u32 p : pf)
    for (u64 k = (a.size() - 1) / p; k; --k) a[k * p] -= a[k];
}
template <class T>
CEXP void mt_divisor(vec<T>& a) { mt_divisor(a, prime_seq(a.size() - 1)); }

}  // namespace tifa_libs::math


#line 5 "src/code/conv/conv_lcm.hpp"

namespace tifa_libs::math {

template <class T>
CEXP vec<T> conv_lcm(vec<T> l, vec<T> r) {
  assert(l.size() == r.size());
  const auto pf = prime_seq((u32)l.size() - 1);
  zt_divisor(l, pf), zt_divisor(r, pf);
  flt_ (u32, i, 0, (u32)l.size()) l[i] *= r[i];
  mt_divisor(l, pf);
  return l;
}

}  // namespace tifa_libs::math


Back to top page