#include<iostream> #include<vector> #include<queue> using namespace std; const int nmax = 125000; int BitPower(int x){ int ans = 0; while(x > 0){ ans += (x&1); x>>=1; } return ans; } int MinFirst(int sum, vector<int>& bitPow){ int s = 1, e = nmax, m; while(s < e){ m = (s+e)/2; if(bitPow[m] < sum)s = m + 1; else e = m; } return s; } int main(){ int n; cin>>n; vector<int> bitPow(nmax, 0); for(int i = 1; i < nmax; i++) bitPow[i] = (i&1) + bitPow[i>>1]; for(int i = 1; i < nmax; i++) bitPow[i] += bitPow[i-1]; queue<int> ans; int c = 0; int next = MinFirst(n, bitPow); while(bitPow[next] > n){ //cout<<n<<" "<<next<<" "<<bitPow[next]<<endl; ans.push(next); n -= bitPow[next] - bitPow[next - 1]; next = MinFirst(n, bitPow); c++; } //while(bitPow[next - 1] > n){ // ans.push(next); // next = MinFirst(n, bitPow); // n -= bitPow[next] - bitPow[next - 1]; // c++; //} while(next > 0){ ans.push(next); next--; c++; } cout<<c<<endl; while(!ans.empty()){ cout<<ans.front()<< " "; ans.pop(); } 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 | #include<iostream> #include<vector> #include<queue> using namespace std; const int nmax = 125000; int BitPower(int x){ int ans = 0; while(x > 0){ ans += (x&1); x>>=1; } return ans; } int MinFirst(int sum, vector<int>& bitPow){ int s = 1, e = nmax, m; while(s < e){ m = (s+e)/2; if(bitPow[m] < sum)s = m + 1; else e = m; } return s; } int main(){ int n; cin>>n; vector<int> bitPow(nmax, 0); for(int i = 1; i < nmax; i++) bitPow[i] = (i&1) + bitPow[i>>1]; for(int i = 1; i < nmax; i++) bitPow[i] += bitPow[i-1]; queue<int> ans; int c = 0; int next = MinFirst(n, bitPow); while(bitPow[next] > n){ //cout<<n<<" "<<next<<" "<<bitPow[next]<<endl; ans.push(next); n -= bitPow[next] - bitPow[next - 1]; next = MinFirst(n, bitPow); c++; } //while(bitPow[next - 1] > n){ // ans.push(next); // next = MinFirst(n, bitPow); // n -= bitPow[next] - bitPow[next - 1]; // c++; //} while(next > 0){ ans.push(next); next--; c++; } cout<<c<<endl; while(!ans.empty()){ cout<<ans.front()<< " "; ans.pop(); } return 0; } |