/* copyscan.c - scan every copy r + j*19# (0 <= j < x#/19#) of one T_19 slot * residue r and print the copies whose forward gap in T_x is exactly G. * T_x = { n : gcd(n(n+2), x#) = 1 }. Route 15, job 2014. * * Usage: copyscan x G r (x in 23..43, r a residue mod 19#) * stdout: one line per hit "p j"; summary to stderr. * * Method: the T_19 slots in [r, r+G] are fixed by r. A copy p attains G iff * p and p+G survive every prime q in 23..x and every interior T_19 slot is * killed by some such q. For each q a table bad_q[p mod q] holds the bitmask * of offsets killed; p mod q is advanced incrementally. */ #include #include #include #include #define B19 9699690ULL typedef unsigned __int128 u128; static int is_slot19(uint64_t n) { static const int P[] = {2, 3, 5, 7, 11, 13, 17, 19}; for (int i = 0; i < 8; i++) { uint64_t q = P[i], m = n % q; if (m == 0 || m == q - 2) return 0; } return 1; } int main(int argc, char **argv) { if (argc != 4) { fprintf(stderr, "usage: copyscan x G r\n"); return 2; } int x = atoi(argv[1]); int G = atoi(argv[2]); uint64_t r = strtoull(argv[3], 0, 10) % B19; static const int QS[] = {23, 29, 31, 37, 41, 43}; int nq = 0; uint64_t ncopy = 1; while (nq < 6 && QS[nq] <= x) ncopy *= QS[nq++]; if (!is_slot19(r)) { fprintf(stderr, "r not a T_19 slot\n"); return 1; } int off[256], n = 0; for (int k = 0; k <= G; k++) if (is_slot19(r + k)) { if (n == 128) { fprintf(stderr, "too many\n"); return 1; } off[n++] = k; } if (off[n - 1] != G) { fprintf(stderr, "x=%d r=%llu: r+G not a T_19 slot; offsets=%d; hits=0\n", x, (unsigned long long)r, n); return 0; } /* bit t set in bad[q][m] if offset off[t] is killed by q when p = m (mod q) */ u128 *bad[6]; for (int a = 0; a < nq; a++) { int q = QS[a]; bad[a] = calloc(q, sizeof(u128)); for (int m = 0; m < q; m++) for (int t = 0; t < n; t++) { int v = (m + off[t]) % q; if (v == 0 || v == q - 2) bad[a][m] |= (u128)1 << t; } } u128 ends = ((u128)1 << 0) | ((u128)1 << (n - 1)); u128 all = (n == 128) ? ~(u128)0 : (((u128)1 << n) - 1); u128 interior = all & ~ends; int m[6], step[6]; for (int a = 0; a < nq; a++) { m[a] = r % QS[a]; step[a] = B19 % QS[a]; } uint64_t hits = 0; for (uint64_t j = 0; j < ncopy; j++) { u128 k = 0; for (int a = 0; a < nq; a++) k |= bad[a][m[a]]; if (!(k & ends) && (k & interior) == interior) { printf("%llu %llu\n", (unsigned long long)(r + j * B19), (unsigned long long)j); hits++; } for (int a = 0; a < nq; a++) { m[a] += step[a]; if (m[a] >= QS[a]) m[a] -= QS[a]; } } fprintf(stderr, "x=%d G=%d r=%llu ncopy=%llu offsets=%d hits=%llu\n", x, G, (unsigned long long)r, (unsigned long long)ncopy, n, (unsigned long long)hits); return 0; }