#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <map>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>
#include <climits>
#include <iomanip>
using namespace std;
int nowy_idx(vector<int> t, int l, int p, int n){
if(t[l]==n) return l;
if(t[p]==n) return p;
int s=l+((p-l)/2);
if(t[s]<n && t[s+1]>=n) return s+1;
if(t[s]<n ) return nowy_idx(t,s,p,n);
return nowy_idx(t,l,s,n);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
vector<int> suma(1,0);
vector<int> war(1,0);
int i=1;
while(suma[suma.size()-1]<=n){
int l=0;
int pom=i;
while(pom!=0){
if(pom%2==1) l++;
pom=pom/2;
}
war.push_back(l);
suma.push_back(suma[suma.size()-1]+l);
i++;
}
int index=suma.size()-1;
vector<int> wyn;
while(n>0){
if(n>0) index=nowy_idx(suma,0,index,n);
if(n==suma[index]){
cout<<wyn.size()+index<<"\n";
for(int i=0; i< wyn.size(); i++){
cout<<wyn[i]<<" ";
}
for(int i=index;i>0; i--) cout<<i<<" ";
return 0;
}
wyn.push_back(index);
n=n-war[index];
}
cout<<wyn.size()<<"\n";
for(int i=0; i< wyn.size(); i++){
cout<<wyn[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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include <iostream> #include <vector> #include <set> #include <algorithm> #include <math.h> #include <map> #include <fstream> #include <stdio.h> #include <stdlib.h> #include <string> #include <ctime> #include <stdlib.h> #include <stdio.h> #include <climits> #include <iomanip> using namespace std; int nowy_idx(vector<int> t, int l, int p, int n){ if(t[l]==n) return l; if(t[p]==n) return p; int s=l+((p-l)/2); if(t[s]<n && t[s+1]>=n) return s+1; if(t[s]<n ) return nowy_idx(t,s,p,n); return nowy_idx(t,l,s,n); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; vector<int> suma(1,0); vector<int> war(1,0); int i=1; while(suma[suma.size()-1]<=n){ int l=0; int pom=i; while(pom!=0){ if(pom%2==1) l++; pom=pom/2; } war.push_back(l); suma.push_back(suma[suma.size()-1]+l); i++; } int index=suma.size()-1; vector<int> wyn; while(n>0){ if(n>0) index=nowy_idx(suma,0,index,n); if(n==suma[index]){ cout<<wyn.size()+index<<"\n"; for(int i=0; i< wyn.size(); i++){ cout<<wyn[i]<<" "; } for(int i=index;i>0; i--) cout<<i<<" "; return 0; } wyn.push_back(index); n=n-war[index]; } cout<<wyn.size()<<"\n"; for(int i=0; i< wyn.size(); i++){ cout<<wyn[i]<<" "; } return 0; } |
English