/* fastscan.c -- job #4128 (route 2): the served lossscan762.c scan, with the same * conventions and the same reduction, made cheap enough for W = 19# = 9699690. * * Conventions are those of #350/#1076, byte-for-byte the served scanner * (served/f1076_lossscan762.c, sha256 dd69e533...): * support = offsets s in [1,L] with gcd(a+s,W)=gcd(a+s+2,W)=1 * A_i(t) = { s in support : (s+t) mod q_i in {0, q_i-2} } * exact = N - |union A_i| * quadruple= |A_0 & A_1 & A_2 & A_3| * atom(i,j)= |A_i & A_j \ (A_k | A_l)| * envelope loss = min(exact, quadruple, min atom) chordal loss = min(exact, min atom) * F = N - sum_i max_t |A_i(t)| * candidate phases: loss > 0 pins each phase (see #1076); envelope 16N candidates, * chordal 16N^2. * * Differences from the served scanner (all output-preserving or documented): * - residue classes are built in O(N + sum q) instead of O(N * sum q); * - a candidate is skipped as soon as its loss is provably <= the running best * (exact <= best, or quadruple <= best in envelope mode, or an atom <= best), which * cannot change the argmax or the recorded witness; * - per-start results are not stored (only the max loss per start), so RAM is O(W); * - optional AEND argument limits the scanned start range (benchmarking only). * * Usage: fastscan W L keep q0 q1 q2 q3 [envelope|chordal] [AEND] * JSON on stdout, timing on stderr. */ #include #include #include #include #include #ifdef _OPENMP #include #endif #define MAXN 512 #define MAXW 8 #define MAXQ 64 #define MAXLOSS 255 #ifdef _MSC_VER #define THREADLOCAL __declspec(thread) #else #define THREADLOCAL __thread #endif static unsigned char *slot2; static int Wg, Lg, Qg[4], CHORDAL; /* per-thread scratch (MSVC has no block-scope thread-local storage) */ static THREADLOCAL int g_off[MAXN]; static THREADLOCAL uint64_t g_CL[4][MAXQ][MAXW], g_MK[4][MAXQ][MAXW]; static THREADLOCAL int g_TA[4][MAXN], g_TB[4][MAXN]; static inline int pc64(uint64_t x) { x -= (x >> 1) & 0x5555555555555555ULL; x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL); x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL; return (int)((x * 0x0101010101010101ULL) >> 56); } static inline int pcw(const uint64_t *m, int w) { int c = 0; for (int i = 0; i < w; i++) c += pc64(m[i]); return c; } static int gcd_int(int a, int b) { while (b) { int r = a % b; a = b; b = r; } return a; } static const int EDGE[6][2] = {{0,1},{0,2},{0,3},{1,2},{1,3},{2,3}}; static const int COMP[6][2] = {{2,3},{1,3},{1,2},{0,3},{0,2},{0,1}}; typedef struct { int a, N, F, maxloss, rank, phase[4], exact, quad, minatom, atoms[6]; } Res; /* one candidate phase vector; returns nothing, updates best/R on a strict improvement */ static inline void consider(const uint64_t M[4][MAXQ][MAXW], const int t[4], int N, int wd, int *best, Res *R) { const uint64_t *M0 = M[0][t[0]], *M1 = M[1][t[1]], *M2 = M[2][t[2]], *M3 = M[3][t[3]]; uint64_t un[MAXW]; for (int w = 0; w < wd; w++) un[w] = M0[w] | M1[w] | M2[w] | M3[w]; int exact = N - pcw(un, wd); if (exact <= *best) return; int quad = 0; for (int w = 0; w < wd; w++) quad += pc64(M0[w] & M1[w] & M2[w] & M3[w]); if (!CHORDAL && quad <= *best) return; int atoms[6], minatom = 1 << 30; for (int e = 0; e < 6; e++) { const uint64_t *Mi = M[EDGE[e][0]][t[EDGE[e][0]]], *Mj = M[EDGE[e][1]][t[EDGE[e][1]]]; const uint64_t *Mk = M[COMP[e][0]][t[COMP[e][0]]], *Ml = M[COMP[e][1]][t[COMP[e][1]]]; int c = 0; for (int w = 0; w < wd; w++) c += pc64(Mi[w] & Mj[w] & ~(Mk[w] | Ml[w])); atoms[e] = c; if (c < minatom) minatom = c; if (minatom <= *best) return; } int loss = exact; if (!CHORDAL && quad < loss) loss = quad; if (minatom < loss) loss = minatom; if (loss > *best) { *best = loss; for (int i = 0; i < 4; i++) R->phase[i] = t[i]; R->exact = exact; R->quad = quad; R->minatom = minatom; for (int e = 0; e < 6; e++) R->atoms[e] = atoms[e]; } } /* full evaluation of one start; returns 0, or 3 on N overflow */ static int eval_start(int a, Res *R) { const int L = Lg, *q = Qg; int *off = g_off; uint64_t (*CL)[MAXQ][MAXW] = g_CL, (*MK)[MAXQ][MAXW] = g_MK; int (*TA)[MAXN] = g_TA, (*TB)[MAXN] = g_TB; int N = 0; for (int s = 1; s <= L; s++) { if (slot2[a + s]) { if (N >= MAXN) { fprintf(stderr, "N overflow at a=%d\n", a); return 3; } off[N++] = s; } } R->a = a; R->N = N; R->F = 0; R->maxloss = 0; R->exact = -1; R->quad = 0; R->minatom = 0; memset(R->phase, 0, sizeof R->phase); memset(R->atoms, 0, sizeof R->atoms); if (N == 0) return 0; int wd = (N + 63) >> 6; for (int i = 0; i < 4; i++) { int qi = q[i]; for (int c = 0; c < qi; c++) for (int w = 0; w < wd; w++) CL[i][c][w] = 0; for (int v = 0; v < N; v++) { int c = off[v] % qi; CL[i][c][v >> 6] |= (uint64_t)1 << (v & 63); } for (int t = 0; t < qi; t++) { const uint64_t *c1 = CL[i][(qi - t) % qi], *c2 = CL[i][(qi - 2 - t + qi) % qi]; for (int w = 0; w < wd; w++) MK[i][t][w] = c1[w] | c2[w]; } for (int v = 0; v < N; v++) { int c = off[v] % qi; TA[i][v] = (qi - c) % qi; TB[i][v] = (qi - 2 - c + qi) % qi; } } /* first-order bound */ int F = N; for (int i = 0; i < 4; i++) { int worst = 0; for (int t = 0; t < q[i]; t++) { int c = pcw(MK[i][t], wd); if (c > worst) worst = c; } F -= worst; } R->F = F; int best = -1, t[4]; if (!CHORDAL) { for (int v = 0; v < N; v++) { int a0 = TA[0][v], a1 = TA[1][v], a2 = TA[2][v], a3 = TA[3][v]; int b0 = TB[0][v], b1 = TB[1][v], b2 = TB[2][v], b3 = TB[3][v]; for (int pat = 0; pat < 16; pat++) { t[0] = (pat & 1) ? b0 : a0; t[1] = (pat & 2) ? b1 : a1; t[2] = (pat & 4) ? b2 : a2; t[3] = (pat & 8) ? b3 : a3; consider(MK, t, N, wd, &best, R); } } } else { for (int v0 = 0; v0 < N; v0++) { int a0 = TA[0][v0], a1 = TA[1][v0], b0 = TB[0][v0], b1 = TB[1][v0]; for (int v1 = 0; v1 < N; v1++) { int a2 = TA[2][v1], a3 = TA[3][v1], b2 = TB[2][v1], b3 = TB[3][v1]; for (int pat = 0; pat < 16; pat++) { t[0] = (pat & 1) ? b0 : a0; t[1] = (pat & 2) ? b1 : a1; t[2] = (pat & 4) ? b2 : a2; t[3] = (pat & 8) ? b3 : a3; consider(MK, t, N, wd, &best, R); } } } } if (best < 0) best = 0; R->maxloss = best; return 0; } static void emit_selected(int a, Res *R) { int off[MAXN], n = 0; for (int s = 1; s <= Lg; s++) if (slot2[a + s]) off[n++] = s; printf("{\"a\":%d,\"N\":%d,\"first_order\":%d,\"rank\":%d,\"max_loss\":%d," "\"witness_phase\":[%d,%d,%d,%d],\"exact\":%d,\"quadruple\":%d," "\"minimum_pair_only\":%d,\"pair_only_atoms\":[%d,%d,%d,%d,%d,%d],\"offsets\":[", R->a, R->N, R->F, R->rank, R->maxloss, R->phase[0], R->phase[1], R->phase[2], R->phase[3], R->exact, R->quad, R->minatom, R->atoms[0], R->atoms[1], R->atoms[2], R->atoms[3], R->atoms[4], R->atoms[5]); for (int k = 0; k < n; k++) printf("%s%d", k ? "," : "", off[k]); printf("]}"); } int main(int argc, char **argv) { if (argc < 8) { fprintf(stderr, "usage: fastscan W L keep q0 q1 q2 q3 [mode] [AEND]\n"); return 2; } int W = atoi(argv[1]), L = atoi(argv[2]), keep = atoi(argv[3]); int aend = W; Wg = W; Lg = L; for (int i = 0; i < 4; i++) Qg[i] = atoi(argv[4 + i]); CHORDAL = (argc > 8 && !strcmp(argv[8], "chordal")); if (argc > 9) aend = atoi(argv[9]); if (aend > W) aend = W; if (L > W) { fprintf(stderr, "L > W\n"); return 2; } clock_t c0 = clock(); slot2 = malloc((size_t)2 * W); if (!slot2) { fprintf(stderr, "alloc slot2 failed\n"); return 4; } for (int r = 0; r < 2 * W; r++) { int r2 = r + 2; if (r2 >= W) r2 %= W; slot2[r] = (unsigned char)(gcd_int(r, W) == 1 && gcd_int(r2, W) == 1); } unsigned char *mloss = malloc((size_t)aend); long long hist[MAXLOSS + 1]; memset(hist, 0, sizeof hist); int minF = 1 << 30, globalmax = 0; long long npos = 0; int nthreads = 1; #ifdef _OPENMP nthreads = omp_get_max_threads(); #endif fprintf(stderr, "[info] W=%d L=%d mode=%s threads=%d\n", W, L, CHORDAL ? "chordal" : "envelope", nthreads); #pragma omp parallel { long long *h = calloc(MAXLOSS + 1, sizeof(long long)); int tminF = 1 << 30; int a; #pragma omp for schedule(dynamic, 2048) for (a = 0; a < aend; a++) { Res R; if (eval_start(a, &R) == 3) { h[0] = -1; continue; } mloss[a] = (unsigned char)(R.maxloss > 255 ? 255 : R.maxloss); h[mloss[a]]++; if (R.F < tminF) tminF = R.F; } #pragma omp critical { for (int l = 0; l <= MAXLOSS; l++) hist[l] += h[l]; if (tminF < minF) minF = tminF; } free(h); } for (int l = 1; l <= MAXLOSS; l++) npos += hist[l]; for (int l = MAXLOSS; l >= 0; l--) if (hist[l]) { globalmax = l; break; } printf("{\"mode\":\"%s\",\"W\":%d,\"L\":%d,\"q\":[%d,%d,%d,%d],\"ranked_starts\":%d," "\"global_max_loss\":%d,\"starts_with_positive_loss\":%lld,\"minimum_first_order\":%d," "\"loss_histogram\":[", CHORDAL ? "chordal" : "envelope", W, L, Qg[0], Qg[1], Qg[2], Qg[3], aend, globalmax, npos, minF); { int comma = 0; for (int v = 0; v <= MAXLOSS; v++) if (hist[v]) { printf("%s[%d,%lld]", comma ? "," : "", v, hist[v]); comma = 1; } } printf("],\"selected\":["); { int kept = 0, sizes[256]; static int shapes[256][MAXN]; for (int lev = globalmax; lev >= 0 && kept < keep; lev--) { long long above = 0, below = 0; for (int t = lev + 1; t <= MAXLOSS; t++) above += hist[t]; for (int a = 0; a < aend && kept < keep; a++) { if (mloss[a] != lev) continue; Res R; if (eval_start(a, &R) == 3) continue; R.rank = (int)(above + below); below++; int off[MAXN], n = 0; for (int s = 1; s <= L; s++) if (slot2[a + s]) off[n++] = s; int dup = 0; for (int j = 0; j < kept; j++) if (sizes[j] == n) { int same = 1; for (int k = 0; k < n; k++) if (off[k] - off[0] != shapes[j][k] - shapes[j][0]) { same = 0; break; } if (same) { dup = 1; break; } } if (dup) continue; sizes[kept] = n; memcpy(shapes[kept], off, sizeof(int) * (size_t)n); printf("%s", kept ? "," : ""); emit_selected(a, &R); kept++; } } } printf("]}\n"); fprintf(stderr, "[timing] W=%d L=%d cpu=%.2fs\n", W, L, (double)(clock() - c0) / CLOCKS_PER_SEC); return 0; }