#include #include #include void printAsBinary(uint32_t a, int bitSize) { std::string output; for (int i = 0; i < bitSize; i++) { int bit = a & 1; a = a >> 1; output.insert(0, std::to_string(bit)); } std::cout << output << std::endl; } bool findSolution1(std::vector &visited, int current, std::vector &structures, int bitValue) { int maxBitValue = 1 << bitValue; if (structures.size() == maxBitValue) { for (auto i:structures) printAsBinary(i, bitValue); return true; } else { bool foundNew = false; int mask = 1; int newValue; while (true) { newValue = current | mask; if (!visited[newValue]) { foundNew = true; break; } else if (mask << 1 < maxBitValue) { mask = mask << 1; } else break; } if (foundNew) { visited[newValue] = true; structures.push_back(newValue); bool res = findSolution1(visited, newValue, structures, bitValue); structures.pop_back(); visited[newValue] = false; return res; } else { for (int shiftCount = 1; shiftCount <= bitValue; shiftCount++) { int savedBits = 0; for (int i = 0; i < shiftCount - 1; i++) { savedBits = (savedBits << 1) + 1; } savedBits = current & savedBits; newValue = ((current >> shiftCount) << shiftCount) + savedBits; if (!visited[newValue]) { visited[newValue] = true; structures.push_back(newValue); bool res = findSolution1(visited, newValue, structures, bitValue); structures.pop_back(); visited[newValue] = false; if (res) return res; } } return false; } } } bool isPow2(int a) { while (a) { if ((a & 1) == 1 && a != 1) return false; else a = a >> 1; } return true; } bool findSolution2(std::vector &visited, int current, std::vector &structures, int bitValue) { int maxBitValue = 1 << bitValue; if (structures.size() == maxBitValue) { for (auto i:structures) printAsBinary(i, bitValue); return true; } else { for (int i = 1; i < maxBitValue; i++) { int checkValue = i & current; if (!visited[i] && isPow2(checkValue)) { visited[i] = true; structures.push_back(i); bool res = findSolution2(visited, current, structures, bitValue); structures.pop_back(); visited[i] = false; if (res) return res; } } } return false; } int main() { int n; std::cin >> n; std::vector visited(1 << n); visited[0] = true; std::vector structures; structures.push_back(0); findSolution2(visited, 0, structures, n); return 0; }