"""Job #4495 (route 15 pursuit): a full-period CRT covering census of the twin-Jacobsthal record at x = 23..43, over every T_19 slot residue r mod 19#, with the free primes 23..x as the only unknowns. Definitions (as in #1791's copyscan.c and #2006's crtmodel): n is a T_x slot iff gcd(n(n+2), x#) = 1. A copy p = r + j*19# (0 <= j < x#/19#) has residue vector (p mod q)_{19 vector is a bijection onto prod Z/q (CRT). Residue m of p mod q kills offset k (i.e. p+k is not a T_x slot because of q) iff (m+k) % q in {0, q-2}. COUNT(r, G): the number of vectors that spare offsets 0 and G and kill every T_19 slot strictly inside (r, r+G). Needs r and r+G to be T_19 slots (both checked here; #2006's offsets() checked only r+G). It equals the number of copies of r whose forward T_x gap is exactly G, so sum_r COUNT(r, G) = the number of length-G gaps mod x#. CERT(r, G): the number of vectors that spare offset 0 and kill every T_19 slot in (r, r+G]. A T_x gap longer than G opens at some copy of some T_19 slot r iff CERT(r, G) > 0 (all slots are 5 mod 6, so every gap is a multiple of 6 and a gap > G means no slot in (p, p+G]). So CERT(r, G) = 0 for all r certifies G2(x#) <= G. Both are exact integer counts by a DFS over the free primes with a capacity bound (remaining uncovered slots vs the sum of the remaining primes' best single-class kill) and a reach bound; a pruned branch provably has no completion. Usage: python census4495.py [w=19] (r ranges over the T_19 slots with index in [lo, hi)) Prints one line per r with a nonzero count, then a SUMMARY line. """ import sys, bisect import numpy as np PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53] W = 19 # base wheel; setbase() changes it (the validation runs use w < 19) SMALL = [q for q in PRIMES if q <= W] B19 = 9699690 # = W# (the name is kept from the w = 19 census) FREE_ALL = [q for q in PRIMES if q > W] VCAP = 64 # covering vectors are listed explicitly when a count is <= VCAP def setbase(w): global W, SMALL, B19, FREE_ALL W = w; SMALL = [q for q in PRIMES if q <= w]; FREE_ALL = [q for q in PRIMES if q > w] B19 = 1 for q in SMALL: B19 *= q def slots19(): f = np.ones(B19, dtype=bool) for q in SMALL: f[0::q] = False f[(q - 2) % q::q] = False return f def problem(r, G, Q, mode, Sext, flags, xforb=()): """Returns (count, vectors or None). vectors listed only when count <= VCAP. xforb: extra offsets (relative to r) whose kill is forbidden; used only to reproduce #2006's crt_count on its extras.""" if mode == 'count': if not flags[(r + G) % B19]: return 0, None hi = r + G - 1 else: hi = r + G i0 = bisect.bisect_right(Sext, r); i1 = bisect.bisect_right(Sext, hi) offs = [s - r for s in Sext[i0:i1]] n = len(offs); need = (1 << n) - 1 per = [] # per prime: (q, list of (mask, [classes]), n_admissible, best popcount) for q in Q: forb = {0, q - 2} if mode == 'count': forb |= {(-G) % q, (-G - 2) % q} for e in xforb: forb |= {(-e) % q, (-e - 2) % q} km = {} for t, k in enumerate(offs): a = (-k) % q; b = (-k - 2) % q km[a] = km.get(a, 0) | (1 << t) if b != a: km[b] = km.get(b, 0) | (1 << t) groups = {} nadm = 0 for m in range(q): if m in forb: continue nadm += 1 groups.setdefault(km.get(m, 0), []).append(m) items = sorted(groups.items(), key=lambda kv: -bin(kv[0]).count('1')) per.append((q, items, nadm, bin(items[0][0]).count('1') if items else 0)) if n == 0: c = 1 for p in per: c *= p[2] return c, None # capacity gate if sum(p[3] for p in per) < n: return 0, None per.sort(key=lambda p: -p[3]) nq = len(per) capsuf = [0] * (nq + 1); reachsuf = [0] * (nq + 1); admsuf = [1] * (nq + 1) for i in range(nq - 1, -1, -1): capsuf[i] = capsuf[i + 1] + per[i][3] rr = 0 for mk, _ in per[i][1]: rr |= mk reachsuf[i] = reachsuf[i + 1] | rr admsuf[i] = admsuf[i + 1] * per[i][2] vecs = [] total = 0 def rec(i, done, chosen, mult): nonlocal total if done == need: total += mult * admsuf[i] if len(vecs) <= VCAP: # (group multiplicity is expanded below) vecs.append((tuple(chosen), i)) return if i == nq: return left = need & ~done if (reachsuf[i] & left) != left: return if bin(left).count('1') > capsuf[i]: return for mk, ms in per[i][1]: chosen.append((per[i][0], ms)) rec(i + 1, done | mk, chosen, mult * len(ms)) chosen.pop() rec(0, 0, [], 1) out = None if total <= VCAP: # expand to explicit class vectors (free primes not chosen take every admissible class) out = [] for chosen, i in vecs: partial = [[]] for q, ms in chosen: partial = [v + [(q, m)] for v in partial for m in ms] for j in range(i, nq): q = per[j][0] forb = {0, q - 2} | ({(-G) % q, (-G - 2) % q} if mode == 'count' else set()) for e in xforb: forb |= {(-e) % q, (-e - 2) % q} partial = [v + [(q, m)] for v in partial for m in range(q) if m not in forb] out.extend(sorted(v) for v in partial) return total, out def main(): x, G, mode, lo, hi = int(sys.argv[1]), int(sys.argv[2]), sys.argv[3], int(sys.argv[4]), int(sys.argv[5]) if len(sys.argv) > 6: setbase(int(sys.argv[6])) Q = [q for q in FREE_ALL if q <= x] flags = slots19() S = np.nonzero(flags)[0] if W == 19: assert len(S) == 378675 Sext = [int(v) + k * B19 for k in range(2 + G // B19 + 1) for v in S] # enough periods to cover (r, r+G] tot = 0; nz = 0 for idx in range(lo, min(hi, len(S))): r = int(S[idx]) c, vecs = problem(r, G, Q, mode, Sext, flags) if c: nz += 1; tot += c print(f'NZ {mode} x={x} G={G} r={r} count={c} vectors={vecs}', flush=True) print(f'SUMMARY {mode} x={x} G={G} idx[{lo},{hi}) nonzero_r={nz} total={tot}', flush=True) if __name__ == '__main__': main()