/* Question: does the phase-robust Hunter tree budget retain information that matched old edge/pair spectra discard? Exhaustive finite supports only. */ #include #include #include #include #include typedef struct { int A, B, S, phaseB[3], phaseS[3], SatB; } Score; static void check(int ok, const char *reason) { if (!ok) { fprintf(stderr, "FAIL: %s\n", reason); exit(2); } } static int pc(uint64_t mask) { return __builtin_popcountll(mask); } static int smaller(int a, int b) { return a < b ? a : b; } static int compare_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; } static int support(const uint64_t *slots, int D, uint64_t W, uint64_t t, int L, uint64_t *points) { int lo = 0, hi = D; while (lo < hi) { int mid = (lo + hi) / 2; if (slots[mid] < t) lo = mid + 1; else hi = mid; } int i = lo == D ? 0 : lo, n = 0; uint64_t offset = lo == D ? W : 0; while (slots[i] + offset < t + L) { check(n < 63, "support exceeds 63-bit declared scope"); points[n++] = slots[i] + offset; if (++i == D) { i = 0; offset += W; } } return n; } /* Three primes have three possible spanning trees. The maximum edge sum is the sum of all three overlaps minus their minimum, not all-pair subtraction. */ static Score evaluate(const uint64_t *points, int n, const int *q) { uint64_t masks[3][23] = {{0}}; int sizes[3][23] = {{0}}, maxima[3] = {0}; for (int j = 0; j < 3; j++) { for (int i = 0; i < n; i++) { int r = points[i] % q[j]; masks[j][(q[j] - r) % q[j]] |= 1ULL << i; masks[j][(2 * q[j] - 2 - r) % q[j]] |= 1ULL << i; } for (int a = 0; a < q[j]; a++) { sizes[j][a] = pc(masks[j][a]); if (sizes[j][a] > maxima[j]) maxima[j] = sizes[j][a]; } } Score score = {n - maxima[0] - maxima[1] - maxima[2], 999, 999, {0}, {0}, 0}; for (int a = 0; a < q[0]; a++) for (int b = 0; b < q[1]; b++) { uint64_t x = masks[0][a], y = masks[1][b]; int ab = pc(x & y); for (int c = 0; c < q[2]; c++) { uint64_t z = masks[2][c]; int ac = pc(x & z), bc = pc(y & z); int B = n - sizes[0][a] - sizes[1][b] - sizes[2][c] + ab + ac + bc - smaller(ab, smaller(ac, bc)); int S = n - pc(x | y | z); check(B <= S, "pointwise tree budget exceeds actual survivors"); if (B < score.B) { score.B = B; score.SatB = S; score.phaseB[0] = a; score.phaseB[1] = b; score.phaseB[2] = c; } if (S < score.S) { score.S = S; score.phaseS[0] = a; score.phaseS[1] = b; score.phaseS[2] = c; } } } check(score.A <= score.B && score.B <= score.S, "robust budget ordering"); return score; } static void histogram(const uint64_t *h, int length, int offset) { printf("["); int first = 1; for (int i = 0; i < length; i++) if (h[i]) { if (!first) printf(","); first = 0; printf("[%d,%" PRIu64 "]", i - offset, h[i]); } printf("]"); } int main(void) { int wordCount; check(scanf("%d", &wordCount) == 1 && wordCount > 0 && wordCount <= 66, "word count"); printf("{\"words\":["); for (int word = 0; word < wordCount; word++) { uint64_t W; int D, retain, lengthCount, q[3], lengths[3]; check(scanf("%" SCNu64 " %d %d %d %d %d %d", &W, &D, &retain, &lengthCount, &q[0], &q[1], &q[2]) == 7, "word header"); check(W > 0 && D > 0 && D <= 4455 && lengthCount > 0 && lengthCount <= 3, "word dimensions"); for (int j = 0; j < 3; j++) check(q[j] >= 7 && q[j] <= 23, "prime scope"); uint64_t *slots = malloc(sizeof(uint64_t) * D), *events = malloc(sizeof(uint64_t) * 2 * D); check(slots && events, "allocation"); for (int i = 0; i < D; i++) { check(scanf("%" SCNu64, &slots[i]) == 1 && slots[i] < W, "slot value"); if (i) check(slots[i] > slots[i - 1], "slots must be strictly sorted"); } for (int j = 0; j < lengthCount; j++) check(scanf("%d", &lengths[j]) == 1 && lengths[j] > 0, "length"); if (word) printf(","); printf("{\"word_index\":%d,\"statistics\":[", word); for (int li = 0; li < lengthCount; li++) { int L = lengths[li]; for (int i = 0; i < D; i++) { events[2 * i] = (slots[i] + 1) % W; events[2 * i + 1] = (slots[i] + W - ((L - 1) % W)) % W; } qsort(events, 2 * D, sizeof(uint64_t), compare_u64); int E = 0; for (int i = 0; i < 2 * D; i++) if (!i || events[i] != events[i - 1]) events[E++] = events[i]; uint64_t histB[256] = {0}, histS[64] = {0}, histLoss[256] = {0}; uint64_t worstPoints[63], worstTime = 0; int worstN = 0, minB = 999, minS = 999, minA = 999; int incomplete = 0, nonpositive = 0, improved = 0; Score worst = {0}; int *values = retain ? malloc(sizeof(int) * E * 3) : NULL; for (int ei = 0; ei < E; ei++) { uint64_t points[63]; int n = support(slots, D, W, events[ei], L, points); Score s = evaluate(points, n, q); check(s.B >= -128 && s.B < 128 && s.S >= 0 && s.S < 64 && s.S - s.B < 256, "histogram bounds"); histB[s.B + 128]++; histS[s.S]++; histLoss[s.S - s.B]++; if (s.B <= 0) nonpositive++; if (s.B <= 0 && s.S > 0) incomplete++; if (s.B > s.A) improved++; if (s.B < minB) { minB = s.B; worst = s; worstTime = events[ei]; worstN = n; memcpy(worstPoints, points, sizeof(uint64_t) * n); } if (s.S < minS) minS = s.S; if (s.A < minA) minA = s.A; if (retain) { values[3 * ei] = s.A; values[3 * ei + 1] = s.B; values[3 * ei + 2] = s.S; } } if (li) printf(","); printf("{\"L\":%d,\"event_window_types\":%d,\"minimum_first_order\":%d,\"minimum_tree\":%d,\"minimum_exact\":%d,\"nonpositive_tree_windows\":%d,\"noncover_tree_failures\":%d,\"tree_improvement_windows\":%d,\"phase_vectors_per_window\":%d,\"tree_distribution\":", L, E, minA, minB, minS, nonpositive, incomplete, improved, q[0] * q[1] * q[2]); histogram(histB, 256, 128); printf(",\"exact_distribution\":"); histogram(histS, 64, 0); printf(",\"loss_distribution\":"); histogram(histLoss, 256, 0); printf(",\"worst\":{\"t\":%" PRIu64 ",\"points\":[", worstTime); for (int i = 0; i < worstN; i++) { if (i) printf(","); printf("%" PRIu64, worstPoints[i]); } printf("],\"phase_tree\":[%d,%d,%d],\"phase_exact\":[%d,%d,%d],\"tree_budget\":%d,\"actual_survivors_at_tree_phase\":%d,\"minimum_exact_at_this_window\":%d}", worst.phaseB[0], worst.phaseB[1], worst.phaseB[2], worst.phaseS[0], worst.phaseS[1], worst.phaseS[2], worst.B, worst.SatB, worst.S); if (retain) { printf(",\"event_values\":["); for (int i = 0; i < E; i++) { if (i) printf(","); printf("[%" PRIu64 ",%d,%d,%d]", events[i], values[3 * i], values[3 * i + 1], values[3 * i + 2]); } printf("]"); free(values); } printf("}"); } printf("]}"); free(slots); free(events); } printf("],\"all_pointwise_and_robust_ordering_gates_passed\":true}\n"); return 0; }