#include <bits/stdc++.h> using namespace std; typedef long long ll; #ifdef LOCAL #define debug(...) __VA_ARGS__ #else #define debug(...) {} #endif int pre[1000005]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int i; int n; cin>>n; for (i = 1; i < n+1; i++){ int akt = 0; for (int j = 0; j < 20; j++){ if (i&(1<<j)) akt++; } pre[i] = pre[i-1]+akt; } vector<int> wy; for (i = n; i ; i--){ if (pre[i] >= n && pre[i-1] < n){ wy.push_back(i); for (int j = 0; j < 20; j++) if (i&(1<<j)) n--; } } cout<<wy.size()<<"\n"; for (int j : wy) cout<<j<<" "; cout<<"\n"; return 0; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #ifdef LOCAL #define debug(...) __VA_ARGS__ #else #define debug(...) {} #endif int pre[1000005]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int i; int n; cin>>n; for (i = 1; i < n+1; i++){ int akt = 0; for (int j = 0; j < 20; j++){ if (i&(1<<j)) akt++; } pre[i] = pre[i-1]+akt; } vector<int> wy; for (i = n; i ; i--){ if (pre[i] >= n && pre[i-1] < n){ wy.push_back(i); for (int j = 0; j < 20; j++) if (i&(1<<j)) n--; } } cout<<wy.size()<<"\n"; for (int j : wy) cout<<j<<" "; cout<<"\n"; return 0; } |