Problem 121

Disc game: turn k has 1 blue / k red. 15 turns. Max prize fund so expected profit.

Answer2269
Output2269
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

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

Flow source

# Project Euler 121
# Disc game: turn k has 1 blue / k red. 15 turns. Max prize fund so expected profit.

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

function main() -> i32 {
    let turns: i32 = 15
    # dp[b] = number of ways to get b blues (unnormalized product of disc counts)
    # After turn t (1..turns), total discs = t+1, blue=1, red=t
    let dp: ptr<i64> = calloc((turns + 1) as i64, 8)
    let ndp: ptr<i64> = calloc((turns + 1) as i64, 8)
    if dp == null || ndp == null { return 1 }
    dp[0] = 1
    for t in 1..(turns + 1) {
        for j in 0..(turns + 1) {
            ndp[j] = 0
        }
        for b in 0..t {
            # draw red: factor t
            ndp[b] = ndp[b] + dp[b] * (t as i64)
            # draw blue: factor 1
            ndp[b + 1] = ndp[b + 1] + dp[b]
        }
        for j in 0..(turns + 1) {
            dp[j] = ndp[j]
        }
    }
    let mut win: i64 = 0
    let mut total: i64 = 1
    for i in 1..(turns + 2) {
        total = total * (i as i64)
    }
    # total outcomes = (turns+1)!
    let need: i32 = turns / 2 + 1
    for i in need..(turns + 1) {
        win = win + dp[i]
    }
    # max prize = floor(total/win)
    printf("%lld\n", total / win)
    free(dp)
    free(ndp)
    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; }

int32_t main(void);



int32_t main(void) {
    int32_t turns = 15;
    int64_t* dp = (int64_t*)(calloc(((int64_t)((turns + 1))), 8));
    int64_t* ndp = (int64_t*)(calloc(((int64_t)((turns + 1))), 8));
    if ((dp == NULL || ndp == NULL)) {
        return 1;
    }
    dp[0] = 1;
    int32_t __flow_step_1 = 1;
    for (int32_t t = 1; (1 <= (turns + 1)) ? t < (turns + 1) : t > (turns + 1); t += (1 <= (turns + 1)) ? 1 : -1) {
        int32_t __flow_step_2 = 1;
        for (int32_t j = 0; (0 <= (turns + 1)) ? j < (turns + 1) : j > (turns + 1); j += (0 <= (turns + 1)) ? 1 : -1) {
            ndp[j] = 0;
        }
        int32_t __flow_step_3 = 1;
        for (int32_t b = 0; (0 <= t) ? b < t : b > t; b += (0 <= t) ? 1 : -1) {
            ndp[b] = (ndp[b] + (dp[b] * ((int64_t)(t))));
            ndp[(b + 1)] = (ndp[(b + 1)] + dp[b]);
        }
        int32_t __flow_step_4 = 1;
        for (int32_t j = 0; (0 <= (turns + 1)) ? j < (turns + 1) : j > (turns + 1); j += (0 <= (turns + 1)) ? 1 : -1) {
            dp[j] = ndp[j];
        }
    }
    int64_t win = 0;
    int64_t total = 1;
    int32_t __flow_step_5 = 1;
    for (int32_t i = 1; (1 <= (turns + 2)) ? i < (turns + 2) : i > (turns + 2); i += (1 <= (turns + 2)) ? 1 : -1) {
        total = (total * ((int64_t)(i)));
    }
    int32_t need = (FLOW_CHECKED_DIV((turns), (2)) + 1);
    int32_t __flow_step_6 = 1;
    for (int32_t i = need; (need <= (turns + 1)) ? i < (turns + 1) : i > (turns + 1); i += (need <= (turns + 1)) ? 1 : -1) {
        win = (win + dp[i]);
    }
    printf("%lld\n", FLOW_CHECKED_DIV((total), (win)));
    free(dp);
    free(ndp);
    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 private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @main() -> i32 {
    %0 = arith.constant 15 : i32
    %2 = arith.constant 1 : i32
    %3 = arith.addi %0, %2 : i32
    %4 = arith.extsi %3 : i32 to i64
    %5 = arith.constant 8 : i32
    %6 = arith.extsi %5 : i32 to i64
    %1 = func.call @calloc(%4, %6) : (i64, i64) -> !llvm.ptr
    %8 = arith.constant 1 : i32
    %9 = arith.addi %0, %8 : i32
    %10 = arith.extsi %9 : i32 to i64
    %11 = arith.constant 8 : i32
    %12 = arith.extsi %11 : i32 to i64
    %7 = func.call @calloc(%10, %12) : (i64, i64) -> !llvm.ptr
    %13 = llvm.mlir.zero : !llvm.ptr
    %14 = llvm.icmp "eq" %1, %13 : !llvm.ptr
    %15 = scf.if %14 -> (i1) {
      %16 = arith.constant true
      scf.yield %16 : i1
    } else {
      %17 = llvm.mlir.zero : !llvm.ptr
      %18 = llvm.icmp "eq" %7, %17 : !llvm.ptr
      scf.yield %18 : i1
    }
    cf.cond_br %15, ^bb0, ^bb1
    ^bb0:
      %19 = arith.constant 1 : i32
      func.return %19 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %20 = arith.constant 1 : i32
    %21 = arith.constant 0 : i32
    %22 = arith.extsi %20 : i32 to i64
    %23 = arith.extsi %21 : i32 to i64
    %24 = llvm.getelementptr %1[%23] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %22, %24 : i64, !llvm.ptr
    %25 = arith.constant 1 : i32
    %26 = arith.constant 1 : i32
    %27 = arith.addi %0, %26 : i32
    %28 = arith.index_cast %25 : i32 to index
    %29 = arith.index_cast %27 : i32 to index
    %31 = arith.constant 1 : index
    %32 = arith.constant -1 : index
    %33 = arith.cmpi sle, %28, %29 : index
    %30 = arith.select %33, %31, %32 : index
    cf.br ^bb3(%28 : index)
    ^bb3(%34: index):
    %35 = arith.cmpi slt, %34, %29 : index
    %36 = arith.cmpi sgt, %34, %29 : index
    %37 = arith.select %33, %35, %36 : i1
    cf.cond_br %37, ^bb4(%34 : index), ^bb5(%34 : index)
    ^bb4(%38: index):
      %39 = arith.constant 0 : i32
      %40 = arith.constant 1 : i32
      %41 = arith.addi %0, %40 : i32
      %42 = arith.index_cast %39 : i32 to index
      %43 = arith.index_cast %41 : i32 to index
      %45 = arith.constant 1 : index
      %46 = arith.constant -1 : index
      %47 = arith.cmpi sle, %42, %43 : index
      %44 = arith.select %47, %45, %46 : index
      cf.br ^bb6(%42 : index)
      ^bb6(%48: index):
      %49 = arith.cmpi slt, %48, %43 : index
      %50 = arith.cmpi sgt, %48, %43 : index
      %51 = arith.select %47, %49, %50 : i1
      cf.cond_br %51, ^bb7(%48 : index), ^bb8(%48 : index)
      ^bb7(%52: index):
        %53 = arith.constant 0 : i32
        %54 = arith.extsi %53 : i32 to i64
        %55 = arith.index_cast %52 : index to i64
        %56 = llvm.getelementptr %7[%55] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %54, %56 : i64, !llvm.ptr
        %57 = arith.addi %52, %44 : index
        cf.br ^bb6(%57 : index)
      ^bb8(%58: index):
      %59 = arith.constant 0 : i32
      %60 = arith.index_cast %59 : i32 to index
      %61 = arith.index_cast %38 : i32 to index
      %63 = arith.constant 1 : index
      %64 = arith.constant -1 : index
      %65 = arith.cmpi sle, %60, %61 : index
      %62 = arith.select %65, %63, %64 : index
      cf.br ^bb9(%60 : index)
      ^bb9(%66: index):
      %67 = arith.cmpi slt, %66, %61 : index
      %68 = arith.cmpi sgt, %66, %61 : index
      %69 = arith.select %65, %67, %68 : i1
      cf.cond_br %69, ^bb10(%66 : index), ^bb11(%66 : index)
      ^bb10(%70: index):
        %72 = arith.index_cast %70 : index to i64
        %73 = llvm.getelementptr %7[%72] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %71 = llvm.load %73 : !llvm.ptr -> i64
        %75 = arith.index_cast %70 : index to i64
        %76 = llvm.getelementptr %1[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %74 = llvm.load %76 : !llvm.ptr -> i64
        %77 = arith.index_cast %38 : index to i64
        %78 = arith.muli %74, %77 : i64
        %79 = arith.addi %71, %78 : i64
        %80 = arith.index_cast %70 : index to i64
        %81 = llvm.getelementptr %7[%80] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %79, %81 : i64, !llvm.ptr
        %83 = arith.constant 1 : i32
        %85 = arith.index_cast %70 : index to i32
        %84 = arith.addi %85, %83 : i32
        %86 = arith.extsi %84 : i32 to i64
        %87 = llvm.getelementptr %7[%86] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %82 = llvm.load %87 : !llvm.ptr -> i64
        %89 = arith.index_cast %70 : index to i64
        %90 = llvm.getelementptr %1[%89] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %88 = llvm.load %90 : !llvm.ptr -> i64
        %91 = arith.addi %82, %88 : i64
        %92 = arith.constant 1 : i32
        %94 = arith.index_cast %70 : index to i32
        %93 = arith.addi %94, %92 : i32
        %95 = arith.extsi %93 : i32 to i64
        %96 = llvm.getelementptr %7[%95] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %91, %96 : i64, !llvm.ptr
        %97 = arith.addi %70, %62 : index
        cf.br ^bb9(%97 : index)
      ^bb11(%98: index):
      %99 = arith.constant 0 : i32
      %100 = arith.constant 1 : i32
      %101 = arith.addi %0, %100 : i32
      %102 = arith.index_cast %99 : i32 to index
      %103 = arith.index_cast %101 : i32 to index
      %105 = arith.constant 1 : index
      %106 = arith.constant -1 : index
      %107 = arith.cmpi sle, %102, %103 : index
      %104 = arith.select %107, %105, %106 : index
      cf.br ^bb12(%102 : index)
      ^bb12(%108: index):
      %109 = arith.cmpi slt, %108, %103 : index
      %110 = arith.cmpi sgt, %108, %103 : index
      %111 = arith.select %107, %109, %110 : i1
      cf.cond_br %111, ^bb13(%108 : index), ^bb14(%108 : index)
      ^bb13(%112: index):
        %114 = arith.index_cast %112 : index to i64
        %115 = llvm.getelementptr %7[%114] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %113 = llvm.load %115 : !llvm.ptr -> i64
        %116 = arith.index_cast %112 : index to i64
        %117 = llvm.getelementptr %1[%116] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %113, %117 : i64, !llvm.ptr
        %118 = arith.addi %112, %104 : index
        cf.br ^bb12(%118 : index)
      ^bb14(%119: index):
      %120 = arith.addi %38, %30 : index
      cf.br ^bb3(%120 : index)
    ^bb5(%121: index):
    %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 = arith.constant 1 : i32
    %127 = arith.extsi %126 : i32 to i64
    %128 = llvm.mlir.constant(1 : i64) : i64
    %129 = llvm.alloca %128 x i64 : (i64) -> !llvm.ptr
    llvm.store %127, %129 : i64, !llvm.ptr
    %130 = arith.constant 1 : i32
    %131 = arith.constant 2 : i32
    %132 = arith.addi %0, %131 : i32
    %133 = arith.index_cast %130 : i32 to index
    %134 = arith.index_cast %132 : i32 to index
    %136 = arith.constant 1 : index
    %137 = arith.constant -1 : index
    %138 = arith.cmpi sle, %133, %134 : index
    %135 = arith.select %138, %136, %137 : index
    cf.br ^bb15(%133 : index)
    ^bb15(%139: index):
    %140 = arith.cmpi slt, %139, %134 : index
    %141 = arith.cmpi sgt, %139, %134 : index
    %142 = arith.select %138, %140, %141 : i1
    cf.cond_br %142, ^bb16(%139 : index), ^bb17(%139 : index)
    ^bb16(%143: index):
      %144 = llvm.load %129 : !llvm.ptr -> i64
      %145 = arith.index_cast %143 : index to i64
      %146 = arith.muli %144, %145 : i64
      llvm.store %146, %129 : i64, !llvm.ptr
      %147 = arith.addi %143, %135 : index
      cf.br ^bb15(%147 : index)
    ^bb17(%148: index):
    %149 = arith.constant 2 : i32
    %150 = arith.divsi %0, %149 : i32
    %151 = arith.constant 1 : i32
    %152 = arith.addi %150, %151 : i32
    %153 = arith.constant 1 : i32
    %154 = arith.addi %0, %153 : i32
    %155 = arith.index_cast %152 : i32 to index
    %156 = arith.index_cast %154 : i32 to index
    %158 = arith.constant 1 : index
    %159 = arith.constant -1 : index
    %160 = arith.cmpi sle, %155, %156 : index
    %157 = arith.select %160, %158, %159 : index
    cf.br ^bb18(%155 : index)
    ^bb18(%161: index):
    %162 = arith.cmpi slt, %161, %156 : index
    %163 = arith.cmpi sgt, %161, %156 : index
    %164 = arith.select %160, %162, %163 : i1
    cf.cond_br %164, ^bb19(%161 : index), ^bb20(%161 : index)
    ^bb19(%165: index):
      %166 = llvm.load %125 : !llvm.ptr -> i64
      %168 = arith.index_cast %165 : index to i64
      %169 = llvm.getelementptr %1[%168] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %167 = llvm.load %169 : !llvm.ptr -> i64
      %170 = arith.addi %166, %167 : i64
      llvm.store %170, %125 : i64, !llvm.ptr
      %171 = arith.addi %165, %157 : index
      cf.br ^bb18(%171 : index)
    ^bb20(%172: index):
    %173 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %174 = llvm.load %129 : !llvm.ptr -> i64
    %175 = llvm.load %125 : !llvm.ptr -> i64
    %176 = arith.divsi %174, %175 : i64
    %177 = llvm.call @printf(%173, %176) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%1) : (!llvm.ptr) -> ()
    func.call @free(%7) : (!llvm.ptr) -> ()
    %180 = arith.constant 0 : i32
    func.return %180 : i32
  }
}