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
#include <bits/stdc++.h>
using namespace std;
int powersOf2[31];
int numberOfBits[1000003];
int main()
{
    ios_base::sync_with_stdio(0);
    int n;
    cin>>n;
    numberOfBits[0] = 0;
    numberOfBits[1] = 1;
    powersOf2[0]=1;
    for(int i=1;i<31;i++)
        powersOf2[i]=powersOf2[i-1]*2;
    int sum=0;
    int currentPower=0;
    int lastIndex=0;
    while(sum<n)
    {
        for(int i=0;i+powersOf2[currentPower]<powersOf2[currentPower+1];i++)
        {
            numberOfBits[i+powersOf2[currentPower]]=numberOfBits[i]+1;
            // cerr<<i<<" "<<numberOfBits[i];
            // cerr<<i+powersOf2[currentPower]<<" "<<numberOfBits[i+powersOf2[currentPower]]<<endl;
            sum += numberOfBits[i]+1;
            lastIndex= i+powersOf2[currentPower];
            if(sum>n)
            {
                break;
            }
        }
            currentPower++;
    }
    int result=lastIndex;

    for(int i=lastIndex;i>0;i--)
    {
        if(numberOfBits[i]<=sum-n)
        {
            // cerr<<numberOfBits[i]<<" "<<sum<<" "<<n<<endl;
            sum-=numberOfBits[i];
            numberOfBits[i]=0;
            result--;
        }
    }
    cout<<result<<endl;
    for(int i=lastIndex;i>0;i--)
    {
        if(numberOfBits[i]>0)
        {
            cout<<i<<" ";
        }
    }
    if(result>0)
        cout<<endl;
    return 0;
}