/* Route 82, job 4321: exact-small-prime hybrid of M3 (m3_4310.c, #1930). Tile T_x: s with s, s+2 coprime to every prime 5 <= q <= x (offsets are multiples of 6). Small primes 5 <= q <= y: the start residue vector r = (s mod q) runs over all prod (q-2) admissible classes, so which offsets survive the small primes is known exactly in each class. Big primes y < q <= x: M3 on the surviving interior candidates only, P(empty | H, r) ~ prod_{i in C_r} (1 - p_i) * exp( sum_{i= x gives the exact tile density (no big primes). Lambda over folds exactly as in m3_4310.c. Heuristic model, no fitted parameter. Usage: h4321 x y NMAX -> JSON on stdout (deterministic). */ #include #include #include #include typedef unsigned __int128 u128; typedef unsigned long long u64; static int Q[40], nq = 0; static u128 *MK[40]; static int SQ[10], ns = 0; /* small primes */ static int R = 0; static u64 (*AL)[4]; /* alive bitmask per class over offsets 0..NMAX */ static int pc(u128 v) { return __builtin_popcountll((u64)v) + __builtin_popcountll((u64)(v >> 64)); } static double Fm(const u128 *m) { double f = 1; for (int j = 0; j < nq; j++) { int nu = pc(m[j]); if (nu >= Q[j]) return 0; f *= (double)(Q[j] - nu) / (Q[j] - 2); } return f; } static void addm(u128 *m, int h) { for (int j = 0; j < nq; j++) m[j] |= MK[j][h % Q[j]]; } static int alive(int r, int i) { return (AL[r][i >> 6] >> (i & 63)) & 1; } static double W[512][512], lp[512]; static int sure[512]; /* H given by offsets (units of 6); interior candidate offsets I (length k) */ static double cell(const int *H, int nh, const int *I, int k) { u128 mH[40] = {0}, t[40]; for (int s = 0; s < nh; s++) addm(mH, 6 * H[s]); double F0 = Fm(mH); if (F0 == 0) return 0; double p[512]; for (int a = 0; a < k; a++) { int same = 1; for (int j = 0; j < nq; j++) { t[j] = mH[j] | MK[j][6 * I[a] % Q[j]]; if (t[j] != mH[j]) same = 0; } sure[a] = same; p[a] = same ? 1 : Fm(t) / F0; lp[a] = same ? 0 : log1p(-p[a]); } for (int a = 0; a < k; a++) for (int b = a + 1; b < k; b++) { if (sure[a] || sure[b]) { W[a][b] = 0; continue; } for (int j = 0; j < nq; j++) t[j] = mH[j] | MK[j][6 * I[a] % Q[j]] | MK[j][6 * I[b] % Q[j]]; W[a][b] = (Fm(t) / F0 - p[a] * p[b]) / ((1 - p[a]) * (1 - p[b])); } double tot = 0; int C[512]; for (int r = 0; r < R; r++) { int ok = 1; for (int s = 0; s < nh; s++) if (!alive(r, H[s])) { ok = 0; break; } if (!ok) continue; int c = 0, dead = 0; double L = 0; for (int a = 0; a < k; a++) if (alive(r, I[a])) { if (sure[a]) { dead = 1; break; } C[c++] = a; L += lp[a]; } if (dead) continue; for (int u = 0; u < c; u++) { const double *w = W[C[u]]; for (int v = u + 1; v < c; v++) L += w[C[v]]; } tot += exp(L); } return F0 * tot / R; } int main(int argc, char **argv) { if (argc != 4) { fprintf(stderr, "usage: h4321 x y NMAX\n"); return 2; } int x = atoi(argv[1]), y = atoi(argv[2]), n = atoi(argv[3]); if (n > 255) { fprintf(stderr, "NMAX <= 255\n"); return 2; } for (int q = 5; q <= x; q++) { int pr = 1; for (int d = 2; d * d <= q; d++) if (q % d == 0) pr = 0; if (!pr) continue; if (q <= y) SQ[ns++] = q; else Q[nq++] = q; } for (int j = 0; j < nq; j++) { int q = Q[j]; MK[j] = calloc(q, sizeof(u128)); for (int t = 0; t < q; t++) { int a = (q - t) % q, b = (a + q - 2) % q; MK[j][t] = ((u128)1 << a) | ((u128)1 << b); } } R = 1; for (int s = 0; s < ns; s++) R *= SQ[s] - 2; AL = calloc(R, sizeof *AL); int res[10] = {0}; for (int s = 0; s < ns; s++) res[s] = 1; /* first admissible residue (s != 0, s != q-2) */ for (int r = 0; r < R; r++) { for (int i = 0; i <= n; i++) { int ok = 1; for (int s = 0; s < ns; s++) { int v = (res[s] + 6 * i) % SQ[s]; if (v == 0 || v == SQ[s] - 2) { ok = 0; break; } } if (ok) AL[r][i >> 6] |= 1ULL << (i & 63); } for (int s = 0; s < ns; s++) { /* odometer over admissible residues */ do res[s]++; while (res[s] < SQ[s] && res[s] == SQ[s] - 2); if (res[s] < SQ[s]) break; res[s] = 1; } } double *h = calloc(n + 1, sizeof(double)), *N = calloc((size_t)(n + 1) * (n + 1), sizeof(double)); int I[512]; for (int A = 1; A <= n; A++) { int H[2] = {0, A}, k = 0; for (int i = 1; i < A; i++) I[k++] = i; h[A] = cell(H, 2, I, k); } for (int A = 1; A < n; A++) for (int B = 1; A + B <= n; B++) { int H[3] = {0, A, A + B}, k = 0; for (int i = 1; i < A + B; i++) if (i != A) I[k++] = i; N[(size_t)A * (n + 1) + B] = cell(H, 3, I, k); } double mt = 0, kt = 0; for (int A = 1; A <= n; A++) mt += h[A]; for (int A = 1; A < n; A++) for (int B = 1; A + B <= n; B++) kt += N[(size_t)A * (n + 1) + B]; printf("{\"x\": %d, \"y\": %d, \"classes\": %d, \"NMAX\": %d, \"h_last_over_max\": %.3e, \"sum_h\": %.9f, \"folds\": {", x, y, R, n, h[n] / h[2], mt); int P[] = {5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}, first = 1; for (int t = 0; t < 13; t++) { int p = P[t]; if (p > x) continue; double ms = 0, ks = 0; for (int A = 1; A <= n; A++) { int r = 6 * A % p; if (r == 0 || r == 2 || r == p - 2) ms += h[A]; } for (int A = 1; A < n; A++) { int r = 6 * A % p; if (!(r == 0 || r == 2 || r == p - 2)) continue; for (int B = 1; A + B <= n; B++) { int s = 6 * B % p; if (s == 0 || s == 2 || s == p - 2) ks += N[(size_t)A * (n + 1) + B]; } } printf("%s\"%d\": %.6f", first ? "" : ", ", p, (ks / kt) / ((ms / mt) * (ms / mt))); first = 0; } printf("}, \"rho60\": %.6f}\n", (N[(size_t)10 * (n + 1) + 10] / kt) / ((h[10] / mt) * (h[10] / mt))); return 0; }