#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main() {
const vector<int> set = { 0, 1, 2, 3 };
const auto k = 3;
vector<vector<int>::const_iterator> its(k, cbegin(set));
do {
transform(cbegin(its), cend(its), ostream_iterator<int>(cout), [](const auto& i) { return *i; });
cout << endl;
for (auto it = rbegin(its); it != rend(its) && ++*it == cend(set); ++it) *it = cbegin(set);
} while (count(cbegin(its), cend(its), cbegin(set)) != k);
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aXRlcmF0b3I+CiNpbmNsdWRlIDx2ZWN0b3I+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7Cgljb25zdCB2ZWN0b3I8aW50PiBzZXQgPSB7IDAsIDEsIDIsIDMgfTsKCWNvbnN0IGF1dG8gayA9IDM7CgoJdmVjdG9yPHZlY3RvcjxpbnQ+Ojpjb25zdF9pdGVyYXRvcj4gaXRzKGssIGNiZWdpbihzZXQpKTsKCQoJZG8gewoJCXRyYW5zZm9ybShjYmVnaW4oaXRzKSwgY2VuZChpdHMpLCBvc3RyZWFtX2l0ZXJhdG9yPGludD4oY291dCksIFtdKGNvbnN0IGF1dG8mIGkpIHsgcmV0dXJuICppOyB9KTsKCgkJY291dCA8PCBlbmRsOwoKCQlmb3IgKGF1dG8gaXQgPSByYmVnaW4oaXRzKTsgaXQgIT0gcmVuZChpdHMpICYmICsrKml0ID09IGNlbmQoc2V0KTsgKytpdCkgKml0ID0gY2JlZ2luKHNldCk7Cgl9IHdoaWxlIChjb3VudChjYmVnaW4oaXRzKSwgY2VuZChpdHMpLCBjYmVnaW4oc2V0KSkgIT0gayk7Cn0=