CP-templates

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

View the Project on GitHub Misuki743/CP-templates

:heavy_check_mark: test/gcd_convolution.test.cpp

Depends on

Code

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

#include "../default/t.cpp"
#include "../modint/MontgomeryModInt.cpp"
#include "../numtheory/linear_sieve.cpp"
#include "../numtheory/zeta_mobius_on_divisibility_lattice.cpp"
#include "../numtheory/gcd_convolution.cpp"

signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  linear_sieve<1'000'001> ls;

  int n; cin >> n;
  vector<mint> a(n), b(n);
  for(mint &x : a) cin >> x;
  for(mint &x : b) cin >> x;
  a.insert(a.begin(), mint(0));
  b.insert(b.begin(), mint(0));
  auto c = gcd_convolution(ls, a, b);
  c.erase(c.begin());
  cout << c << '\n';

  return 0;
}
#line 1 "test/gcd_convolution.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/gcd_convolution"

#line 1 "default/t.cpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>

#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

#define pb push_back
#define eb emplace_back
#define clock chrono::steady_clock::now().time_since_epoch().count()

using namespace std;

template<size_t I = 0, typename... args>
ostream& print_tuple(ostream& os, const tuple<args...> tu) {
  os << get<I>(tu);
  if constexpr (I + 1 != sizeof...(args)) {
    os << ' ';
    print_tuple<I + 1>(os, tu);
  }
  return os;
}
template<typename... args>
ostream& operator<<(ostream& os, const tuple<args...> tu) {
  return print_tuple(os, tu);
}
template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2> pr) {
  return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(size_t i = 0; T x : arr) {
    os << x;
    if (++i != N) os << ' ';
  }
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(size_t i = 0; T x : vec) {
    os << x;
    if (++i != size(vec)) os << ' ';
  }
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const set<T> &s) {
  for(size_t i = 0; T x : s) {
    os << x;
    if (++i != size(s)) os << ' ';
  }
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const multiset<T> &s) {
  for(size_t i = 0; T x : s) {
    os << x;
    if (++i != size(s)) os << ' ';
  }
  return os;
}
template<class T1, class T2>
ostream& operator<<(ostream& os, const map<T1, T2> &m) {
  for(size_t i = 0; pair<T1, T2> x : m) {
    os << x.first << " : " << x.second;
    if (++i != size(m)) os << ", ";
  }
  return os;
}

#ifdef DEBUG
#define dbg(...) cerr << '(', _do(#__VA_ARGS__), cerr << ") = ", _do2(__VA_ARGS__)
template<typename T> void _do(T &&x) { cerr << x; }
template<typename T, typename ...S> void _do(T &&x, S&&...y) { cerr << x << ", "; _do(y...); }
template<typename T> void _do2(T &&x) { cerr << x << endl; }
template<typename T, typename ...S> void _do2(T &&x, S&&...y) { cerr << x << ", "; _do2(y...); }
#else
#define dbg(...)
#endif

using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<typename T> using vc = vector<T>;
template<typename T> using vvc = vc<vc<T>>;
template<typename T> using vvvc = vc<vvc<T>>;

using vi = vc<int>;
using vll = vc<ll>;
using vvi = vvc<int>;
using vvll = vvc<ll>;

template<typename T> using min_heap = priority_queue<T, vc<T>, greater<T>>;
template<typename T> using max_heap = priority_queue<T>;

template<typename R, typename F, typename... Args>
concept R_invocable = requires(F&& f, Args&&... args) {
  { std::invoke(std::forward<F>(f), std::forward<Args>(args)...) } -> std::same_as<R>;
};
template<ranges::forward_range rng, class T = ranges::range_value_t<rng>, typename F>
requires R_invocable<T, F, T, T>
void pSum(rng &&v, F f) {
  if (!v.empty())
    for(T p = *v.begin(); T &x : v | views::drop(1))
      x = p = f(p, x);
}
template<ranges::forward_range rng, class T = ranges::range_value_t<rng>>
void pSum(rng &&v) {
  if (!v.empty())
    for(T p = *v.begin(); T &x : v | views::drop(1))
      x = p = p + x;
}

template<ranges::forward_range rng>
void Unique(rng &v) {
  ranges::sort(v);
  v.resize(unique(v.begin(), v.end()) - v.begin());
}

template<ranges::random_access_range rng>
rng invPerm(rng p) {
  rng ret = p;
  for(int i = 0; i < ssize(p); i++)
    ret[p[i]] = i;
  return ret;
}

template<ranges::random_access_range rng>
vi argSort(rng p) {
  vi id(size(p));
  iota(id.begin(), id.end(), 0);
  ranges::sort(id, {}, [&](int i) { return pair(p[i], i); });
  return id;
}

template<ranges::random_access_range rng, class T = ranges::range_value_t<rng>, typename F>
requires invocable<F, T&>
vi argSort(rng p, F proj) {
  vi id(size(p));
  iota(id.begin(), id.end(), 0);
  ranges::sort(id, {}, [&](int i) { return pair(proj(p[i]), i); });
  return id;
}

template<bool directed>
vvi read_graph(int n, int m, int base) {
  vvi g(n);
  for(int i = 0; i < m; i++) {
    int u, v; cin >> u >> v;
    u -= base, v -= base;
    g[u].emplace_back(v);
    if constexpr (!directed)
      g[v].emplace_back(u);
  }
  return g;
}

template<bool directed>
vvi adjacency_list(int n, vc<pii> e, int base) {
  vvi g(n);
  for(auto [u, v] : e) {
    u -= base, v -= base;
    g[u].emplace_back(v);
    if constexpr (!directed)
      g[v].emplace_back(u);
  }
  return g;
}

template<class T>
void setBit(T &msk, int bit, bool x) { (msk &= ~(T(1) << bit)) |= T(x) << bit; }
template<class T> void onBit(T &msk, int bit) { setBit(msk, bit, true); }
template<class T> void offBit(T &msk, int bit) { setBit(msk, bit, false); }
template<class T> void flipBit(T &msk, int bit) { msk ^= T(1) << bit; }
template<class T> bool getBit(T msk, int bit) { return msk >> bit & T(1); }

template<class T>
T floorDiv(T a, T b) {
  if (b < 0) a *= -1, b *= -1;
  return a >= 0 ? a / b : (a - b + 1) / b;
}
template<class T>
T ceilDiv(T a, T b) {
  if (b < 0) a *= -1, b *= -1;
  return a >= 0 ? (a + b - 1) / b : a / b;
}

template<class T> bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T> bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; }

#line 1 "modint/MontgomeryModInt.cpp"
//reference: https://github.com/NyaanNyaan/library/blob/master/modint/montgomery-modint.hpp#L10
//note: mod should be an odd prime less than 2^30.

template<uint32_t mod>
struct MontgomeryModInt {
  using mint = MontgomeryModInt;
  using i32 = int32_t;
  using u32 = uint32_t;
  using u64 = uint64_t;

  static constexpr u32 get_r() {
    u32 res = 1, base = mod;
    for(i32 i = 0; i < 31; i++)
      res *= base, base *= base;
    return -res;
  }

  static constexpr u32 get_mod() {
    return mod;
  }

  static constexpr u32 n2 = -u64(mod) % mod; //2^64 % mod
  static constexpr u32 r = get_r(); //-P^{-1} % 2^32

  u32 a;

  static u32 reduce(const u64 &b) {
    return (b + u64(u32(b) * r) * mod) >> 32;
  }

  static u32 transform(const u64 &b) {
    return reduce(u64(b) * n2);
  }

  MontgomeryModInt() : a(0) {}
  MontgomeryModInt(const int64_t &b) 
    : a(transform(b % mod + mod)) {}

  mint pow(u64 k) const {
    mint res(1), base(*this);
    while(k) {
      if (k & 1) 
        res *= base;
      base *= base, k >>= 1;
    }
    return res;
  }

  mint inverse() const { return (*this).pow(mod - 2); }

  u32 get() const {
    u32 res = reduce(a);
    return res >= mod ? res - mod : res;
  }

  mint& operator+=(const mint &b) {
    if (i32(a += b.a - 2 * mod) < 0) a += 2 * mod;
    return *this;
  }

  mint& operator-=(const mint &b) {
    if (i32(a -= b.a) < 0) a += 2 * mod;
    return *this;
  }

  mint& operator*=(const mint &b) {
    a = reduce(u64(a) * b.a);
    return *this;
  }

  mint& operator/=(const mint &b) {
    a = reduce(u64(a) * b.inverse().a);
    return *this;
  }

  mint operator-() { return mint() - mint(*this); }
  bool operator==(mint b) const {
    return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);
  }
  bool operator!=(mint b) const {
    return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);
  }

  friend mint operator+(mint c, mint d) { return c += d; }
  friend mint operator-(mint c, mint d) { return c -= d; }
  friend mint operator*(mint c, mint d) { return c *= d; }
  friend mint operator/(mint c, mint d) { return c /= d; }

  friend ostream& operator<<(ostream& os, const mint& b) {
    return os << b.get();
  }
  friend istream& operator>>(istream& is, mint& b) {
    int64_t val;
    is >> val;
    b = mint(val);
    return is;
  }
};

using mint = MontgomeryModInt<998244353>;
#line 1 "numtheory/linear_sieve.cpp"
template<int32_t C>
struct linear_sieve {
  array<int, C> mpf = {};
  vector<int> prime;
  linear_sieve() {
    if (C > 2)
      iota(mpf.begin() + 2, mpf.end(), 2);
    for(int i = 2; i < C; i++) {
      if (mpf[i] == i)
        prime.emplace_back(i);
      for(int64_t p : prime) {
        if (p > mpf[i] or p * i >= C)
          break;
        mpf[p * i] = p;
      }
    }
  }

  vector<pair<int, int>> prime_factorize(int x) {
    vector<pair<int, int>> r;
    while(mpf[x]) {
      r.emplace_back(mpf[x], 0);
      while(x % r.back().first == 0)
        x /= r.back().first, r.back().second++;
    }
    return r;
  }

  vector<int> prime_factor_enumerate(int x) {
    vector<int> r;
    while(mpf[x]) {
      r.emplace_back(mpf[x]);
      while(x % r.back() == 0)
        x /= r.back();
    }
    return r;
  }

  vector<int> divisor_enumerate(int x, bool sorted = true) {
    vector<int> divisor = {1};
    for(auto [p, f] : prime_factorize(x)) {
      vector<int> nxt;
      nxt.reserve(ssize(divisor) * (f + 1));
      for(int64_t i = 0, q = 1; i <= f; i++, q *= p)
        for(int d : divisor)
          nxt.emplace_back(d * q);
      divisor.swap(nxt);
    }
    if (sorted)
      ranges::sort(divisor);
    return divisor;
  }
};
#line 1 "numtheory/zeta_mobius_on_divisibility_lattice.cpp"
//#include "numtheory/linear_sieve"

template<class T, int32_t C>
vector<T> zeta_transform_on_divisor(linear_sieve<C> &ls, vector<T> f) {
  assert(ssize(f) <= C);
  for(int64_t p : ls.prime) {
    if (p >= ssize(f)) break;
    for(int i = 1; i * p < ssize(f); i++)
      f[i * p] += f[i];
  }
  return f;
}

template<class T, int32_t C>
vector<T> mobius_transform_on_divisor(linear_sieve<C> &ls, vector<T> f) {
  assert(ssize(f) <= C);
  for(int64_t p : ls.prime) {
    if (p >= ssize(f)) break;
    for(int i = (ssize(f) - 1) / p; i > 0; i--)
      f[i * p] -= f[i];
  }
  return f;
}

template<class T, int32_t C>
vector<T> zeta_transform_on_multiple(linear_sieve<C> &ls, vector<T> f) {
  assert(ssize(f) <= C);
  for(int64_t p : ls.prime) {
    if (p >= ssize(f)) break;
    for(int i = (ssize(f) - 1) / p; i > 0; i--)
      f[i] += f[i * p];
  }
  return f;
}

template<class T, int32_t C>
vector<T> mobius_transform_on_multiple(linear_sieve<C> &ls, vector<T> f) {
  assert(ssize(f) <= C);
  for(int64_t p : ls.prime) {
    if (p >= ssize(f)) break;
    for(int i = 1; i * p < ssize(f); i++)
      f[i] -= f[i * p];
  }
  return f;
}
#line 1 "numtheory/gcd_convolution.cpp"
//#include "numtheory/linear_sieve.cpp"
//#include "numtheory/zeta_mobius_on_divisibility_lattice.cpp"

template<class T, int32_t C>
vector<T> gcd_convolution(linear_sieve<C> &ls, vector<T> a, vector<T> b) {
  assert(ssize(a) == ssize(b));
  a = zeta_transform_on_multiple(ls, a);
  b = zeta_transform_on_multiple(ls, b);
  for(int i = 0; i < ssize(a); i++)
    a[i] *= b[i];
  return mobius_transform_on_multiple(ls, a);
}
#line 8 "test/gcd_convolution.test.cpp"

signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  linear_sieve<1'000'001> ls;

  int n; cin >> n;
  vector<mint> a(n), b(n);
  for(mint &x : a) cin >> x;
  for(mint &x : b) cin >> x;
  a.insert(a.begin(), mint(0));
  b.insert(b.begin(), mint(0));
  auto c = gcd_convolution(ls, a, b);
  c.erase(c.begin());
  cout << c << '\n';

  return 0;
}
Back to top page