#include <stdio.h> #include <vector> int popcount(int v) { int c; for (c = 0; v; c++) { v &= v - 1; } return c; } int main() { int pow; !scanf("%d", &pow); std::vector<int> sequence; int curr_pow = 0; for (int i = 0; curr_pow < pow; i++) { sequence.push_back(i + 1); curr_pow += popcount(i + 1); } int overflow = curr_pow - pow; for (int i = sequence.size() - 1; i > 0; i--) { if (popcount(sequence[i]) == overflow) { sequence.erase(sequence.begin() + i); break; } } printf("%lu\n", sequence.size()); for (int i = sequence.size() - 1; i >= 0; i--) { printf("%d ", sequence[i]); } printf("\n"); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <stdio.h> #include <vector> int popcount(int v) { int c; for (c = 0; v; c++) { v &= v - 1; } return c; } int main() { int pow; !scanf("%d", &pow); std::vector<int> sequence; int curr_pow = 0; for (int i = 0; curr_pow < pow; i++) { sequence.push_back(i + 1); curr_pow += popcount(i + 1); } int overflow = curr_pow - pow; for (int i = sequence.size() - 1; i > 0; i--) { if (popcount(sequence[i]) == overflow) { sequence.erase(sequence.begin() + i); break; } } printf("%lu\n", sequence.size()); for (int i = sequence.size() - 1; i >= 0; i--) { printf("%d ", sequence[i]); } printf("\n"); } |