#include #include std::vector seen(1); size_t unseen = 0; size_t other(size_t p) { seen[p] = true; while (unseen < seen.size() && seen[unseen]) { unseen++; } for (size_t v = unseen;; v++) { if (v == seen.size()) { seen.resize(2 * seen.size()); } if (!seen[v]) { size_t pv = p & v; if (pv==0 || pv==p) { return v; } } } } constexpr size_t unknown = size_t(-1); std::vector a(10'001, unknown); size_t u = 0; int main() { size_t v = 0; for (size_t n = 0; u < a.size(); n++) { if (v < a.size()) { a[v] = n; while (u < a.size() && a[u] != unknown) { std::cout << u << ' ' << a[u] << std::endl; u++; } } v = other(v); } return 0; }