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
#include <cstdio>
#include <bitset>

using namespace std;

int main()
{
    int n;
    string numbers = "";
    int k = 0;
    int ones = 0;
    scanf("%d", &n);
    for(int i = 1; i<=n;i++){
        int tmp = bitset<20>(i).count();
        int tmp2 = bitset<20>(i+1).count();
        if(ones+tmp2==n && tmp < tmp2){
            k++;
            ones+=tmp2;
            numbers = to_string(i+1) + " " + numbers;
            i++;
        }
        else if(ones+tmp<=n){
            k++;
            ones+=tmp;
            numbers = to_string(i) + " " + numbers;
        }
        if(ones==n){
            break;
        }
    }
    printf("%d \n%s", k, numbers.c_str());
}