#include <iostream> #include <vector> using namespace std; const int N = (int)1e6+1; int n, sum, tab[N], k; vector <int> V; int main(){ ios_base::sync_with_stdio(0); cin>>n; for (int i=1; i<=n; i++){ tab[i] = __builtin_popcount(i); sum+=tab[i]; k++; if (sum>=n) break; } while (k){ if (sum - tab[k] >= n) sum -= tab[k]; else V.push_back(k); k--; } cout<<V.size()<<"\n"; for (auto x : V) cout<<x<<" "; cout<<"\n"; }
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 | #include <iostream> #include <vector> using namespace std; const int N = (int)1e6+1; int n, sum, tab[N], k; vector <int> V; int main(){ ios_base::sync_with_stdio(0); cin>>n; for (int i=1; i<=n; i++){ tab[i] = __builtin_popcount(i); sum+=tab[i]; k++; if (sum>=n) break; } while (k){ if (sum - tab[k] >= n) sum -= tab[k]; else V.push_back(k); k--; } cout<<V.size()<<"\n"; for (auto x : V) cout<<x<<" "; cout<<"\n"; } |