Problem 232
The Race: optimal strategy win probability.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | O(n * s^2) |
| Space complexity | O(n) | O(s^2) |
| Approach | Flow solution | Markov chain or DP over states |
| Verdict | Unknown |
Flow source
# Project Euler 232
# The Race: optimal strategy win probability.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
let mut CACHE: ptr<f64> = null
let mut MAXS: i32 = 0
function two_wins(need_one: i32, need_two: i32) -> f64 {
if need_two == 0 { return 1.0 }
if need_one == 0 { return 0.0 }
let idx: i64 = ((need_one - 1) as i64) * (MAXS as i64) + ((need_two - 1) as i64)
if CACHE[idx] >= 0.0 { return CACHE[idx] }
let mut best: f64 = 0.0
let mut bet: i32 = 1
while true {
let win2: f64 = 0.5 / (bet as f64)
let lose2: f64 = 1.0 - win2
let mut next_two: i32 = need_two - bet
if need_two < bet { next_two = 0 }
let mut current: f64 = 0.5 * win2 * two_wins(need_one - 1, next_two)
current = current + 0.5 * win2 * two_wins(need_one, next_two)
current = current + 0.5 * lose2 * two_wins(need_one - 1, need_two)
current = current / (1.0 - 0.5 * lose2)
if best < current { best = current }
if next_two == 0 { break }
bet = bet * 2
}
CACHE[idx] = best
return best
}
function main() -> i32 {
MAXS = 100
CACHE = calloc((MAXS as i64) * (MAXS as i64), 8)
if CACHE == null { return 1 }
let mut i: i64 = 0
let N: i64 = (MAXS as i64) * (MAXS as i64)
while i < N {
CACHE[i] = 0.0 - 1.0
i = i + 1
}
let result: f64 = 0.5 * two_wins(MAXS - 1, MAXS) + 0.5 * two_wins(MAXS, MAXS)
printf("%.8f\n", result)
free(CACHE)
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 two_wins_i32_i32(int32_t need_one, int32_t need_two);
int32_t main(void);
/* Module statics */
static double* CACHE = NULL;
static int32_t MAXS = 0;
double two_wins_i32_i32(int32_t need_one, int32_t need_two) {
if (need_two == 0) {
return 1.0;
}
if (need_one == 0) {
return 0.0;
}
int64_t idx = ((((int64_t)((need_one - 1))) * ((int64_t)(MAXS))) + ((int64_t)((need_two - 1))));
if (CACHE[idx] >= 0.0) {
return CACHE[idx];
}
double best = 0.0;
int32_t bet = 1;
while (1) {
double win2 = (0.5 / ((double)(bet)));
double lose2 = (1.0 - win2);
int32_t next_two = (need_two - bet);
if (need_two < bet) {
next_two = 0;
}
double current = ((0.5 * win2) * two_wins_i32_i32((need_one - 1), next_two));
current = (current + ((0.5 * win2) * two_wins_i32_i32(need_one, next_two)));
current = (current + ((0.5 * lose2) * two_wins_i32_i32((need_one - 1), need_two)));
current = (current / (1.0 - (0.5 * lose2)));
if (best < current) {
best = current;
}
if (next_two == 0) {
break;
}
bet = (bet * 2);
}
CACHE[idx] = best;
return best;
}
int32_t main(void) {
MAXS = 100;
CACHE = calloc((((int64_t)(MAXS)) * ((int64_t)(MAXS))), 8);
if (CACHE == NULL) {
return 1;
}
int64_t i = 0;
int64_t N = (((int64_t)(MAXS)) * ((int64_t)(MAXS)));
while (i < N) {
CACHE[i] = (0.0 - 1.0);
i = (i + 1);
}
double result = ((0.5 * two_wins_i32_i32((MAXS - 1), MAXS)) + (0.5 * two_wins_i32_i32(MAXS, MAXS)));
printf("%.8f\n", result);
free(CACHE);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.8f\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
// Module static: CACHE
llvm.mlir.global internal @CACHE() {addr_space = 0 : i32} : !llvm.ptr {
%0 = llvm.mlir.zero : !llvm.ptr
llvm.return %0 : !llvm.ptr
}
// Module static: MAXS
llvm.mlir.global internal @MAXS(0 : i32) : i32
func.func @two_wins(%arg0: i32, %arg1: i32) -> f64 {
%1 = arith.constant 0 : i32
%2 = arith.cmpi eq, %arg1, %1 : i32
cf.cond_br %2, ^bb0, ^bb1
^bb0:
%3 = arith.constant 1.0 : f32
%4 = arith.extf %3 : f32 to f64
func.return %4 : f64
^bb1:
cf.br ^bb2
^bb2:
%5 = arith.constant 0 : i32
%6 = arith.cmpi eq, %arg0, %5 : i32
cf.cond_br %6, ^bb3, ^bb4
^bb3:
%7 = arith.constant 0.0 : f32
%8 = arith.extf %7 : f32 to f64
func.return %8 : f64
^bb4:
cf.br ^bb5
^bb5:
%9 = arith.constant 1 : i32
%10 = arith.subi %arg0, %9 : i32
%11 = arith.extsi %10 : i32 to i64
%12 = llvm.mlir.addressof @MAXS : !llvm.ptr
%13 = llvm.load %12 : !llvm.ptr -> i32
%14 = arith.extsi %13 : i32 to i64
%15 = arith.muli %11, %14 : i64
%16 = arith.constant 1 : i32
%17 = arith.subi %arg1, %16 : i32
%18 = arith.extsi %17 : i32 to i64
%19 = arith.addi %15, %18 : i64
%21 = llvm.mlir.addressof @CACHE : !llvm.ptr
%22 = llvm.load %21 : !llvm.ptr -> !llvm.ptr
%23 = llvm.getelementptr %22[%19] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%20 = llvm.load %23 : !llvm.ptr -> f64
%24 = arith.constant 0.0 : f32
%26 = arith.extf %24 : f32 to f64
%25 = arith.cmpf oge, %20, %26 : f64
cf.cond_br %25, ^bb6, ^bb7
^bb6:
%28 = llvm.mlir.addressof @CACHE : !llvm.ptr
%29 = llvm.load %28 : !llvm.ptr -> !llvm.ptr
%30 = llvm.getelementptr %29[%19] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%27 = llvm.load %30 : !llvm.ptr -> f64
func.return %27 : f64
^bb7:
cf.br ^bb8
^bb8:
%31 = arith.constant 0.0 : f32
%32 = arith.extf %31 : f32 to f64
%33 = llvm.mlir.constant(1 : i64) : i64
%34 = llvm.alloca %33 x f64 : (i64) -> !llvm.ptr
llvm.store %32, %34 : f64, !llvm.ptr
%35 = arith.constant 1 : i32
%36 = llvm.mlir.constant(1 : i64) : i64
%37 = llvm.alloca %36 x i32 : (i64) -> !llvm.ptr
llvm.store %35, %37 : i32, !llvm.ptr
cf.br ^bb9
^bb9:
%38 = arith.constant 1 : i1
cf.cond_br %38, ^bb10, ^bb11
^bb10:
%39 = arith.constant 0.5 : f32
%40 = llvm.load %37 : !llvm.ptr -> i32
%41 = arith.sitofp %40 : i32 to f64
%43 = arith.extf %39 : f32 to f64
%42 = arith.divf %43, %41 : f64
%44 = arith.constant 1.0 : f32
%46 = arith.extf %44 : f32 to f64
%45 = arith.subf %46, %42 : f64
%47 = llvm.load %37 : !llvm.ptr -> i32
%48 = arith.subi %arg1, %47 : i32
%49 = llvm.mlir.constant(1 : i64) : i64
%50 = llvm.alloca %49 x i32 : (i64) -> !llvm.ptr
llvm.store %48, %50 : i32, !llvm.ptr
%51 = llvm.load %37 : !llvm.ptr -> i32
%52 = arith.cmpi slt, %arg1, %51 : i32
cf.cond_br %52, ^bb12, ^bb13
^bb12:
%53 = arith.constant 0 : i32
llvm.store %53, %50 : i32, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%54 = arith.constant 0.5 : f32
%56 = arith.extf %54 : f32 to f64
%55 = arith.mulf %56, %42 : f64
%58 = arith.constant 1 : i32
%59 = arith.subi %arg0, %58 : i32
%60 = llvm.load %50 : !llvm.ptr -> i32
%57 = func.call @two_wins(%59, %60) : (i32, i32) -> f64
%61 = arith.mulf %55, %57 : f64
%62 = llvm.mlir.constant(1 : i64) : i64
%63 = llvm.alloca %62 x f64 : (i64) -> !llvm.ptr
llvm.store %61, %63 : f64, !llvm.ptr
%64 = llvm.load %63 : !llvm.ptr -> f64
%65 = arith.constant 0.5 : f32
%67 = arith.extf %65 : f32 to f64
%66 = arith.mulf %67, %42 : f64
%69 = llvm.load %50 : !llvm.ptr -> i32
%68 = func.call @two_wins(%arg0, %69) : (i32, i32) -> f64
%70 = arith.mulf %66, %68 : f64
%71 = arith.addf %64, %70 : f64
llvm.store %71, %63 : f64, !llvm.ptr
%72 = llvm.load %63 : !llvm.ptr -> f64
%73 = arith.constant 0.5 : f32
%75 = arith.extf %73 : f32 to f64
%74 = arith.mulf %75, %45 : f64
%77 = arith.constant 1 : i32
%78 = arith.subi %arg0, %77 : i32
%76 = func.call @two_wins(%78, %arg1) : (i32, i32) -> f64
%79 = arith.mulf %74, %76 : f64
%80 = arith.addf %72, %79 : f64
llvm.store %80, %63 : f64, !llvm.ptr
%81 = llvm.load %63 : !llvm.ptr -> f64
%82 = arith.constant 1.0 : f32
%83 = arith.constant 0.5 : f32
%85 = arith.extf %83 : f32 to f64
%84 = arith.mulf %85, %45 : f64
%87 = arith.extf %82 : f32 to f64
%86 = arith.subf %87, %84 : f64
%88 = arith.divf %81, %86 : f64
llvm.store %88, %63 : f64, !llvm.ptr
%89 = llvm.load %34 : !llvm.ptr -> f64
%90 = llvm.load %63 : !llvm.ptr -> f64
%91 = arith.cmpf olt, %89, %90 : f64
cf.cond_br %91, ^bb15, ^bb16
^bb15:
%92 = llvm.load %63 : !llvm.ptr -> f64
llvm.store %92, %34 : f64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%93 = llvm.load %50 : !llvm.ptr -> i32
%94 = arith.constant 0 : i32
%95 = arith.cmpi eq, %93, %94 : i32
cf.cond_br %95, ^bb18, ^bb19
^bb18:
cf.br ^bb11
^bb19:
cf.br ^bb20
^bb20:
%96 = llvm.load %37 : !llvm.ptr -> i32
%97 = arith.constant 2 : i32
%98 = arith.muli %96, %97 : i32
llvm.store %98, %37 : i32, !llvm.ptr
cf.br ^bb9
^bb11:
%99 = llvm.load %34 : !llvm.ptr -> f64
%100 = llvm.mlir.addressof @CACHE : !llvm.ptr
%101 = llvm.load %100 : !llvm.ptr -> !llvm.ptr
%102 = llvm.getelementptr %101[%19] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %99, %102 : f64, !llvm.ptr
%103 = llvm.load %34 : !llvm.ptr -> f64
func.return %103 : f64
}
func.func @main() -> i32 {
%104 = arith.constant 100 : i32
%105 = llvm.mlir.addressof @MAXS : !llvm.ptr
llvm.store %104, %105 : i32, !llvm.ptr
%107 = llvm.mlir.addressof @MAXS : !llvm.ptr
%108 = llvm.load %107 : !llvm.ptr -> i32
%109 = arith.extsi %108 : i32 to i64
%110 = llvm.mlir.addressof @MAXS : !llvm.ptr
%111 = llvm.load %110 : !llvm.ptr -> i32
%112 = arith.extsi %111 : i32 to i64
%113 = arith.muli %109, %112 : i64
%114 = arith.constant 8 : i32
%115 = arith.extsi %114 : i32 to i64
%106 = func.call @calloc(%113, %115) : (i64, i64) -> !llvm.ptr
%116 = llvm.mlir.addressof @CACHE : !llvm.ptr
llvm.store %106, %116 : !llvm.ptr, !llvm.ptr
%117 = llvm.mlir.addressof @CACHE : !llvm.ptr
%118 = llvm.load %117 : !llvm.ptr -> !llvm.ptr
%119 = llvm.mlir.zero : !llvm.ptr
%120 = llvm.icmp "eq" %118, %119 : !llvm.ptr
cf.cond_br %120, ^bb21, ^bb22
^bb21:
%121 = arith.constant 1 : i32
func.return %121 : i32
^bb22:
cf.br ^bb23
^bb23:
%122 = arith.constant 0 : i32
%123 = arith.extsi %122 : i32 to i64
%124 = llvm.mlir.constant(1 : i64) : i64
%125 = llvm.alloca %124 x i64 : (i64) -> !llvm.ptr
llvm.store %123, %125 : i64, !llvm.ptr
%126 = llvm.mlir.addressof @MAXS : !llvm.ptr
%127 = llvm.load %126 : !llvm.ptr -> i32
%128 = arith.extsi %127 : i32 to i64
%129 = llvm.mlir.addressof @MAXS : !llvm.ptr
%130 = llvm.load %129 : !llvm.ptr -> i32
%131 = arith.extsi %130 : i32 to i64
%132 = arith.muli %128, %131 : i64
cf.br ^bb24
^bb24:
%133 = llvm.load %125 : !llvm.ptr -> i64
%134 = arith.cmpi slt, %133, %132 : i64
cf.cond_br %134, ^bb25, ^bb26
^bb25:
%135 = arith.constant 0.0 : f32
%136 = arith.constant 1.0 : f32
%137 = arith.subf %135, %136 : f32
%138 = llvm.mlir.addressof @CACHE : !llvm.ptr
%139 = llvm.load %138 : !llvm.ptr -> !llvm.ptr
%140 = llvm.load %125 : !llvm.ptr -> i64
%141 = arith.extf %137 : f32 to f64
%142 = llvm.getelementptr %139[%140] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %141, %142 : f64, !llvm.ptr
%143 = llvm.load %125 : !llvm.ptr -> i64
%144 = arith.constant 1 : i32
%146 = arith.extsi %144 : i32 to i64
%145 = arith.addi %143, %146 : i64
llvm.store %145, %125 : i64, !llvm.ptr
cf.br ^bb24
^bb26:
%147 = arith.constant 0.5 : f32
%149 = llvm.mlir.addressof @MAXS : !llvm.ptr
%150 = llvm.load %149 : !llvm.ptr -> i32
%151 = arith.constant 1 : i32
%152 = arith.subi %150, %151 : i32
%153 = llvm.mlir.addressof @MAXS : !llvm.ptr
%154 = llvm.load %153 : !llvm.ptr -> i32
%148 = func.call @two_wins(%152, %154) : (i32, i32) -> f64
%156 = arith.extf %147 : f32 to f64
%155 = arith.mulf %156, %148 : f64
%157 = arith.constant 0.5 : f32
%159 = llvm.mlir.addressof @MAXS : !llvm.ptr
%160 = llvm.load %159 : !llvm.ptr -> i32
%161 = llvm.mlir.addressof @MAXS : !llvm.ptr
%162 = llvm.load %161 : !llvm.ptr -> i32
%158 = func.call @two_wins(%160, %162) : (i32, i32) -> f64
%164 = arith.extf %157 : f32 to f64
%163 = arith.mulf %164, %158 : f64
%165 = arith.addf %155, %163 : f64
%166 = llvm.mlir.addressof @str_0 : !llvm.ptr
%167 = llvm.call @printf(%166, %165) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%169 = llvm.mlir.addressof @CACHE : !llvm.ptr
%170 = llvm.load %169 : !llvm.ptr -> !llvm.ptr
func.call @free(%170) : (!llvm.ptr) -> ()
%171 = arith.constant 0 : i32
func.return %171 : i32
}
}