proconlib

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

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: In-place multiple Möbius transform (tools/multiple_moebius_inplace.hpp)

template <std::ranges::random_access_range R>
requires std::ranges::output_range<R, std::ranges::range_value_t<R>>
void multiple_moebius_inplace(R&& b);

template <tools::commutative_group G, std::ranges::random_access_range R>
requires std::ranges::output_range<R, std::ranges::range_value_t<R>>
void multiple_moebius_inplace(R&& b);

Assume that the following relationship holds between $a = (a_0, a_1, \ldots, a_{N - 1})$ and $b = (b_0, b_1, \ldots, b_{N - 1})$.

\[\begin{align*} b_i &= \left\{\begin{array}{ll} a_0 & (i = 0)\\ \displaystyle \sum_{\substack{1 \leq j < N \\ i \mid j}} a_j & (i > 0) \end{array}\right. \end{align*}\]

Given $b$, it updates $b$ to $a$.

Constraints

Time Complexity

License

Author

Depends on

Required by

Verified with

Code

#ifndef TOOLS_MULTIPLE_MOEBIUS_INPLACE_HPP
#define TOOLS_MULTIPLE_MOEBIUS_INPLACE_HPP

#include <iterator>
#include <ranges>
#include <utility>
#include "tools/commutative_group.hpp"
#include "tools/eratosthenes_sieve.hpp"
#include "tools/groups.hpp"

namespace tools {
  template <tools::commutative_group G, std::ranges::random_access_range R>
  requires std::ranges::output_range<R, std::ranges::range_value_t<R>>
  void multiple_moebius_inplace(R&& b) {
    const int N = std::ranges::distance(b);
    if (N < 2) return;

    tools::eratosthenes_sieve<int> sieve(N - 1);
    for (const auto p : sieve.prime_range(1, N - 1)) {
      for (int i = 1; i * p < N; ++i) {
        std::ranges::begin(b)[i] = G::op(std::ranges::begin(b)[i], G::inv(std::ranges::begin(b)[i * p]));
      }
    }
  }

  template <std::ranges::random_access_range R>
  requires std::ranges::output_range<R, std::ranges::range_value_t<R>>
  void multiple_moebius_inplace(R&& b) {
    tools::multiple_moebius_inplace<tools::groups::plus<std::ranges::range_value_t<R>>>(std::forward<R>(b));
  }
}

#endif
#line 1 "tools/multiple_moebius_inplace.hpp"



#include <iterator>
#include <ranges>
#include <utility>
#line 1 "tools/commutative_group.hpp"



#line 1 "tools/commutative_monoid.hpp"



#line 1 "tools/monoid.hpp"



#include <concepts>

namespace tools {
  template <typename M>
  concept monoid = requires(typename M::T x, typename M::T y) {
    { M::op(x, y) } -> std::same_as<typename M::T>;
    { M::e() } -> std::same_as<typename M::T>;
  };
}


#line 5 "tools/commutative_monoid.hpp"

namespace tools {
  template <typename M>
  concept commutative_monoid = tools::monoid<M>;
}


#line 1 "tools/group.hpp"



#line 6 "tools/group.hpp"

namespace tools {
  template <typename G>
  concept group = tools::monoid<G> && requires(typename G::T x) {
    { G::inv(x) } -> std::same_as<typename G::T>;
  };
}


#line 6 "tools/commutative_group.hpp"

namespace tools {
  template <typename G>
  concept commutative_group = tools::group<G> && tools::commutative_monoid<G>;
}


#line 1 "tools/eratosthenes_sieve.hpp"



#include <array>
#include <cstdint>
#include <vector>
#include <cstddef>
#line 9 "tools/eratosthenes_sieve.hpp"
#include <cassert>
#include <algorithm>

namespace tools {
  template <typename T>
  class eratosthenes_sieve {
    constexpr static std::array<std::uint64_t, 15> init = {
      UINT64_C(0b0010100000100010100010100010000010100000100010100010100010000010),
      UINT64_C(0b1000001010000010001010001010001000001010000010001010001010001000),
      UINT64_C(0b1000100000101000001000101000101000100000101000001000101000101000),
      UINT64_C(0b0010100010000010100000100010100010100010000010100000100010100010),
      UINT64_C(0b1010001010001000001010000010001010001010001000001010000010001010),
      UINT64_C(0b1000101000101000100000101000001000101000101000100000101000001000),
      UINT64_C(0b0000100010100010100010000010100000100010100010100010000010100000),
      UINT64_C(0b1010000010001010001010001000001010000010001010001010001000001010),
      UINT64_C(0b0000101000001000101000101000100000101000001000101000101000100000),
      UINT64_C(0b0010000010100000100010100010100010000010100000100010100010100010),
      UINT64_C(0b1010001000001010000010001010001010001000001010000010001010001010),
      UINT64_C(0b1000101000100000101000001000101000101000100000101000001000101000),
      UINT64_C(0b0010100010100010000010100000100010100010100010000010100000100010),
      UINT64_C(0b0010001010001010001000001010000010001010001010001000001010000010),
      UINT64_C(0b1000001000101000101000100000101000001000101000101000100000101000),
    };
    std::vector<std::uint64_t> m_is_prime;
    int m_n;

  public:
    class prime_iterable {
    private:
      tools::eratosthenes_sieve<T> const *m_parent;
      int m_l;
      int m_r;

    public:
      class iterator {
      private:
        tools::eratosthenes_sieve<T> const *m_parent;
        int m_p;

      public:
        using difference_type = std::ptrdiff_t;
        using value_type = T;
        using reference = const T&;
        using pointer = const T*;
        using iterator_category = std::input_iterator_tag;

        iterator() = default;
        iterator(tools::eratosthenes_sieve<T> const * const parent, const int p) : m_parent(parent), m_p(p) {
          for (; this->m_p <= parent->m_n && !parent->is_prime(this->m_p); ++this->m_p);
        }

        value_type operator*() const {
          return this->m_p;
        }
        iterator& operator++() {
          for (++this->m_p; this->m_p <= this->m_parent->m_n && !this->m_parent->is_prime(this->m_p); ++this->m_p);
          return *this;
        }
        iterator operator++(int) {
          const auto self = *this;
          ++*this;
          return self;
        }
        friend bool operator==(const iterator lhs, const iterator rhs) {
          assert(lhs.m_parent == rhs.m_parent);
          return lhs.m_p == rhs.m_p;
        }
        friend bool operator!=(const iterator lhs, const iterator rhs) {
          return !(lhs == rhs);
        }
      };

      prime_iterable() = default;
      prime_iterable(tools::eratosthenes_sieve<T> const * const parent, const int l, const int r) : m_parent(parent), m_l(l), m_r(r) {
      }

      iterator begin() const {
        return iterator(this->m_parent, this->m_l);
      };
      iterator end() const {
        return iterator(this->m_parent, this->m_r + 1);
      }
    };

    eratosthenes_sieve() = default;
    explicit eratosthenes_sieve(const int n) : m_n(n) {
      assert(n >= 1);
      this->m_is_prime.reserve((n >> 6) + 1);
      for (int i = 0; i <= n; i += 960) {
        std::copy(init.begin(), n < i + 959 ? std::next(init.begin(), (n >> 6) % 15 + 1) : init.end(), std::back_inserter(this->m_is_prime));
      }
      this->m_is_prime[0] ^= UINT64_C(0b101110);

      int i = 7;
      while (true) {
        if (n < i * i) break;
        if (this->is_prime(i)) { // 7
          int j = i * i;
          while (true) {
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 7 * 7
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 7 * 11
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 7 * 13
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 7 * 17
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 7 * 19
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 7 * 23
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 7 * 29
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 7 * 1
            j += i * 6;
            if (n < j) break;
          }
        }
        i += 4;
        if (n < i * i) break;
        if (this->is_prime(i)) { // 11
          int j = i * i;
          while (true) {
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 11 * 11
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 11 * 13
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 11 * 17
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 11 * 19
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 11 * 23
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 11 * 29
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 11 * 1
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 11 * 7
            j += i * 4;
            if (n < j) break;
          }
        }
        i += 2;
        if (n < i * i) break;
        if (this->is_prime(i)) { // 13
          int j = i * i;
          while (true) {
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 13 * 13
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 13 * 17
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 13 * 19
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 13 * 23
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 13 * 29
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 13 * 1
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 13 * 7
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 13 * 11
            j += i + i;
            if (n < j) break;
          }
        }
        i += 4;
        if (n < i * i) break;
        if (this->is_prime(i)) { // 17
          int j = i * i;
          while (true) {
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 17 * 17
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 17 * 19
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 17 * 23
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 17 * 29
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 17 * 1
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 17 * 7
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 17 * 11
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 17 * 13
            j += i * 4;
            if (n < j) break;
          }
        }
        i += 2;
        if (n < i * i) break;
        if (this->is_prime(i)) { // 19
          int j = i * i;
          while (true) {
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 19 * 19
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 19 * 23
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 19 * 29
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 19 * 1
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 19 * 7
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 19 * 11
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 19 * 13
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 19 * 17
            j += i + i;
            if (n < j) break;
          }
        }
        i += 4;
        if (n < i * i) break;
        if (this->is_prime(i)) { // 23
          int j = i * i;
          while (true) {
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 23 * 23
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 23 * 29
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 23 * 1
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 23 * 7
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 23 * 11
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 23 * 13
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 23 * 17
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 23 * 19
            j += i * 4;
            if (n < j) break;
          }
        }
        i += 6;
        if (n < i * i) break;
        if (this->is_prime(i)) { // 29
          int j = i * i;
          while (true) {
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 29 * 29
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 29 * 1
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 29 * 7
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 29 * 11
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 29 * 13
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 29 * 17
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 29 * 19
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 29 * 23
            j += i * 6;
            if (n < j) break;
          }
        }
        i += 2;
        if (n < i * i) break;
        if (this->is_prime(i)) { // 1
          int j = i * i;
          while (true) {
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 1 * 1
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 1 * 7
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 1 * 11
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 1 * 13
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 1 * 17
            j += i + i;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 1 * 19
            j += i * 4;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 1 * 23
            j += i * 6;
            if (n < j) break;
            this->m_is_prime[j >> 6] &= ~(UINT64_C(1) << (j & 0b111111)); // 1 * 29
            j += i + i;
            if (n < j) break;
          }
        }
        i += 6;
      }
    }

    inline bool is_prime(const int i) const {
      assert(1 <= i && i <= this->m_n);
      return (this->m_is_prime[i >> 6] >> (i & 0b111111)) & 1;
    }

    prime_iterable prime_range(const int l, const int r) const {
      assert(1 <= l && l <= r && r <= this->m_n);
      return prime_iterable(this, l, r);
    }
  };
}


#line 1 "tools/groups.hpp"



#line 5 "tools/groups.hpp"
#include <type_traits>
#line 1 "tools/arithmetic.hpp"



#line 1 "tools/integral.hpp"



#line 1 "tools/is_integral.hpp"



#line 5 "tools/is_integral.hpp"

namespace tools {
  template <typename T>
  struct is_integral : std::is_integral<T> {};

  template <typename T>
  inline constexpr bool is_integral_v = tools::is_integral<T>::value;
}


#line 5 "tools/integral.hpp"

namespace tools {
  template <typename T>
  concept integral = tools::is_integral_v<T>;
}


#line 6 "tools/arithmetic.hpp"

namespace tools {
  template <typename T>
  concept arithmetic = tools::integral<T> || std::floating_point<T>;
}


#line 7 "tools/groups.hpp"

namespace tools {
  namespace groups {
    template <typename G>
    struct bit_xor {
      using T = G;
      static T op(const T& x, const T& y) {
        return x ^ y;
      }
      static T e() {
        return T(0);
      }
      static T inv(const T& x) {
        return x;
      }
    };

    template <typename G>
    struct multiplies {
      using T = G;
      static T op(const T& x, const T& y) {
        return x * y;
      }
      static T e() {
        return T(1);
      }
      static T inv(const T& x) {
        return e() / x;
      }
    };

    template <typename G>
    struct plus {
      using T = G;
      static T op(const T& x, const T& y) {
        return x + y;
      }
      static T e() {
        return T(0);
      }
      static T inv(const T& x) {
        return -x;
      }
    };
  }
}


#line 10 "tools/multiple_moebius_inplace.hpp"

namespace tools {
  template <tools::commutative_group G, std::ranges::random_access_range R>
  requires std::ranges::output_range<R, std::ranges::range_value_t<R>>
  void multiple_moebius_inplace(R&& b) {
    const int N = std::ranges::distance(b);
    if (N < 2) return;

    tools::eratosthenes_sieve<int> sieve(N - 1);
    for (const auto p : sieve.prime_range(1, N - 1)) {
      for (int i = 1; i * p < N; ++i) {
        std::ranges::begin(b)[i] = G::op(std::ranges::begin(b)[i], G::inv(std::ranges::begin(b)[i * p]));
      }
    }
  }

  template <std::ranges::random_access_range R>
  requires std::ranges::output_range<R, std::ranges::range_value_t<R>>
  void multiple_moebius_inplace(R&& b) {
    tools::multiple_moebius_inplace<tools::groups::plus<std::ranges::range_value_t<R>>>(std::forward<R>(b));
  }
}


Back to top page