allocate mem(2^30)

row(n) = {
	my (b=Vecrev(binary(n)), m=matrix(1+#b, 1+#b));

	\\ m[1+i,j+1] -> values with i and j bits consumed
	\\				 encoded as: real part = value so far
	\\							 imaginary part = 2^(# bits so far)

	for (i=1, 1+#b,
		for (j=1, 1+#b,
			if (i==1 && j==1,
					m[i,j] = [0+I],

					m[i,j] = Set(concat([
						if (i>1,
								apply (s -> s + imag(s)*(b[i-1]+I), m[i-1,j]),
								[]
						),
						if (j>1,
								apply (s -> s + imag(s)*(b[j-1]+I), m[i,j-1]),
								[]
						),
						if (i>1 && j>1 && b[i-1]==b[j-1],
								apply (s -> s + imag(s)*(b[i-1]+I), m[i-1,j-1]),
								[]
						)
					]));
			);
		);
	);

	Set(apply(real, m[1+#b, 1+#b]))
}

{
	print ("0 0");

	a = vector(2^13);
	u = 1;

	for (n=1, oo,
		r = row(n);
		for (k=1, #r,
			if (r[k] > #a,
					break,
				a[r[k]]==0,
					a[r[k]] = n;
					while (a[u],
						print (u " " a[u]);
						if (u++ > #a,
							break (3);
						);
					);
			);
		);
	);
}

quit