#include #include #include #define MAX 50000 bool ok(int n) { int s = 0; for (int r=2; n; r++) { int d = 1 << (n%r); if (s & d) { return false; } s += d; n /= r; } return true; } int *pos = 0; // offset to next OK int *nxt = 0; // offset to next OK bool seen[MAX]; int unseen = 1; int other(int p) { seen[p] = true; while (seen[unseen]) { unseen++; } int v = unseen + pos[p+unseen]; while (p+v