#include <bits/stdc++.h> using namespace std; int func(int x){ int y = x; int z = 0; while(y > 0){ if(y % 2 == 1){ z++; } y /= 2; } return z; } int wart[25]; int tab[25]; int ver[200000]; int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int a; cin>>a; vector<pair<int,int>> V; for(int i = 0; i < 200000;i++){ V.push_back({i,func(i)}); } vector<pair<int,int>> ans; int z = 0; int k = 0; while(z < a){ if(V[k].second + z <= a){ z+= V[k].second; ans.push_back({V[k].first,V[k].second}); wart[V[k].second]++; k++; }else{ int y = V[k].second + z; y-= a; if(wart[y] > 0){ wart[y]--; wart[V[k].second]++; tab[y]++; z+= V[k].second - y; ans.push_back({V[k].first,V[k].second}); } k++; } } int p = ans.size() - 1; for(int i = ans.size() - 1; i >= 0;i--){ if(tab[ans[i].second] > 0){ tab[ans[i].second]--; ver[i] = 1; p--; } } ver[0] = 1; cout<<p <<"\n"; for(int i = ans.size() - 1; i >= 0;i--){ if(ver[i] == 0){ cout<<ans[i].first <<" "; } } }
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> using namespace std; int func(int x){ int y = x; int z = 0; while(y > 0){ if(y % 2 == 1){ z++; } y /= 2; } return z; } int wart[25]; int tab[25]; int ver[200000]; int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int a; cin>>a; vector<pair<int,int>> V; for(int i = 0; i < 200000;i++){ V.push_back({i,func(i)}); } vector<pair<int,int>> ans; int z = 0; int k = 0; while(z < a){ if(V[k].second + z <= a){ z+= V[k].second; ans.push_back({V[k].first,V[k].second}); wart[V[k].second]++; k++; }else{ int y = V[k].second + z; y-= a; if(wart[y] > 0){ wart[y]--; wart[V[k].second]++; tab[y]++; z+= V[k].second - y; ans.push_back({V[k].first,V[k].second}); } k++; } } int p = ans.size() - 1; for(int i = ans.size() - 1; i >= 0;i--){ if(tab[ans[i].second] > 0){ tab[ans[i].second]--; ver[i] = 1; p--; } } ver[0] = 1; cout<<p <<"\n"; for(int i = ans.size() - 1; i >= 0;i--){ if(ver[i] == 0){ cout<<ans[i].first <<" "; } } } |