/* mstar2p.c -- job #1326 / route 24: exact m*(T_x) = max{m : maxsum_m(T_x) < THR}, THR = 4*Ghat(x), on the cyclic twin tile * T_x = {r mod x# : r, r+2 coprime to x#}, without materialising the tile. * Tile construction as work/2637/maxsum.c (#1800): T_B by recursive folding (B <= 29), then up to two stream folds q1, q2. * Two-pointer: m*+1 = min over slots i of L(i) = min{m : p_{i+m} - p_i >= THR} = 1 + #{slots in (p_i, p_i+THR)}; O(1) per slot, * independent of m. Also the exact profile maxsum_m for m <= MM (MM <= 64) for custody/gating. * Parallel: the step range [0, D_B*q1*q2) is split into NT contiguous pieces; each thread owns the windows STARTING at its kept * slots and runs past its end (the cyclic wrap is the same stream continued) until every owned window is closed. * usage: mstar2p B q1 q2 THR MM NT (q = 0: no fold) stdout: one JSON line; progress on stderr. * cc -O2 -o mstar2p mstar2p.c -lpthread */ #include #include #include #include static const int PR[] = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; #define RING 4096 static int B, Q1, Q2, MM, NT; static uint64_t THR, P, D, first, TOT; static uint16_t *gap; static uint64_t *cyc_off; /* cyc_off[b] = position offset of step b*BLK within one T_B period */ #define BLK 65536ULL static uint8_t m1[65536], m2[65536]; typedef struct { uint64_t s0, s1, kept, gapsum, minL, best[65]; int t; } job_t; static void *run(void *arg) { job_t *J = arg; uint64_t s = J->s0; uint64_t k = s / D, i = s % D; uint64_t n = k * P + cyc_off[i / BLK]; for (uint64_t b = (i / BLK) * BLK; b < i; b++) n += gap[b]; int r1 = Q1 ? (int)(n % Q1) : 1, r2 = Q2 ? (int)(n % Q2) : 1; uint64_t pos[RING]; uint64_t cnt = 0; /* kept slots seen by this thread; slot c at pos[c % RING] */ uint64_t owned = 0; /* owned slots are c in [0, owned) (steps < s1) */ int closed_range = 0; uint64_t tp = 0; /* two-pointer: oldest owned slot not yet closed */ uint64_t minL = UINT64_MAX, best[65] = {0}, gapsum = 0; for (;;) { int keep = !(Q1 && (r1 == 0 || r1 == Q1 - 2)) && !(Q2 && (r2 == 0 || r2 == Q2 - 2)); if (keep) { if (!closed_range) owned = cnt + 1; pos[cnt % RING] = n; if (cnt >= 1 && cnt - 1 < owned) gapsum += n - pos[(cnt - 1) % RING]; for (int m = 1; m <= MM && (uint64_t)m <= cnt; m++) { uint64_t st = cnt - m; if (st >= owned) continue; uint64_t v = n - pos[st % RING]; if (v > best[m]) best[m] = v; } while (tp < cnt && tp < owned && n - pos[tp % RING] >= THR) { uint64_t L = cnt - tp; if (L < minL) minL = L; tp++; } if (cnt - tp >= RING - 1) { fprintf(stderr, "ring overflow\n"); exit(4); } cnt++; if (closed_range && tp >= owned && cnt > owned + MM) break; } int g = gap[i]; n += g; if (Q1) { r1 += m1[g]; if (r1 >= Q1) r1 -= Q1; } if (Q2) { r2 += m2[g]; if (r2 >= Q2) r2 -= Q2; } if (++i == D) i = 0; if (++s == J->s1) closed_range = 1; if (J->t == 0 && (s & ((1ULL << 32) - 1)) == 0) fprintf(stderr, "t0 step %llu / %llu\n", (unsigned long long)(s - J->s0), (unsigned long long)(J->s1 - J->s0)); } J->kept = owned; J->gapsum = gapsum; J->minL = minL; for (int m = 0; m <= 64; m++) J->best[m] = best[m]; return NULL; } int main(int argc, char **argv) { if (argc < 7) { fprintf(stderr, "usage: mstar2p B q1 q2 THR MM NT\n"); return 2; } B = atoi(argv[1]); Q1 = atoi(argv[2]); Q2 = atoi(argv[3]); THR = strtoull(argv[4], 0, 10); MM = atoi(argv[5]); NT = atoi(argv[6]); if (MM > 64 || NT < 1 || B > 29) return 2; P = 6; D = 1; uint64_t *p = malloc(8); p[0] = 5; for (int ii = 1; PR[ii] <= B; ii++) { int f = PR[ii]; uint64_t Dn = D * (f - 2), c = 0; uint64_t *np = malloc(Dn * 8); for (int k = 0; k < f; k++) for (uint64_t i = 0; i < D; i++) { uint64_t n = (uint64_t)k * P + p[i]; int r = (int)(n % f); if (r != 0 && r != f - 2) np[c++] = n; } if (c != Dn) return 3; free(p); p = np; P *= f; D = Dn; } gap = malloc(D * 2); first = p[0]; for (uint64_t i = 0; i + 1 < D; i++) gap[i] = (uint16_t)(p[i + 1] - p[i]); gap[D - 1] = (uint16_t)(P - p[D - 1] + p[0]); uint64_t nb = (D + BLK - 1) / BLK; cyc_off = malloc(nb * 8); for (uint64_t b = 0; b < nb; b++) cyc_off[b] = p[b * BLK]; free(p); for (int g = 0; g < 65536; g++) { m1[g] = Q1 ? g % Q1 : 0; m2[g] = Q2 ? g % Q2 : 0; } TOT = D * (Q1 ? Q1 : 1) * (Q2 ? Q2 : 1); fprintf(stderr, "T_%d: D=%llu P=%llu; steps %llu, threads %d\n", B, (unsigned long long)D, (unsigned long long)P, (unsigned long long)TOT, NT); job_t *J = calloc(NT, sizeof(job_t)); pthread_t *th = malloc(NT * sizeof(pthread_t)); for (int t = 0; t < NT; t++) { J[t].t = t; J[t].s0 = TOT / NT * t; J[t].s1 = t == NT - 1 ? TOT : TOT / NT * (t + 1); pthread_create(&th[t], 0, run, &J[t]); } uint64_t kept = 0, gs = 0, minL = UINT64_MAX, best[65] = {0}; for (int t = 0; t < NT; t++) { pthread_join(th[t], 0); kept += J[t].kept; gs += J[t].gapsum; if (J[t].minL < minL) minL = J[t].minL; for (int m = 1; m <= MM; m++) if (J[t].best[m] > best[m]) best[m] = J[t].best[m]; } uint64_t Pf = P * (Q1 ? Q1 : 1) * (Q2 ? Q2 : 1); printf("{\"B\":%d,\"q1\":%d,\"q2\":%d,\"period\":%llu,\"D\":%llu,\"gapsum\":%llu,\"THR\":%llu,\"mstar\":%llu,\"maxsum\":[", B, Q1, Q2, (unsigned long long)Pf, (unsigned long long)kept, (unsigned long long)gs, (unsigned long long)THR, (unsigned long long)(minL - 1)); for (int m = 1; m <= MM; m++) printf("%s%llu", m > 1 ? "," : "", (unsigned long long)best[m]); printf("]}\n"); return 0; }