/* wincov.c (job 4318, route 80): independent exact check of a seam exceedance for one prime set Q at tile T_x, for sets too large for brute.c's full residue product. Shares no code with certk.py. covers(S): can the slot list S be killed by the primes of Q, each with ONE residue A (kills n iff n = A or A+2 mod q)? Exact search: the lowest unkilled slot s must be killed by some unused prime q, and then A is s mod q or (s - 2) mod q: at most 2|Q| branches per level, |Q| levels. Every assignment that kills S is reached. nonwrap = the largest L such that some L consecutive block-0 slots are coverable (monotone in L). seam = the largest u + v + 1 over two-sided windows {c - g_{u-1..0}, c, c + g_{0..v-1}} (slots of blocks 0..1 around c = P - 1) that are coverable; prints a residue vector for the best window. Q is exactly the given primes (no extra singles). Build: cc -O2 -o wincov wincov.c usage: ./wincov x q1 q2 ... */ #include #include #include static int k, qs[16], used[16], A_of[16], best_A[16]; static long *S; static int nS; static int killed_by(long n, int i) { long m = ((n - A_of[i]) % qs[i] + qs[i]) % qs[i]; return m == 0 || m == 2; } static int rec(void) { int s = -1; for (int t = 0; t < nS && s < 0; t++) { int dead = 0; for (int i = 0; i < k && !dead; i++) if (used[i] && killed_by(S[t], i)) dead = 1; if (!dead) s = t; } if (s < 0) return 1; for (int i = 0; i < k; i++) { if (used[i]) continue; long cand[2] = { ((S[s]) % qs[i] + qs[i]) % qs[i], ((S[s] - 2) % qs[i] + qs[i]) % qs[i] }; for (int c = 0; c < 2; c++) { used[i] = 1; A_of[i] = (int)cand[c]; if (rec()) return 1; used[i] = 0; } } return 0; } static int covers(long *list, int n) { S = list; nS = n; memset(used, 0, sizeof used); return rec(); } static int isprime(int p) { if (p < 2) return 0; for (int d = 2; d * d <= p; d++) if (p % d == 0) return 0; return 1; } int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "usage: wincov x q1 q2 ...\n"); return 2; } int x = atoi(argv[1]); k = argc - 2; for (int i = 0; i < k; i++) qs[i] = atoi(argv[2 + i]); long P = 2; for (int p = 3; p <= x; p++) if (isprime(p)) P *= p; long *R = malloc(sizeof(long) * 2 * P); int NR = 0, D = 0; for (long n = 1; n < 2 * P; n += 2) { int ok = 1; for (int p = 3; p <= x && ok; p++) if (isprime(p) && (n % p == 0 || (n + 2) % p == 0)) ok = 0; if (ok) { R[NR++] = n; if (n < P) D++; } } if (R[D - 1] != P - 1) { fprintf(stderr, "c is not the last slot\n"); return 2; } /* nonwrap: largest coverable run of consecutive block-0 slots */ int nw = 0; for (int L = 1; L <= D; L++) { int any = 0; for (int i = 0; i + L <= D && !any; i++) if (covers(R + i, L)) any = 1; if (!any) break; nw = L; } /* seam: two-sided windows around c (index D-1 in R), u <= D-2 left slots, v <= D-1 right slots */ int c = D - 1, bs = 0, bu = 0, bv = 0; for (int L = 3; L <= 2 * D - 2; L++) { int any = 0; for (int u = 1; u <= L - 2 && !any; u++) { int v = L - 1 - u; if (u > D - 2 || v > D - 1) continue; if (covers(R + c - u, L)) { any = 1; bs = L; bu = u; bv = v; memcpy(best_A, A_of, sizeof A_of); } } if (!any) break; } printf("{\"x\": %d, \"P\": %ld, \"D\": %d, \"Q\": [", x, P, D); for (int i = 0; i < k; i++) printf("%s%d", i ? ", " : "", qs[i]); printf("], \"max_nonwrap\": %d, \"max_seam\": %d, \"seam_uv\": [%d, %d], \"exceedance\": %s, \"whole_block_killable\": %s, \"seam_residues\": [", nw, bs, bu, bv, bs > nw ? "true" : "false", nw >= D ? "true" : "false"); for (int i = 0; i < k; i++) printf("%s%d", i ? ", " : "", best_A[i]); printf("], \"seam_slots\": ["); for (int t = c - bu; t <= c + bv; t++) printf("%s%ld", t > c - bu ? ", " : "", R[t]); printf("]}\n"); return 0; }