// kstar.c -- K*(s) = longest run of consecutive level-s slots all killed by the // entering primes Q = {q prime : s < q <= 2s}, computed by the BLOCK+PHASE // reduction instead of the full P(2s)# walk. // // Reduction (exact): // * level-s slots = {r : r and r+2 are coprime to P(s)#}; periodic with period // P(s)#, so in ANY block the slot positions (shifted home) are the same list. // * a slot r is killed by q iff q | r or q | r+2, i.e. iff r mod q lies in the // 2-set {a, a-2} ("phase a" of q). // * in block k the phase of q is (k*P(s)#) mod q; gcd(P(s)#,q)=1, so as k runs // over Z/prod(Q) the phase vector runs over ALL of prod_q Z_q (CRT). // ==> K*(s) >= L <=> some cyclic window of L consecutive slots of the block // [0,P(s)#) is covered by some choice of one phase per q in Q. // // Usage: kstar [lo] [hi] // lo,hi : the half-open range of window STARTING slots to test // (default 0..P(s)#, i.e. every cyclic window of the block, including // the ones that wrap past P(s)#). Splitting [0,P(s)#) into disjoint // segments tests every window exactly once, in parallel. #include #include #include #include static int NQ, L; static int qn[16]; static int cnt[8][64], pc[8][64], hist[8][128], mx[8]; static uint8_t ring[64][8]; static uint64_t posring[64]; static int rhead = -1, wfull = 0; static long long NWIN = 0, NFILT = 0, NDFS = 0, NNODE = 0, NSLOT = 0; static int found = 0; static uint64_t wpos = 0; static int wclass[8][2], wused[8]; static uint32_t FULLMASK; static int nprimes_le(int v, int *P) { int n = 0; for (int k = 2; k <= v; k++) { int ok = 1; for (int d = 2; d * d <= k; d++) if (k % d == 0) { ok = 0; break; } if (ok) P[n++] = k; } return n; } static int primes_in(int lo, int hi, int *P) { int n = 0; for (int k = lo + 1; k <= hi; k++) { int ok = 1; for (int d = 2; d * d <= k; d++) if (k % d == 0) { ok = 0; break; } if (ok) P[n++] = k; } return n; } static inline int pcnt(uint32_t x) { return __builtin_popcount(x); } static inline void pc_upd(int j, int a, int sgn) { int v = pc[j][a]; hist[j][v]--; v += sgn; pc[j][a] = v; hist[j][v]++; if (v > mx[j]) mx[j] = v; } static void win_init(void) { memset(cnt, 0, sizeof cnt); memset(pc, 0, sizeof pc); memset(hist, 0, sizeof hist); memset(mx, 0, sizeof mx); for (int j = 0; j < NQ; j++) hist[j][0] = qn[j]; rhead = -1; wfull = 0; } static inline void win_add(const uint8_t *res) { for (int j = 0; j < NQ; j++) { int q = qn[j], c = res[j], a2 = c + 2; if (a2 >= q) a2 -= q; pc_upd(j, c, +1); pc_upd(j, a2, +1); cnt[j][c]++; } } static inline void win_del(const uint8_t *res) { for (int j = 0; j < NQ; j++) { int q = qn[j], c = res[j], a2 = c + 2; if (a2 >= q) a2 -= q; pc_upd(j, c, -1); pc_upd(j, a2, -1); cnt[j][c]--; while (mx[j] > 0 && hist[j][mx[j]] == 0) mx[j]--; } } static inline uint8_t *slotat(int i) { return ring[(rhead + 1 + i) % L]; } static inline uint64_t slotpos(int i) { return posring[(rhead + 1 + i) % L]; } // exact covering test: choose one phase per prime so every window slot is hit static int dfs(uint32_t cov, int used) { NNODE++; if (cov == FULLMASK) return 1; int i = 0; while (cov >> i & 1) i++; int rem = L - pcnt(cov), cap = 0; for (int j = 0; j < NQ; j++) if (!(used >> j & 1)) cap += mx[j]; if (cap < rem) return 0; for (int j = 0; j < NQ; j++) { if (used >> j & 1) continue; int q = qn[j], c = slotat(i)[j]; for (int t = 0; t < 2; t++) { int a = (t == 0) ? c : (c + 2 >= q ? c + 2 - q : c + 2); int a2 = a - 2; if (a2 < 0) a2 += q; uint32_t m = cov; for (int s2 = 0; s2 < L; s2++) if (!(m >> s2 & 1)) { int rr = slotat(s2)[j]; if (rr == a || rr == a2) m |= (uint32_t)1 << s2; } if (m == cov) continue; if (dfs(m, used | (1 << j))) { wclass[j][0] = a; wclass[j][1] = a2; wused[j] = 1; return 1; } } } return 0; } static uint64_t TESTFROM = 0, TESTTO = 0; static void test_window(void) { // a window is identified by its FIRST slot; test exactly those whose first // slot starts in [TESTFROM, TESTTO) if (slotpos(0) < TESTFROM || slotpos(0) >= TESTTO) return; NWIN++; int cap = 0; for (int j = 0; j < NQ; j++) cap += mx[j]; if (cap < L) return; NFILT++; for (int j = 0; j < NQ; j++) wused[j] = 0; NDFS++; if (dfs(0, 0)) { found = 1; wpos = slotpos(0); printf("WITNESS L=%d start=%llu\n", L, (unsigned long long)wpos); printf(" slots:"); for (int i = 0; i < L; i++) printf(" %llu", (unsigned long long)slotpos(i)); printf("\n phases:"); for (int j = 0; j < NQ; j++) if (wused[j]) printf(" q%d={%d,%d}", qn[j], wclass[j][0], wclass[j][1]); printf("\n"); fflush(stdout); } } static inline void feed(const uint8_t *res, uint64_t pos) { if (!wfull) { rhead = (rhead + 1) % L; memcpy(ring[rhead], res, NQ); posring[rhead] = pos; win_add(res); if (rhead == L - 1) { wfull = 1; test_window(); } } else { uint8_t *old = ring[(rhead + 1) % L]; win_del(old); rhead = (rhead + 1) % L; memcpy(ring[rhead], res, NQ); posring[rhead] = pos; win_add(res); test_window(); } } int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "usage: kstar s L [lo] [hi]\n"); return 2; } int s = atoi(argv[1]); L = atoi(argv[2]); if (L < 1 || L > 31) { fprintf(stderr, "L must be 1..31\n"); return 2; } int P[64], np = nprimes_le(s, P); uint64_t Pv = 1; for (int i = 0; i < np; i++) Pv *= (uint64_t)P[i]; uint64_t Dv = 1; for (int i = 0; i < np; i++) if (P[i] >= 3) Dv *= (uint64_t)(P[i] - 2); NQ = primes_in(s, 2 * s, qn); uint64_t lo = (argc > 3) ? strtoull(argv[3], 0, 10) : 0; uint64_t hi = (argc > 4) ? strtoull(argv[4], 0, 10) : Pv; FULLMASK = (1u << L) - 1; static int gmod[512][8]; for (int g = 0; g < 512; g++) for (int j = 0; j < NQ; j++) gmod[g][j] = g % qn[j]; int pvm[8]; for (int j = 0; j < NQ; j++) pvm[j] = (int)(Pv % (uint64_t)qn[j]); printf("kstar s=%d L=%d P(s)#=%llu D_s=%llu NQ=%d range=[%llu,%llu)\n", s, L, (unsigned long long)Pv, (unsigned long long)Dv, NQ, (unsigned long long)lo, (unsigned long long)hi); printf(" Q ="); for (int j = 0; j < NQ; j++) printf(" %d", qn[j]); printf("\n"); fflush(stdout); const uint64_t SEG = 1u << 22; uint8_t *seg = malloc(SEG); if (!seg) { fprintf(stderr, "alloc fail\n"); return 3; } uint64_t prevpos = 0; uint8_t cur[8]; TESTFROM = lo; TESTTO = hi; // sieve [lo-WARM, hi+WARM): the walk must run past hi so that windows whose // first slot is in [lo,hi) are completed and tested, and past Pv for the // wrap-around windows (the slot pattern and the sieve are periodic mod Pv). const uint64_t WARM = 20000; // >= span of an L=31 window uint64_t sieve0 = (lo > WARM) ? (lo - WARM) : 0; uint64_t sieve1 = hi + WARM; for (uint64_t base = sieve0; base < sieve1 && !found; base += SEG) { uint64_t len = (base + SEG <= sieve1) ? SEG : (sieve1 - base); memset(seg, 0, len); for (int i = 0; i < np; i++) { uint64_t p = (uint64_t)P[i]; uint64_t st = (p - base % p) % p; for (uint64_t r = st; r < len; r += p) seg[r] = 1; uint64_t st2 = ((p - 2) % p + p - base % p) % p; for (uint64_t r = st2; r < len; r += p) seg[r] = 1; } for (uint64_t r = 0; r < len; r++) { if (seg[r]) continue; uint64_t pos = base + r; if (NSLOT == 0) { for (int j = 0; j < NQ; j++) cur[j] = (uint8_t)(pos % (uint64_t)qn[j]); win_init(); } else { uint64_t g = pos - prevpos; if (g < 512) for (int j = 0; j < NQ; j++) { int c = cur[j] + gmod[g][j]; if (c >= qn[j]) c -= qn[j]; cur[j] = (uint8_t)c; } else for (int j = 0; j < NQ; j++) cur[j] = (uint8_t)(pos % (uint64_t)qn[j]); } prevpos = pos; NSLOT++; feed(cur, pos); if (found) break; } } (void)pvm; printf("RESULT s=%d L=%d slots=%lld windows=%lld filter_pass=%lld dfs=%lld nodes=%lld found=%d", s, L, NSLOT, NWIN, NFILT, NDFS, NNODE, found); if (found) printf(" at=%llu", (unsigned long long)wpos); printf("\n"); return found ? 0 : 1; }