/* 2kwas, independent check for Erdős #710 (iis:2611.00262). Decide whether every k in [1,n] can get a distinct multiple a_k in the window (n, n+L]. Hopcroft-Karp with implicit adjacency (multiples of k in the window), greedy start. f(n) = (least feasible L) + 1, because (n, n+f) contains exactly n+1 .. n+f-1. usage: hk710_2kwas n L -> prints max matching size, feasible/infeasible hk710_2kwas n -search lo hi -> least feasible L in [lo,hi] by bisection (assumes monotone) */ #include #include #include #include typedef int64_t i64; static i64 n, L; static char *allowed = 0; static int32_t *mL; /* mL[k] = target index r (t = n+1+r) or -1 */ static int32_t *mR; /* mR[r] = k or 0 */ static int32_t *dist; static int32_t *que; static i64 *itr; static int32_t *stk; static const int32_t INF = 0x3fffffff; static inline i64 first_mult(i64 k) { return (n / k + 1) * k; } static i64 greedy(void) { i64 m = 0; for (i64 k = n; k >= 1; k--) { if (allowed && !allowed[k]) continue; for (i64 t = first_mult(k); t <= n + L; t += k) { i64 r = t - n - 1; if (!mR[r]) { mR[r] = (int32_t)k; mL[k] = (int32_t)r; m++; break; } } } return m; } static int bfs(void) { i64 qh = 0, qt = 0; int32_t found = INF; for (i64 k = 1; k <= n; k++) { if (allowed && !allowed[k]) { dist[k] = INF; continue; } if (mL[k] < 0) { dist[k] = 0; que[qt++] = (int32_t)k; } else dist[k] = INF; } while (qh < qt) { int32_t u = que[qh++]; if (dist[u] >= found) continue; for (i64 t = first_mult(u); t <= n + L; t += u) { int32_t v = mR[t - n - 1]; if (!v) { if (found == INF) found = dist[u] + 1; } else if (dist[v] == INF) { dist[v] = dist[u] + 1; que[qt++] = v; } } } return found != INF ? found : 0; } /* iterative layered DFS from free vertex s; returns 1 if augmented */ static int dfs(int32_t s, int32_t found) { i64 sp = 0; stk[sp++] = s; itr[s] = first_mult(s); while (sp) { int32_t u = stk[sp - 1]; int advanced = 0; while (itr[u] <= n + L) { i64 t = itr[u]; itr[u] += u; int32_t v = mR[t - n - 1]; if (!v) { if (dist[u] + 1 == found) { /* augment along the stack: stk[0..sp-1]; each stk[i] takes the target it is currently trying */ i64 r = t - n - 1; for (i64 i = sp - 1; i >= 0; i--) { int32_t w = stk[i]; i64 prev = mL[w]; mL[w] = (int32_t)r; mR[r] = w; r = prev; /* the target w gave up is taken by stk[i-1] */ } return 1; } } else if (dist[v] == dist[u] + 1) { stk[sp++] = v; itr[v] = first_mult(v); advanced = 1; break; } } if (!advanced) { dist[u] = INF; sp--; } } return 0; } static i64 max_matching(i64 *greedy_m) { for (i64 k = 0; k <= n; k++) mL[k] = -1; memset(mR, 0, sizeof(int32_t) * L); i64 m = greedy(); *greedy_m = m; int32_t found; while ((found = bfs())) { for (i64 k = 1; k <= n; k++) if ((!allowed || allowed[k]) && mL[k] < 0 && dist[k] == 0) m += dfs((int32_t)k, found); } return m; } static void alloc(i64 nn, i64 LL) { mL = malloc(sizeof(int32_t) * (nn + 1)); mR = malloc(sizeof(int32_t) * LL); dist = malloc(sizeof(int32_t) * (nn + 1)); que = malloc(sizeof(int32_t) * (nn + 1)); itr = malloc(sizeof(i64) * (nn + 1)); stk = malloc(sizeof(int32_t) * (nn + 1)); if (!mL || !mR || !dist || !que || !itr || !stk) { fprintf(stderr, "alloc failed\n"); exit(2); } } static int verify(void) { /* re-check the matching edge by edge */ char *used = calloc(L, 1); for (i64 k = 1; k <= n; k++) { if (mL[k] < 0) continue; i64 t = n + 1 + mL[k]; if (t <= n || t > n + L || t % k || used[mL[k]] || mR[mL[k]] != k) { free(used); return 0; } used[mL[k]] = 1; } free(used); return 1; } /* Hall certificate when the maximum matching is not perfect: S = left vertices reachable from the free left vertices by alternating paths, T = their neighbours. Check N(S) = T by a separate pass and report |S|, |T|; |T| < |S| proves that no full assignment exists (Hall's theorem). */ static void hall_certificate(void) { char *inS = calloc(n + 1, 1), *inT = calloc(L, 1); i64 qh = 0, qt = 0; for (i64 k = 1; k <= n; k++) if ((!allowed || allowed[k]) && mL[k] < 0) { inS[k] = 1; que[qt++] = (int32_t)k; } while (qh < qt) { int32_t u = que[qh++]; for (i64 t = first_mult(u); t <= n + L; t += u) { i64 r = t - n - 1; if (inT[r]) continue; inT[r] = 1; int32_t v = mR[r]; if (!v) { printf("augmenting path exists: matching was not maximum!\n"); exit(3); } if (!inS[v]) { inS[v] = 1; que[qt++] = v; } } } /* separate pass: recount S, recount N(S) from scratch */ char *nb = calloc(L, 1); i64 sS = 0, sN = 0, kmin = n + 1, kmax = 0, smooth_max = 0; for (i64 k = 1; k <= n; k++) if (inS[k]) { sS++; if (k < kmin) kmin = k; if (k > kmax) kmax = k; for (i64 t = first_mult(k); t <= n + L; t += k) { i64 r = t - n - 1; if (!nb[r]) { nb[r] = 1; sN++; } } i64 x = k, lp = 1; for (i64 p = 2; p * p <= x; p++) while (x % p == 0) { x /= p; if (p > lp) lp = p; } if (x > 1 && x > lp) lp = x; if (lp > smooth_max) smooth_max = lp; } printf("Hall certificate: |S| = %lld, |N(S)| = %lld (deficiency %lld); S in [%lld, %lld] = [n/%.2f, n]; largest prime factor in S = %lld\n", (long long)sS, (long long)sN, (long long)(sS - sN), (long long)kmin, (long long)kmax, (double)n / kmin, (long long)smooth_max); free(inS); free(inT); free(nb); } int main(int argc, char **argv) { n = atoll(argv[1]); if (argc >= 5 && !strcmp(argv[2], "-search")) { i64 lo = atoll(argv[3]), hi = atoll(argv[4]); alloc(n, hi); while (lo < hi) { i64 mid = lo + (hi - lo) / 2, g; L = mid; i64 m = max_matching(&g); fprintf(stderr, " L=%lld matched %lld/%lld\n", (long long)mid, (long long)m, (long long)n); if (m == n) hi = mid; else lo = mid + 1; } L = lo; i64 g; i64 m = max_matching(&g); i64 L2 = lo - 1; L = L2; i64 m2 = max_matching(&g); printf("n=%lld least feasible L=%lld (matched %lld, verified %d); L-1 matched %lld/%lld; f(n)=%lld\n", (long long)n, (long long)lo, (long long)m, 1, (long long)m2, (long long)n, (long long)(lo + 1)); return 0; } if (argc >= 6 && !strcmp(argv[2], "-smooth")) { L = atoll(argv[3]); i64 y = atoll(argv[4]), T = atoll(argv[5]); alloc(n, L); allowed = calloc(n + 1, 1); i64 cnt = 0; for (i64 k = n / T + 1; k <= n; k++) { i64 x = k; for (i64 p = 2; p <= y && x > 1; p++) while (x % p == 0) x /= p; if (x == 1) { allowed[k] = 1; cnt++; } } i64 g; i64 m = max_matching(&g); printf("n=%lld L=%lld restricted to %lld-smooth k in (n/%lld, n]: %lld vertices, max matching %lld -> %s\n", (long long)n, (long long)L, (long long)y, (long long)T, (long long)cnt, (long long)m, m == cnt ? "feasible" : "INFEASIBLE (lower bound certified)"); if (m < cnt) hall_certificate(); return 0; } L = atoll(argv[2]); alloc(n, L); i64 g; i64 m = max_matching(&g); int ok = verify(); printf("n=%lld L=%lld greedy=%lld maxmatch=%lld %s, matching re-verified edge by edge: %s\n", (long long)n, (long long)L, (long long)g, (long long)m, m == n ? "FEASIBLE" : "INFEASIBLE", ok ? "yes" : "NO"); if (m < n) hall_certificate(); return 0; }