cpp-library

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

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: nt/convolve.hpp

Depends on

Verified with

Code

#pragma once
#include "types.hpp"
#include "nt/primes.hpp"

// deg defaults to a_last - a
template <class It1, class It2>
auto convolve(It1 a, It1 a_last, It2 b, It2 b_last, int deg = -1) {
  int n = a_last - a, m = b_last - b;
  if (deg == -1) deg = n;
  vector<decltype(a[0] * b[0])> res(deg);
  rep2(i, 1, min(n, deg)) rep2(j, 1, m) {
    if (i * j >= deg) break;
    res[i * j] += a[i] * b[j];
  }
  return res;
}

// deg defaults to a_last - a
// a[1] = 1, a[nm] = a[n] * a[m] if gcd(n, m) == 1
template <class It1, class It2>
auto convolve_multiplicative(It1 a, It1 a_last, It2 b, It2 b_last,
                             int deg = -1) {
  int n = a_last - a, m = b_last - b;
  if (deg == -1) deg = n;
  vector<decltype(a[0] * b[0])> res(deg);
  copy(b, b + min(m, deg), res.begin());
  for (auto p : primes(deg)) {
    repr2(i, 1, (deg - 1) / p + 1) {
      int n = p * i;
      for (int q = p, m = i; ; q *= p, m /= p) {
        res[n] += a[q] * res[m];
        if (m % p != 0) break;
      }
    }
  }
  return res;
}
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "types.hpp"

template <class It>
using val_t = typename iterator_traits<It>::value_type;
#line 3 "arith/sat.hpp"

template <class T, class U>
auto sat_add(T a, U b) {
  using V = common_type_t<T, U>;
  V res;
  return __builtin_add_overflow((V)a, (V)b, &res)
             ? (a < 0 ? numeric_limits<V>::min() : numeric_limits<V>::max())
             : res;
}
template <class T, class U>
auto sat_sub(T a, U b) {
  using V = common_type_t<T, U>;
  V res;
  return __builtin_sub_overflow((V)a, (V)b, &res)
             ? (a < 0 ? numeric_limits<V>::min() : numeric_limits<V>::max())
             : res;
}
template <class T, class U>
auto sat_mul(T a, U b) {
  using V = common_type_t<T, U>;
  V res;
  return __builtin_mul_overflow((V)a, (V)b, &res)
             ? ((a < 0) == (b < 0) ? numeric_limits<V>::max()
                                   : numeric_limits<V>::min())
             : res;
}
#line 4 "nt/primes.hpp"

vector<bool> sieve(int upto) {
  vector<bool> prime(upto + 1, true);
  prime[0] = prime[1] = false;
  for (int p = 2; p * p <= upto; p++)
    if (prime[p])
      for (int q = p * p; q <= upto; q += p) prime[q] = false;
  return prime;
}

vector<int> primes(int upto) {
  vector<int> ans;
  vector<bool> sieved = sieve(upto);
  for (int n = 2; n <= upto; n++)
    if (sieved[n]) ans.push_back(n);
  return ans;
}

bool is_prime(int n) {
  if (n == 2) return true;
  if (n % 2 == 0) return false;
  for (int d = 3; sat_mul(d, d) <= n; d += 2)
    if (n % d == 0) return false;
  return true;
}
#line 4 "nt/convolve.hpp"

// deg defaults to a_last - a
template <class It1, class It2>
auto convolve(It1 a, It1 a_last, It2 b, It2 b_last, int deg = -1) {
  int n = a_last - a, m = b_last - b;
  if (deg == -1) deg = n;
  vector<decltype(a[0] * b[0])> res(deg);
  rep2(i, 1, min(n, deg)) rep2(j, 1, m) {
    if (i * j >= deg) break;
    res[i * j] += a[i] * b[j];
  }
  return res;
}

// deg defaults to a_last - a
// a[1] = 1, a[nm] = a[n] * a[m] if gcd(n, m) == 1
template <class It1, class It2>
auto convolve_multiplicative(It1 a, It1 a_last, It2 b, It2 b_last,
                             int deg = -1) {
  int n = a_last - a, m = b_last - b;
  if (deg == -1) deg = n;
  vector<decltype(a[0] * b[0])> res(deg);
  copy(b, b + min(m, deg), res.begin());
  for (auto p : primes(deg)) {
    repr2(i, 1, (deg - 1) / p + 1) {
      int n = p * i;
      for (int q = p, m = i; ; q *= p, m /= p) {
        res[n] += a[q] * res[m];
        if (m % p != 0) break;
      }
    }
  }
  return res;
}
Back to top page