Problem 872

Recursive Tree — path sum from x to root n via largest power-of-two jumps.

Answer2903144925319290239
Output2903144925319290239
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n^2)
Space complexityO(1)O(n^2)
ApproachFlow solutionBottom-up DP
VerdictOptimal

Flow source

# Project Euler 872
# Recursive Tree — path sum from x to root n via largest power-of-two jumps.

function f(n: i64, x: i64) -> i64 {
    let mut total: i64 = x
    let mut curr: i64 = x
    while curr != n {
        let mut diff: i64 = n - curr
        let mut k: i64 = 0
        let mut p: i64 = 1
        while (p << 1) <= diff {
            p = p << 1
            k = k + 1
        }
        curr = curr + p
        total = total + curr
    }
    return total
}

function ipow(base: i64, exp: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base
    let mut e: i64 = exp
    while e > 0 {
        if (e & 1) != 0 {
            r = r * b
        }
        b = b * b
        e = e >> 1
    }
    return r
}

function main() -> i32 {
    printf("%lld\n", f(ipow(10, 17), ipow(9, 17)))
    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 f_i64_i64(int64_t n, int64_t x);
int64_t ipow_i64_i64(int64_t base, int64_t exp);
int32_t main(void);

int64_t f_i64_i64(int64_t n, int64_t x) {
    int64_t total = x;
    int64_t curr = x;
    while (curr != n) {
        int64_t diff = (n - curr);
        int64_t k = 0;
        int64_t p = 1;
        while (FLOW_CHECKED_SHL((p), (1)) <= diff) {
            p = FLOW_CHECKED_SHL((p), (1));
            k = (k + 1);
        }
        curr = (curr + p);
        total = (total + curr);
    }
    return total;
}

int64_t ipow_i64_i64(int64_t base, int64_t exp) {
    int64_t r = 1;
    int64_t b = base;
    int64_t e = exp;
    while (e > 0) {
        if ((e & 1) != 0) {
            r = (r * b);
        }
        b = (b * b);
        e = FLOW_CHECKED_SHR((e), (1));
    }
    return r;
}

int32_t main(void) {
    printf("%lld\n", f_i64_i64(ipow_i64_i64(10, 17), ipow_i64_i64(9, 17)));
    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 @f(%arg0: i64, %arg1: i64) -> i64 {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %1 : i64, !llvm.ptr
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %3 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %4 = llvm.load %3 : !llvm.ptr -> i64
    %5 = arith.cmpi ne, %4, %arg0 : i64
    cf.cond_br %5, ^bb1, ^bb2
    ^bb1:
      %6 = llvm.load %3 : !llvm.ptr -> i64
      %7 = arith.subi %arg0, %6 : i64
      %8 = llvm.mlir.constant(1 : i64) : i64
      %9 = llvm.alloca %8 x i64 : (i64) -> !llvm.ptr
      llvm.store %7, %9 : i64, !llvm.ptr
      %10 = arith.constant 0 : i32
      %11 = arith.extsi %10 : i32 to i64
      %12 = llvm.mlir.constant(1 : i64) : i64
      %13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
      llvm.store %11, %13 : i64, !llvm.ptr
      %14 = arith.constant 1 : i32
      %15 = arith.extsi %14 : i32 to i64
      %16 = llvm.mlir.constant(1 : i64) : i64
      %17 = llvm.alloca %16 x i64 : (i64) -> !llvm.ptr
      llvm.store %15, %17 : i64, !llvm.ptr
      cf.br ^bb3
      ^bb3:
      %18 = llvm.load %17 : !llvm.ptr -> i64
      %19 = arith.constant 1 : i32
      %21 = arith.extsi %19 : i32 to i64
      %20 = arith.shli %18, %21 : i64
      %22 = llvm.load %9 : !llvm.ptr -> i64
      %23 = arith.cmpi sle, %20, %22 : i64
      cf.cond_br %23, ^bb4, ^bb5
      ^bb4:
        %24 = llvm.load %17 : !llvm.ptr -> i64
        %25 = arith.constant 1 : i32
        %27 = arith.extsi %25 : i32 to i64
        %26 = arith.shli %24, %27 : i64
        llvm.store %26, %17 : i64, !llvm.ptr
        %28 = llvm.load %13 : !llvm.ptr -> i64
        %29 = arith.constant 1 : i32
        %31 = arith.extsi %29 : i32 to i64
        %30 = arith.addi %28, %31 : i64
        llvm.store %30, %13 : i64, !llvm.ptr
        cf.br ^bb3
      ^bb5:
      %32 = llvm.load %3 : !llvm.ptr -> i64
      %33 = llvm.load %17 : !llvm.ptr -> i64
      %34 = arith.addi %32, %33 : i64
      llvm.store %34, %3 : i64, !llvm.ptr
      %35 = llvm.load %1 : !llvm.ptr -> i64
      %36 = llvm.load %3 : !llvm.ptr -> i64
      %37 = arith.addi %35, %36 : i64
      llvm.store %37, %1 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %38 = llvm.load %1 : !llvm.ptr -> i64
    func.return %38 : i64
  }
  func.func @ipow(%arg0: i64, %arg1: i64) -> i64 {
    %39 = arith.constant 1 : i32
    %40 = arith.extsi %39 : i32 to i64
    %41 = llvm.mlir.constant(1 : i64) : i64
    %42 = llvm.alloca %41 x i64 : (i64) -> !llvm.ptr
    llvm.store %40, %42 : i64, !llvm.ptr
    %43 = llvm.mlir.constant(1 : i64) : i64
    %44 = llvm.alloca %43 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %44 : i64, !llvm.ptr
    %45 = llvm.mlir.constant(1 : i64) : i64
    %46 = llvm.alloca %45 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %46 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %47 = llvm.load %46 : !llvm.ptr -> i64
    %48 = arith.constant 0 : i32
    %50 = arith.extsi %48 : i32 to i64
    %49 = arith.cmpi sgt, %47, %50 : i64
    cf.cond_br %49, ^bb7, ^bb8
    ^bb7:
      %51 = llvm.load %46 : !llvm.ptr -> i64
      %52 = arith.constant 1 : i32
      %54 = arith.extsi %52 : i32 to i64
      %53 = arith.andi %51, %54 : i64
      %55 = arith.constant 0 : i32
      %57 = arith.extsi %55 : i32 to i64
      %56 = arith.cmpi ne, %53, %57 : i64
      cf.cond_br %56, ^bb9, ^bb10
      ^bb9:
        %58 = llvm.load %42 : !llvm.ptr -> i64
        %59 = llvm.load %44 : !llvm.ptr -> i64
        %60 = arith.muli %58, %59 : i64
        llvm.store %60, %42 : i64, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %61 = llvm.load %44 : !llvm.ptr -> i64
      %62 = llvm.load %44 : !llvm.ptr -> i64
      %63 = arith.muli %61, %62 : i64
      llvm.store %63, %44 : i64, !llvm.ptr
      %64 = llvm.load %46 : !llvm.ptr -> i64
      %65 = arith.constant 1 : i32
      %67 = arith.extsi %65 : i32 to i64
      %66 = arith.shrsi %64, %67 : i64
      llvm.store %66, %46 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %68 = llvm.load %42 : !llvm.ptr -> i64
    func.return %68 : i64
  }
  func.func @main() -> i32 {
    %69 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %72 = arith.constant 10 : i32
    %73 = arith.constant 17 : i32
    %74 = arith.extsi %72 : i32 to i64
    %75 = arith.extsi %73 : i32 to i64
    %71 = func.call @ipow(%74, %75) : (i64, i64) -> i64
    %77 = arith.constant 9 : i32
    %78 = arith.constant 17 : i32
    %79 = arith.extsi %77 : i32 to i64
    %80 = arith.extsi %78 : i32 to i64
    %76 = func.call @ipow(%79, %80) : (i64, i64) -> i64
    %70 = func.call @f(%71, %76) : (i64, i64) -> i64
    %81 = llvm.call @printf(%69, %70) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %82 = arith.constant 0 : i32
    func.return %82 : i32
  }
}