#include<bits/stdc++.h>
using namespace std;
int n;
unsigned int next_value;
int count_bits(unsigned int value) {
bitset<sizeof(value) * CHAR_BIT> bits (value);
return bits.count();
}
int find_ban(unsigned int max_value, int bits) {
int ban = 0;
for (unsigned int i = 0; i < max_value; i++) {
if (count_bits(i) == bits) {
ban = i;
}
}
return ban;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
while (n > 0) {
next_value++;
n -= count_bits(next_value);
}
unsigned int banned_value = find_ban(next_value, abs(n));
cout << (banned_value ? next_value - 1 : next_value) << "\n";
do {
if (next_value != banned_value) {
cout << next_value << " ";
}
} while (--next_value);
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 46 | #include<bits/stdc++.h> using namespace std; int n; unsigned int next_value; int count_bits(unsigned int value) { bitset<sizeof(value) * CHAR_BIT> bits (value); return bits.count(); } int find_ban(unsigned int max_value, int bits) { int ban = 0; for (unsigned int i = 0; i < max_value; i++) { if (count_bits(i) == bits) { ban = i; } } return ban; } int main() { ios_base::sync_with_stdio(false); cin >> n; while (n > 0) { next_value++; n -= count_bits(next_value); } unsigned int banned_value = find_ban(next_value, abs(n)); cout << (banned_value ? next_value - 1 : next_value) << "\n"; do { if (next_value != banned_value) { cout << next_value << " "; } } while (--next_value); return 0; } |
English