torl(n) = { my (rr=[]); while (n, my (r=valuation(n+(n%2), 2)); rr = concat(r, rr); n\=2^r); rr }
fromrl(rr) = { my (v=0); for (k=1, #rr, v = (v+(k%2))*2^rr[k]-(k%2)); v }

{
	print ("0 0");
	a = vector(2^13-1);
	for (n=1, #a,
		if (!a[n],
			mm = [0]; nb=0;
			forperm (vecsort(torl(n)), m, if (nb++>#mm, mm=concat(mm, vector(#mm))); mm[nb]=fromrl(m));
			mm = vecsort(mm[1..nb]);
			for (k=1, #mm,
				a[mm[k]] = mm[#mm+1-k];
			);
		);
		print (n " " a[n]);
	);
}

quit