#include<cstdio>
#include<vector>
int main() {
int n;
scanf("%d", &n);
int bits = 1;
int pow_bits = 1;
int moc = 1;
int x = 1;
while (moc < n) {
pow_bits *= 2;
moc += pow_bits + moc;
bits += 1;
x <<= 1;
x += 1;
// printf(">> bits:%d moc:%d x:%d\n", bits, moc, x);
}
std::vector<int> ret;
while (n > 0) {
while (moc - __builtin_popcount(x) >= n) {
moc -= __builtin_popcount(x);
--x;
// printf("<< bits:%d moc:%d x:%d\n", bits, moc, x);
}
//printf("%d\n", x);
ret.push_back(x);
n -= __builtin_popcount(x);
moc -= __builtin_popcount(x);
x--;
// printf("\t n:%d moc:%d x:%d\n", n, moc, x);
}
printf("%zu\n", ret.size());
for (auto x : ret)
printf("%d ", x);
printf("\n");
return 0;
}
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 | #include<cstdio> #include<vector> int main() { int n; scanf("%d", &n); int bits = 1; int pow_bits = 1; int moc = 1; int x = 1; while (moc < n) { pow_bits *= 2; moc += pow_bits + moc; bits += 1; x <<= 1; x += 1; // printf(">> bits:%d moc:%d x:%d\n", bits, moc, x); } std::vector<int> ret; while (n > 0) { while (moc - __builtin_popcount(x) >= n) { moc -= __builtin_popcount(x); --x; // printf("<< bits:%d moc:%d x:%d\n", bits, moc, x); } //printf("%d\n", x); ret.push_back(x); n -= __builtin_popcount(x); moc -= __builtin_popcount(x); x--; // printf("\t n:%d moc:%d x:%d\n", n, moc, x); } printf("%zu\n", ret.size()); for (auto x : ret) printf("%d ", x); printf("\n"); return 0; } |
English