s = 0 unseen = 1 seen(v) = bittest(s, v) see(v) = s = bitor(s, 2^v); while (seen(unseen), unseen++) { hh = [0]; x = 0; \\ mask of forbidden values v0 = 1; \\ least possible value for (n = 1, #a = vector(10 000), for (v = v0, oo, if (!bittest(x, v), ok = 1; m = 0; \\ mask of earlier values nb = 0; \\ number of prior terms equal to v forstep (k = n-1, 1, -1, if (a[k]==v, h = hammingweight(m); if (nb && hh[nb]==h, \\ v is forbidden x += 2^v; while (bittest(x, v0), v0++; ); ok = 0; break; ); if (seen(h), ok = 0; break; ); if (nb++ > #hh, hh = concat(hh, vector(#hh)); ); hh[nb] = h; ); m = bitor(m, 2^a[k]); ); if (ok, for (k = 1, nb, see(hh[k]); ); print (n " " a[n] = v); break; ); ); ); ); } quit