/* ties.c - ties between last digits of primes (prime puzzle 1287)
   Arnav Garg, Nov 2026

   tie for [a,b]: a prime p ending in a or b where #primes<=p ending in a
   equals #primes<=p ending in b. triples/quadruple: all counts equal.
   prints largest tie and number of ties for all 6 pairs, 4 triples and
   [1,3,7,9], primes < L.

   sieve is a segmented wheel-30 one, byte i = the 8 numbers 30i+r,
   r = 1,7,11,13,17,19,23,29. 30 is a multiple of 10 so every bit position
   has a fixed last digit -> digit counts of a 64 bit word are popcounts.

   pass 1: per chunk, count each digit + min/max of each pair difference
   pass 2: redo only the chunks where some difference can hit 0, prime by prime

   extending: --from L0 --counts c1,c3,c7,c9 starts at L0 (multiple of 30)
   with the counts of primes < L0. --ckpt file saves finished chunks so a
   killed run can resume, --time sec stops after sec seconds (exit 2).

   cc -O3 -march=native -o ties ties.c -lpthread -lm
*/
#include <inttypes.h>
#include <math.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>

static const uint8_t R[8] = {1, 7, 11, 13, 17, 19, 23, 29};
static const int DIG[8] = {0, 2, 0, 1, 2, 3, 1, 3}; // 0..3 = digit 1,3,7,9
static const int DIGV[4] = {1, 3, 7, 9};
#define W8(x) ((uint64_t)(x) * 0x0101010101010101ULL)
static const uint64_t DM[4] = {W8(0x05), W8(0x48), W8(0x12), W8(0xA0)};

// same order as the table on the puzzle page
static const int PA[6][2] = {{0, 3}, {2, 3}, {0, 2}, {1, 2}, {0, 1}, {1, 3}};
static const int TR[4][3] = {{0, 1, 3}, {0, 1, 2}, {1, 2, 3}, {0, 2, 3}};
#define NCOMB 11

typedef struct {
    int64_t cnt[4];
    int64_t lo[6], hi[6];    // min/max of c_a - c_b inside the chunk
} summary_t;

typedef struct {
    uint64_t p;
    int comb;
} event_t;

typedef struct {
    int64_t ties[NCOMB];
    uint64_t last[NCOMB];
    event_t *ev;             // triple/quad ties + first few [1,9] ties
    size_t nev, cap;
    int npairev;
} result_t;

static uint64_t L, TB, CB, SB, NCH, B0;   // B0 = L0/30
static int64_t C0[4] = {0, 1, 0, 0};       // 3 is not in the wheel so count it here
static FILE *CK;
static pthread_mutex_t CKLOCK = PTHREAD_MUTEX_INITIALIZER;
static uint8_t *SDONE, *RDONE;
static double TLIMIT, TSTART;
static atomic_int STOPPED;
static uint32_t *P;          // primes 7..sqrt(L)
static uint8_t (*PMASK)[8];
static uint32_t (*PBOFF)[8];
static size_t NP;
static summary_t *SUM;
static result_t *RES;
static int64_t (*START)[4];
static uint8_t *NEED;
static atomic_uint_fast64_t NEXT;
static int PASS, NTHREADS;
static int PAIREV_MAX = 40;  // for A399129

static double now(void) {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec + tv.tv_usec * 1e-6;
}

static int bitof(unsigned r) {
    for (int j = 0; j < 8; j++)
        if (R[j] == r) return j;
    return -1;
}

static void make_primes(uint64_t lim) {
    uint8_t *c = calloc(lim + 1, 1);
    P = malloc(sizeof(uint32_t) * (lim / 2 + 16));
    for (uint64_t i = 2; i * i <= lim; i++)
        if (!c[i])
            for (uint64_t k = i * i; k <= lim; k += i) c[k] = 1;
    NP = 0;
    for (uint64_t i = 7; i <= lim; i++)
        if (!c[i]) P[NP++] = (uint32_t)i;
    free(c);
    PMASK = malloc(NP * 8);
    PBOFF = malloc(NP * 8 * sizeof(uint32_t));
    for (size_t i = 0; i < NP; i++)
        for (int j = 0; j < 8; j++) {
            uint64_t m = (uint64_t)P[i] * R[j];
            PMASK[i][j] = (uint8_t)(1u << bitof(m % 30));
            PBOFF[i][j] = (uint32_t)(m / 30);
        }
}

// primes < segment size are crossed off normally, bigger ones go in buckets
// (one list per segment, entries = offset and prime*8+residue)
static size_t NSMALL;

typedef struct {
    uint32_t off, pj;
} bucket_entry_t;

typedef struct {
    uint64_t *next;
    bucket_entry_t **b;
    uint32_t *bn, *bcap;
    uint64_t nseg;
} sieve_ctx_t;

static void bucket_push(sieve_ctx_t *x, uint64_t seg, uint32_t off, uint32_t pj) {
    if (x->bn[seg] == x->bcap[seg]) {
        x->bcap[seg] = x->bcap[seg] ? 2 * x->bcap[seg] : 1024;
        x->b[seg] = realloc(x->b[seg], x->bcap[seg] * sizeof(bucket_entry_t));
    }
    x->b[seg][x->bn[seg]++] = (bucket_entry_t){off, pj};
}

// first multiple p*q >= 30*c0 for each residue (q >= p)
static void chunk_init(uint64_t c0, uint64_t cend, sieve_ctx_t *x) {
    uint64_t clen = cend - c0;
    for (uint64_t g = 0; g < x->nseg; g++) x->bn[g] = 0;
    for (size_t k = 0; k < NSMALL * 8; k++) x->next[k] = UINT64_MAX;
    for (size_t i = 0; i < NP; i++) {
        uint64_t p = P[i];
        if (p * p >= 30 * cend) return;
        for (int j = 0; j < 8; j++) {
            uint64_t b = PBOFF[i][j];
            uint64_t t1 = p > R[j] ? (p - R[j] + 29) / 30 : 0;
            uint64_t t2 = c0 > b ? (c0 - b + p - 1) / p : 0;
            uint64_t t = t1 > t2 ? t1 : t2;
            uint64_t off = p * t + b - c0;
            if (i < NSMALL) x->next[i * 8 + j] = off;
            else if (off < clen) bucket_push(x, off / SB, (uint32_t)off, (uint32_t)(i * 8 + j));
        }
    }
}

static void sieve_seg(uint8_t *seg, uint64_t c0, uint64_t clen, uint64_t s, uint64_t n, sieve_ctx_t *x) {
    memset(seg, 0xFF, n);
    memset(seg + n, 0, (8 - n % 8) % 8);
    uint64_t e = s + n;
    for (size_t i = 0; i < NSMALL; i++) {
        uint64_t *nx = x->next + i * 8;
        if (nx[0] == UINT64_MAX) break;
        uint64_t p = P[i];
        for (int j = 0; j < 8; j++) {
            uint64_t o = nx[j];
            uint8_t m = (uint8_t)~PMASK[i][j];
            for (; o < e; o += p) seg[o - s] &= m;
            nx[j] = o;
        }
    }
    uint64_t g = s / SB;
    bucket_entry_t *be = x->b[g];
    for (uint32_t k = 0; k < x->bn[g]; k++) {
        uint32_t pj = be[k].pj;
        uint64_t o = be[k].off;
        seg[o - s] &= (uint8_t)~PMASK[pj >> 3][pj & 7];
        o += P[pj >> 3];
        if (o < clen) bucket_push(x, o / SB, (uint32_t)o, pj);
    }
    x->bn[g] = 0;
    if (c0 + s == 0) seg[0] &= (uint8_t)~1;
    uint64_t absend = c0 + e;
    if (absend == TB) {                     // clear numbers >= L
        uint64_t base = 30 * (TB - 1);
        uint8_t keep = 0;
        for (int j = 0; j < 8; j++)
            if (base + R[j] < L) keep |= (uint8_t)(1u << j);
        seg[n - 1] &= keep;
    }
}

static void push_ev(result_t *r, uint64_t p, int comb) {
    if (r->nev == r->cap) {
        r->cap = r->cap ? 2 * r->cap : 64;
        r->ev = realloc(r->ev, r->cap * sizeof(event_t));
    }
    r->ev[r->nev++] = (event_t){p, comb};
}

static void scan_summary(const uint8_t *seg, uint64_t n, int64_t c[4], summary_t *sm) {
    for (uint64_t w = 0; w < (n + 7) / 8; w++) {
        uint64_t x;
        memcpy(&x, seg + 8 * w, 8);
        int64_t k[4];
        for (int d = 0; d < 4; d++) k[d] = __builtin_popcountll(x & DM[d]);
        for (int q = 0; q < 6; q++) {
            int a = PA[q][0], b = PA[q][1];
            int64_t df = c[a] - c[b];
            if (df - k[b] < sm->lo[q]) sm->lo[q] = df - k[b];
            if (df + k[a] > sm->hi[q]) sm->hi[q] = df + k[a];
        }
        for (int d = 0; d < 4; d++) c[d] += k[d];
    }
}

static void scan_exact(const uint8_t *seg, uint64_t n, uint64_t absbyte, int64_t c[4], result_t *r) {
    for (uint64_t w = 0; w < (n + 7) / 8; w++) {
        uint64_t x;
        memcpy(&x, seg + 8 * w, 8);
        int64_t k[4];
        for (int d = 0; d < 4; d++) k[d] = __builtin_popcountll(x & DM[d]);
        int near = 0;
        for (int q = 0; q < 6 && !near; q++) {
            int a = PA[q][0], b = PA[q][1];
            int64_t df = c[a] - c[b];
            near = k[a] + k[b] > 0 && df - k[b] <= 0 && df + k[a] >= 0;
        }
        if (!near) {
            for (int d = 0; d < 4; d++) c[d] += k[d];
            continue;
        }
        while (x) {
            int bpos = __builtin_ctzll(x);
            x &= x - 1;
            int j = bpos & 7;
            uint64_t p = 30 * (absbyte + 8 * w + (bpos >> 3)) + R[j];
            int dx = DIG[j];
            c[dx]++;
            for (int q = 0; q < 6; q++)
                if ((PA[q][0] == dx || PA[q][1] == dx) && c[PA[q][0]] == c[PA[q][1]]) {
                    r->ties[q]++;
                    r->last[q] = p;
                    if (q == 0 && r->npairev < PAIREV_MAX) {
                        push_ev(r, p, q);
                        r->npairev++;
                    }
                }
            for (int t = 0; t < 4; t++) {
                const int *g = TR[t];
                if ((g[0] == dx || g[1] == dx || g[2] == dx) && c[g[0]] == c[g[1]] && c[g[1]] == c[g[2]]) {
                    r->ties[6 + t]++;
                    r->last[6 + t] = p;
                    push_ev(r, p, 6 + t);
                }
            }
            if (c[0] == c[1] && c[1] == c[2] && c[2] == c[3]) {
                r->ties[10]++;
                r->last[10] = p;
                push_ev(r, p, 10);
            }
        }
    }
}

// checkpoint lines:  S ch cnt[4] lo/hi[6]   or   R ch ties/last[11] nev npairev (p comb)...
static void ckpt_write(uint64_t ch) {
    pthread_mutex_lock(&CKLOCK);
    if (PASS == 1) {
        summary_t *m = &SUM[ch];
        fprintf(CK, "S %" PRIu64, ch);
        for (int d = 0; d < 4; d++) fprintf(CK, " %" PRId64, m->cnt[d]);
        for (int q = 0; q < 6; q++) fprintf(CK, " %" PRId64 " %" PRId64, m->lo[q], m->hi[q]);
        SDONE[ch] = 1;
    } else {
        result_t *r = &RES[ch];
        fprintf(CK, "R %" PRIu64, ch);
        for (int q = 0; q < NCOMB; q++) fprintf(CK, " %" PRId64 " %" PRIu64, r->ties[q], r->last[q]);
        fprintf(CK, " %zu %d", r->nev, r->npairev);
        for (size_t i = 0; i < r->nev; i++) fprintf(CK, " %" PRIu64 " %d", r->ev[i].p, r->ev[i].comb);
        RDONE[ch] = 1;
    }
    fputc('\n', CK);
    fflush(CK);
    pthread_mutex_unlock(&CKLOCK);
}

// read old checkpoint (must be same L/L0/sizes), throw away a half written last line
static void ckpt_open(const char *path) {
    char head[256];
    snprintf(head, sizeof head, "H %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRId64 " %" PRId64 " %" PRId64
             " %" PRId64, L, B0, CB, SB, C0[0], C0[1], C0[2], C0[3]);
    FILE *f = fopen(path, "r");
    long good = 0;
    size_t ns = 0, nr = 0;
    if (f) {
        size_t cap = 1 << 16;
        char *line = malloc(cap);
        int first = 1;
        for (;;) {
            size_t len = 0;
            int ch;
            while ((ch = fgetc(f)) != EOF && ch != '\n') {
                if (len + 2 > cap) line = realloc(line, cap *= 2);
                line[len++] = (char)ch;
            }
            if (ch == EOF) break;
            line[len] = 0;
            if (first) {
                if (strcmp(line, head)) {
                    fprintf(stderr, "checkpoint %s is for another run:\n  %s\n  want %s\n", path, line, head);
                    exit(1);
                }
                first = 0;
            } else if (line[0] == 'S') {
                char *e = line + 2;
                uint64_t c = strtoull(e, &e, 10);
                summary_t *m = &SUM[c];
                for (int d = 0; d < 4; d++) m->cnt[d] = strtoll(e, &e, 10);
                for (int q = 0; q < 6; q++) m->lo[q] = strtoll(e, &e, 10), m->hi[q] = strtoll(e, &e, 10);
                SDONE[c] = 1;
                ns++;
            } else if (line[0] == 'R') {
                char *e = line + 2;
                uint64_t c = strtoull(e, &e, 10);
                result_t *r = &RES[c];
                for (int q = 0; q < NCOMB; q++) r->ties[q] = strtoll(e, &e, 10), r->last[q] = strtoull(e, &e, 10);
                size_t n = strtoull(e, &e, 10);
                r->npairev = (int)strtol(e, &e, 10);
                r->nev = r->cap = 0;
                r->ev = NULL;
                for (size_t i = 0; i < n; i++) {
                    uint64_t p = strtoull(e, &e, 10);
                    push_ev(r, p, (int)strtol(e, &e, 10));
                }
                RDONE[c] = 1;
                nr++;
            }
            good = ftell(f);
        }
        free(line);
        fclose(f);
        if (truncate(path, good)) { perror("truncate"); exit(1); }
    }
    CK = fopen(path, "a");
    if (!CK) { perror(path); exit(1); }
    if (good == 0) fprintf(CK, "%s\n", head), fflush(CK);
    printf("checkpoint %s: %zu pass-1 and %zu pass-2 chunks already done\n", path, ns, nr);
}

static void *worker(void *arg) {
    (void)arg;
    uint8_t *seg = aligned_alloc(64, (SB + 127) / 64 * 64);
    sieve_ctx_t x;
    x.nseg = CB / SB;
    x.next = malloc((NSMALL + 1) * 8 * sizeof(uint64_t));
    x.b = calloc(x.nseg, sizeof *x.b);
    x.bn = calloc(x.nseg, sizeof *x.bn);
    x.bcap = calloc(x.nseg, sizeof *x.bcap);
    for (;;) {
        if (TLIMIT > 0 && now() - TSTART > TLIMIT) {
            atomic_store(&STOPPED, 1);
            break;
        }
        uint64_t ch = atomic_fetch_add(&NEXT, 1);
        if (ch >= NCH) break;
        if (PASS == 1 && SDONE[ch]) continue;
        if (PASS == 2 && (!NEED[ch] || RDONE[ch])) continue;
        uint64_t c0 = B0 + ch * CB, cend = c0 + CB < TB ? c0 + CB : TB;
        chunk_init(c0, cend, &x);
        int64_t c[4] = {0, 0, 0, 0};
        if (PASS == 2) memcpy(c, START[ch], sizeof c);
        summary_t *sm = &SUM[ch];
        if (PASS == 1)
            for (int q = 0; q < 6; q++) sm->lo[q] = sm->hi[q] = 0;
        for (uint64_t s = 0; s < cend - c0; s += SB) {
            uint64_t n = cend - c0 - s < SB ? cend - c0 - s : SB;
            sieve_seg(seg, c0, cend - c0, s, n, &x);
            if (PASS == 1) scan_summary(seg, n, c, sm);
            else scan_exact(seg, n, c0 + s, c, &RES[ch]);
        }
        if (PASS == 1) memcpy(sm->cnt, c, sizeof c);
        if (CK) ckpt_write(ch);
    }
    free(seg);
    free(x.next);
    for (uint64_t g = 0; g < x.nseg; g++) free(x.b[g]);
    free(x.b);
    free(x.bn);
    free(x.bcap);
    return NULL;
}

static void run_pass(int pass) {
    PASS = pass;
    atomic_store(&NEXT, 0);
    pthread_t th[256];
    for (int i = 0; i < NTHREADS; i++) pthread_create(&th[i], NULL, worker, NULL);
    for (int i = 0; i < NTHREADS; i++) pthread_join(th[i], NULL);
}

static int cmp_ev(const void *a, const void *b) {
    const event_t *x = a, *y = b;
    if (x->comb != y->comb) return x->comb - y->comb;
    return x->p < y->p ? -1 : x->p > y->p;
}

static void comb_name(int q, char *s) {
    if (q < 6) sprintf(s, "[%d,%d]", DIGV[PA[q][0]], DIGV[PA[q][1]]);
    else if (q < 10) sprintf(s, "[%d,%d,%d]", DIGV[TR[q - 6][0]], DIGV[TR[q - 6][1]], DIGV[TR[q - 6][2]]);
    else strcpy(s, "[1,3,7,9]");
}

int main(int argc, char **argv) {
    if (argc < 2) {
        fprintf(stderr, "usage: %s L [threads] [chunk_bytes] [seg_bytes]\n"
                        "  counts ties among primes < L (L may be written 1e13)\n", argv[0]);
        return 1;
    }
    L = (uint64_t)strtold(argv[1], NULL);
    const char *ckpath = NULL;
    uint64_t L0 = 0;
    int npos = 0, have_counts = 0;
    char *pos[3] = {0};
    for (int i = 2; i < argc; i++) {
        if (!strcmp(argv[i], "--from") && i + 1 < argc) L0 = (uint64_t)strtold(argv[++i], NULL);
        else if (!strcmp(argv[i], "--ckpt") && i + 1 < argc) ckpath = argv[++i];
        else if (!strcmp(argv[i], "--time") && i + 1 < argc) TLIMIT = atof(argv[++i]);
        else if (!strcmp(argv[i], "--counts") && i + 1 < argc) {
            have_counts = sscanf(argv[++i], "%" SCNd64 ",%" SCNd64 ",%" SCNd64 ",%" SCNd64, &C0[0], &C0[1], &C0[2],
                                 &C0[3]) == 4;
        } else if (npos < 3) pos[npos++] = argv[i];
    }
    NTHREADS = pos[0] ? atoi(pos[0]) : (int)sysconf(_SC_NPROCESSORS_ONLN);
    CB = pos[1] ? strtoull(pos[1], NULL, 10) : (1u << 24);
    SB = pos[2] ? strtoull(pos[2], NULL, 10) : (1u << 16);
    if (L0 % 30 || L0 >= L || (L0 > 0 && !have_counts)) {
        fprintf(stderr, "--from L0 needs L0 a multiple of 30 below L, and --counts c1,c3,c7,c9 (primes < L0)\n");
        return 1;
    }
    B0 = L0 / 30;
    if (L < 8 || SB % 8 || CB % SB || CB > (1ULL << 31) || NTHREADS < 1 || NTHREADS > 256) {
        fprintf(stderr, "need L >= 8, seg_bytes a multiple of 8, chunk_bytes a multiple of seg_bytes and at most 2^31\n");
        return 1;
    }
    TB = (L + 29) / 30;
    NCH = (TB - B0 + CB - 1) / CB;
    uint64_t sq = (uint64_t)sqrtl((long double)L);
    while (sq * sq > L) sq--;
    while ((sq + 1) * (sq + 1) <= L) sq++;
    make_primes(sq + 1);
    for (NSMALL = 0; NSMALL < NP && P[NSMALL] < SB; NSMALL++) {}
    SUM = calloc(NCH, sizeof(summary_t));
    RES = calloc(NCH, sizeof(result_t));
    START = calloc(NCH, sizeof *START);
    NEED = calloc(NCH, 1);
    SDONE = calloc(NCH, 1);
    RDONE = calloc(NCH, 1);
    if (L0) printf("range [%" PRIu64 ", L) from counts %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 "\n", L0, C0[0],
                   C0[1], C0[2], C0[3]);
    if (ckpath) ckpt_open(ckpath);
    TSTART = now();
    printf("L=%" PRIu64 " threads=%d chunks=%" PRIu64 " chunk_bytes=%" PRIu64 " seg_bytes=%" PRIu64
           " sieving_primes=%zu\n", L, NTHREADS, NCH, CB, SB, NP);
    fflush(stdout);

    double t0 = now();
    run_pass(1);
    double t1 = now();
    if (atomic_load(&STOPPED)) {
        uint64_t d = 0;
        for (uint64_t ch = 0; ch < NCH; ch++) d += SDONE[ch];
        printf("INCOMPLETE: time limit in pass 1, %" PRIu64 " of %" PRIu64 " chunks done; run again to resume\n", d, NCH);
        return 2;
    }

    // prefix sums -> starting counts of each chunk
    int64_t c[4];
    memcpy(c, C0, sizeof c);
    uint64_t need = 0;
    for (uint64_t ch = 0; ch < NCH; ch++) {
        memcpy(START[ch], c, sizeof c);
        for (int q = 0; q < 6; q++) {
            int64_t off = c[PA[q][0]] - c[PA[q][1]];
            if (off + SUM[ch].lo[q] <= 0 && off + SUM[ch].hi[q] >= 0) NEED[ch] = 1;
        }
        need += NEED[ch];
        for (int d = 0; d < 4; d++) c[d] += SUM[ch].cnt[d];
    }
    printf("pass 1 (summary): %.2f s; %" PRIu64 " of %" PRIu64 " chunks need pass 2\n", t1 - t0, need, NCH);
    fflush(stdout);
    run_pass(2);
    double t2 = now();
    printf("pass 2 (exact):   %.2f s\n", t2 - t1);
    if (atomic_load(&STOPPED)) {
        uint64_t d = 0;
        for (uint64_t ch = 0; ch < NCH; ch++) d += NEED[ch] && RDONE[ch];
        printf("INCOMPLETE: time limit in pass 2, %" PRIu64 " of %" PRIu64 " chunks done; run again to resume\n", d, need);
        return 2;
    }

    int64_t ties[NCOMB] = {0};
    uint64_t last[NCOMB] = {0};
    size_t nev = 0;
    for (uint64_t ch = 0; ch < NCH; ch++) nev += RES[ch].nev;
    event_t *ev = malloc((nev + 1) * sizeof(event_t));
    nev = 0;
    for (uint64_t ch = 0; ch < NCH; ch++) {
        for (int q = 0; q < NCOMB; q++) {
            ties[q] += RES[ch].ties[q];
            if (RES[ch].last[q] > last[q]) last[q] = RES[ch].last[q];
        }
        memcpy(ev + nev, RES[ch].ev, RES[ch].nev * sizeof(event_t));
        nev += RES[ch].nev;
    }
    qsort(ev, nev, sizeof(event_t), cmp_ev);

    printf("\ncounts of primes < L ending in 1,3,7,9: %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64
           "  (pi(L) = %" PRId64 ")\n", c[0], c[1], c[2], c[3],
           c[0] + c[1] + c[2] + c[3] + (L > 2) + (L > 5));
    if (L0) printf("(ties below are those in [%" PRIu64 ", L) only)\n", L0);
    printf("\n%-10s %22s %14s\n", "digits", "largest tie", "ties");
    for (int q = 0; q < NCOMB; q++) {
        char nm[16];
        comb_name(q, nm);
        if (ties[q]) printf("%-10s %22" PRIu64 " %14" PRId64 "\n", nm, last[q], ties[q]);
        else printf("%-10s %22s %14d\n", nm, "none", 0);
    }
    printf("\nfirst [1,9] ties:");
    int shown = 0;
    for (size_t i = 0; i < nev && shown < PAIREV_MAX; i++)
        if (ev[i].comb == 0) printf(" %" PRIu64, ev[i].p), shown++;
    printf("\n");
    for (size_t i = 0; i < nev; i++)
        if (ev[i].comb >= 6) {
            char nm[16];
            comb_name(ev[i].comb, nm);
            printf("tie %s %" PRIu64 "\n", nm, ev[i].p);
        }
    printf("\ntotal %.2f s\n", t2 - t0);
    return 0;
}
