// #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int bitz(int i)
{
int total = 0;
while (i != 0)
{
if (i % 2)
{
++total;
}
i /= 2;
}
return total;
}
int main()
{
FAST int n;
cin >> n;
int total = 0;
map<int, int> nums;
int bitSize, i;
for (i = 0; total < n;)
{
i++;
bitSize = bitz(i);
total += bitSize;
nums[i] = bitSize;
}
int num, sz;
unordered_map<int, bool> to_rm;
for (auto iter = nums.rbegin(); iter != nums.rend() && total != n; ++iter)
{
num = iter->first;
sz = iter->second;
if (total - sz < n)
{
continue;
}
to_rm[num] = true;
total -= sz;
}
cout << nums.size() - to_rm.size() << endl;
for (auto iter = nums.rbegin(); iter != nums.rend(); ++iter)
{
if (!to_rm[iter->first])
{
cout << iter->first << " ";
}
}
cout << 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | // #include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #include <iostream> #include <vector> #include <map> #include <unordered_map> using namespace std; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int bitz(int i) { int total = 0; while (i != 0) { if (i % 2) { ++total; } i /= 2; } return total; } int main() { FAST int n; cin >> n; int total = 0; map<int, int> nums; int bitSize, i; for (i = 0; total < n;) { i++; bitSize = bitz(i); total += bitSize; nums[i] = bitSize; } int num, sz; unordered_map<int, bool> to_rm; for (auto iter = nums.rbegin(); iter != nums.rend() && total != n; ++iter) { num = iter->first; sz = iter->second; if (total - sz < n) { continue; } to_rm[num] = true; total -= sz; } cout << nums.size() - to_rm.size() << endl; for (auto iter = nums.rbegin(); iter != nums.rend(); ++iter) { if (!to_rm[iter->first]) { cout << iter->first << " "; } } cout << endl; } |
English