#!/usr/bin/env python3 """Route 168, job 4255: faithful CNF for the A144311 rung "R consecutive multiples of 6 coverable at the first n primes", solved by CaDiCaL 1.9.5 (python-sat), DRAT proof on UNSAT. python sat4255.py n R [--proof out.drat] [--cnf out.cnf] [--conf MAX_CONFLICTS] Object. a(n) = A144311(n) = 6R + 5 (n > 1): with 2 and 3 every integer that is not a multiple of 6 is +-1 mod 2 or mod 3, so a run of 6R + 5 integers 6K+1 .. 6(K+R)+5 is covered iff its R multiples 6(K+j), j = 1..R, are each +-1 mod some prime 5 <= p <= p_n. For each such p the window's offset K mod p is free (CRT), so the decision variable is y[p,s] = "K = s mod p" (p values per prime, at most one true), NOT a choice between the residues +1 and -1: once K mod p is fixed, 6(K+j) = +-1 mod p hits both classes j = +-u - K (u = 6^-1 mod p). Clause for position j: OR over p, over s in {(u-j) mod p, (-u-j) mod p} of y[p,s]. Unassigned primes are harmless (any offset may be chosen), so only at-most-one is needed; pairwise AMO. SAT: decode K by CRT and check the ORIGINAL statement on the 6R+5 consecutive integers (bigints), independently of the CNF. UNSAT: write the DRAT proof for drat-trim. """ import argparse import sys import time def primes_upto_n(n): ps, c = [], 2 while len(ps) < n: if all(c % q for q in ps if q * q <= c): ps.append(c) c += 1 return ps def encode(n, R): ps = primes_upto_n(n) var, nv = {}, 0 for p in ps[2:]: for s in range(p): nv += 1 var[p, s] = nv cls = [] for p in ps[2:]: vs = [var[p, s] for s in range(p)] for i in range(p): for k in range(i + 1, p): cls.append([-vs[i], -vs[k]]) for j in range(1, R + 1): c = [] for p in ps[2:]: u = pow(6, -1, p) for s in sorted({(u - j) % p, (-u - j) % p}): c.append(var[p, s]) cls.append(c) return ps, var, nv, cls def check_run(ps, offs, R): """offs[p] = K mod p. Build K by CRT (K = 0 mod 6), check 6K+1 .. 6(K+R)+5 against the definition.""" K, M = 0, 6 for p in ps[2:]: s = offs.get(p, 0) # K' = K + M*t, want K' = s mod p (K was chosen = 0 mod 6, so the 6 | M factor keeps that) t = ((s - K) * pow(M, -1, p)) % p K, M = K + M * t, M * p lo, hi = 6 * K + 1, 6 * (K + R) + 5 bad = [x for x in range(lo, hi + 1) if not any(x % q in (1, q - 1) for q in ps)] return hi - lo + 1, bad def main(): ap = argparse.ArgumentParser() ap.add_argument('n', type=int) ap.add_argument('R', type=int) ap.add_argument('--proof') ap.add_argument('--cnf') ap.add_argument('--conf', type=int, default=0) a = ap.parse_args() ps, var, nv, cls = encode(a.n, a.R) if a.cnf: with open(a.cnf, 'w') as fh: fh.write(f'p cnf {nv} {len(cls)}\n') for c in cls: fh.write(' '.join(map(str, c)) + ' 0\n') from pysat.solvers import Cadical195 t0 = time.process_time() s = Cadical195(bootstrap_with=cls, with_proof=bool(a.proof)) if a.conf: s.conf_budget(a.conf) st = s.solve_limited() else: st = s.solve() dt = time.process_time() - t0 stats = s.accum_stats() out = {'n': a.n, 'p_n': ps[-1], 'R': a.R, 'run_len': 6 * a.R + 5, 'vars': nv, 'clauses': len(cls), 'status': {True: 'SAT', False: 'UNSAT', None: 'UNDECIDED'}[st], 'conflicts': stats.get('conflicts'), 'decisions': stats.get('decisions')} if st is True: m = set(v for v in s.get_model() if v > 0) offs = {p: sv for (p, sv), v in var.items() if v in m} L, bad = check_run(ps, offs, a.R) out['model_check'] = {'run_len': L, 'uncovered_integers': len(bad)} if st is False and a.proof: pr = s.get_proof() with open(a.proof, 'w') as fh: fh.write('\n'.join(pr) + '\n') out['proof_lines'] = len(pr) out['proof_added'] = sum(1 for l in pr if not l.startswith('d ')) print(__import__('json').dumps(out)) print(f'solver cpu {dt:.2f}s', file=sys.stderr) if __name__ == '__main__': main()