#include <iostream> #include <vector> using namespace std; const int N = 1000002; int sum_pref[N+5], sum_bin[N+5], n, s, a; bool test = false; vector<int> odp; void binary(int liczba){ int akt = liczba; while(liczba){ sum_bin[akt] += liczba%2; liczba/=2; } sum_pref[akt] = sum_pref[akt - 1] + sum_bin[akt]; } bool spr(int x){ return (sum_pref[x] < n); } int binsearch(int l, int p){ int sr; while(p - l > 1){ sr = (p + l)/2; if(spr(sr)) l = sr; else p = sr; } return p; } int main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n; for (int i = 1; i <= N; i++) { binary(i); } s = binsearch(1, 1000002); a = sum_pref[s] - n; odp.push_back(s); for (int i = s - 1; i >= 1; i--) { if(!test && sum_bin[i] == a){ test = true; }else{ odp.push_back(i); } } cout<<odp.size()<<"\n"; for(int i : odp){ cout<<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 | #include <iostream> #include <vector> using namespace std; const int N = 1000002; int sum_pref[N+5], sum_bin[N+5], n, s, a; bool test = false; vector<int> odp; void binary(int liczba){ int akt = liczba; while(liczba){ sum_bin[akt] += liczba%2; liczba/=2; } sum_pref[akt] = sum_pref[akt - 1] + sum_bin[akt]; } bool spr(int x){ return (sum_pref[x] < n); } int binsearch(int l, int p){ int sr; while(p - l > 1){ sr = (p + l)/2; if(spr(sr)) l = sr; else p = sr; } return p; } int main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n; for (int i = 1; i <= N; i++) { binary(i); } s = binsearch(1, 1000002); a = sum_pref[s] - n; odp.push_back(s); for (int i = s - 1; i >= 1; i--) { if(!test && sum_bin[i] == a){ test = true; }else{ odp.push_back(i); } } cout<<odp.size()<<"\n"; for(int i : odp){ cout<<i<<" "; } return 0; } |