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 <bits/stdc++.h>
using namespace std;

int countSetBits(int n)
{
    int count_ = 0;
    while (n) {
        count_ += n & 1;
        n >>= 1;
    }
    return count_;
}

int main()
{
    int n;
    cin>>n;

    int suma=0;
    int kon=0;

    while(suma<=n)
    {
        kon++;
        suma+=countSetBits(kon);
    }

    int last=kon;
    int a=-1;
    while(a==-1)
    {
        if(suma-countSetBits(kon)==n)
        {
            a=kon;
        }
        else kon--;
    }

    if(a==0)cout<<last<<endl;
    else cout<<last-1<<endl;
    for(int i=last;i>0;i--)
    {
        if(i!=a)cout<<i<<" ";
    }
}