Problem 398

Expected second-shortest segment length E(10^7, 100) to 5 decimals.

Answer2010.59096
Output2010.59096
StatusPASS
Native helperno
Runtime50 ms
Peak memory1072 KB
Time complexityO(1) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(1)O(n * s^2)
Space complexityO(n)O(s^2)
ApproachFlow solutionMarkov chain or DP over states
VerdictUnknown

Flow source

# Project Euler 398
# Expected second-shortest segment length E(10^7, 100) to 5 decimals.

function binom_ratio(top_n: i32, bot_n: i32, r: i32) -> f64 {
    if top_n < r { return 0.0 }
    let mut prod: f64 = 1.0
    let mut i: i32 = 0
    while i < r {
        prod = prod * ((top_n - i) as f64) / ((bot_n - i) as f64)
        i = i + 1
    }
    return prod
}

function expected_second_shortest(n: i32, m: i32) -> f64 {
    if m == 1 { return n as f64 }
    let r: i32 = m - 1
    let n_total: i32 = n - 1
    let inv_den: ptr<f64> = calloc(r as i64, 8)
    if inv_den == null { return 0.0 }
    let mut i: i32 = 0
    while i < r {
        inv_den[i] = 1.0 / ((n_total - i) as f64)
        i = i + 1
    }
    let mut N2: i32 = n_total
    let mut N1: i32 = n_total
    let mut acc: f64 = 0.0
    while N2 >= r {
        let mut base: f64 = 1.0
        i = 0
        while i < r {
            base = base * ((N2 - i) as f64) * inv_den[i]
            i = i + 1
        }
        let mut factor: f64 = 0.0
        if N1 >= r {
            let ratio: f64 = binom_ratio(N1, N2, r)
            factor = (m as f64) - ((m - 1) as f64) * ratio
        } else {
            factor = m as f64
        }
        acc = acc + base * factor
        N2 = N2 - (m - 1)
        N1 = N1 - m
    }
    free(inv_den)
    return acc
}

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function main() -> i32 {
    let ans: f64 = expected_second_shortest(10000000, 100)
    printf("%.5f\n", ans)
    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; }

double binom_ratio_i32_i32_i32(int32_t top_n, int32_t bot_n, int32_t r);
double expected_second_shortest_i32_i32(int32_t n, int32_t m);
int32_t main(void);

double binom_ratio_i32_i32_i32(int32_t top_n, int32_t bot_n, int32_t r) {
    if (top_n < r) {
        return 0.0;
    }
    double prod = 1.0;
    int32_t i = 0;
    while (i < r) {
        prod = ((prod * ((double)((top_n - i)))) / ((double)((bot_n - i))));
        i = (i + 1);
    }
    return prod;
}

double expected_second_shortest_i32_i32(int32_t n, int32_t m) {
    if (m == 1) {
        return ((double)(n));
    }
    int32_t r = (m - 1);
    int32_t n_total = (n - 1);
    double* inv_den = (double*)(calloc(((int64_t)(r)), 8));
    if (inv_den == NULL) {
        return 0.0;
    }
    int32_t i = 0;
    while (i < r) {
        inv_den[i] = (1.0 / ((double)((n_total - i))));
        i = (i + 1);
    }
    int32_t N2 = n_total;
    int32_t N1 = n_total;
    double acc = 0.0;
    while (N2 >= r) {
        double base = 1.0;
        i = 0;
        while (i < r) {
            base = ((base * ((double)((N2 - i)))) * inv_den[i]);
            i = (i + 1);
        }
        double factor = 0.0;
        if (N1 >= r) {
            double ratio = binom_ratio_i32_i32_i32(N1, N2, r);
            factor = (((double)(m)) - (((double)((m - 1))) * ratio));
        } else {
            factor = ((double)(m));
        }
        acc = (acc + (base * factor));
        N2 = (N2 - (m - 1));
        N1 = (N1 - m);
    }
    free(inv_den);
    return acc;
}



int32_t main(void) {
    double ans = expected_second_shortest_i32_i32(10000000, 100);
    printf("%.5f\n", ans);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%.5f\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func @binom_ratio(%arg0: i32, %arg1: i32, %arg2: i32) -> f64 {
    %0 = arith.cmpi slt, %arg0, %arg2 : i32
    cf.cond_br %0, ^bb0, ^bb1
    ^bb0:
      %1 = arith.constant 0.0 : f32
      %2 = arith.extf %1 : f32 to f64
      func.return %2 : f64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %3 = arith.constant 1.0 : f32
    %4 = arith.extf %3 : f32 to f64
    %5 = llvm.mlir.constant(1 : i64) : i64
    %6 = llvm.alloca %5 x f64 : (i64) -> !llvm.ptr
    llvm.store %4, %6 : f64, !llvm.ptr
    %7 = arith.constant 0 : i32
    %8 = llvm.mlir.constant(1 : i64) : i64
    %9 = llvm.alloca %8 x i32 : (i64) -> !llvm.ptr
    llvm.store %7, %9 : i32, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %10 = llvm.load %9 : !llvm.ptr -> i32
    %11 = arith.cmpi slt, %10, %arg2 : i32
    cf.cond_br %11, ^bb4, ^bb5
    ^bb4:
      %12 = llvm.load %6 : !llvm.ptr -> f64
      %13 = llvm.load %9 : !llvm.ptr -> i32
      %14 = arith.subi %arg0, %13 : i32
      %15 = arith.sitofp %14 : i32 to f64
      %16 = arith.mulf %12, %15 : f64
      %17 = llvm.load %9 : !llvm.ptr -> i32
      %18 = arith.subi %arg1, %17 : i32
      %19 = arith.sitofp %18 : i32 to f64
      %20 = arith.divf %16, %19 : f64
      llvm.store %20, %6 : f64, !llvm.ptr
      %21 = llvm.load %9 : !llvm.ptr -> i32
      %22 = arith.constant 1 : i32
      %23 = arith.addi %21, %22 : i32
      llvm.store %23, %9 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %24 = llvm.load %6 : !llvm.ptr -> f64
    func.return %24 : f64
  }
  func.func @expected_second_shortest(%arg0: i32, %arg1: i32) -> f64 {
    %25 = arith.constant 1 : i32
    %26 = arith.cmpi eq, %arg1, %25 : i32
    cf.cond_br %26, ^bb6, ^bb7
    ^bb6:
      %27 = arith.sitofp %arg0 : i32 to f64
      func.return %27 : f64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %28 = arith.constant 1 : i32
    %29 = arith.subi %arg1, %28 : i32
    %30 = arith.constant 1 : i32
    %31 = arith.subi %arg0, %30 : i32
    %33 = arith.extsi %29 : i32 to i64
    %34 = arith.constant 8 : i32
    %35 = arith.extsi %34 : i32 to i64
    %32 = func.call @calloc(%33, %35) : (i64, i64) -> !llvm.ptr
    %36 = llvm.mlir.zero : !llvm.ptr
    %37 = llvm.icmp "eq" %32, %36 : !llvm.ptr
    cf.cond_br %37, ^bb9, ^bb10
    ^bb9:
      %38 = arith.constant 0.0 : f32
      %39 = arith.extf %38 : f32 to f64
      func.return %39 : f64
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %40 = arith.constant 0 : i32
    %41 = llvm.mlir.constant(1 : i64) : i64
    %42 = llvm.alloca %41 x i32 : (i64) -> !llvm.ptr
    llvm.store %40, %42 : i32, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %43 = llvm.load %42 : !llvm.ptr -> i32
    %44 = arith.cmpi slt, %43, %29 : i32
    cf.cond_br %44, ^bb13, ^bb14
    ^bb13:
      %45 = arith.constant 1.0 : f32
      %46 = llvm.load %42 : !llvm.ptr -> i32
      %47 = arith.subi %31, %46 : i32
      %48 = arith.sitofp %47 : i32 to f64
      %50 = arith.extf %45 : f32 to f64
      %49 = arith.divf %50, %48 : f64
      %51 = llvm.load %42 : !llvm.ptr -> i32
      %52 = arith.extsi %51 : i32 to i64
      %53 = llvm.getelementptr %32[%52] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      llvm.store %49, %53 : f64, !llvm.ptr
      %54 = llvm.load %42 : !llvm.ptr -> i32
      %55 = arith.constant 1 : i32
      %56 = arith.addi %54, %55 : i32
      llvm.store %56, %42 : i32, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %57 = llvm.mlir.constant(1 : i64) : i64
    %58 = llvm.alloca %57 x i32 : (i64) -> !llvm.ptr
    llvm.store %31, %58 : i32, !llvm.ptr
    %59 = llvm.mlir.constant(1 : i64) : i64
    %60 = llvm.alloca %59 x i32 : (i64) -> !llvm.ptr
    llvm.store %31, %60 : i32, !llvm.ptr
    %61 = arith.constant 0.0 : f32
    %62 = arith.extf %61 : f32 to f64
    %63 = llvm.mlir.constant(1 : i64) : i64
    %64 = llvm.alloca %63 x f64 : (i64) -> !llvm.ptr
    llvm.store %62, %64 : f64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %65 = llvm.load %58 : !llvm.ptr -> i32
    %66 = arith.cmpi sge, %65, %29 : i32
    cf.cond_br %66, ^bb16, ^bb17
    ^bb16:
      %67 = arith.constant 1.0 : f32
      %68 = arith.extf %67 : f32 to f64
      %69 = llvm.mlir.constant(1 : i64) : i64
      %70 = llvm.alloca %69 x f64 : (i64) -> !llvm.ptr
      llvm.store %68, %70 : f64, !llvm.ptr
      %71 = arith.constant 0 : i32
      llvm.store %71, %42 : i32, !llvm.ptr
      cf.br ^bb18
      ^bb18:
      %72 = llvm.load %42 : !llvm.ptr -> i32
      %73 = arith.cmpi slt, %72, %29 : i32
      cf.cond_br %73, ^bb19, ^bb20
      ^bb19:
        %74 = llvm.load %70 : !llvm.ptr -> f64
        %75 = llvm.load %58 : !llvm.ptr -> i32
        %76 = llvm.load %42 : !llvm.ptr -> i32
        %77 = arith.subi %75, %76 : i32
        %78 = arith.sitofp %77 : i32 to f64
        %79 = arith.mulf %74, %78 : f64
        %81 = llvm.load %42 : !llvm.ptr -> i32
        %82 = arith.extsi %81 : i32 to i64
        %83 = llvm.getelementptr %32[%82] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        %80 = llvm.load %83 : !llvm.ptr -> f64
        %84 = arith.mulf %79, %80 : f64
        llvm.store %84, %70 : f64, !llvm.ptr
        %85 = llvm.load %42 : !llvm.ptr -> i32
        %86 = arith.constant 1 : i32
        %87 = arith.addi %85, %86 : i32
        llvm.store %87, %42 : i32, !llvm.ptr
        cf.br ^bb18
      ^bb20:
      %88 = arith.constant 0.0 : f32
      %89 = arith.extf %88 : f32 to f64
      %90 = llvm.mlir.constant(1 : i64) : i64
      %91 = llvm.alloca %90 x f64 : (i64) -> !llvm.ptr
      llvm.store %89, %91 : f64, !llvm.ptr
      %92 = llvm.load %60 : !llvm.ptr -> i32
      %93 = arith.cmpi sge, %92, %29 : i32
      cf.cond_br %93, ^bb21, ^bb22
      ^bb21:
        %95 = llvm.load %60 : !llvm.ptr -> i32
        %96 = llvm.load %58 : !llvm.ptr -> i32
        %94 = func.call @binom_ratio(%95, %96, %29) : (i32, i32, i32) -> f64
        %97 = arith.sitofp %arg1 : i32 to f64
        %98 = arith.constant 1 : i32
        %99 = arith.subi %arg1, %98 : i32
        %100 = arith.sitofp %99 : i32 to f64
        %101 = arith.mulf %100, %94 : f64
        %102 = arith.subf %97, %101 : f64
        llvm.store %102, %91 : f64, !llvm.ptr
        cf.br ^bb23
      ^bb22:
        %103 = arith.sitofp %arg1 : i32 to f64
        llvm.store %103, %91 : f64, !llvm.ptr
        cf.br ^bb23
      ^bb23:
      %104 = llvm.load %64 : !llvm.ptr -> f64
      %105 = llvm.load %70 : !llvm.ptr -> f64
      %106 = llvm.load %91 : !llvm.ptr -> f64
      %107 = arith.mulf %105, %106 : f64
      %108 = arith.addf %104, %107 : f64
      llvm.store %108, %64 : f64, !llvm.ptr
      %109 = llvm.load %58 : !llvm.ptr -> i32
      %110 = arith.constant 1 : i32
      %111 = arith.subi %arg1, %110 : i32
      %112 = arith.subi %109, %111 : i32
      llvm.store %112, %58 : i32, !llvm.ptr
      %113 = llvm.load %60 : !llvm.ptr -> i32
      %114 = arith.subi %113, %arg1 : i32
      llvm.store %114, %60 : i32, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    func.call @free(%32) : (!llvm.ptr) -> ()
    %116 = llvm.load %64 : !llvm.ptr -> f64
    func.return %116 : f64
  }
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @main() -> i32 {
    %118 = arith.constant 10000000 : i32
    %119 = arith.constant 100 : i32
    %117 = func.call @expected_second_shortest(%118, %119) : (i32, i32) -> f64
    %120 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %121 = llvm.call @printf(%120, %117) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    %122 = arith.constant 0 : i32
    func.return %122 : i32
  }
}