#include <bits/stdc++.h>
using namespace std;
int x[1100000][4];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
x[0][2]=0;
x[1][2]=1;
x[2][2]=1;
x[3][2]=2;
x[1][0]=0;
x[2][0]=1;
x[3][0]=1;
x[4][0]=2;
x[1][1]=1;
x[2][1]=2;
x[3][1]=3;
x[4][1]=3;
int binary=4, L=5, no=4, n;
cin >> n;
while(L<=n)
{
x[no][2]=x[(no-binary)][2]+1;
for(int i=x[no][2]; i>0; i=i-1)
{
x[L-i+x[no][2]][0]=L-i;
x[L-i+x[no][2]][1]=no;
}
L=L+x[no][2];
no=no+1;
if((binary*2)<=no)
{
binary=binary*2;
}
}
int trans=n;
int c=0;
while(trans>0)
{
x[c][3]=x[trans][1];
trans=x[trans][0];
c=c+1;
}
cout << c << "\n";
for(int j=0; j<c; j++)
{
cout << x[j][3] << " ";
}
}
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 | #include <bits/stdc++.h> using namespace std; int x[1100000][4]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); x[0][2]=0; x[1][2]=1; x[2][2]=1; x[3][2]=2; x[1][0]=0; x[2][0]=1; x[3][0]=1; x[4][0]=2; x[1][1]=1; x[2][1]=2; x[3][1]=3; x[4][1]=3; int binary=4, L=5, no=4, n; cin >> n; while(L<=n) { x[no][2]=x[(no-binary)][2]+1; for(int i=x[no][2]; i>0; i=i-1) { x[L-i+x[no][2]][0]=L-i; x[L-i+x[no][2]][1]=no; } L=L+x[no][2]; no=no+1; if((binary*2)<=no) { binary=binary*2; } } int trans=n; int c=0; while(trans>0) { x[c][3]=x[trans][1]; trans=x[trans][0]; c=c+1; } cout << c << "\n"; for(int j=0; j<c; j++) { cout << x[j][3] << " "; } } |
English