/* iecell.c -- job 4301 (route 82 pursuit): exact lag-1 gap-pair cells of the twin tile by CRT inclusion-exclusion. C port of #1918's ie4256.py with the same definitions: T_x = { r mod x# : gcd(r(r+2), x#) = 1 }; every opener is 5 mod 6, so gaps and offsets are multiples of 6. N(H; I) = #{ r mod x# : r+h in T_x (h in H), r+i not in T_x (i in I) } = sum_{S subset I} (-1)^|S| prod_{p<=x} (p - nu_p(H u S)), nu_p(A) = |{-a, -a-2 mod p : a in A}|. Single gap a: H = {0, a}, I = interior multiples of 6 -> h_a Lag-1 pair (a, b): H = {0, a, a+b}, I = multiples of 6 in (0, a+b) minus a -> N(a, b) Exact pruning (as in work/2802/rhoie.c): a subset whose residues fill every class mod some p <= x has product 0, and so has every superset, so the depth-first enumeration drops the branch. No term with nonzero value is skipped. Per-prime residue sets are 128-bit masks (x <= 127). Sums are unsigned 128-bit (exact mod 2^128) and every count is <= D < 2^126, so printed values are exact; x with D >= 2^126 is refused. Output (stdout, deterministic): JSON {x, A, D, hist: {a: h_a}, pairs1: [[a, b, N], ...] for a+b <= A, terms: total nonzero terms}. Progress/timing on stderr. Usage: iecell cc -O2 -o iecell iecell.c -lpthread */ #include #include #include #include #include typedef __int128 i128; static int NP, PR[40]; typedef unsigned __int128 u128; static u128 FULL[40]; static int pc(u128 v) { return __builtin_popcountll((uint64_t)v) + __builtin_popcountll((uint64_t)(v >> 64)); } typedef struct { int n; u128 pm[128][40]; u128 base[40]; u128 total; uint64_t terms; } job_t; static u128 mres(int p, int a) { int r1 = ((-a) % p + p) % p, r2 = ((-a - 2) % p + p) % p; return ((u128)1 << r1) | ((u128)1 << r2); } static void rec(job_t *J, int start, const u128 *mask, int sign) { u128 nm[40]; for (int t = start; t < J->n; t++) { u128 prod = 1; int zero = 0; for (int k = 0; k < NP; k++) { nm[k] = mask[k] | J->pm[t][k]; if (nm[k] == FULL[k]) { zero = 1; break; } prod *= PR[k] - pc(nm[k]); } if (zero) continue; if (sign > 0) J->total -= prod; else J->total += prod; /* exact mod 2^128 (unsigned wraparound) */ J->terms++; rec(J, t + 1, nm, -sign); } } /* count N(H; interior of [0, H[last]] minus H) */ static void count(int nh, const int *H, i128 *out, uint64_t *terms) { job_t *J = calloc(1, sizeof(job_t)); int L = H[nh - 1]; for (int k = 0; k < NP; k++) { J->base[k] = 0; for (int j = 0; j < nh; j++) J->base[k] |= mres(PR[k], H[j]); } for (int i = 6; i < L; i += 6) { int inH = 0; for (int j = 0; j < nh; j++) if (H[j] == i) inH = 1; if (inH) continue; for (int k = 0; k < NP; k++) J->pm[J->n][k] = mres(PR[k], i); J->n++; } u128 prod = 1; int zero = 0; for (int k = 0; k < NP; k++) { if (J->base[k] == FULL[k]) zero = 1; else prod *= PR[k] - pc(J->base[k]); } J->total = 0; J->terms = 0; if (!zero) { J->total = prod; J->terms = 1; rec(J, 0, J->base, 1); } *out = (i128)J->total; *terms = J->terms; free(J); } static int X, A, NT, ncell, next_cell; static int (*cells)[3]; /* {kind (1 single, 2 pair), a, b} */ static i128 *res; static uint64_t *rterms; static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; static void *worker(void *arg) { (void)arg; for (;;) { pthread_mutex_lock(&mu); int c = next_cell++; pthread_mutex_unlock(&mu); if (c >= ncell) return NULL; int H[3], nh; if (cells[c][0] == 1) { H[0] = 0; H[1] = cells[c][1]; nh = 2; } else { H[0] = 0; H[1] = cells[c][1]; H[2] = cells[c][1] + cells[c][2]; nh = 3; } count(nh, H, &res[c], &rterms[c]); } } static void p128(i128 v) { char buf[64]; int n = 0, neg = v < 0; unsigned __int128 u = neg ? -(unsigned __int128)v : (unsigned __int128)v; if (u == 0) buf[n++] = '0'; while (u) { buf[n++] = '0' + (int)(u % 10); u /= 10; } if (neg) putchar('-'); while (n) putchar(buf[--n]); } int main(int argc, char **argv) { if (argc < 4) { fprintf(stderr, "usage: iecell \n"); return 2; } X = atoi(argv[1]); A = atoi(argv[2]); NT = atoi(argv[3]); if (X > 127 || A > 6 * 128) { fprintf(stderr, "x <= 127, A <= 768\n"); return 2; } for (int q = 2; q <= X; q++) { int p = 1; for (int d = 2; d * d <= q; d++) if (q % d == 0) p = 0; if (p) { PR[NP] = q; FULL[NP] = ((u128)1 << q) - 1; NP++; } } { double l2 = 0; for (int k = 0; k < NP; k++) l2 += __builtin_log2(PR[k] > 3 ? PR[k] - 2 : 1); /* every term and every count is <= D = prod (p - 2); exact while D < 2^126 */ if (l2 >= 126.0) { fprintf(stderr, "D >= 2^126: counts would not fit (x too large)\n"); return 2; } } cells = malloc(sizeof(int[3]) * 20000); for (int a = 6; a <= A; a += 6) { cells[ncell][0] = 1; cells[ncell][1] = a; cells[ncell][2] = 0; ncell++; } for (int s = A; s >= 12; s -= 6) /* largest sums first for load balance */ for (int a = 6; a < s; a += 6) { cells[ncell][0] = 2; cells[ncell][1] = a; cells[ncell][2] = s - a; ncell++; } res = calloc(ncell, sizeof(i128)); rterms = calloc(ncell, sizeof(uint64_t)); pthread_t th[64]; for (int i = 0; i < NT; i++) pthread_create(&th[i], NULL, worker, NULL); for (int i = 0; i < NT; i++) pthread_join(th[i], NULL); i128 D = 1; for (int k = 0; k < NP; k++) D *= PR[k] - pc(mres(PR[k], 0)); uint64_t tt = 0; for (int c = 0; c < ncell; c++) tt += rterms[c]; printf("{\"x\": %d, \"A\": %d, \"D\": ", X, A); p128(D); printf(", \"terms\": %llu,\n \"hist\": {", (unsigned long long)tt); int first = 1; for (int c = 0; c < ncell; c++) if (cells[c][0] == 1) { printf("%s\"%d\": ", first ? "" : ", ", cells[c][1]); p128(res[c]); first = 0; } printf("},\n \"pairs1\": ["); first = 1; for (int a = 6; a <= A; a += 6) for (int b = 6; a + b <= A; b += 6) for (int c = 0; c < ncell; c++) if (cells[c][0] == 2 && cells[c][1] == a && cells[c][2] == b) { if (res[c] != 0) { printf("%s[%d, %d, ", first ? "" : ", ", a, b); p128(res[c]); printf("]"); first = 0; } break; } printf("]}\n"); fprintf(stderr, "x=%d A=%d cells=%d terms=%llu\n", X, A, ncell, (unsigned long long)tt); return 0; }