library

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

View the Project on GitHub maspypy/library

:warning: setfunc/sps_inv.hpp

Depends on

Code

#include "setfunc/ranked_zeta.hpp"

template <typename mint, int LIM>
vc<mint> sps_inv(vc<mint>& dp) {
  const int N = topbit(len(dp));
  assert(len(dp) == (1 << N) && dp[0] == mint(1));
  auto RA = ranked_zeta<mint, LIM>(dp);
  array<mint, LIM + 1> g;
  FOR(s, 1 << N) {
    auto& f = RA[s];
    g[0] = 1;
    FOR(k, 1, N + 1) {
      g[k] = 0;
      FOR(i, k) g[k] -= g[i] * f[k - i];
    }
    RA[s] = g;
  }
  return ranked_mobius<mint, LIM>(RA);
}
#line 1 "setfunc/ranked_zeta.hpp"

#line 1 "setfunc/bitwise_transform.hpp"

namespace bitwise {

enum class trans_type {
  hadamard,
  superset_zeta,
  superset_mobius,
  subset_zeta,
  subset_mobius,
  ranked_zeta,
  ranked_mobius,
  superset_zeta_or
};

template <typename ARR>
inline void ranked_add(ARR& a, const ARR& b) {
  for (int d = 0; d < int(a.size()); ++d) a[d] += b[d];
}

template <typename ARR>
inline void ranked_sub(ARR& a, const ARR& b) {
  for (int d = 0; d < int(a.size()); ++d) a[d] -= b[d];
}

template <trans_type type, int N, typename T>
inline void bitwise_transform_fixed(T* a) {
  static_assert(N >= 1 && (N & (N - 1)) == 0);
  if constexpr (N == 1) {
    return;
  } else {
    constexpr int H = N / 2;
    bitwise_transform_fixed<type, H>(a);
    bitwise_transform_fixed<type, H>(a + H);
    if constexpr (type == trans_type::hadamard) {
      for (int i = 0; i < H; ++i) {
        auto x = a[i], y = a[H + i];
        a[i] = x + y, a[H + i] = x - y;
      }
    }
    if constexpr (type == trans_type::superset_zeta) {
      for (int i = 0; i < H; ++i) a[i] += a[H + i];
    }
    if constexpr (type == trans_type::superset_mobius) {
      for (int i = 0; i < H; ++i) a[i] -= a[H + i];
    }
    if constexpr (type == trans_type::subset_zeta) {
      for (int i = 0; i < H; ++i) a[H + i] += a[i];
    }
    if constexpr (type == trans_type::subset_mobius) {
      for (int i = 0; i < H; ++i) a[H + i] -= a[i];
    }
    if constexpr (type == trans_type::ranked_zeta) {
      for (int i = 0; i < H; ++i) ranked_add(a[H + i], a[i]);
    }
    if constexpr (type == trans_type::ranked_mobius) {
      for (int i = 0; i < H; ++i) ranked_sub(a[H + i], a[i]);
    }
    if constexpr (type == trans_type::superset_zeta_or) {
      for (int i = 0; i < H; ++i) a[i] |= a[H + i];
    }
  }
}

template <trans_type type, int N, typename T>
inline void bitwise_transform_dispatch(vc<T>& a) {
  if (len(a) == N) {
    return bitwise_transform_fixed<type, N>(a.data());
  }
  if constexpr (N > 1) {
    return bitwise_transform_dispatch<type, N / 2>(a);
  }
}

template <trans_type type, typename T>
inline void bitwise_transform(vc<T>& a) {
  int n = len(a);
  assert(n >= 1);
  assert((n & (n - 1)) == 0);
  assert(n <= (1 << 25));
  bitwise_transform_dispatch<type, 1 << 25>(a);
}
}  // namespace bitwise
#line 3 "setfunc/ranked_zeta.hpp"

template <typename T, int LIM>
vc<array<T, LIM + 1>> ranked_zeta(const vc<T>& f) {
  int n = topbit(len(f));
  assert(n <= LIM);
  assert(len(f) == 1 << n);
  vc<array<T, LIM + 1>> Rf(1 << n);
  for (int s = 0; s < (1 << n); ++s) Rf[s][popcnt(s)] = f[s];
  bitwise::bitwise_transform<bitwise::trans_type::ranked_zeta>(Rf);
  return Rf;
}

template <typename T, int LIM>
vc<T> ranked_mobius(vc<array<T, LIM + 1>>& Rf) {
  bitwise::bitwise_transform<bitwise::trans_type::ranked_mobius>(Rf);
  vc<T> f(len(Rf));
  for (int s = 0; s < len(f); ++s) f[s] = Rf[s][popcnt(s)];
  return f;
}
#line 2 "setfunc/sps_inv.hpp"

template <typename mint, int LIM>
vc<mint> sps_inv(vc<mint>& dp) {
  const int N = topbit(len(dp));
  assert(len(dp) == (1 << N) && dp[0] == mint(1));
  auto RA = ranked_zeta<mint, LIM>(dp);
  array<mint, LIM + 1> g;
  FOR(s, 1 << N) {
    auto& f = RA[s];
    g[0] = 1;
    FOR(k, 1, N + 1) {
      g[k] = 0;
      FOR(i, k) g[k] -= g[i] * f[k - i];
    }
    RA[s] = g;
  }
  return ranked_mobius<mint, LIM>(RA);
}
Back to top page