#include <bits/stdc++.h> using namespace std; #define mk make_pair #define fst first #define sz(x) x.size() #define snd second #define pb push_back #define VAR(v) #v << " = " << v << " " #define debug if (0) #define M_PI 3.14159265358979323846 typedef complex<long double> C; typedef long long ll; typedef pair<int, int> ii; typedef vector<ll> Matrix; const int maxn = (int)1e6 + 9; const int INF = (int)1e9 + 7; int mod = (int)1e9 + 7; int arr[maxn]; int num_of_bits(int x) { int ans = 0; while (x) { ans += x & 1; x >>= 1; } return ans; } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); debug; else cin.tie(NULL), cout.tie(NULL); int n; cin >> n; int idx = 0, sum = 0; vector<int> ans; while (sum < n) { idx++; sum += num_of_bits(idx); arr[idx] = sum; } for (int i = idx; i >= 1; i--) { if (arr[i - 1] < n) { ans.pb(i); n -= num_of_bits(i); } } cout << sz(ans) << endl; for (int i = 0; i < sz(ans); i++) { cout << ans[i] << " "; } 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 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 | #include <bits/stdc++.h> using namespace std; #define mk make_pair #define fst first #define sz(x) x.size() #define snd second #define pb push_back #define VAR(v) #v << " = " << v << " " #define debug if (0) #define M_PI 3.14159265358979323846 typedef complex<long double> C; typedef long long ll; typedef pair<int, int> ii; typedef vector<ll> Matrix; const int maxn = (int)1e6 + 9; const int INF = (int)1e9 + 7; int mod = (int)1e9 + 7; int arr[maxn]; int num_of_bits(int x) { int ans = 0; while (x) { ans += x & 1; x >>= 1; } return ans; } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); debug; else cin.tie(NULL), cout.tie(NULL); int n; cin >> n; int idx = 0, sum = 0; vector<int> ans; while (sum < n) { idx++; sum += num_of_bits(idx); arr[idx] = sum; } for (int i = idx; i >= 1; i--) { if (arr[i - 1] < n) { ans.pb(i); n -= num_of_bits(i); } } cout << sz(ans) << endl; for (int i = 0; i < sz(ans); i++) { cout << ans[i] << " "; } cout << "\n"; return 0; } |