/* job 2775 / route 117: K*(B,R) by a wheel mod P, O(block) memory. * * Objects as in job2753/stream-run.py (#1384): slots of base B = P*p are the * r in [0, B*prod R) with r != 0,-2 (mod q) for every prime q | B; a slot is * killed if r = 0 or -2 (mod q) for some q in R; K* is the maximal CYCLIC run * of consecutive slots that are all killed. * * Slots are enumerated as r = P*k + a with a an alive class mod P (the wheel), * k in [0, p*prod R). For a prime q coprime to P the conditions r = 0,-2 (mod q) * are two residues of k mod q per class, marked by strided writes per block. * The cyclic maximum is max(best linear run, prefix run + suffix run). * * stdin : one row per line "P p n q1 .. qn" (p = 1: base P itself) * stdout: one line per row, input order: "P p q1 .. qn K" (K = -1: no break) * argv[1]: thread count (rows are distributed over threads). * cc -O2 -o kstar kstar.c -lpthread */ #include #include #include #include #include #define MAXR 16 typedef struct { uint64_t P, p; int n; uint64_t q[MAXR]; long long K; } Row; static Row *rows; static int nrows; static int next_row; static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; 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 %llu mod %llu\n", (unsigned long long)a, (unsigned long long)m); exit(2); } return (uint64_t)(t < 0 ? t + (int64_t)m : t); } static long long kstar(const Row *w) { uint64_t P = w->P, fac[16]; int nf = 0; { uint64_t n = P; for (uint64_t d = 2; d * d <= n; d++) if (n % d == 0) { fac[nf++] = d; while (n % d == 0) n /= d; } if (n > 1) fac[nf++] = n; } 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 r = a % fac[i]; if (r == 0 || r == (fac[i] - 2) % fac[i]) { ok = 0; break; } } if (ok) cls[m++] = a; } /* primes acting on k: R (kill, 1) and p (dead, 2), each coprime to P */ int np = w->n + (w->p > 1); uint64_t pr[MAXR + 1]; uint8_t val[MAXR + 1]; for (int i = 0; i < w->n; i++) { pr[i] = w->q[i]; val[i] = 1; } if (w->p > 1) { pr[w->n] = w->p; val[w->n] = 2; } uint64_t nk = 1; for (int i = 0; i < np; i++) nk *= pr[i]; /* residues of k per (prime, class, which) */ uint64_t *res = malloc(sizeof(uint64_t) * np * m * 2); for (int i = 0; i < np; i++) { uint64_t q = pr[i], Pi = inv_mod(P % q, q); for (int j = 0; j < m; j++) { uint64_t a = cls[j] % q; res[(i * m + j) * 2 + 0] = ((q - a) % q) * Pi % q; res[(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; 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 pass = 1; pass <= 2; pass++) /* kills first, dead overrides */ for (int i = 0; i < np; i++) { if (val[i] != pass) continue; uint64_t q = pr[i], kbm = kb % q; for (int j = 0; j < m; j++) for (int s = 0; s < 2; s++) { uint64_t r0 = res[(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] = (uint8_t)pass; } } uint64_t tot = w_ * m; for (uint64_t x = 0; x < tot; x++) { uint8_t s = buf[x]; if (s == 2) continue; if (s == 1) { cur++; continue; } if (!seen) { prefix = cur; seen = 1; } if (cur > best) best = cur; cur = 0; } } free(buf); free(res); free(cls); if (!seen) return -1; if (cur > best) best = cur; if (prefix + cur > best) best = prefix + cur; return best; } static void *worker(void *arg) { (void)arg; for (;;) { pthread_mutex_lock(&mu); int i = next_row++; pthread_mutex_unlock(&mu); if (i >= nrows) return NULL; rows[i].K = kstar(&rows[i]); } } int main(int argc, char **argv) { int nt = argc > 1 ? atoi(argv[1]) : 1; if (nt < 1) nt = 1; int cap = 1024; rows = malloc(sizeof(Row) * cap); unsigned long long P, p; int n; while (scanf("%llu %llu %d", &P, &p, &n) == 3) { if (n > MAXR) { fprintf(stderr, "too many R\n"); return 2; } if (nrows == cap) { cap *= 2; rows = realloc(rows, sizeof(Row) * cap); } Row *r = &rows[nrows]; r->P = P; r->p = p; r->n = n; for (int i = 0; i < n; i++) { unsigned long long q; if (scanf("%llu", &q) != 1) return 2; r->q[i] = q; } nrows++; } pthread_t th[64]; if (nt > 64) nt = 64; for (int t = 0; t < nt; t++) pthread_create(&th[t], NULL, worker, NULL); for (int t = 0; t < nt; t++) pthread_join(th[t], NULL); for (int i = 0; i < nrows; i++) { printf("%llu %llu", (unsigned long long)rows[i].P, (unsigned long long)rows[i].p); for (int j = 0; j < rows[i].n; j++) printf(" %llu", (unsigned long long)rows[i].q[j]); printf(" %lld\n", rows[i].K); } return 0; }