#include <iostream> #include <algorithm> #include <vector> using namespace std; int T[1000000]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int i,q=n; for (i=1;i<1000000 && q>0;i++) { int z=i; T[i]=0; while (z) { T[i]+=z%2; z/=2; } q-=T[i]; } i--; q*=-1; n = i; int first = i; i--; for (;i>0 && q>0;i--) { if (T[i]<=q) { q-=T[i]; T[i]=0; n--; } } std::cout << n << std::endl; n = first; for (;n>0;n--) { if (T[n]>0) std::cout << n << ' '; } std::cout << std::endl; }
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 <iostream> #include <algorithm> #include <vector> using namespace std; int T[1000000]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int i,q=n; for (i=1;i<1000000 && q>0;i++) { int z=i; T[i]=0; while (z) { T[i]+=z%2; z/=2; } q-=T[i]; } i--; q*=-1; n = i; int first = i; i--; for (;i>0 && q>0;i--) { if (T[i]<=q) { q-=T[i]; T[i]=0; n--; } } std::cout << n << std::endl; n = first; for (;n>0;n--) { if (T[n]>0) std::cout << n << ' '; } std::cout << std::endl; } |