/* witness4288.c — record WHERE the maximal killed run sits, for the p = 1 rows. * * Same model as the served kstar.c (sha 4d3faa05...): the slots are enumerated * x = k*m + j -> r = P*k + cls[j] with cls the alive classes mod P, m = |cls|, * nk = prod(R); slot x is killed iff r = 0 or -2 (mod q) for some q in R; * K* is the maximal CYCLIC run of consecutive killed slots. The served engine * counts only; this one also reports the winning run's start (as an x) so the * value's LOWER bound can be re-checked against the definition by a reader. * * This program is a witness tool, not the measuring instrument: the values * reported in the return come from the served engine. * * stdin : "P n q1 .. qn" (p = 1 rows only) * stdout: "P n q1 .. qn K start_x len wrapped" * cc: MSVC cl /O2 witness4288.c /Fe:witness4288.exe (no threads needed) */ #include #include #include #include #define MAXR 16 static uint64_t inv_mod(uint64_t a, uint64_t m) { int64_t t = 0, nt = 1, r = (int64_t)m, nr = (int64_t)(a % m); while (nr) { int64_t qq = r / nr, x; x = t - qq * nt; t = nt; nt = x; x = r - qq * nr; r = nr; nr = x; } if (r != 1) { fprintf(stderr, "not invertible\n"); exit(2); } return (uint64_t)(t < 0 ? t + (int64_t)m : t); } int main(void) { unsigned long long P; int n; while (scanf("%llu %d", &P, &n) == 2) { if (n > MAXR) return 2; uint64_t q[MAXR]; for (int i = 0; i < n; i++) { unsigned long long v; if (scanf("%llu", &v) != 1) return 2; q[i] = v; } /* wheel: alive classes mod P */ uint64_t fac[16]; int nf = 0; { uint64_t t = P; for (uint64_t d = 2; d * d <= t; d++) if (t % d == 0) { fac[nf++] = d; while (t % d == 0) t /= d; } if (t > 1) fac[nf++] = t; } uint64_t *cls = malloc(sizeof(uint64_t) * P); int m = 0; for (uint64_t a = 0; a < P; a++) { int ok = 1; for (int i = 0; i < nf; i++) { uint64_t rr = a % fac[i]; if (rr == 0 || rr == (fac[i] - 2) % fac[i]) { ok = 0; break; } } if (ok) cls[m++] = a; } uint64_t nk = 1; for (int i = 0; i < n; i++) nk *= q[i]; uint64_t *res = malloc(sizeof(uint64_t) * (size_t)n * m * 2); for (int i = 0; i < n; i++) { uint64_t Q = q[i], Pi = inv_mod(P % Q, Q); for (int j = 0; j < m; j++) { uint64_t a = cls[j] % Q; res[((size_t)i * m + j) * 2 + 0] = ((Q - a) % Q) * Pi % Q; res[((size_t)i * m + j) * 2 + 1] = ((2 * Q - a - 2) % Q) * Pi % Q; } } uint64_t blk = (4u << 20) / (uint64_t)m; if (blk < 64) blk = 64; uint8_t *buf = malloc(blk * m); long long best = 0, cur = 0, prefix = 0; uint64_t best_x = 0, cur_x = 0, suffix_x = 0; int seen = 0; for (uint64_t kb = 0; kb < nk; kb += blk) { uint64_t w_ = nk - kb < blk ? nk - kb : blk; memset(buf, 0, w_ * m); for (int i = 0; i < n; i++) { uint64_t Q = q[i], kbm = kb % Q; for (int j = 0; j < m; j++) for (int s = 0; s < 2; s++) { uint64_t r0 = res[((size_t)i * m + j) * 2 + s]; uint64_t off = (r0 + Q - kbm) % Q; for (uint64_t k = off; k < w_; k += Q) buf[k * m + j] = 1; } } uint64_t xb = kb * (uint64_t)m, tot = w_ * (uint64_t)m; for (uint64_t x = 0; x < tot; x++) { if (buf[x]) { if (cur == 0) cur_x = xb + x; cur++; continue; } if (!seen) { prefix = cur; seen = 1; } if (cur > best) { best = cur; best_x = cur_x; } cur = 0; } } suffix_x = cur_x; if (!seen) { printf("%llu %d", P, n); for (int i = 0; i < n; i++) printf(" %llu", q[i]); printf(" -1 0 0 0\n"); continue; } if (cur > best) { best = cur; best_x = cur_x; } long long K = best, wrap = prefix + cur; uint64_t sx = best_x; long long len = best; int wrapped = 0; if (wrap > best) { K = wrap; len = wrap; sx = suffix_x; wrapped = 1; } printf("%llu %d", P, n); for (int i = 0; i < n; i++) printf(" %llu", q[i]); printf(" %lld %llu %lld %d\n", K, (unsigned long long)sx, len, wrapped); } return 0; }