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/mytest_enumerate_twelvefold.test.cpp

Depends on

Code

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

#include "../default/t.cpp"
#include "../enumerate/enumerate_bit.cpp"
#include "../enumerate/enumerate_twelvefold.cpp"

void a_plus_b() {
  int a, b; cin >> a >> b;
  cout << a + b << '\n';
}

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

  { //[0..n-1]^k
    for(int n = 0; n <= 7; n++) {
      int cnt = 1;
      for(int k = 0; k <= 7; k++, cnt *= n) {
        vector<vector<int>> S;
        enumerate_cartesian_power(n, k, [&](vector<int> a) {
          assert(ssize(a) == k);
          assert(a.empty() or ranges::min(a) >= 0);
          assert(a.empty() or ranges::max(a) < n);
          S.push_back(std::move(a));
        });
        assert(cnt == ssize(S));
        Unique(S);
        assert(cnt == ssize(S));
      }
    }
  }

  { //permutation
    int cnt = 1;
    for(int n = 0; n <= 9; cnt *= ++n) {
      vector<vector<int>> S;
      enumerate_permutation(n, [&](vector<int> p) {
        assert(ssize(p) == n);
        S.push_back(p);
        ranges::sort(p);
        for(int i = 0; i < n; i++)
          assert(p[i] == i);
      });
      assert(cnt == ssize(S));
      Unique(S);
      assert(cnt == ssize(S));
    }
  }

  { //combination
    int C[17][17] = {};
    for(int i = 0; i < 17; i++)
      C[i][0] = C[i][i] = 1;
    for(int i = 2; i < 17; i++)
      for(int j = 1; j < i; j++)
        C[i][j] = C[i - 1][j - 1] + C[i - 1][j];

    for(int n = 0; n < 17; n++) {
      for(int k = 0; k < 17; k++) {
        vector<vector<int>> S;
        enumerate_combination(n, k, [&](vector<int> a) {
          assert(ssize(a) == k);
          assert(a.empty() or ranges::min(a) >= 0);
          assert(a.empty() or ranges::max(a) < n);
          S.push_back(a);
          Unique(a);
          assert(ssize(a) == k);
        });
        assert(ssize(S) == C[n][k]);
        Unique(S);
        assert(ssize(S) == C[n][k]);
      }
    }
  }

  { //set partition
    int bell[12] = {1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975, 678570};
    for(int n = 0; n < 12; n++) {
      vector<vector<int>> S;
      enumerate_set_partition(n, [&](vector<int> p) {
        ranges::sort(p);
        int msk = 0;
        for(int x : p) {
          assert((x | ((1 << n) - 1)) == (1 << n) - 1);
          assert(x > 0);
          assert((msk & x) == 0);
          msk |= x;
        }
        assert(msk == (1 << n) - 1);
        S.push_back(std::move(p));
      });
      assert(ssize(S) == bell[n]);
      Unique(S);
      assert(ssize(S) == bell[n]);
    }
  }

  { //f[0] + f[1] + ... + f[n - 1] = sum, f[i] >= 0
    int C[17][17] = {};
    for(int i = 0; i < 17; i++)
      C[i][0] = C[i][i] = 1;
    for(int i = 2; i < 17; i++)
      for(int j = 1; j < i; j++)
        C[i][j] = C[i - 1][j - 1] + C[i - 1][j];

    for(int n = 0; n < 17; n++) {
      for(int sum = 0; n - 1 + sum < 17; sum++) {
        vector<vector<int>> S;
        enumerate_multisubset(n, sum, [&](vector<int> f) {
          assert(ssize(f) == n);
          assert(f.empty() or ranges::min(f) >= 0);
          assert(accumulate(f.begin(), f.end(), 0) == sum);
          S.emplace_back(f);
        });
        if (n == 0)
          assert(ssize(S) == (sum == 0));
        else
          assert(ssize(S) == C[n - 1 + sum][sum]);
        Unique(S);
        if (n == 0)
          assert(ssize(S) == (sum == 0));
        else
          assert(ssize(S) == C[n - 1 + sum][sum]);
      }
    }
  }

  { //integer partition
    int part[50] = {1, 1, 2, 3, 5, 7, 11, 15, 22, 30, 42, 56, 77, 101, 135, 176, 231, 297, 385, 490, 627, 792, 1002, 1255, 1575, 1958, 2436, 3010, 3718, 4565, 5604, 6842, 8349, 10143, 12310, 14883, 17977, 21637, 26015, 31185, 37338, 44583, 53174, 63261, 75175, 89134, 105558, 124754, 147273, 173525};
    for(int n = 0; n < 50; n++) {
      vector<vector<int>> S;
      enumerate_integer_partition(n, [&](vector<int> p) {
        assert(p.empty() or ranges::min(p) > 0);
        assert(ranges::is_sorted(p | views::reverse));
        assert(accumulate(p.begin(), p.end(), 0) == n);
        S.emplace_back(std::move(p));
      });
      assert(ssize(S) == part[n]);
      Unique(S);
      assert(ssize(S) == part[n]);
    }
  }

  a_plus_b();

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

#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 "enumerate/enumerate_bit.cpp"

template<typename F, typename INT>
requires invocable<F, INT>
void enumerate_subset(INT msk, F f) {
  for(INT x = msk; x > 0; x = (x - 1) & msk)
    f(x);
  f(0);
}
#line 1 "enumerate/enumerate_twelvefold.cpp"
//#include "enumerate/bit.cpp"

template<typename F>
requires invocable<F, vector<int>>
void enumerate_cartesian_power(int n, int k, F f) {
  assert(min(n, k) >= 0);
  vector<int> p(k);
  auto dfs = [&](int i, auto &self) -> void {
    if (i == k) {
      f(p);
    } else {
      for(int x = 0; x < n; x++) {
        p[i] = x;
        self(i + 1, self);
      }
    }
  };
  dfs(0, dfs);
}

template<typename F>
requires invocable<F, vector<int>>
void enumerate_permutation(int n, F f) {
  assert(n >= 0);
  vector<int> p(n);
  iota(p.begin(), p.end(), 0);
  do { f(p); } while(next_permutation(p.begin(), p.end()));
}

template<typename F>
requires invocable<F, vector<int>>
void enumerate_combination(int n, int k, F f) {
  assert(min(n, k) >= 0);
  vector<int> p;
  auto dfs = [&](auto &self) -> void {
    if (ssize(p) == k) {
      f(p);
    } else {
      for(int x = (p.empty() ? 0 : p.back() + 1); x + k - ssize(p) <= n; x++) {
        p.emplace_back(x);
        self(self);
        p.pop_back();
      }
    }
  };
  dfs(dfs);
}

template<typename F>
requires invocable<F, vector<int>>
void enumerate_set_partition(int n, F f) {
  assert(n >= 0);
  vector<int> p;
  int msk = (1 << n) - 1;
  auto dfs = [&](auto &self) -> void {
    if (msk == 0) {
      f(p);
    } else {
      int x = msk & (-msk);
      msk ^= x;
      enumerate_subset(msk, [&](int sub) {
        p.emplace_back(sub | x);
        msk ^= sub;
        self(self);
        msk ^= sub;
        p.pop_back();
      });
      msk ^= x;
    }
  };
  dfs(dfs);
}

template<typename F>
requires invocable<F, vector<int>>
void enumerate_multisubset(int n, int sum, F f) {
  assert(min(n, sum) >= 0);
  vector<int> p(n);
  auto dfs = [&](int i, auto &self) -> void {
    if (i == n) {
      if (sum == 0) f(p);
    } else {
      for(int x = sum; x >= 0; x--) {
        p[i] = x, sum -= x;
        self(i + 1, self);
        sum += x;
      }
    }
  };
  dfs(0, dfs);
}

template<typename F>
requires invocable<F, vector<int>>
void enumerate_integer_partition(int n, F f) {
  assert(n >= 0);
  vector<int> p;
  auto dfs = [&](int s, auto &self) -> void {
    if (s == 0) {
      f(p);
    } else {
      for(int x = (p.empty() ? s : min(p.back(), s)); x > 0; x--) {
        p.emplace_back(x);
        self(s - x, self);
        p.pop_back();
      }
    }
  };
  dfs(n, dfs);
}
#line 6 "test/mytest_enumerate_twelvefold.test.cpp"

void a_plus_b() {
  int a, b; cin >> a >> b;
  cout << a + b << '\n';
}

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

  { //[0..n-1]^k
    for(int n = 0; n <= 7; n++) {
      int cnt = 1;
      for(int k = 0; k <= 7; k++, cnt *= n) {
        vector<vector<int>> S;
        enumerate_cartesian_power(n, k, [&](vector<int> a) {
          assert(ssize(a) == k);
          assert(a.empty() or ranges::min(a) >= 0);
          assert(a.empty() or ranges::max(a) < n);
          S.push_back(std::move(a));
        });
        assert(cnt == ssize(S));
        Unique(S);
        assert(cnt == ssize(S));
      }
    }
  }

  { //permutation
    int cnt = 1;
    for(int n = 0; n <= 9; cnt *= ++n) {
      vector<vector<int>> S;
      enumerate_permutation(n, [&](vector<int> p) {
        assert(ssize(p) == n);
        S.push_back(p);
        ranges::sort(p);
        for(int i = 0; i < n; i++)
          assert(p[i] == i);
      });
      assert(cnt == ssize(S));
      Unique(S);
      assert(cnt == ssize(S));
    }
  }

  { //combination
    int C[17][17] = {};
    for(int i = 0; i < 17; i++)
      C[i][0] = C[i][i] = 1;
    for(int i = 2; i < 17; i++)
      for(int j = 1; j < i; j++)
        C[i][j] = C[i - 1][j - 1] + C[i - 1][j];

    for(int n = 0; n < 17; n++) {
      for(int k = 0; k < 17; k++) {
        vector<vector<int>> S;
        enumerate_combination(n, k, [&](vector<int> a) {
          assert(ssize(a) == k);
          assert(a.empty() or ranges::min(a) >= 0);
          assert(a.empty() or ranges::max(a) < n);
          S.push_back(a);
          Unique(a);
          assert(ssize(a) == k);
        });
        assert(ssize(S) == C[n][k]);
        Unique(S);
        assert(ssize(S) == C[n][k]);
      }
    }
  }

  { //set partition
    int bell[12] = {1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975, 678570};
    for(int n = 0; n < 12; n++) {
      vector<vector<int>> S;
      enumerate_set_partition(n, [&](vector<int> p) {
        ranges::sort(p);
        int msk = 0;
        for(int x : p) {
          assert((x | ((1 << n) - 1)) == (1 << n) - 1);
          assert(x > 0);
          assert((msk & x) == 0);
          msk |= x;
        }
        assert(msk == (1 << n) - 1);
        S.push_back(std::move(p));
      });
      assert(ssize(S) == bell[n]);
      Unique(S);
      assert(ssize(S) == bell[n]);
    }
  }

  { //f[0] + f[1] + ... + f[n - 1] = sum, f[i] >= 0
    int C[17][17] = {};
    for(int i = 0; i < 17; i++)
      C[i][0] = C[i][i] = 1;
    for(int i = 2; i < 17; i++)
      for(int j = 1; j < i; j++)
        C[i][j] = C[i - 1][j - 1] + C[i - 1][j];

    for(int n = 0; n < 17; n++) {
      for(int sum = 0; n - 1 + sum < 17; sum++) {
        vector<vector<int>> S;
        enumerate_multisubset(n, sum, [&](vector<int> f) {
          assert(ssize(f) == n);
          assert(f.empty() or ranges::min(f) >= 0);
          assert(accumulate(f.begin(), f.end(), 0) == sum);
          S.emplace_back(f);
        });
        if (n == 0)
          assert(ssize(S) == (sum == 0));
        else
          assert(ssize(S) == C[n - 1 + sum][sum]);
        Unique(S);
        if (n == 0)
          assert(ssize(S) == (sum == 0));
        else
          assert(ssize(S) == C[n - 1 + sum][sum]);
      }
    }
  }

  { //integer partition
    int part[50] = {1, 1, 2, 3, 5, 7, 11, 15, 22, 30, 42, 56, 77, 101, 135, 176, 231, 297, 385, 490, 627, 792, 1002, 1255, 1575, 1958, 2436, 3010, 3718, 4565, 5604, 6842, 8349, 10143, 12310, 14883, 17977, 21637, 26015, 31185, 37338, 44583, 53174, 63261, 75175, 89134, 105558, 124754, 147273, 173525};
    for(int n = 0; n < 50; n++) {
      vector<vector<int>> S;
      enumerate_integer_partition(n, [&](vector<int> p) {
        assert(p.empty() or ranges::min(p) > 0);
        assert(ranges::is_sorted(p | views::reverse));
        assert(accumulate(p.begin(), p.end(), 0) == n);
        S.emplace_back(std::move(p));
      });
      assert(ssize(S) == part[n]);
      Unique(S);
      assert(ssize(S) == part[n]);
    }
  }

  a_plus_b();

  return 0;
}
Back to top page