#include <iostream> #include <vector> using namespace std; const int MAXN = 1e6+1; long long pref[MAXN]; vector <int> ans; int bit(int a){ int res = 0; while (a){ if (a&1) res++; a = a>>1; } return res; } // zwraca najmniejsza liczbe taka ze pref[i] >= a int bin(int a, int p, int k){ int res = 10*MAXN; int ret; while (p<=k){ int mid = (p+k)/2; if (pref[mid] >= a){ if (res >= pref[mid]){ res = pref[mid]; ret = mid; } k = mid-1; } else p = mid+1; } return ret; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i=1; i<=n; i++) pref[i] = pref[i-1]+bit(i); int last = n; while (n){ int ind = bin(n, 1, last); ans.push_back(ind); n -= pref[ind]-pref[ind-1]; } cout << ans.size() << "\n"; for (auto x : ans) 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <iostream> #include <vector> using namespace std; const int MAXN = 1e6+1; long long pref[MAXN]; vector <int> ans; int bit(int a){ int res = 0; while (a){ if (a&1) res++; a = a>>1; } return res; } // zwraca najmniejsza liczbe taka ze pref[i] >= a int bin(int a, int p, int k){ int res = 10*MAXN; int ret; while (p<=k){ int mid = (p+k)/2; if (pref[mid] >= a){ if (res >= pref[mid]){ res = pref[mid]; ret = mid; } k = mid-1; } else p = mid+1; } return ret; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i=1; i<=n; i++) pref[i] = pref[i-1]+bit(i); int last = n; while (n){ int ind = bin(n, 1, last); ans.push_back(ind); n -= pref[ind]-pref[ind-1]; } cout << ans.size() << "\n"; for (auto x : ans) cout << x << " "; cout << "\n"; } |