#include<bits/stdc++.h> using namespace std; #define st first #define nd second const int INF = 1e6 + 7; int n, k, licz, roz, wart, t[INF]; long long pref[INF]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1; i <= 1e6; i++){ int x = i; while(x > 0){ if(x % 2 == 1){ licz++; } x /= 2; } t[i] = licz; licz = 0; pref[i] += pref[i - 1]; pref[i] += t[i]; if(pref[i] > n){ k = i; wart = pref[i]; break; } if(pref[i] == n){ cout << i << endl; for(int j = i; j >= 1; j--){ cout << j << " "; } return 0; } } roz = wart - n; int res = k; for(int i = k; i >= 1; i--){ if(t[i] <= roz){ roz -= t[i]; t[i] = 0; res--; } } cout << res << endl; for(int i = k; i >= 1; i--){ if(t[i] > 0){ cout << i << " "; } } }
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 | #include<bits/stdc++.h> using namespace std; #define st first #define nd second const int INF = 1e6 + 7; int n, k, licz, roz, wart, t[INF]; long long pref[INF]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1; i <= 1e6; i++){ int x = i; while(x > 0){ if(x % 2 == 1){ licz++; } x /= 2; } t[i] = licz; licz = 0; pref[i] += pref[i - 1]; pref[i] += t[i]; if(pref[i] > n){ k = i; wart = pref[i]; break; } if(pref[i] == n){ cout << i << endl; for(int j = i; j >= 1; j--){ cout << j << " "; } return 0; } } roz = wart - n; int res = k; for(int i = k; i >= 1; i--){ if(t[i] <= roz){ roz -= t[i]; t[i] = 0; res--; } } cout << res << endl; for(int i = k; i >= 1; i--){ if(t[i] > 0){ cout << i << " "; } } } |