/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <vector>
using namespace std;
int number_of_bits(int n) {
int ret = 0;
while(n > 0) {
if(n & 1)
ret++;
n >>= 1;
}
return ret;
}
int main()
{
int n;
cin >> n;
int sum_all = 0;
int first = 0;
while(sum_all < n)
sum_all += number_of_bits(++first);
int surplus = sum_all - n;
vector<int> skipped;
int cursor = first;
for(int cursor = first; surplus > 0; cursor--) {
int a = number_of_bits(cursor);
if(a <= surplus) {
surplus -= a;
skipped.push_back(cursor);
}
}
cout << first - skipped.size() << endl;
auto it = skipped.cbegin();
for(int i = first; i>0; i--) {
if(it != skipped.cend() && *it == i) {
it++;
continue;
}
cout << i << " ";
}
cout << endl;
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 47 48 49 50 51 52 53 54 55 56 57 58 | /****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> #include <vector> using namespace std; int number_of_bits(int n) { int ret = 0; while(n > 0) { if(n & 1) ret++; n >>= 1; } return ret; } int main() { int n; cin >> n; int sum_all = 0; int first = 0; while(sum_all < n) sum_all += number_of_bits(++first); int surplus = sum_all - n; vector<int> skipped; int cursor = first; for(int cursor = first; surplus > 0; cursor--) { int a = number_of_bits(cursor); if(a <= surplus) { surplus -= a; skipped.push_back(cursor); } } cout << first - skipped.size() << endl; auto it = skipped.cbegin(); for(int i = first; i>0; i--) { if(it != skipped.cend() && *it == i) { it++; continue; } cout << i << " "; } cout << endl; return 0; } |
English