proconlib

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

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: Multiset $S$ such that $\{\sum_{x \in S'} x | S' \subseteq S\} = \{0, 1, \ldots, N\}$ and $|S| = O(\log N)$ (tools/logn_integer_partition.hpp)

template <typename T>
std::vector<T> logn_integer_partition(T n);

It returns a multiset $S$ such that $\{\sum_{x \in S’} x | S’ \subseteq S\} = \{0, 1, \ldots, N\}$ and $|S| = O(\log N)$.

Constraints

Time Complexity

License

Author

Verified with

Code

#ifndef TOOLS_LOGN_INTEGER_PARTITION_HPP
#define TOOLS_LOGN_INTEGER_PARTITION_HPP

#include <vector>
#include <cassert>

namespace tools {
  template <typename T>
  ::std::vector<T> logn_integer_partition(T n) {
    assert(n >= 0);
    ::std::vector<T> res;
    for (T pow2 = 1; pow2 < n; n -= pow2, pow2 *= 2) {
      res.push_back(pow2);
    }
    if (n > 0) res.push_back(n);
    return res;
  }
}

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



#include <vector>
#include <cassert>

namespace tools {
  template <typename T>
  ::std::vector<T> logn_integer_partition(T n) {
    assert(n >= 0);
    ::std::vector<T> res;
    for (T pow2 = 1; pow2 < n; n -= pow2, pow2 *= 2) {
      res.push_back(pow2);
    }
    if (n > 0) res.push_back(n);
    return res;
  }
}


Back to top page