/* rhoie.c -- job 2802 (route 52): closed-form side of the twin-tile gap census. c_x(O) = prod_{q<=x} (q - #{(-o) mod q : o in O}) (CRT count of r with r+o coprime to x# for all o in O) cop_x(g) = c_x({0,2,g,g+2}); rho_ie(g) = sum_{nonempty S in M_g} (-1)^{|S|+1} c_x({0,2,g,g+2} u {H,H+2 : H in S}), M_g = {6, 12, ..., g-6} (#855's alternating sum; every opener is 5 mod 6). Exact pruning: if S's offsets cover every residue mod some q <= x then c_x = 0 for S and for every superset of S, so the depth-first enumeration (elements in increasing order) drops that branch. The sum is unchanged; only zero terms are skipped. Prints JSON: per g the product, rho_ie, N_closed = cop - rho_ie, nonzero-term count. Usage: rhoie [term budget per g] cc -O2 -o rhoie rhoie.c */ #include #include #include static int nq, Q[16], cnt[16][64], dist[16], m, M[512]; static int64_t total; static uint64_t terms, budget; static int over; static int add(int o) { int full = 0; for (int i = 0; i < nq; i++) { int r = ((-o) % Q[i] + Q[i]) % Q[i]; if (cnt[i][r]++ == 0) dist[i]++; if (dist[i] == Q[i]) full = 1; } return full; } static void rem(int o) { for (int i = 0; i < nq; i++) { int r = ((-o) % Q[i] + Q[i]) % Q[i]; if (--cnt[i][r] == 0) dist[i]--; } } static int64_t prodc(void) { int64_t v = 1; for (int i = 0; i < nq; i++) v *= Q[i] - dist[i]; return v; } static void rec(int idx, int sign) { for (int i = idx; i < m && !over; i++) { int f1 = add(M[i]), f2 = add(M[i] + 2); if (!(f1 || f2)) { total += sign * prodc(); if (++terms > budget) over = 1; rec(i + 1, -sign); } rem(M[i] + 2); rem(M[i]); } } int main(int argc, char **argv) { int x = atoi(argv[1]), G = atoi(argv[2]); budget = argc > 3 ? strtoull(argv[3], 0, 10) : 4000000000ULL; 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) Q[nq++] = q; } printf("{\"x\": %d, \"classes\": {", x); for (int g = 6; g <= G; g += 6) { for (int i = 0; i < nq; i++) { dist[i] = 0; for (int r = 0; r < 64; r++) cnt[i][r] = 0; } add(0); add(2); add(g); add(g + 2); int64_t cop = prodc(); m = 0; for (int h = 6; h < g; h += 6) M[m++] = h; total = 0; terms = 0; over = 0; if (cop) rec(0, 1); if (over) printf("%s\n \"%d\": {\"cop_product\": %lld, \"rho_ie\": null, \"N_closed\": null, \"nonzero_terms\": \"> %llu\"}", g == 6 ? "" : ",", g, (long long)cop, (unsigned long long)budget); else printf("%s\n \"%d\": {\"cop_product\": %lld, \"rho_ie\": %lld, \"N_closed\": %lld, \"nonzero_terms\": %llu}", g == 6 ? "" : ",", g, (long long)cop, (long long)total, (long long)(cop - total), (unsigned long long)terms); fprintf(stderr, "g=%d terms=%llu%s\n", g, (unsigned long long)terms, over ? " (budget exceeded)" : ""); fflush(stdout); } printf("}}\n"); return 0; }