#include<bits/stdc++.h> #define pb push_back #define f first #define s second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define vll vector<pll> #define vii vector<pii> #define vi vector<int> #define vl vector<ll> #define vvi vector<vi> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vb> #define mid (l+r)/2 #define endl '\n' #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) using namespace std; const int N = 1000200; vi pr(N); int main() { turbo int n; cin >> n; for(int i = 1; i < N; i++) pr[i] = pr[i-1]+bits(i); int ind = 1; while(pr[ind] < n) ind++; if(pr[ind] == n) { cout << ind << "\n"; for(int i = ind; i; i--) cout << i << " "; return 0; } unordered_set<int> se; int sum = pr[ind]; for(int i = ind; i; i--) if(sum-bits(i) >= n) { se.insert(i); sum-=bits(i); } cout << ind-se.size() << "\n"; for(int i = ind; i; i--) if(se.find(i) == se.end()) cout << i << " "; }
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 | #include<bits/stdc++.h> #define pb push_back #define f first #define s second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define vll vector<pll> #define vii vector<pii> #define vi vector<int> #define vl vector<ll> #define vvi vector<vi> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vb> #define mid (l+r)/2 #define endl '\n' #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) using namespace std; const int N = 1000200; vi pr(N); int main() { turbo int n; cin >> n; for(int i = 1; i < N; i++) pr[i] = pr[i-1]+bits(i); int ind = 1; while(pr[ind] < n) ind++; if(pr[ind] == n) { cout << ind << "\n"; for(int i = ind; i; i--) cout << i << " "; return 0; } unordered_set<int> se; int sum = pr[ind]; for(int i = ind; i; i--) if(sum-bits(i) >= n) { se.insert(i); sum-=bits(i); } cout << ind-se.size() << "\n"; for(int i = ind; i; i--) if(se.find(i) == se.end()) cout << i << " "; } |