#!/usr/bin/env python3 """Route 4, job 959: is prefix n*-1 (LP-silent) actually coverable? Solves the integer phase-cover problem (one phase per prime in Q, every slot killed) with HiGHS MILP and re-verifies any returned phase vector in plain integer arithmetic. A verified phase vector is a cover (SAT side). An 'infeasible' MILP status is reported as the solver's status only, not as a certificate. stdout is the artifact; no randomness. python cover959.py 9409 52 [more a n pairs] """ import importlib.util import os import sys import numpy as np from scipy.optimize import milp, LinearConstraint, Bounds from scipy.sparse import coo_matrix HERE = os.path.dirname(os.path.abspath(__file__)) spec = importlib.util.spec_from_file_location('rw', os.path.join(HERE, 'r370-route4-weighted.py')) rw = importlib.util.module_from_spec(spec) spec.loader.exec_module(rw) sys.stdout.reconfigure(newline='\n') P = 97 OLD, QS = rw.old_primes(P), rw.new_primes(P) def solve(D, tlim=300): off, o = {}, 0 for q in QS: off[q] = o o += q rows, cols, vals = [], [], [] for qi, q in enumerate(QS): for b in range(q): rows.append(qi); cols.append(off[q] + b); vals.append(1) for i, s in enumerate(D): r = len(QS) + i for q in QS: rows.append(r); cols.append(off[q] + (-s) % q); vals.append(1) rows.append(r); cols.append(off[q] + (-s - 2) % q); vals.append(1) A = coo_matrix((vals, (rows, cols)), shape=(len(QS) + len(D), o)).tocsr() lb = np.concatenate([np.ones(len(QS)), np.ones(len(D))]) ub = np.concatenate([np.ones(len(QS)), np.full(len(D), np.inf)]) res = milp(np.zeros(o), constraints=LinearConstraint(A, lb, ub), integrality=np.ones(o), bounds=Bounds(0, 1), options={'time_limit': tlim, 'disp': False}) if res.x is None: return res.status, res.message, None phase = {} for q in QS: seg = np.round(res.x[off[q]:off[q] + q]).astype(int) assert seg.sum() == 1 phase[q] = int(np.argmax(seg)) return res.status, res.message, phase def main(): args = list(map(int, sys.argv[1:])) for a, n in zip(args[::2], args[1::2]): D = rw.slots_from(a, n, OLD) st, msg, ph = solve(D) if ph is None: print(f'a={a} n={n} L={D[-1] - a + 1}: no phase vector returned; solver status {st} ({msg})') continue # plain integer re-verification: every slot killed by some chosen phase alive = [s for s in D if not any((s + ph[q]) % q == 0 or (s + ph[q] + 2) % q == 0 for q in QS)] tag = 'COVER verified (every slot killed)' if not alive else f'INVALID: {len(alive)} survivors' print(f'a={a} n={n} L={D[-1] - a + 1}: {tag}; phases b_q: ' + ' '.join(f'{q}:{ph[q]}' for q in QS)) if __name__ == '__main__': main()