#include<bits/stdc++.h> using namespace std; const int N = 1000005; int n, bit[N], pref[N]; vector<int>vec; int main(){ cin >> n; for(int i = 1 ; i < N ; i++){ int x = i; while(x > 0){ bit[i] += (x%2); x /= 2; } pref[i] = pref[i - 1] + bit[i]; } int id = 1; while(pref[id] < n) id++; vec.push_back(id); n -= bit[id]; id--; while(true){ if(n == 0) break; //czy jak skip id, to da sie zrobic n? if(pref[id-1] < n){ vec.push_back(id); n -= bit[id]; } id--; } cout << vec.size()<<"\n"; for(int i = 0 ; i < vec.size() ; i++){ cout << vec[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 | #include<bits/stdc++.h> using namespace std; const int N = 1000005; int n, bit[N], pref[N]; vector<int>vec; int main(){ cin >> n; for(int i = 1 ; i < N ; i++){ int x = i; while(x > 0){ bit[i] += (x%2); x /= 2; } pref[i] = pref[i - 1] + bit[i]; } int id = 1; while(pref[id] < n) id++; vec.push_back(id); n -= bit[id]; id--; while(true){ if(n == 0) break; //czy jak skip id, to da sie zrobic n? if(pref[id-1] < n){ vec.push_back(id); n -= bit[id]; } id--; } cout << vec.size()<<"\n"; for(int i = 0 ; i < vec.size() ; i++){ cout << vec[i]<<" "; } } |