#include <bits/stdc++.h> using namespace std; int n; int tab[121000]; pair<int,int> T[121000]; int sum,li; int sumbit(int n) { int suma=0; for (int i = 17; i >= 0; i--) { int k = n >> i; if (k & 1) suma++; } return suma; } int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; // If the element is present at the middle // itself if (arr[mid] == x) return mid; // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present // in right subarray return binarySearch(arr, mid + 1, r, x); } // We reach here when element is not // present in array return -1; } int next(int arr[], int target, int end) { int start = 0; int ans = -1; while (start <= end) { int mid = (start + end) / 2; // Move to right side if target is // greater. if (arr[mid] <= target) start = mid + 1; // Move left side. else { ans = mid; end = mid - 1; } } return ans; } int last(pair<int,int> arr[], int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < arr[mid + 1].first) && arr[mid].first == x) return mid; else if (x < arr[mid].first) return last(arr, low, (mid - 1), x, n); else return last(arr, (mid + 1), high, x, n); } return -1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i=1;i<=120607;i++) { tab[i]+=tab[i-1]+sumbit(i); T[i].first=sumbit(i); T[i].second=i; } int res=binarySearch(tab,1,120607,n); if(res!=-1) { cout << res << endl; for(int i=res;i>=1;i--) { cout << i << " "; } } else { int res1 = next(tab,n,120607); sort(T,T+res1); int wynik=last(T,1,res1+1,tab[res1]-n,res1+1); int wynik1=T[wynik].second; cout << res1 - 1 << endl; for(int i=res1;i>=1;i--) { if(i==wynik1) { continue; } else 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include <bits/stdc++.h> using namespace std; int n; int tab[121000]; pair<int,int> T[121000]; int sum,li; int sumbit(int n) { int suma=0; for (int i = 17; i >= 0; i--) { int k = n >> i; if (k & 1) suma++; } return suma; } int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; // If the element is present at the middle // itself if (arr[mid] == x) return mid; // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present // in right subarray return binarySearch(arr, mid + 1, r, x); } // We reach here when element is not // present in array return -1; } int next(int arr[], int target, int end) { int start = 0; int ans = -1; while (start <= end) { int mid = (start + end) / 2; // Move to right side if target is // greater. if (arr[mid] <= target) start = mid + 1; // Move left side. else { ans = mid; end = mid - 1; } } return ans; } int last(pair<int,int> arr[], int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < arr[mid + 1].first) && arr[mid].first == x) return mid; else if (x < arr[mid].first) return last(arr, low, (mid - 1), x, n); else return last(arr, (mid + 1), high, x, n); } return -1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i=1;i<=120607;i++) { tab[i]+=tab[i-1]+sumbit(i); T[i].first=sumbit(i); T[i].second=i; } int res=binarySearch(tab,1,120607,n); if(res!=-1) { cout << res << endl; for(int i=res;i>=1;i--) { cout << i << " "; } } else { int res1 = next(tab,n,120607); sort(T,T+res1); int wynik=last(T,1,res1+1,tab[res1]-n,res1+1); int wynik1=T[wynik].second; cout << res1 - 1 << endl; for(int i=res1;i>=1;i--) { if(i==wynik1) { continue; } else cout << i << " "; } } } |