Problem 122

Sum of m(k) for 1≤k≤200 — minimal multiplication-chain length to reach k.

Answer1582
Output1582
StatusPASS
Native helperno
Runtime0 ms
Peak memory1088 KB
Time complexityO(n log n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n log n)O(n log n)
Space complexityO(n)O(n)
ApproachFlow solutionAddition chain DP
VerdictOptimal

Flow source

# Project Euler 122
# Sum of m(k) for 1≤k≤200 — minimal multiplication-chain length to reach k.

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

function dfs(chain: ptr<i32>, len: i32, depth: i32, limit: i32, best: ptr<i32>) -> void {
    let cur: i32 = chain[len - 1]
    if depth > best[cur] {
        return
    }
    best[cur] = depth
    let mut i: i32 = len - 1
    while i >= 0 {
        let nxt: i32 = cur + chain[i]
        if nxt <= limit {
            if depth + 1 <= best[nxt] {
                chain[len] = nxt
                dfs(chain, len + 1, depth + 1, limit, best)
            }
        }
        i = i - 1
    }
}

function main() -> i32 {
    let limit: i32 = 200
    let best: ptr<i32> = calloc((limit + 1) as i64, 4)
    let chain: ptr<i32> = calloc(64, 4)
    if best == null || chain == null { return 1 }
    let mut i: i32 = 0
    while i <= limit {
        best[i] = 999
        i = i + 1
    }
    chain[0] = 1
    dfs(chain, 1, 0, limit, best)
    let mut total: i64 = 0
    i = 1
    while i <= limit {
        total = total + (best[i] as i64)
        i = i + 1
    }
    printf("%lld\n", total)
    free(chain)
    free(best)
    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; }

void dfs_ptr_i32_i32_i32_i32_ptr_i32(int32_t* chain, int32_t len, int32_t depth, int32_t limit, int32_t* best);
int32_t main(void);



void dfs_ptr_i32_i32_i32_i32_ptr_i32(int32_t* chain, int32_t len, int32_t depth, int32_t limit, int32_t* best) {
    int32_t cur = chain[(len - 1)];
    if (depth > best[cur]) {
        return;
    }
    best[cur] = depth;
    int32_t i = (len - 1);
    while (i >= 0) {
        int32_t nxt = (cur + chain[i]);
        if (nxt <= limit) {
            if ((depth + 1) <= best[nxt]) {
                chain[len] = nxt;
                dfs_ptr_i32_i32_i32_i32_ptr_i32(chain, (len + 1), (depth + 1), limit, best);
            }
        }
        i = (i - 1);
    }
}

int32_t main(void) {
    int32_t limit = 200;
    int32_t* best = (int32_t*)(calloc(((int64_t)((limit + 1))), 4));
    int32_t* chain = (int32_t*)(calloc(64, 4));
    if ((best == NULL || chain == NULL)) {
        return 1;
    }
    int32_t i = 0;
    while (i <= limit) {
        best[i] = 999;
        i = (i + 1);
    }
    chain[0] = 1;
    dfs_ptr_i32_i32_i32_i32_ptr_i32(chain, 1, 0, limit, best);
    int64_t total = 0;
    i = 1;
    while (i <= limit) {
        total = (total + ((int64_t)(best[i])));
        i = (i + 1);
    }
    printf("%lld\n", total);
    free(chain);
    free(best);
    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 @dfs(%arg0: !llvm.ptr, %arg1: i32, %arg2: i32, %arg3: i32, %arg4: !llvm.ptr) -> () {
    %1 = arith.constant 1 : i32
    %2 = arith.subi %arg1, %1 : i32
    %3 = arith.extsi %2 : i32 to i64
    %4 = llvm.getelementptr %arg0[%3] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    %0 = llvm.load %4 : !llvm.ptr -> i32
    %6 = arith.extsi %0 : i32 to i64
    %7 = llvm.getelementptr %arg4[%6] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    %5 = llvm.load %7 : !llvm.ptr -> i32
    %8 = arith.cmpi sgt, %arg2, %5 : i32
    cf.cond_br %8, ^bb0, ^bb1
    ^bb0:
      func.return
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %9 = arith.extsi %0 : i32 to i64
    %10 = llvm.getelementptr %arg4[%9] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %arg2, %10 : i32, !llvm.ptr
    %11 = arith.constant 1 : i32
    %12 = arith.subi %arg1, %11 : i32
    %13 = llvm.mlir.constant(1 : i64) : i64
    %14 = llvm.alloca %13 x i32 : (i64) -> !llvm.ptr
    llvm.store %12, %14 : i32, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %15 = llvm.load %14 : !llvm.ptr -> i32
    %16 = arith.constant 0 : i32
    %17 = arith.cmpi sge, %15, %16 : i32
    cf.cond_br %17, ^bb4, ^bb5
    ^bb4:
      %19 = llvm.load %14 : !llvm.ptr -> i32
      %20 = arith.extsi %19 : i32 to i64
      %21 = llvm.getelementptr %arg0[%20] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %18 = llvm.load %21 : !llvm.ptr -> i32
      %22 = arith.addi %0, %18 : i32
      %23 = arith.cmpi sle, %22, %arg3 : i32
      cf.cond_br %23, ^bb6, ^bb7
      ^bb6:
        %24 = arith.constant 1 : i32
        %25 = arith.addi %arg2, %24 : i32
        %27 = arith.extsi %22 : i32 to i64
        %28 = llvm.getelementptr %arg4[%27] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %26 = llvm.load %28 : !llvm.ptr -> i32
        %29 = arith.cmpi sle, %25, %26 : i32
        cf.cond_br %29, ^bb9, ^bb10
        ^bb9:
          %30 = arith.extsi %arg1 : i32 to i64
          %31 = llvm.getelementptr %arg0[%30] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %22, %31 : i32, !llvm.ptr
          %33 = arith.constant 1 : i32
          %34 = arith.addi %arg1, %33 : i32
          %35 = arith.constant 1 : i32
          %36 = arith.addi %arg2, %35 : i32
          func.call @dfs(%arg0, %34, %36, %arg3, %arg4) : (!llvm.ptr, i32, i32, i32, !llvm.ptr) -> ()
          cf.br ^bb11
        ^bb10:
          cf.br ^bb11
        ^bb11:
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %37 = llvm.load %14 : !llvm.ptr -> i32
      %38 = arith.constant 1 : i32
      %39 = arith.subi %37, %38 : i32
      llvm.store %39, %14 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    func.return
  }
  func.func @main() -> i32 {
    %40 = arith.constant 200 : i32
    %42 = arith.constant 1 : i32
    %43 = arith.addi %40, %42 : i32
    %44 = arith.extsi %43 : i32 to i64
    %45 = arith.constant 4 : i32
    %46 = arith.extsi %45 : i32 to i64
    %41 = func.call @calloc(%44, %46) : (i64, i64) -> !llvm.ptr
    %48 = arith.constant 64 : i32
    %49 = arith.constant 4 : i32
    %50 = arith.extsi %48 : i32 to i64
    %51 = arith.extsi %49 : i32 to i64
    %47 = func.call @calloc(%50, %51) : (i64, i64) -> !llvm.ptr
    %52 = llvm.mlir.zero : !llvm.ptr
    %53 = llvm.icmp "eq" %41, %52 : !llvm.ptr
    %54 = scf.if %53 -> (i1) {
      %55 = arith.constant true
      scf.yield %55 : i1
    } else {
      %56 = llvm.mlir.zero : !llvm.ptr
      %57 = llvm.icmp "eq" %47, %56 : !llvm.ptr
      scf.yield %57 : i1
    }
    cf.cond_br %54, ^bb12, ^bb13
    ^bb12:
      %58 = arith.constant 1 : i32
      func.return %58 : i32
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %59 = arith.constant 0 : i32
    %60 = llvm.mlir.constant(1 : i64) : i64
    %61 = llvm.alloca %60 x i32 : (i64) -> !llvm.ptr
    llvm.store %59, %61 : i32, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %62 = llvm.load %61 : !llvm.ptr -> i32
    %63 = arith.cmpi sle, %62, %40 : i32
    cf.cond_br %63, ^bb16, ^bb17
    ^bb16:
      %64 = arith.constant 999 : i32
      %65 = llvm.load %61 : !llvm.ptr -> i32
      %66 = arith.extsi %65 : i32 to i64
      %67 = llvm.getelementptr %41[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %64, %67 : i32, !llvm.ptr
      %68 = llvm.load %61 : !llvm.ptr -> i32
      %69 = arith.constant 1 : i32
      %70 = arith.addi %68, %69 : i32
      llvm.store %70, %61 : i32, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %71 = arith.constant 1 : i32
    %72 = arith.constant 0 : i32
    %73 = arith.extsi %72 : i32 to i64
    %74 = llvm.getelementptr %47[%73] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %71, %74 : i32, !llvm.ptr
    %76 = arith.constant 1 : i32
    %77 = arith.constant 0 : i32
    func.call @dfs(%47, %76, %77, %40, %41) : (!llvm.ptr, i32, i32, i32, !llvm.ptr) -> ()
    %78 = arith.constant 0 : i32
    %79 = arith.extsi %78 : i32 to i64
    %80 = llvm.mlir.constant(1 : i64) : i64
    %81 = llvm.alloca %80 x i64 : (i64) -> !llvm.ptr
    llvm.store %79, %81 : i64, !llvm.ptr
    %82 = arith.constant 1 : i32
    llvm.store %82, %61 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %83 = llvm.load %61 : !llvm.ptr -> i32
    %84 = arith.cmpi sle, %83, %40 : i32
    cf.cond_br %84, ^bb19, ^bb20
    ^bb19:
      %85 = llvm.load %81 : !llvm.ptr -> i64
      %87 = llvm.load %61 : !llvm.ptr -> i32
      %88 = arith.extsi %87 : i32 to i64
      %89 = llvm.getelementptr %41[%88] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %86 = llvm.load %89 : !llvm.ptr -> i32
      %90 = arith.extsi %86 : i32 to i64
      %91 = arith.addi %85, %90 : i64
      llvm.store %91, %81 : i64, !llvm.ptr
      %92 = llvm.load %61 : !llvm.ptr -> i32
      %93 = arith.constant 1 : i32
      %94 = arith.addi %92, %93 : i32
      llvm.store %94, %61 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %95 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %96 = llvm.load %81 : !llvm.ptr -> i64
    %97 = llvm.call @printf(%95, %96) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%47) : (!llvm.ptr) -> ()
    func.call @free(%41) : (!llvm.ptr) -> ()
    %100 = arith.constant 0 : i32
    func.return %100 : i32
  }
}