#!/usr/bin/env python3 """Faithful Python transcription of the served engine's traversal (a144311_shared_flush.cpp), used as an independent cross-check of the COMPILED binary. This does not assume I know the published indexing of A144311. It reproduces the engine's own DFS from the C++ source line by line and compares its computed value / best / nodes / leaves / records against the binary at several n. A match means the binary computes what its source says. Run: python3 crosscheck_engine.py Exit 0 only if every comparison matches. """ import sys PLIST = [5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83] PSKIP = [2, 5, 4, 9, 6, 13, 8, 10, 21, 25, 14, 29, 16, 18, 20, 41, 45, 24, 49, 53, 28] class Engine: def __init__(self, n, mb): self.n = n self.maxk = n - 2 self.best = mb self.nodes = 0 self.records = 0 self.leaves = 0 # ---- extendTrav: grow arr/v/num from lm to nb ---- def extend(self, st, k, nb): target = nb if nb > st["lm"] else st["lm"] arr, num, v = st["arr"], st["num"], st["v"] if len(arr) > target: st["lm"] = target return for j in range(len(arr), target + 1): arr.append(0) for ki in range(k): if j % PLIST[ki] == st["rem"][ki] or (j + PSKIP[ki]) % PLIST[ki] == st["rem"][ki]: arr[j] = 1 break if arr[j] == 0: num[0] += 1 for ki in range(k + 1, self.maxk): v[ki][j % PLIST[ki]] += 1 v[ki][(j + PSKIP[ki]) % PLIST[ki]] += 1 st["lm"] = target # ---- dfs ---- def dfs(self, st, k): self.nodes += 1 if k < self.maxk: p, skip = PLIST[k], PSKIP[k] for i in range(1, p): if i == skip: continue pos = 0 for ii in range(1, i): if ii != skip: pos += 1 if k < st["splitk"] and pos != st["target"][k]: continue self.extend(st, k, self.best) lm = st["lm"] arr1 = list(st["arr"]); num1 = [st["num"][0]] v1 = [list(row) for row in st["v"]] j = i while j <= lm: if arr1[j] == 0: arr1[j] = 1 num1[0] -= 1 for ki in range(k + 1, self.maxk): v1[ki][j % PLIST[ki]] -= 1 v1[ki][(j + PSKIP[ki]) % PLIST[ki]] -= 1 j += p j = i - skip if j < 0: j += p while j <= lm: if arr1[j] == 0: arr1[j] = 1 num1[0] -= 1 for ki in range(k + 1, self.maxk): v1[ki][j % PLIST[ki]] -= 1 v1[ki][(j + PSKIP[ki]) % PLIST[ki]] -= 1 j += p s = 0 for ki in range(k + 1, self.maxk): si = v1[ki][1] for jj in range(2, PLIST[ki]): if v1[ki][jj] > si: si = v1[ki][jj] s += si if s >= num1[0]: st["rem"][k] = i st2 = {"arr": arr1, "num": num1, "v": v1, "lm": lm, "rem": st["rem"], "target": st["target"], "splitk": st["splitk"]} self.dfs(st2, k + 1) self.extend(st, k, self.best) else: self.leaves += 1 flag = True while flag: st["lm"] += 1 flag = False for i in range(k): if (st["lm"] % PLIST[i] == st["rem"][i] or (st["lm"] + PSKIP[i]) % PLIST[i] == st["rem"][i]): flag = True break st["lm"] -= 1 if st["lm"] >= len(st["arr"]): self.records += 1 if st["lm"] > self.best: self.best = st["lm"] # ---- runTuple / worker ---- def run_tuple(self, mb, splitk, target): st = {"lm": mb, "rem": [0] * self.maxk, "target": target, "splitk": splitk} arr = [0] * (mb + 1) num = [mb] v = [] for i in range(self.maxk): p = PLIST[i] row = [0] * p for j in range(1, p): row[j] = (mb + p - j) // p + (mb + p - j + PSKIP[i]) // p if j == PSKIP[i]: row[j] = 0 elif j < PSKIP[i]: row[j] -= 1 v.append(row) st.update({"arr": arr, "num": num, "v": v}) self.dfs(st, 0) def run(self, mb, splitk): if splitk <= 0: self.run_tuple(mb, splitk, []) return w = [0] * splitk w[splitk - 1] = 1 for kk in range(splitk - 2, -1, -1): w[kk] = w[kk + 1] * (PLIST[kk + 1] - 2) total = (PLIST[0] - 2) * w[0] for t in range(total): target = [(t // w[kk]) % (PLIST[kk] - 2) for kk in range(splitk)] mb_cur = self.best self.run_tuple(mb_cur, splitk, target) return def main(): cases = [(16, 1)] ok = True for n, N in cases: e = Engine(n, 1) e.run(1, 0 if N == 1 else 3) # the engine reports value = 6*best+5; for N=1 old-style, splitk=0 print(f"n={n} N={N}: value={6*e.best+5} best={e.best} nodes={e.nodes} " f"records={e.records} leaves={e.leaves}") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())