#!/usr/bin/env python3 """Job #2030 (triage of route 86): all-seed completion count. For a wheel T_w and a level x > w with record G = G2(x#), for EVERY seed s in T_w read the inner T_w slots of [s, s+G] and count (exact DP over covered-slot masks) the alignments j mod x#/w# such that every inner slot is killed by some prime w < q <= x and both endpoints survive. Sum over seeds must equal nmax (record multiplicity); the number of attaining seeds tests 'one seed and its mirror'. Usage: python allseed2030.py w x G nmax -> JSON line on stdout, progress on stderr.""" import sys, math, json, time def primorial(x): r = 1 for q in range(2, x + 1): if all(q % d for d in range(2, int(q ** 0.5) + 1)): r *= q return r def primes_between(a, b): return [q for q in range(a + 1, b + 1) if all(q % d for d in range(2, int(q ** 0.5) + 1))] def run(w, x, G, nmax): Pw = primorial(w); Qs = primes_between(w, x) slots = [r for r in range(Pw) if math.gcd(r, Pw) == 1 and math.gcd(r + 2, Pw) == 1] isslot = bytearray(Pw) for r in slots: isslot[r] = 1 Pwq = {q: Pw % q for q in Qs} t0 = time.time(); total = 0; attaining = [] for idx, s in enumerate(slots): inner = [o for o in range(1, G) if isslot[(s + o) % Pw]] n = len(inner); full = (1 << n) - 1 if n == 0: continue # counting prune: sum over primes of the max kill (over endpoint-alive residues) must reach n tables = []; cap = 0 for q in Qs: tab = []; best = 0 for r in range(q): base = (r * Pwq[q] + s) % q if base in (0, q - 2) or (base + G) % q in (0, q - 2): continue killed = 0 for k, o in enumerate(inner): if (base + o) % q in (0, q - 2): killed |= 1 << k tab.append(killed); c = bin(killed).count('1'); best = max(best, c) tables.append(tab); cap += best if cap < n: continue dp = {0: 1} for tab in tables: nd = {} for m, c in dp.items(): for killed in tab: mm = m | killed; nd[mm] = nd.get(mm, 0) + c dp = nd cnt = dp.get(full, 0) if cnt: total += cnt; attaining.append((s, n, cnt)) if idx % 20000 == 0 and idx: print(f"[progress] w={w} x={x} {idx}/{len(slots)} seeds, total so far {total}, {time.time()-t0:.0f}s", file=sys.stderr, flush=True) res = dict(wheel=w, x=x, G=G, nmax=nmax, seeds=len(slots), sum_completions=total, matches_nmax=(total == nmax), attaining_seeds=[dict(seed=s, inner=n, completions=c) for s, n, c in attaining], n_attaining_seeds=len(attaining), seconds=round(time.time() - t0, 1)) return res if __name__ == '__main__': w, x, G, nmax = map(int, sys.argv[1:5]) print(json.dumps(run(w, x, G, nmax)))