/* brute.c (job 4318, route 80): independent exhaustive check of a seam exceedance for one prime set M at tile T_x. Shares no code with certk.py. For EVERY residue vector A in prod_{q in M} Z/q it computes nonwrap_A = longest run of consecutive block-0 slots with at most H alive slots, seam_A = longest run of consecutive slots of blocks 0..1 (integers [0, 2P)) that contains c = P-1 and at least one slot on each side of c, with at most H alive slots, where H in {0, 1} is the number of extra "large" primes (q0 > 2P + 2 kills at most one slot of [0, 2P) and any one slot it likes). Prints max nonwrap, max seam, and a residue vector attaining max seam. Exceedance: max seam > max nonwrap. If max nonwrap < D, no residue vector kills a whole block. Build: cc -O2 -o brute brute.c usage: ./brute x H q1 q2 ... */ #include #include #include #define MAXS 4096 static int x, H, k, D, NR, qs[16]; static long P; static long R[MAXS]; /* slots of [0, 2P) */ static unsigned char *kill[16]; /* kill[i][A * NR + s] : prime i with residue A kills slot s */ static unsigned char dead[MAXS]; /* multiplicity of kills per slot, maintained incrementally */ static int best_nw = -1, best_seam = -1, curA[16], seamA[16], nwA[16]; static int is_slot(long n) { if (n % 2 == 0) return 0; for (int p = 3; p <= x; p += 2) { int pr = 1; for (int d = 3; d * d <= p; d += 2) if (p % d == 0) { pr = 0; break; } if (pr && (n % p == 0 || (n + 2) % p == 0)) return 0; } return 1; } static int longest_block(void) { /* block 0: slots 0..D-1, at most H alive */ int best = 0, i = 0, alive = 0; for (int j = 0; j < D; j++) { alive += !dead[j]; while (alive > H) { alive -= !dead[i]; i++; } if (j - i + 1 > best) best = j - i + 1; } return best; } static int longest_seam(void) { /* runs over R containing c = index D-1, two-sided, at most H alive */ int c = D - 1, best = 0; for (int h = 0; h <= H; h++) { /* h = holes used on the left including c */ int used = 0, i = c; if (!dead[c]) used++; if (used > h) continue; /* extend left as far as possible with at most h holes total (c included) */ while (i - 1 >= 0 && used + !dead[i - 1] <= h) { i--; used += !dead[i]; } int j = c, u2 = used; while (j + 1 < NR && u2 + !dead[j + 1] <= H) { j++; u2 += !dead[j]; } if (i < c && j > c && j - i + 1 > best) best = j - i + 1; } return best; } static void rec(int d) { if (d == k) { int nw = longest_block(), sm = longest_seam(); if (nw > best_nw) { best_nw = nw; memcpy(nwA, curA, sizeof curA); } if (sm > best_seam) { best_seam = sm; memcpy(seamA, curA, sizeof curA); } return; } for (int A = 0; A < qs[d]; A++) { unsigned char *kk = kill[d] + (long)A * NR; for (int s = 0; s < NR; s++) dead[s] += kk[s]; curA[d] = A; rec(d + 1); for (int s = 0; s < NR; s++) dead[s] -= kk[s]; } } int main(int argc, char **argv) { if (argc < 4) { fprintf(stderr, "usage: brute x H q1 q2 ...\n"); return 2; } x = atoi(argv[1]); H = atoi(argv[2]); k = argc - 3; for (int i = 0; i < k; i++) qs[i] = atoi(argv[3 + i]); P = 2; for (int p = 3; p <= x; p += 2) { int pr = 1; for (int d = 3; d * d <= p; d += 2) if (p % d == 0) pr = 0; if (pr) P *= p; } NR = 0; D = 0; for (long n = 0; n < 2 * P; n++) if (is_slot(n)) { if (NR >= MAXS) { fprintf(stderr, "too many slots\n"); return 2; } R[NR++] = n; if (n < P) D++; } if (R[D - 1] != P - 1) { fprintf(stderr, "c is not the last slot\n"); return 2; } for (int i = 0; i < k; i++) { kill[i] = calloc((size_t)qs[i] * NR, 1); for (int A = 0; A < qs[i]; A++) for (int s = 0; s < NR; s++) { long m = ((R[s] - A) % qs[i] + qs[i]) % qs[i]; kill[i][(long)A * NR + s] = (m == 0 || m == 2); } } memset(dead, 0, sizeof dead); rec(0); printf("{\"x\": %d, \"P\": %ld, \"D\": %d, \"H\": %d, \"M\": [", x, P, D, H); for (int i = 0; i < k; i++) printf("%s%d", i ? ", " : "", qs[i]); printf("], \"max_nonwrap\": %d, \"max_seam\": %d, \"exceedance\": %s, \"whole_block_killable\": %s, \"seam_residues\": [", best_nw, best_seam, best_seam > best_nw ? "true" : "false", best_nw >= D ? "true" : "false"); for (int i = 0; i < k; i++) printf("%s%d", i ? ", " : "", seamA[i]); printf("], \"nonwrap_residues\": ["); for (int i = 0; i < k; i++) printf("%s%d", i ? ", " : "", nwA[i]); printf("]}\n"); return 0; }