s = 0
unseen = 1
seen(v) = bit test(s, v)
see(v) = s = bit or(s, 2^v); while (seen(unseen), unseen++)

\\ first unseen number v such that p AND v = 0
cc = [0]
avoid(p) = {
	if (!seen(0),
		return (0);
	);

	my (w=1, b, c);
	for (e=0, oo,
		if (!bittest(p, e),
			if (#cc==w,
				cc=concat(cc, vector(#cc));
			);
			b = 2^e;
			for (k=1, w,
				c = cc[w+k] = b+cc[k];
				if (!seen(c),
					return (c);
				);
			);

			w*=2;
		);
	);
}

{
	x = [0];
	a = vector(10 000);
	u = 1;

	see(0);
	print ("0 0");

	for (n=1, oo,
		see(v=avoid(x[n]));

		if (v<=#a,
			a[v] = n;
			while (a[u],
				print (u " " a[u]);
				if (u++ > #a,
					break (2);
				);
			);
		);

		while (n+v > #x,
			x = concat(x, vector(#x));
		);
		x[n+v] = bitor(x[n+v], v);
	);
}

quit