#include<cstdio> #include<vector> using namespace std; int main() { int expectedSum; scanf("%d\n", &expectedSum); vector<int> powers(1, 1); int currSum = 1; int currCopyIx = -1; int currIx = 1; int currTwoPower = 2; while (currSum < expectedSum) { if (currIx + 1 == currTwoPower * 2) { currCopyIx = -1; currTwoPower *= 2; } if (currCopyIx == -1) { powers.push_back(1); } else { powers.push_back(powers[currCopyIx] + 1); } currSum += powers[currIx]; currIx++; currCopyIx++; } int powerToRemove = currSum - expectedSum; if (powerToRemove == 0) { printf("%lu\n", powers.size()); } else { printf("%lu\n", powers.size() - 1); } printf("%lu", powers.size()); for (int i = powers.size() - 2; i >= 0; i--) { if (powers[i] == powerToRemove) { powerToRemove = 0; } else { printf(" %d", i + 1); } } }
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 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include<cstdio> #include<vector> using namespace std; int main() { int expectedSum; scanf("%d\n", &expectedSum); vector<int> powers(1, 1); int currSum = 1; int currCopyIx = -1; int currIx = 1; int currTwoPower = 2; while (currSum < expectedSum) { if (currIx + 1 == currTwoPower * 2) { currCopyIx = -1; currTwoPower *= 2; } if (currCopyIx == -1) { powers.push_back(1); } else { powers.push_back(powers[currCopyIx] + 1); } currSum += powers[currIx]; currIx++; currCopyIx++; } int powerToRemove = currSum - expectedSum; if (powerToRemove == 0) { printf("%lu\n", powers.size()); } else { printf("%lu\n", powers.size() - 1); } printf("%lu", powers.size()); for (int i = powers.size() - 2; i >= 0; i--) { if (powers[i] == powerToRemove) { powerToRemove = 0; } else { printf(" %d", i + 1); } } } |