Problem 884

Compute S(10^17) where D(n) is the number of steps obtained by repeatedly subtracting the largest perfect cube not exceeding the current value. Uses a cube-interval recurrence with memoization.

Answer1105985795684653500
Output1105985795684653500
StatusPASS
Native helperno
Runtime60 ms
Peak memory45248 KB
Time complexityO(n) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionSearch with pruning or sieve
VerdictOptimal

Flow source

# Project Euler 884: Removing Cubes
# Compute S(10^17) where D(n) is the number of steps obtained by repeatedly
# subtracting the largest perfect cube not exceeding the current value.
# Uses a cube-interval recurrence with memoization.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
    function pow(x: f64, y: f64) -> f64
    function round(x: f64) -> f64
}

const MEMO_LIMIT: i64 = 5000000

let mut base_pref: ptr<i64> = null
let mut base_pref_len: i32 = 0
let mut memo: ptr<i64> = null
let mut memo_valid: ptr<i8> = null
let mut delta_prefix: ptr<i64> = null
let mut delta_prefix_len: i32 = 0

function icbrt(n: i64) -> i64 {
    if n <= 0 {
        return 0
    }
    let d: f64 = (n as f64)
    let mut x: i64 = (round(pow(d, 1.0 / 3.0)) as i64)
    while (((x + 1) as i128) * ((x + 1) as i128) * ((x + 1) as i128)) <= (n as i128) {
        x = x + 1
    }
    while ((x as i128) * (x as i128) * (x as i128)) > (n as i128) {
        x = x - 1
    }
    return x
}

function d_greedy(n: i64) -> i64 {
    let mut nn: i64 = n
    let mut steps: i64 = 0
    while nn > 0 {
        let k: i64 = icbrt(nn)
        nn = nn - k * k * k
        steps = steps + 1
    }
    return steps
}

function build_base_prefix(limit: i32) -> void {
    let d: ptr<i64> = calloc((limit as i64), 8)
    let mut i: i32 = 0
    while i < limit {
        d[i] = d_greedy((i as i64))
        i = i + 1
    }
    base_pref = calloc(((limit + 1) as i64), 8)
    base_pref_len = limit + 1
    let mut s: i64 = 0
    i = 0
    while i < limit {
        s = s + d[i]
        base_pref[(i + 1) as i64] = s
        i = i + 1
    }
    free(d)
}

function F(N: i64) -> i64 {
    if N <= 0 {
        return 0
    }
    if N < (base_pref_len as i64) {
        return base_pref[N]
    }
    if N <= MEMO_LIMIT {
        let idx: i64 = N
        if memo_valid[idx] != 0 {
            return memo[idx]
        }
    }

    let K: i64 = icbrt(N - 1)
    let K3: i128 = (K as i128) * (K as i128) * (K as i128)
    let L: i64 = (N - (K3 as i64))

    let res: i64 = (N - 1) + delta_prefix[K - 1] + F(L)

    if N <= MEMO_LIMIT {
        let idx: i64 = N
        memo[idx] = res
        memo_valid[idx] = 1
    }
    return res
}

function compute_S(N: i64) -> i64 {
    if N <= 1 {
        return 0
    }

    let K_target: i64 = icbrt(N - 1)

    let max_delta: i128 = (3 as i128) * (K_target as i128) * (K_target as i128) + (3 as i128) * (K_target as i128) + (1 as i128)
    let K0_max: i64 = icbrt((max_delta - (1 as i128)) as i64)

    delta_prefix = calloc((K0_max + 1), 8)
    delta_prefix_len = (K0_max + 1) as i32

    build_base_prefix(64)

    memo = calloc((MEMO_LIMIT + 1), 8)
    memo_valid = calloc((MEMO_LIMIT + 1), 1)

    let mut total_F_delta: i64 = 0
    let mut k: i64 = 1
    while k < K_target {
        let delta_k: i64 = 3 * k * k + 3 * k + 1
        let f_delta: i64 = F(delta_k)
        total_F_delta = total_F_delta + f_delta
        if k <= K0_max {
            delta_prefix[k] = total_F_delta
        }
        k = k + 1
    }

    let K3: i128 = (K_target as i128) * (K_target as i128) * (K_target as i128)
    let L: i64 = (N - (K3 as i64))
    let result: i64 = (N - 1) + total_F_delta + F(L)

    free(base_pref)
    free(delta_prefix)
    free(memo)
    free(memo_valid)
    return result
}

function main() -> i32 {
    printf("%lld\n", compute_S(100000000000000000))
    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 icbrt_i64(int64_t n);
int64_t d_greedy_i64(int64_t n);
void build_base_prefix_i32(int32_t limit);
int64_t F_i64(int64_t N);
int64_t compute_S_i64(int64_t N);
int32_t main(void);

static const int64_t MEMO_LIMIT = 5000000;

/* Module statics */
static int64_t* base_pref = NULL;
static int32_t base_pref_len = 0;
static int64_t* memo = NULL;
static int8_t* memo_valid = NULL;
static int64_t* delta_prefix = NULL;
static int32_t delta_prefix_len = 0;





int64_t icbrt_i64(int64_t n) {
    if (n <= 0) {
        return 0;
    }
    double d = ((double)(n));
    int64_t x = ((int64_t)(round(pow(d, (1.0 / 3.0)))));
    while (((((__int128)((x + 1))) * ((__int128)((x + 1)))) * ((__int128)((x + 1)))) <= ((__int128)(n))) {
        x = (x + 1);
    }
    while (((((__int128)(x)) * ((__int128)(x))) * ((__int128)(x))) > ((__int128)(n))) {
        x = (x - 1);
    }
    return x;
}

int64_t d_greedy_i64(int64_t n) {
    int64_t nn = n;
    int64_t steps = 0;
    while (nn > 0) {
        int64_t k = icbrt_i64(nn);
        nn = (nn - ((k * k) * k));
        steps = (steps + 1);
    }
    return steps;
}

void build_base_prefix_i32(int32_t limit) {
    int64_t* d = (int64_t*)(calloc(((int64_t)(limit)), 8));
    int32_t i = 0;
    while (i < limit) {
        d[i] = d_greedy_i64(((int64_t)(i)));
        i = (i + 1);
    }
    base_pref = calloc(((int64_t)((limit + 1))), 8);
    base_pref_len = (limit + 1);
    int64_t s = 0;
    i = 0;
    while (i < limit) {
        s = (s + d[i]);
        base_pref[((int64_t)((i + 1)))] = s;
        i = (i + 1);
    }
    free(d);
}

int64_t F_i64(int64_t N) {
    if (N <= 0) {
        return 0;
    }
    if (N < ((int64_t)(base_pref_len))) {
        return base_pref[N];
    }
    if (N <= MEMO_LIMIT) {
        int64_t idx = N;
        if (memo_valid[idx] != 0) {
            return memo[idx];
        }
    }
    int64_t K = icbrt_i64((N - 1));
    __int128 K3 = ((((__int128)(K)) * ((__int128)(K))) * ((__int128)(K)));
    int64_t L = (N - ((int64_t)(K3)));
    int64_t res = (((N - 1) + delta_prefix[(K - 1)]) + F_i64(L));
    if (N <= MEMO_LIMIT) {
        int64_t idx = N;
        memo[idx] = res;
        memo_valid[idx] = 1;
    }
    return res;
}

int64_t compute_S_i64(int64_t N) {
    if (N <= 1) {
        return 0;
    }
    int64_t K_target = icbrt_i64((N - 1));
    __int128 max_delta = ((((((__int128)(3)) * ((__int128)(K_target))) * ((__int128)(K_target))) + (((__int128)(3)) * ((__int128)(K_target)))) + ((__int128)(1)));
    int64_t K0_max = icbrt_i64(((int64_t)((max_delta - ((__int128)(1))))));
    delta_prefix = calloc((K0_max + 1), 8);
    delta_prefix_len = ((int32_t)((K0_max + 1)));
    build_base_prefix_i32(64);
    memo = calloc((MEMO_LIMIT + 1), 8);
    memo_valid = calloc((MEMO_LIMIT + 1), 1);
    int64_t total_F_delta = 0;
    int64_t k = 1;
    while (k < K_target) {
        int64_t delta_k = ((((3 * k) * k) + (3 * k)) + 1);
        int64_t f_delta = F_i64(delta_k);
        total_F_delta = (total_F_delta + f_delta);
        if (k <= K0_max) {
            delta_prefix[k] = total_F_delta;
        }
        k = (k + 1);
    }
    __int128 K3 = ((((__int128)(K_target)) * ((__int128)(K_target))) * ((__int128)(K_target)));
    int64_t L = (N - ((int64_t)(K3)));
    int64_t result = (((N - 1) + total_F_delta) + F_i64(L));
    free(base_pref);
    free(delta_prefix);
    free(memo);
    free(memo_valid);
    return result;
}

int32_t main(void) {
    printf("%lld\n", compute_S_i64(100000000000000000));
    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 private @pow(f64, f64) -> f64
  func.func private @round(f64) -> f64
  // Constant: MEMO_LIMIT
  llvm.mlir.global internal constant @MEMO_LIMIT(5000000 : i64) : i64
  // Module static: base_pref
  llvm.mlir.global internal @base_pref() {addr_space = 0 : i32} : !llvm.ptr {
    %0 = llvm.mlir.zero : !llvm.ptr
    llvm.return %0 : !llvm.ptr
  }
  // Module static: base_pref_len
  llvm.mlir.global internal @base_pref_len(0 : i32) : i32
  // Module static: memo
  llvm.mlir.global internal @memo() {addr_space = 0 : i32} : !llvm.ptr {
    %1 = llvm.mlir.zero : !llvm.ptr
    llvm.return %1 : !llvm.ptr
  }
  // Module static: memo_valid
  llvm.mlir.global internal @memo_valid() {addr_space = 0 : i32} : !llvm.ptr {
    %2 = llvm.mlir.zero : !llvm.ptr
    llvm.return %2 : !llvm.ptr
  }
  // Module static: delta_prefix
  llvm.mlir.global internal @delta_prefix() {addr_space = 0 : i32} : !llvm.ptr {
    %3 = llvm.mlir.zero : !llvm.ptr
    llvm.return %3 : !llvm.ptr
  }
  // Module static: delta_prefix_len
  llvm.mlir.global internal @delta_prefix_len(0 : i32) : i32
  func.func @icbrt(%arg0: i64) -> i64 {
    %4 = arith.constant 0 : i32
    %6 = arith.extsi %4 : i32 to i64
    %5 = arith.cmpi sle, %arg0, %6 : i64
    cf.cond_br %5, ^bb0, ^bb1
    ^bb0:
      %7 = arith.constant 0 : i32
      %8 = arith.extsi %7 : i32 to i64
      func.return %8 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %9 = arith.sitofp %arg0 : i64 to f64
    %12 = arith.constant 1.0 : f32
    %13 = arith.constant 3.0 : f32
    %14 = arith.divf %12, %13 : f32
    %15 = arith.extf %14 : f32 to f64
    %11 = func.call @pow(%9, %15) : (f64, f64) -> f64
    %10 = func.call @round(%11) : (f64) -> f64
    %16 = arith.fptosi %10 : f64 to i64
    %17 = llvm.mlir.constant(1 : i64) : i64
    %18 = llvm.alloca %17 x i64 : (i64) -> !llvm.ptr
    llvm.store %16, %18 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %19 = llvm.load %18 : !llvm.ptr -> i64
    %20 = arith.constant 1 : i32
    %22 = arith.extsi %20 : i32 to i64
    %21 = arith.addi %19, %22 : i64
    %23 = arith.extsi %21 : i64 to i128
    %24 = llvm.load %18 : !llvm.ptr -> i64
    %25 = arith.constant 1 : i32
    %27 = arith.extsi %25 : i32 to i64
    %26 = arith.addi %24, %27 : i64
    %28 = arith.extsi %26 : i64 to i128
    %30 = arith.trunci %23 : i128 to i64
    %31 = arith.trunci %28 : i128 to i64
    %29 = arith.muli %30, %31 : i64
    %32 = llvm.load %18 : !llvm.ptr -> i64
    %33 = arith.constant 1 : i32
    %35 = arith.extsi %33 : i32 to i64
    %34 = arith.addi %32, %35 : i64
    %36 = arith.extsi %34 : i64 to i128
    %38 = arith.trunci %36 : i128 to i64
    %37 = arith.muli %29, %38 : i64
    %39 = arith.extsi %arg0 : i64 to i128
    %41 = arith.trunci %39 : i128 to i64
    %40 = arith.cmpi sle, %37, %41 : i64
    cf.cond_br %40, ^bb4, ^bb5
    ^bb4:
      %42 = llvm.load %18 : !llvm.ptr -> i64
      %43 = arith.constant 1 : i32
      %45 = arith.extsi %43 : i32 to i64
      %44 = arith.addi %42, %45 : i64
      llvm.store %44, %18 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    cf.br ^bb6
    ^bb6:
    %46 = llvm.load %18 : !llvm.ptr -> i64
    %47 = arith.extsi %46 : i64 to i128
    %48 = llvm.load %18 : !llvm.ptr -> i64
    %49 = arith.extsi %48 : i64 to i128
    %51 = arith.trunci %47 : i128 to i64
    %52 = arith.trunci %49 : i128 to i64
    %50 = arith.muli %51, %52 : i64
    %53 = llvm.load %18 : !llvm.ptr -> i64
    %54 = arith.extsi %53 : i64 to i128
    %56 = arith.trunci %54 : i128 to i64
    %55 = arith.muli %50, %56 : i64
    %57 = arith.extsi %arg0 : i64 to i128
    %59 = arith.trunci %57 : i128 to i64
    %58 = arith.cmpi sgt, %55, %59 : i64
    cf.cond_br %58, ^bb7, ^bb8
    ^bb7:
      %60 = llvm.load %18 : !llvm.ptr -> i64
      %61 = arith.constant 1 : i32
      %63 = arith.extsi %61 : i32 to i64
      %62 = arith.subi %60, %63 : i64
      llvm.store %62, %18 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %64 = llvm.load %18 : !llvm.ptr -> i64
    func.return %64 : i64
  }
  func.func @d_greedy(%arg0: i64) -> i64 {
    %65 = llvm.mlir.constant(1 : i64) : i64
    %66 = llvm.alloca %65 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %66 : i64, !llvm.ptr
    %67 = arith.constant 0 : i32
    %68 = arith.extsi %67 : i32 to i64
    %69 = llvm.mlir.constant(1 : i64) : i64
    %70 = llvm.alloca %69 x i64 : (i64) -> !llvm.ptr
    llvm.store %68, %70 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %71 = llvm.load %66 : !llvm.ptr -> i64
    %72 = arith.constant 0 : i32
    %74 = arith.extsi %72 : i32 to i64
    %73 = arith.cmpi sgt, %71, %74 : i64
    cf.cond_br %73, ^bb10, ^bb11
    ^bb10:
      %76 = llvm.load %66 : !llvm.ptr -> i64
      %75 = func.call @icbrt(%76) : (i64) -> i64
      %77 = llvm.load %66 : !llvm.ptr -> i64
      %78 = arith.muli %75, %75 : i64
      %79 = arith.muli %78, %75 : i64
      %80 = arith.subi %77, %79 : i64
      llvm.store %80, %66 : i64, !llvm.ptr
      %81 = llvm.load %70 : !llvm.ptr -> i64
      %82 = arith.constant 1 : i32
      %84 = arith.extsi %82 : i32 to i64
      %83 = arith.addi %81, %84 : i64
      llvm.store %83, %70 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %85 = llvm.load %70 : !llvm.ptr -> i64
    func.return %85 : i64
  }
  func.func @build_base_prefix(%arg0: i32) -> () {
    %87 = arith.extsi %arg0 : i32 to i64
    %88 = arith.constant 8 : i32
    %89 = arith.extsi %88 : i32 to i64
    %86 = func.call @calloc(%87, %89) : (i64, i64) -> !llvm.ptr
    %90 = arith.constant 0 : i32
    %91 = llvm.mlir.constant(1 : i64) : i64
    %92 = llvm.alloca %91 x i32 : (i64) -> !llvm.ptr
    llvm.store %90, %92 : i32, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %93 = llvm.load %92 : !llvm.ptr -> i32
    %94 = arith.cmpi slt, %93, %arg0 : i32
    cf.cond_br %94, ^bb13, ^bb14
    ^bb13:
      %96 = llvm.load %92 : !llvm.ptr -> i32
      %97 = arith.extsi %96 : i32 to i64
      %95 = func.call @d_greedy(%97) : (i64) -> i64
      %98 = llvm.load %92 : !llvm.ptr -> i32
      %99 = arith.extsi %98 : i32 to i64
      %100 = llvm.getelementptr %86[%99] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %95, %100 : i64, !llvm.ptr
      %101 = llvm.load %92 : !llvm.ptr -> i32
      %102 = arith.constant 1 : i32
      %103 = arith.addi %101, %102 : i32
      llvm.store %103, %92 : i32, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %105 = arith.constant 1 : i32
    %106 = arith.addi %arg0, %105 : i32
    %107 = arith.extsi %106 : i32 to i64
    %108 = arith.constant 8 : i32
    %109 = arith.extsi %108 : i32 to i64
    %104 = func.call @calloc(%107, %109) : (i64, i64) -> !llvm.ptr
    %110 = llvm.mlir.addressof @base_pref : !llvm.ptr
    llvm.store %104, %110 : !llvm.ptr, !llvm.ptr
    %111 = arith.constant 1 : i32
    %112 = arith.addi %arg0, %111 : i32
    %113 = llvm.mlir.addressof @base_pref_len : !llvm.ptr
    llvm.store %112, %113 : i32, !llvm.ptr
    %114 = arith.constant 0 : i32
    %115 = arith.extsi %114 : i32 to i64
    %116 = llvm.mlir.constant(1 : i64) : i64
    %117 = llvm.alloca %116 x i64 : (i64) -> !llvm.ptr
    llvm.store %115, %117 : i64, !llvm.ptr
    %118 = arith.constant 0 : i32
    llvm.store %118, %92 : i32, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %119 = llvm.load %92 : !llvm.ptr -> i32
    %120 = arith.cmpi slt, %119, %arg0 : i32
    cf.cond_br %120, ^bb16, ^bb17
    ^bb16:
      %121 = llvm.load %117 : !llvm.ptr -> i64
      %123 = llvm.load %92 : !llvm.ptr -> i32
      %124 = arith.extsi %123 : i32 to i64
      %125 = llvm.getelementptr %86[%124] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %122 = llvm.load %125 : !llvm.ptr -> i64
      %126 = arith.addi %121, %122 : i64
      llvm.store %126, %117 : i64, !llvm.ptr
      %127 = llvm.load %117 : !llvm.ptr -> i64
      %128 = llvm.mlir.addressof @base_pref : !llvm.ptr
      %129 = llvm.load %128 : !llvm.ptr -> !llvm.ptr
      %130 = llvm.load %92 : !llvm.ptr -> i32
      %131 = arith.constant 1 : i32
      %132 = arith.addi %130, %131 : i32
      %133 = arith.extsi %132 : i32 to i64
      %134 = llvm.getelementptr %129[%133] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %127, %134 : i64, !llvm.ptr
      %135 = llvm.load %92 : !llvm.ptr -> i32
      %136 = arith.constant 1 : i32
      %137 = arith.addi %135, %136 : i32
      llvm.store %137, %92 : i32, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    func.call @free(%86) : (!llvm.ptr) -> ()
    func.return
  }
  func.func @F(%arg0: i64) -> i64 {
    %139 = arith.constant 0 : i32
    %141 = arith.extsi %139 : i32 to i64
    %140 = arith.cmpi sle, %arg0, %141 : i64
    cf.cond_br %140, ^bb18, ^bb19
    ^bb18:
      %142 = arith.constant 0 : i32
      %143 = arith.extsi %142 : i32 to i64
      func.return %143 : i64
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %144 = llvm.mlir.addressof @base_pref_len : !llvm.ptr
    %145 = llvm.load %144 : !llvm.ptr -> i32
    %146 = arith.extsi %145 : i32 to i64
    %147 = arith.cmpi slt, %arg0, %146 : i64
    cf.cond_br %147, ^bb21, ^bb22
    ^bb21:
      %149 = llvm.mlir.addressof @base_pref : !llvm.ptr
      %150 = llvm.load %149 : !llvm.ptr -> !llvm.ptr
      %151 = llvm.getelementptr %150[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %148 = llvm.load %151 : !llvm.ptr -> i64
      func.return %148 : i64
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %152 = llvm.mlir.addressof @MEMO_LIMIT : !llvm.ptr
    %153 = llvm.load %152 : !llvm.ptr -> i64
    %154 = arith.cmpi sle, %arg0, %153 : i64
    cf.cond_br %154, ^bb24, ^bb25
    ^bb24:
      %156 = llvm.mlir.addressof @memo_valid : !llvm.ptr
      %157 = llvm.load %156 : !llvm.ptr -> !llvm.ptr
      %158 = llvm.getelementptr %157[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %155 = llvm.load %158 : !llvm.ptr -> i8
      %159 = arith.constant 0 : i32
      %161 = arith.extsi %155 : i8 to i32
      %160 = arith.cmpi ne, %161, %159 : i32
      cf.cond_br %160, ^bb27, ^bb28
      ^bb27:
        %163 = llvm.mlir.addressof @memo : !llvm.ptr
        %164 = llvm.load %163 : !llvm.ptr -> !llvm.ptr
        %165 = llvm.getelementptr %164[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %162 = llvm.load %165 : !llvm.ptr -> i64
        func.return %162 : i64
      ^bb28:
        cf.br ^bb29
      ^bb29:
      cf.br ^bb26
    ^bb25:
      cf.br ^bb26
    ^bb26:
    %167 = arith.constant 1 : i32
    %169 = arith.extsi %167 : i32 to i64
    %168 = arith.subi %arg0, %169 : i64
    %166 = func.call @icbrt(%168) : (i64) -> i64
    %170 = arith.extsi %166 : i64 to i128
    %171 = arith.extsi %166 : i64 to i128
    %173 = arith.trunci %170 : i128 to i64
    %174 = arith.trunci %171 : i128 to i64
    %172 = arith.muli %173, %174 : i64
    %175 = arith.extsi %166 : i64 to i128
    %177 = arith.trunci %175 : i128 to i64
    %176 = arith.muli %172, %177 : i64
    %178 = arith.extsi %176 : i64 to i128
    %179 = arith.trunci %178 : i128 to i64
    %180 = arith.subi %arg0, %179 : i64
    %181 = arith.constant 1 : i32
    %183 = arith.extsi %181 : i32 to i64
    %182 = arith.subi %arg0, %183 : i64
    %185 = llvm.mlir.addressof @delta_prefix : !llvm.ptr
    %186 = llvm.load %185 : !llvm.ptr -> !llvm.ptr
    %187 = arith.constant 1 : i32
    %189 = arith.extsi %187 : i32 to i64
    %188 = arith.subi %166, %189 : i64
    %190 = llvm.getelementptr %186[%188] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %184 = llvm.load %190 : !llvm.ptr -> i64
    %191 = arith.addi %182, %184 : i64
    %192 = func.call @F(%180) : (i64) -> i64
    %193 = arith.addi %191, %192 : i64
    %194 = llvm.mlir.addressof @MEMO_LIMIT : !llvm.ptr
    %195 = llvm.load %194 : !llvm.ptr -> i64
    %196 = arith.cmpi sle, %arg0, %195 : i64
    cf.cond_br %196, ^bb30, ^bb31
    ^bb30:
      %197 = llvm.mlir.addressof @memo : !llvm.ptr
      %198 = llvm.load %197 : !llvm.ptr -> !llvm.ptr
      %199 = llvm.getelementptr %198[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %193, %199 : i64, !llvm.ptr
      %200 = arith.constant 1 : i32
      %201 = llvm.mlir.addressof @memo_valid : !llvm.ptr
      %202 = llvm.load %201 : !llvm.ptr -> !llvm.ptr
      %203 = arith.trunci %200 : i32 to i8
      %204 = llvm.getelementptr %202[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %203, %204 : i8, !llvm.ptr
      cf.br ^bb32
    ^bb31:
      cf.br ^bb32
    ^bb32:
    func.return %193 : i64
  }
  func.func @compute_S(%arg0: i64) -> i64 {
    %205 = arith.constant 1 : i32
    %207 = arith.extsi %205 : i32 to i64
    %206 = arith.cmpi sle, %arg0, %207 : i64
    cf.cond_br %206, ^bb33, ^bb34
    ^bb33:
      %208 = arith.constant 0 : i32
      %209 = arith.extsi %208 : i32 to i64
      func.return %209 : i64
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %211 = arith.constant 1 : i32
    %213 = arith.extsi %211 : i32 to i64
    %212 = arith.subi %arg0, %213 : i64
    %210 = func.call @icbrt(%212) : (i64) -> i64
    %214 = arith.constant 3 : i32
    %215 = arith.extsi %214 : i32 to i128
    %216 = arith.extsi %210 : i64 to i128
    %218 = arith.trunci %215 : i128 to i64
    %219 = arith.trunci %216 : i128 to i64
    %217 = arith.muli %218, %219 : i64
    %220 = arith.extsi %210 : i64 to i128
    %222 = arith.trunci %220 : i128 to i64
    %221 = arith.muli %217, %222 : i64
    %223 = arith.constant 3 : i32
    %224 = arith.extsi %223 : i32 to i128
    %225 = arith.extsi %210 : i64 to i128
    %227 = arith.trunci %224 : i128 to i64
    %228 = arith.trunci %225 : i128 to i64
    %226 = arith.muli %227, %228 : i64
    %229 = arith.addi %221, %226 : i64
    %230 = arith.constant 1 : i32
    %231 = arith.extsi %230 : i32 to i128
    %233 = arith.trunci %231 : i128 to i64
    %232 = arith.addi %229, %233 : i64
    %234 = arith.extsi %232 : i64 to i128
    %236 = arith.constant 1 : i32
    %237 = arith.extsi %236 : i32 to i128
    %239 = arith.trunci %234 : i128 to i64
    %240 = arith.trunci %237 : i128 to i64
    %238 = arith.subi %239, %240 : i64
    %235 = func.call @icbrt(%238) : (i64) -> i64
    %242 = arith.constant 1 : i32
    %244 = arith.extsi %242 : i32 to i64
    %243 = arith.addi %235, %244 : i64
    %245 = arith.constant 8 : i32
    %246 = arith.extsi %245 : i32 to i64
    %241 = func.call @calloc(%243, %246) : (i64, i64) -> !llvm.ptr
    %247 = llvm.mlir.addressof @delta_prefix : !llvm.ptr
    llvm.store %241, %247 : !llvm.ptr, !llvm.ptr
    %248 = arith.constant 1 : i32
    %250 = arith.extsi %248 : i32 to i64
    %249 = arith.addi %235, %250 : i64
    %251 = arith.trunci %249 : i64 to i32
    %252 = llvm.mlir.addressof @delta_prefix_len : !llvm.ptr
    llvm.store %251, %252 : i32, !llvm.ptr
    %254 = arith.constant 64 : i32
    func.call @build_base_prefix(%254) : (i32) -> ()
    %256 = llvm.mlir.addressof @MEMO_LIMIT : !llvm.ptr
    %257 = llvm.load %256 : !llvm.ptr -> i64
    %258 = arith.constant 1 : i32
    %260 = arith.extsi %258 : i32 to i64
    %259 = arith.addi %257, %260 : i64
    %261 = arith.constant 8 : i32
    %262 = arith.extsi %261 : i32 to i64
    %255 = func.call @calloc(%259, %262) : (i64, i64) -> !llvm.ptr
    %263 = llvm.mlir.addressof @memo : !llvm.ptr
    llvm.store %255, %263 : !llvm.ptr, !llvm.ptr
    %265 = llvm.mlir.addressof @MEMO_LIMIT : !llvm.ptr
    %266 = llvm.load %265 : !llvm.ptr -> i64
    %267 = arith.constant 1 : i32
    %269 = arith.extsi %267 : i32 to i64
    %268 = arith.addi %266, %269 : i64
    %270 = arith.constant 1 : i32
    %271 = arith.extsi %270 : i32 to i64
    %264 = func.call @calloc(%268, %271) : (i64, i64) -> !llvm.ptr
    %272 = llvm.mlir.addressof @memo_valid : !llvm.ptr
    llvm.store %264, %272 : !llvm.ptr, !llvm.ptr
    %273 = arith.constant 0 : i32
    %274 = arith.extsi %273 : i32 to i64
    %275 = llvm.mlir.constant(1 : i64) : i64
    %276 = llvm.alloca %275 x i64 : (i64) -> !llvm.ptr
    llvm.store %274, %276 : i64, !llvm.ptr
    %277 = arith.constant 1 : i32
    %278 = arith.extsi %277 : i32 to i64
    %279 = llvm.mlir.constant(1 : i64) : i64
    %280 = llvm.alloca %279 x i64 : (i64) -> !llvm.ptr
    llvm.store %278, %280 : i64, !llvm.ptr
    cf.br ^bb36
    ^bb36:
    %281 = llvm.load %280 : !llvm.ptr -> i64
    %282 = arith.cmpi slt, %281, %210 : i64
    cf.cond_br %282, ^bb37, ^bb38
    ^bb37:
      %283 = arith.constant 3 : i32
      %284 = llvm.load %280 : !llvm.ptr -> i64
      %286 = arith.extsi %283 : i32 to i64
      %285 = arith.muli %286, %284 : i64
      %287 = llvm.load %280 : !llvm.ptr -> i64
      %288 = arith.muli %285, %287 : i64
      %289 = arith.constant 3 : i32
      %290 = llvm.load %280 : !llvm.ptr -> i64
      %292 = arith.extsi %289 : i32 to i64
      %291 = arith.muli %292, %290 : i64
      %293 = arith.addi %288, %291 : i64
      %294 = arith.constant 1 : i32
      %296 = arith.extsi %294 : i32 to i64
      %295 = arith.addi %293, %296 : i64
      %297 = func.call @F(%295) : (i64) -> i64
      %298 = llvm.load %276 : !llvm.ptr -> i64
      %299 = arith.addi %298, %297 : i64
      llvm.store %299, %276 : i64, !llvm.ptr
      %300 = llvm.load %280 : !llvm.ptr -> i64
      %301 = arith.cmpi sle, %300, %235 : i64
      cf.cond_br %301, ^bb39, ^bb40
      ^bb39:
        %302 = llvm.load %276 : !llvm.ptr -> i64
        %303 = llvm.mlir.addressof @delta_prefix : !llvm.ptr
        %304 = llvm.load %303 : !llvm.ptr -> !llvm.ptr
        %305 = llvm.load %280 : !llvm.ptr -> i64
        %306 = llvm.getelementptr %304[%305] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %302, %306 : i64, !llvm.ptr
        cf.br ^bb41
      ^bb40:
        cf.br ^bb41
      ^bb41:
      %307 = llvm.load %280 : !llvm.ptr -> i64
      %308 = arith.constant 1 : i32
      %310 = arith.extsi %308 : i32 to i64
      %309 = arith.addi %307, %310 : i64
      llvm.store %309, %280 : i64, !llvm.ptr
      cf.br ^bb36
    ^bb38:
    %311 = arith.extsi %210 : i64 to i128
    %312 = arith.extsi %210 : i64 to i128
    %314 = arith.trunci %311 : i128 to i64
    %315 = arith.trunci %312 : i128 to i64
    %313 = arith.muli %314, %315 : i64
    %316 = arith.extsi %210 : i64 to i128
    %318 = arith.trunci %316 : i128 to i64
    %317 = arith.muli %313, %318 : i64
    %319 = arith.extsi %317 : i64 to i128
    %320 = arith.trunci %319 : i128 to i64
    %321 = arith.subi %arg0, %320 : i64
    %322 = arith.constant 1 : i32
    %324 = arith.extsi %322 : i32 to i64
    %323 = arith.subi %arg0, %324 : i64
    %325 = llvm.load %276 : !llvm.ptr -> i64
    %326 = arith.addi %323, %325 : i64
    %327 = func.call @F(%321) : (i64) -> i64
    %328 = arith.addi %326, %327 : i64
    %330 = llvm.mlir.addressof @base_pref : !llvm.ptr
    %331 = llvm.load %330 : !llvm.ptr -> !llvm.ptr
    func.call @free(%331) : (!llvm.ptr) -> ()
    %333 = llvm.mlir.addressof @delta_prefix : !llvm.ptr
    %334 = llvm.load %333 : !llvm.ptr -> !llvm.ptr
    func.call @free(%334) : (!llvm.ptr) -> ()
    %336 = llvm.mlir.addressof @memo : !llvm.ptr
    %337 = llvm.load %336 : !llvm.ptr -> !llvm.ptr
    func.call @free(%337) : (!llvm.ptr) -> ()
    %339 = llvm.mlir.addressof @memo_valid : !llvm.ptr
    %340 = llvm.load %339 : !llvm.ptr -> !llvm.ptr
    func.call @free(%340) : (!llvm.ptr) -> ()
    func.return %328 : i64
  }
  func.func @main() -> i32 {
    %341 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %343 = arith.constant 99999995705032704 : i32
    %344 = arith.extsi %343 : i32 to i64
    %342 = func.call @compute_S(%344) : (i64) -> i64
    %345 = llvm.call @printf(%341, %342) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %346 = arith.constant 0 : i32
    func.return %346 : i32
  }
}