#!/usr/bin/env python3 """Route 4, job 764: explicit weighted certificates at the key prefixes. Recomputes the min-max weights deterministically for a named prefix and prints the certificate in the 19-numbers form, then tests @mikecann's dead-slot construction w(x) = 3, w(other) = 2 (his #1173), which the tight case explains: the phases that kill a dead slot are strictly below their prime's maximum, so every prime contributes exactly 2*M(q), and 2*sum_q M(q) < 2*|D| + 1. python3 certificates.py 53 67 68 """ import importlib.util import sys from collections import Counter from fractions import Fraction spec = importlib.util.spec_from_file_location("r4w", "job764/route4-weighted.py") R4 = importlib.util.module_from_spec(spec) spec.loader.exec_module(R4) sys.stdout.reconfigure(newline='\n') def per_prime_max(D, qs, w): """(sum_q max_b W(q,b), {q: max}) for rational weights, exact.""" tot, per = 0, {} for q in qs: W = Counter() for s, x in zip(D, w): if x: W[(-s) % q] += x W[(-s - 2) % q] += x per[q] = max(W.values()) if W else 0 tot += per[q] return tot, per def main(): p = 97 old, qs = R4.old_primes(p), R4.new_primes(p) slots = R4.slots_from(9409, 200, old) sol = R4.WeightedSolver(qs) for arg in sys.argv[1:]: n = int(arg) D = slots[:n] print(f'## prefix n = {n}: L = {D[-1] - 9409 + 1}, slots {D[0]}..{D[-1]}') f1, per1 = per_prime_max(D, qs, [Fraction(1)] * n) print(f' uniform w = 1: sum_q max_b W(q,b) = {f1}, W(D) = {n}, ' f'F1 = |D| - sum_q M(q) = {n - f1}') (f, w, it), slack, cnt = sol.search(D, qs, R4.ITERS) ic = sol.integer_certificate(D, qs, w) print(f' min-max search: min f(w) <= {f:.6f} (at iter {it}); ' f'fractional-cover slack = {slack}') if ic is not None: sw, tot, per = ic print(f' CERTIFICATE (integer w, deficit {tot - sw} of {tot}):') print(f' sum_q max_b W(q,b) = {sw} < sum_s w_s = {tot}') print(' per-prime maxima: ' + ', '.join(f'{q}:{per[q]}' for q in qs)) wi = [int(Fraction(float(x)).limit_denominator(10 ** 6) * 10 ** 6) for x in w] print(' w_s x 10^6: ' + ' '.join(str(x) for x in wi)) else: print(f' no integer certificate from this search (slack {slack})') # @mikecann #1173: w = 3 at a slot in no argmax kill set, 2 elsewhere M, am = {}, {} for q in qs: c = Counter() for s in D: c[(-s) % q] += 1 c[(-s - 2) % q] += 1 M[q] = max(c.values()) am[q] = [b for b in range(q) if c[b] == M[q]] dead = next((s for s in D if not any( (s + b) % q == 0 or (s + b + 2) % q == 0 for q in qs for b in am[q])), None) if dead is not None: w3 = [Fraction(3) if s == dead else Fraction(2) for s in D] f3, per3 = per_prime_max(D, qs, w3) print(f' dead slot x = {dead}; mikecann w(x)=3, others 2: ' f'sum_q max_b W(q,b) = {f3} < W(D) = {2 * n + 1} -> ' f'{"STRICT DEFICIT" if f3 < 2 * n + 1 else "no deficit"}') print(' per-prime maxima: ' + ', '.join(f'{q}:{per3[q]}' for q in qs)) else: print(f' no dead slot at this prefix; the 3/2 construction gives ' f'no deficit (checked: {per_prime_max(D, qs, [Fraction(3)] + [Fraction(2)] * (n - 1))[0]} vs {2 * n + 1})') print() if __name__ == '__main__': main()