#include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; #define pb push_back #define mp make_pair #define st first #define nd second #define x first #define y second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi v; map<int,int> bits_sum; map<int,int> bits_cnt; bits_sum[0] = 0; bits_cnt[0] = 0; int m = 0; for(int i=1; i<1000000; i++){ int a=i; int a_bits = 0; while(a>0){ if(a%2!=0){ a_bits++; } a = a/2; } bits_sum[i] = bits_sum[i-1]+a_bits; bits_cnt[i] = a_bits; if(bits_sum[i]>=n){ m = i; break; } } int p = m; int curr = m; while(n>0){ if(bits_sum[curr]>=n){ p = curr; curr--; }else if(bits_sum[curr]<n){ n-=bits_cnt[p]; v.pb(p); } } cout << v.size() << "\n"; for(int i=0; i<v.size(); i++){ cout << v[i] << " "; } 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; #define pb push_back #define mp make_pair #define st first #define nd second #define x first #define y second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi v; map<int,int> bits_sum; map<int,int> bits_cnt; bits_sum[0] = 0; bits_cnt[0] = 0; int m = 0; for(int i=1; i<1000000; i++){ int a=i; int a_bits = 0; while(a>0){ if(a%2!=0){ a_bits++; } a = a/2; } bits_sum[i] = bits_sum[i-1]+a_bits; bits_cnt[i] = a_bits; if(bits_sum[i]>=n){ m = i; break; } } int p = m; int curr = m; while(n>0){ if(bits_sum[curr]>=n){ p = curr; curr--; }else if(bits_sum[curr]<n){ n-=bits_cnt[p]; v.pb(p); } } cout << v.size() << "\n"; for(int i=0; i<v.size(); i++){ cout << v[i] << " "; } return 0; } |