Problem 106

For n=12, how many subset pairs need equality testing? Disjoint B,C with |B|=|C|=k>=2: need test iff the sorted pairing is not strictly componentwise ordered (Catalan-related). Count = C(n, 2k) * C(2k, k) / 2 * (1 - C_k / (C(2k,k)/(k+1 wait))) Number needing test for size k: C(n,2k) * (C(2k,k)/2 - C_{k-1}) where C_m is Catalan number = (1/(m+1))*C(2m,m), and for pairs: Need test count for equal size k: C(n,2k) * C(2k,k) * (k-1)/(2*(k+1))

Answer21384
Output21384
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n * sum)
Space complexityO(1)O(sum)
ApproachFlow solutionSubset sum DP
VerdictUnknown

Flow source

# Project Euler 106
# For n=12, how many subset pairs need equality testing?

# Disjoint B,C with |B|=|C|=k>=2: need test iff the sorted pairing is not
# strictly componentwise ordered (Catalan-related).
# Count = C(n, 2k) * C(2k, k) / 2 * (1 - C_k / (C(2k,k)/(k+1 wait)))
# Number needing test for size k: C(n,2k) * (C(2k,k)/2 - C_{k-1})
# where C_m is Catalan number = (1/(m+1))*C(2m,m), and for pairs:
# Need test count for equal size k: C(n,2k) * C(2k,k) * (k-1)/(2*(k+1))

function comb(n: i64, k: i64) -> i64 {
    if k < 0 || k > n { return 0 }
    if k > n - k { k = n - k }
    let mut r: i64 = 1
    let mut i: i64 = 1
    while i <= k {
        r = r * (n - k + i) / i
        i = i + 1
    }
    return r
}

function main() -> i32 {
    let n: i64 = 12
    let mut total: i64 = 0
    let mut k: i64 = 2
    while 2 * k <= n {
        # C(n,2k) * C(2k,k) * (k-1) / (2*(k+1))
        let c1: i64 = comb(n, 2 * k)
        let c2: i64 = comb(2 * k, k)
        total = total + c1 * c2 * (k - 1) / (2 * (k + 1))
        k = k + 1
    }
    printf("%lld\n", total)
    return 0
}

Generated C

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
    while (flow_temp_head) {
        flow_temp_node* n = flow_temp_head;
        flow_temp_head = n->next;
        free(n);
    }
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
    flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
    if (!node) return NULL;
    node->next = flow_temp_head;
    flow_temp_head = node;
    if (!flow_temp_atexit_set) {
        flow_temp_atexit_set = 1;
        atexit(flow_temp_free_all);
    }
    return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
    size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
    char* r = (char*)flow_temp_alloc(la + lb + 1);
    if (!r) return NULL;
    if (la) memcpy(r, a, la);
    if (lb) memcpy(r + la, b, lb);
    r[la + lb] = '\0';
    return r;
}

#define __flow_in_arr(arr, val) __extension__ ({ \
    int _found = 0; \
    size_t _n = sizeof(arr)/sizeof((arr)[0]); \
    for (size_t _i = 0; _i < _n; _i++) { \
        if ((arr)[_i] == (val)) { _found = 1; break; } \
    } _found; })

/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
    fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
    abort();
#if defined(__GNUC__) || defined(__clang__)
    __builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")

#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif

#include <math.h>

void* _ui_state = NULL;

static inline float i32_to_f32(int32_t v) { return (float)v; }

/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }

int64_t comb_i64_i64(int64_t n, int64_t k);
int32_t main(void);

int64_t comb_i64_i64(int64_t n, int64_t k) {
    if ((k < 0 || k > n)) {
        return 0;
    }
    if (k > (n - k)) {
        k = (n - k);
    }
    int64_t r = 1;
    int64_t i = 1;
    while (i <= k) {
        r = FLOW_CHECKED_DIV(((r * ((n - k) + i))), (i));
        i = (i + 1);
    }
    return r;
}

int32_t main(void) {
    int64_t n = 12;
    int64_t total = 0;
    int64_t k = 2;
    while ((2 * k) <= n) {
        int64_t c1 = comb_i64_i64(n, (2 * k));
        int64_t c2 = comb_i64_i64((2 * k), k);
        total = (total + FLOW_CHECKED_DIV((((c1 * c2) * (k - 1))), ((2 * (k + 1)))));
        k = (k + 1);
    }
    printf("%lld\n", total);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func @comb(%arg0: i64, %arg1: i64) -> i64 {
    %0 = arith.constant 0 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi slt, %arg1, %2 : i64
    %3 = scf.if %1 -> (i1) {
      %4 = arith.constant true
      scf.yield %4 : i1
    } else {
      %5 = arith.cmpi sgt, %arg1, %arg0 : i64
      scf.yield %5 : i1
    }
    cf.cond_br %3, ^bb0, ^bb1
    ^bb0:
      %6 = arith.constant 0 : i32
      %7 = arith.extsi %6 : i32 to i64
      func.return %7 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %8 = arith.subi %arg0, %arg1 : i64
    %9 = arith.cmpi sgt, %arg1, %8 : i64
    %10 = scf.if %9 -> (i64) {
      %11 = arith.subi %arg0, %arg1 : i64
      scf.yield %11 : i64
    } else {
      scf.yield %arg1 : i64
    }
    %12 = arith.constant 1 : i32
    %13 = arith.extsi %12 : i32 to i64
    %14 = llvm.mlir.constant(1 : i64) : i64
    %15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
    llvm.store %13, %15 : i64, !llvm.ptr
    %16 = arith.constant 1 : i32
    %17 = arith.extsi %16 : i32 to i64
    %18 = llvm.mlir.constant(1 : i64) : i64
    %19 = llvm.alloca %18 x i64 : (i64) -> !llvm.ptr
    llvm.store %17, %19 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %20 = llvm.load %19 : !llvm.ptr -> i64
    %21 = arith.cmpi sle, %20, %10 : i64
    cf.cond_br %21, ^bb4, ^bb5
    ^bb4:
      %22 = llvm.load %15 : !llvm.ptr -> i64
      %23 = arith.subi %arg0, %10 : i64
      %24 = llvm.load %19 : !llvm.ptr -> i64
      %25 = arith.addi %23, %24 : i64
      %26 = arith.muli %22, %25 : i64
      %27 = llvm.load %19 : !llvm.ptr -> i64
      %28 = arith.divsi %26, %27 : i64
      llvm.store %28, %15 : i64, !llvm.ptr
      %29 = llvm.load %19 : !llvm.ptr -> i64
      %30 = arith.constant 1 : i32
      %32 = arith.extsi %30 : i32 to i64
      %31 = arith.addi %29, %32 : i64
      llvm.store %31, %19 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %33 = llvm.load %15 : !llvm.ptr -> i64
    func.return %33 : i64
  }
  func.func @main() -> i32 {
    %34 = arith.constant 12 : i32
    %35 = arith.extsi %34 : i32 to i64
    %36 = arith.constant 0 : i32
    %37 = arith.extsi %36 : i32 to i64
    %38 = llvm.mlir.constant(1 : i64) : i64
    %39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
    llvm.store %37, %39 : i64, !llvm.ptr
    %40 = arith.constant 2 : i32
    %41 = arith.extsi %40 : i32 to i64
    %42 = llvm.mlir.constant(1 : i64) : i64
    %43 = llvm.alloca %42 x i64 : (i64) -> !llvm.ptr
    llvm.store %41, %43 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %44 = arith.constant 2 : i32
    %45 = llvm.load %43 : !llvm.ptr -> i64
    %47 = arith.extsi %44 : i32 to i64
    %46 = arith.muli %47, %45 : i64
    %48 = arith.cmpi sle, %46, %35 : i64
    cf.cond_br %48, ^bb7, ^bb8
    ^bb7:
      %50 = arith.constant 2 : i32
      %51 = llvm.load %43 : !llvm.ptr -> i64
      %53 = arith.extsi %50 : i32 to i64
      %52 = arith.muli %53, %51 : i64
      %49 = func.call @comb(%35, %52) : (i64, i64) -> i64
      %55 = arith.constant 2 : i32
      %56 = llvm.load %43 : !llvm.ptr -> i64
      %58 = arith.extsi %55 : i32 to i64
      %57 = arith.muli %58, %56 : i64
      %59 = llvm.load %43 : !llvm.ptr -> i64
      %54 = func.call @comb(%57, %59) : (i64, i64) -> i64
      %60 = llvm.load %39 : !llvm.ptr -> i64
      %61 = arith.muli %49, %54 : i64
      %62 = llvm.load %43 : !llvm.ptr -> i64
      %63 = arith.constant 1 : i32
      %65 = arith.extsi %63 : i32 to i64
      %64 = arith.subi %62, %65 : i64
      %66 = arith.muli %61, %64 : i64
      %67 = arith.constant 2 : i32
      %68 = llvm.load %43 : !llvm.ptr -> i64
      %69 = arith.constant 1 : i32
      %71 = arith.extsi %69 : i32 to i64
      %70 = arith.addi %68, %71 : i64
      %73 = arith.extsi %67 : i32 to i64
      %72 = arith.muli %73, %70 : i64
      %74 = arith.divsi %66, %72 : i64
      %75 = arith.addi %60, %74 : i64
      llvm.store %75, %39 : i64, !llvm.ptr
      %76 = llvm.load %43 : !llvm.ptr -> i64
      %77 = arith.constant 1 : i32
      %79 = arith.extsi %77 : i32 to i64
      %78 = arith.addi %76, %79 : i64
      llvm.store %78, %43 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %80 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %81 = llvm.load %39 : !llvm.ptr -> i64
    %82 = llvm.call @printf(%80, %81) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %83 = arith.constant 0 : i32
    func.return %83 : i32
  }
}