#!/usr/bin/env python3 """indep_w19.py -- job #4128 (route 2): indep2022.py adapted to (W, Q, L, a) arguments. Independent all-phase check, from the definitions of #350/#1076 (not lossscan762.c and not its candidate-phase reduction): every one of the q0*q1*q2*q3 phases is enumerated. A support offset is s in [1,L] with gcd(a+s,W)=gcd(a+s+2,W)=1; prime q at phase t kills s iff (s+t) mod q in {0, q-2}; exact = N - |union of the four killed sets| (survivors) atom(i,j) = |A_i & A_j minus (A_k | A_l)| quadruple = |A_0 & A_1 & A_2 & A_3| chordal loss = min(exact, min atom) envelope loss = min(exact, min atom, quadruple) Usage: indep_w19.py W a:L [a:L ...] -- q0 q1 q2 q3 Prints one JSON object per (a,L) on stdout. """ import json import math import sys from itertools import combinations def check(W, Q, a, L): off = [s for s in range(1, L + 1) if math.gcd(a + s, W) == 1 and math.gcd(a + s + 2, W) == 1] N = len(off) full = (1 << N) - 1 masks = [[sum(1 << k for k, s in enumerate(off) if (s + t) % q in (0, q - 2)) for t in range(q)] for q in Q] pc = int.bit_count maxlc = 0 maxle = 0 npos_c = 0 npos_e = 0 best_c = None best_e = None for t0 in range(Q[0]): A0 = masks[0][t0] for t1 in range(Q[1]): A1 = masks[1][t1] u01 = A0 | A1 for t2 in range(Q[2]): A2 = masks[2][t2] u012 = u01 | A2 for t3 in range(Q[3]): A = (A0, A1, A2, masks[3][t3]) S = N - pc(u012 | A[3]) atoms = [pc(A[i] & A[j] & ~(A[k] | A[l]) & full) for (i, j) in combinations(range(4), 2) for (k, l) in [tuple(x for x in range(4) if x not in (i, j))]] quad = pc(A[0] & A[1] & A[2] & A[3]) lc = min(S, min(atoms)) le = min(lc, quad) if lc: npos_c += 1 if lc > maxlc: maxlc, best_c = lc, {"phase": [t0, t1, t2, t3], "exact": S, "atoms": atoms, "quadruple": quad} if le: npos_e += 1 if le > maxle: maxle, best_e = le, {"phase": [t0, t1, t2, t3], "exact": S, "atoms": atoms, "quadruple": quad} return {"W": W, "q": list(Q), "a": a, "L": L, "N": N, "phases_enumerated": Q[0] * Q[1] * Q[2] * Q[3], "max_chordal_loss": maxlc, "max_envelope_loss": maxle, "phases_with_chordal_loss": npos_c, "phases_with_envelope_loss": npos_e, "chordal_witness": best_c, "envelope_witness": best_e} def main(): argv = sys.argv[1:] if "--" in argv: i = argv.index("--") pairs, qs = argv[:i], [int(x) for x in argv[i + 1:]] else: pairs, qs = argv[:1], [int(x) for x in argv[1:]] W = int(pairs[0]) for spec in pairs[1:]: a, L = (int(x) for x in spec.split(":")) print(json.dumps(check(W, qs, a, L)), flush=True) return 0 if __name__ == "__main__": sys.exit(main())