← All problems
Problem 794
DFS over all feasible insertion histories up to n=18. All boundaries k/n are represented as integers scaled by D = lcm(1..18). The minimal sum at n=17 is recorded, then divided by D and rounded to 12 decimal places (ROUND_HALF_UP).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)?
Space complexity O(1)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 794 - Seventeen Points
#
# DFS over all feasible insertion histories up to n=18.
# All boundaries k/n are represented as integers scaled by D = lcm(1..18).
# The minimal sum at n=17 is recorded, then divided by D and rounded
# to 12 decimal places (ROUND_HALF_UP).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function memcpy(dst: ptr<void>, src: ptr<void>, n: i64) -> ptr<void>
}
const MAX_N: i32 = 18
const TARGET_N: i32 = 17
let mut g_scale: ptr<i64> = null
let mut g_best: i64 = 0
let mut g_best_found: i32 = 0
let mut g_exists_max: i32 = 0
function gcd_ll(a0: i64, b0: i64) -> i64 {
let mut a: i64 = a0
let mut b: i64 = b0
while b != 0 {
let t: i64 = a % b
a = b
b = t
}
return a
}
function lcm_ll(a: i64, b: i64) -> i64 {
return a / gcd_ll(a, b) * b
}
function rec(n: i32, order: ptr<i32>, L: ptr<i64>, U: ptr<i64>, sumL: i64) -> void {
if n == TARGET_N {
if g_best_found == 0 || sumL < g_best {
g_best = sumL
g_best_found = 1
}
}
if n == MAX_N {
g_exists_max = 1
return
}
let m: i32 = n + 1
let sc: i64 = g_scale[m]
let new_order: ptr<i32> = calloc(19, 4)
let newL: ptr<i64> = calloc(19, 8)
let newU: ptr<i64> = calloc(19, 8)
let mut pos: i32 = 0
while pos < m {
let mut i: i32 = 0
while i < pos {
new_order[i] = order[i]
i = i + 1
}
new_order[pos] = m
i = pos
while i < n {
new_order[i + 1] = order[i]
i = i + 1
}
memcpy(newL, L, 152)
memcpy(newU, U, 152)
let mut new_sumL: i64 = sumL
let mut feasible: i32 = 1
let mut k: i32 = 0
while k < m {
let pid: i32 = new_order[k]
let lb: i64 = (k as i64) * sc
let ub: i64 = ((k + 1) as i64) * sc
if lb > newL[pid] {
new_sumL = new_sumL + (lb - newL[pid])
newL[pid] = lb
}
if ub < newU[pid] {
newU[pid] = ub
}
if newL[pid] >= newU[pid] {
feasible = 0
break
}
k = k + 1
}
if feasible != 0 {
rec(m, new_order, newL, newU, new_sumL)
}
pos = pos + 1
}
free(new_order)
free(newL)
free(newU)
}
function solve() -> f64 {
let mut denom: i64 = 1
let mut i: i32 = 1
while i <= MAX_N {
denom = lcm_ll(denom, (i as i64))
i = i + 1
}
g_scale = calloc(19, 8)
let mut n: i32 = 1
while n <= MAX_N {
g_scale[n] = denom / (n as i64)
n = n + 1
}
let L: ptr<i64> = calloc(19, 8)
let U: ptr<i64> = calloc(19, 8)
let mut ii: i32 = 0
while ii <= MAX_N {
L[ii] = 0
U[ii] = denom
ii = ii + 1
}
let order: ptr<i32> = calloc(19, 4)
order[0] = 1
g_best_found = 0
g_best = 0
g_exists_max = 0
rec(1, order, L, U, 0)
# Round g_best / denom to 12 decimal places, ROUND_HALF_UP.
let num: i128 = (g_best as i128) * (1000000000000 as i128)
let mut q: i128 = num / (denom as i128)
let r: i128 = num % (denom as i128)
if (2 as i128) * r >= (denom as i128) {
q = q + (1 as i128)
}
free(g_scale)
free(L)
free(U)
free(order)
return ((q as i64) as f64) / 1000000000000.0
}
function main() -> i32 {
printf("%.12f\n", solve())
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 gcd_ll_i64_i64(int64_t a0, int64_t b0);
int64_t lcm_ll_i64_i64(int64_t a, int64_t b);
void rec_i32_ptr_i32_ptr_i64_ptr_i64_i64(int32_t n, int32_t* order, int64_t* L, int64_t* U, int64_t sumL);
double solve(void);
int32_t main(void);
static const int32_t MAX_N = 18;
static const int32_t TARGET_N = 17;
/* Module statics */
static int64_t* g_scale = NULL;
static int64_t g_best = 0;
static int32_t g_best_found = 0;
static int32_t g_exists_max = 0;
int64_t gcd_ll_i64_i64(int64_t a0, int64_t b0) {
int64_t a = a0;
int64_t b = b0;
while (b != 0) {
int64_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
return a;
}
int64_t lcm_ll_i64_i64(int64_t a, int64_t b) {
return (FLOW_CHECKED_DIV((a), (gcd_ll_i64_i64(a, b))) * b);
}
void rec_i32_ptr_i32_ptr_i64_ptr_i64_i64(int32_t n, int32_t* order, int64_t* L, int64_t* U, int64_t sumL) {
if (n == TARGET_N) {
if ((g_best_found == 0 || sumL < g_best)) {
g_best = sumL;
g_best_found = 1;
}
}
if (n == MAX_N) {
g_exists_max = 1;
return;
}
int32_t m = (n + 1);
int64_t sc = g_scale[m];
int32_t* new_order = (int32_t*)(calloc(19, 4));
int64_t* newL = (int64_t*)(calloc(19, 8));
int64_t* newU = (int64_t*)(calloc(19, 8));
int32_t pos = 0;
while (pos < m) {
int32_t i = 0;
while (i < pos) {
new_order[i] = order[i];
i = (i + 1);
}
new_order[pos] = m;
i = pos;
while (i < n) {
new_order[(i + 1)] = order[i];
i = (i + 1);
}
memcpy(newL, L, 152);
memcpy(newU, U, 152);
int64_t new_sumL = sumL;
int32_t feasible = 1;
int32_t k = 0;
while (k < m) {
int32_t pid = new_order[k];
int64_t lb = (((int64_t)(k)) * sc);
int64_t ub = (((int64_t)((k + 1))) * sc);
if (lb > newL[pid]) {
new_sumL = (new_sumL + (lb - newL[pid]));
newL[pid] = lb;
}
if (ub < newU[pid]) {
newU[pid] = ub;
}
if (newL[pid] >= newU[pid]) {
feasible = 0;
break;
}
k = (k + 1);
}
if (feasible != 0) {
rec_i32_ptr_i32_ptr_i64_ptr_i64_i64(m, new_order, newL, newU, new_sumL);
}
pos = (pos + 1);
}
free(new_order);
free(newL);
free(newU);
}
double solve(void) {
int64_t denom = 1;
int32_t i = 1;
while (i <= MAX_N) {
denom = lcm_ll_i64_i64(denom, ((int64_t)(i)));
i = (i + 1);
}
g_scale = calloc(19, 8);
int32_t n = 1;
while (n <= MAX_N) {
g_scale[n] = FLOW_CHECKED_DIV((denom), (((int64_t)(n))));
n = (n + 1);
}
int64_t* L = (int64_t*)(calloc(19, 8));
int64_t* U = (int64_t*)(calloc(19, 8));
int32_t ii = 0;
while (ii <= MAX_N) {
L[ii] = 0;
U[ii] = denom;
ii = (ii + 1);
}
int32_t* order = (int32_t*)(calloc(19, 4));
order[0] = 1;
g_best_found = 0;
g_best = 0;
g_exists_max = 0;
rec_i32_ptr_i32_ptr_i64_ptr_i64_i64(1, order, L, U, 0);
__int128 num = (((__int128)(g_best)) * ((__int128)(1000000000000)));
__int128 q = FLOW_CHECKED_DIV((num), (((__int128)(denom))));
__int128 r = FLOW_CHECKED_MOD((num), (((__int128)(denom))));
if ((((__int128)(2)) * r) >= ((__int128)(denom))) {
q = (q + ((__int128)(1)));
}
free(g_scale);
free(L);
free(U);
free(order);
return (((double)(((int64_t)(q)))) / 1000000000000.0);
}
int32_t main(void) {
printf("%.12f\n", solve());
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.12f\n\00") {addr_space = 0 : i32} : !llvm.array<7 x i8>
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func private @memcpy(!llvm.ptr, !llvm.ptr, i64) -> !llvm.ptr
// Constant: MAX_N
llvm.mlir.global internal constant @MAX_N(18 : i32) : i32
// Constant: TARGET_N
llvm.mlir.global internal constant @TARGET_N(17 : i32) : i32
// Module static: g_scale
llvm.mlir.global internal @g_scale() {addr_space = 0 : i32} : !llvm.ptr {
%0 = llvm.mlir.zero : !llvm.ptr
llvm.return %0 : !llvm.ptr
}
// Module static: g_best
llvm.mlir.global internal @g_best(0 : i64) : i64
// Module static: g_best_found
llvm.mlir.global internal @g_best_found(0 : i32) : i32
// Module static: g_exists_max
llvm.mlir.global internal @g_exists_max(0 : i32) : i32
func.func @gcd_ll(%arg0: i64, %arg1: i64) -> i64 {
%1 = llvm.mlir.constant(1 : i64) : i64
%2 = llvm.alloca %1 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %2 : i64, !llvm.ptr
%3 = llvm.mlir.constant(1 : i64) : i64
%4 = llvm.alloca %3 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %4 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%5 = llvm.load %4 : !llvm.ptr -> i64
%6 = arith.constant 0 : i32
%8 = arith.extsi %6 : i32 to i64
%7 = arith.cmpi ne, %5, %8 : i64
cf.cond_br %7, ^bb1, ^bb2
^bb1:
%9 = llvm.load %2 : !llvm.ptr -> i64
%10 = llvm.load %4 : !llvm.ptr -> i64
%11 = arith.remsi %9, %10 : i64
%12 = llvm.load %4 : !llvm.ptr -> i64
llvm.store %12, %2 : i64, !llvm.ptr
llvm.store %11, %4 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%13 = llvm.load %2 : !llvm.ptr -> i64
func.return %13 : i64
}
func.func @lcm_ll(%arg0: i64, %arg1: i64) -> i64 {
%14 = func.call @gcd_ll(%arg0, %arg1) : (i64, i64) -> i64
%15 = arith.divsi %arg0, %14 : i64
%16 = arith.muli %15, %arg1 : i64
func.return %16 : i64
}
func.func @rec(%arg0: i32, %arg1: !llvm.ptr, %arg2: !llvm.ptr, %arg3: !llvm.ptr, %arg4: i64) -> () {
%17 = llvm.mlir.addressof @TARGET_N : !llvm.ptr
%18 = llvm.load %17 : !llvm.ptr -> i32
%19 = arith.cmpi eq, %arg0, %18 : i32
cf.cond_br %19, ^bb3, ^bb4
^bb3:
%20 = llvm.mlir.addressof @g_best_found : !llvm.ptr
%21 = llvm.load %20 : !llvm.ptr -> i32
%22 = arith.constant 0 : i32
%23 = arith.cmpi eq, %21, %22 : i32
%24 = scf.if %23 -> (i1) {
%25 = arith.constant true
scf.yield %25 : i1
} else {
%26 = llvm.mlir.addressof @g_best : !llvm.ptr
%27 = llvm.load %26 : !llvm.ptr -> i64
%28 = arith.cmpi slt, %arg4, %27 : i64
scf.yield %28 : i1
}
cf.cond_br %24, ^bb6, ^bb7
^bb6:
%29 = llvm.mlir.addressof @g_best : !llvm.ptr
llvm.store %arg4, %29 : i64, !llvm.ptr
%30 = arith.constant 1 : i32
%31 = llvm.mlir.addressof @g_best_found : !llvm.ptr
llvm.store %30, %31 : i32, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%32 = llvm.mlir.addressof @MAX_N : !llvm.ptr
%33 = llvm.load %32 : !llvm.ptr -> i32
%34 = arith.cmpi eq, %arg0, %33 : i32
cf.cond_br %34, ^bb9, ^bb10
^bb9:
%35 = arith.constant 1 : i32
%36 = llvm.mlir.addressof @g_exists_max : !llvm.ptr
llvm.store %35, %36 : i32, !llvm.ptr
func.return
^bb10:
cf.br ^bb11
^bb11:
%37 = arith.constant 1 : i32
%38 = arith.addi %arg0, %37 : i32
%40 = llvm.mlir.addressof @g_scale : !llvm.ptr
%41 = llvm.load %40 : !llvm.ptr -> !llvm.ptr
%42 = arith.extsi %38 : i32 to i64
%43 = llvm.getelementptr %41[%42] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%39 = llvm.load %43 : !llvm.ptr -> i64
%45 = arith.constant 19 : i32
%46 = arith.constant 4 : i32
%47 = arith.extsi %45 : i32 to i64
%48 = arith.extsi %46 : i32 to i64
%44 = func.call @calloc(%47, %48) : (i64, i64) -> !llvm.ptr
%50 = arith.constant 19 : i32
%51 = arith.constant 8 : i32
%52 = arith.extsi %50 : i32 to i64
%53 = arith.extsi %51 : i32 to i64
%49 = func.call @calloc(%52, %53) : (i64, i64) -> !llvm.ptr
%55 = arith.constant 19 : i32
%56 = arith.constant 8 : i32
%57 = arith.extsi %55 : i32 to i64
%58 = arith.extsi %56 : i32 to i64
%54 = func.call @calloc(%57, %58) : (i64, i64) -> !llvm.ptr
%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 ^bb12
^bb12:
%62 = llvm.load %61 : !llvm.ptr -> i32
%63 = arith.cmpi slt, %62, %38 : i32
cf.cond_br %63, ^bb13, ^bb14
^bb13:
%64 = arith.constant 0 : i32
%65 = llvm.mlir.constant(1 : i64) : i64
%66 = llvm.alloca %65 x i32 : (i64) -> !llvm.ptr
llvm.store %64, %66 : i32, !llvm.ptr
cf.br ^bb15
^bb15:
%67 = llvm.load %66 : !llvm.ptr -> i32
%68 = llvm.load %61 : !llvm.ptr -> i32
%69 = arith.cmpi slt, %67, %68 : i32
cf.cond_br %69, ^bb16, ^bb17
^bb16:
%71 = llvm.load %66 : !llvm.ptr -> i32
%72 = arith.extsi %71 : i32 to i64
%73 = llvm.getelementptr %arg1[%72] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%70 = llvm.load %73 : !llvm.ptr -> i32
%74 = llvm.load %66 : !llvm.ptr -> i32
%75 = arith.extsi %74 : i32 to i64
%76 = llvm.getelementptr %44[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %70, %76 : i32, !llvm.ptr
%77 = llvm.load %66 : !llvm.ptr -> i32
%78 = arith.constant 1 : i32
%79 = arith.addi %77, %78 : i32
llvm.store %79, %66 : i32, !llvm.ptr
cf.br ^bb15
^bb17:
%80 = llvm.load %61 : !llvm.ptr -> i32
%81 = arith.extsi %80 : i32 to i64
%82 = llvm.getelementptr %44[%81] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %38, %82 : i32, !llvm.ptr
%83 = llvm.load %61 : !llvm.ptr -> i32
llvm.store %83, %66 : i32, !llvm.ptr
cf.br ^bb18
^bb18:
%84 = llvm.load %66 : !llvm.ptr -> i32
%85 = arith.cmpi slt, %84, %arg0 : i32
cf.cond_br %85, ^bb19, ^bb20
^bb19:
%87 = llvm.load %66 : !llvm.ptr -> i32
%88 = arith.extsi %87 : i32 to i64
%89 = llvm.getelementptr %arg1[%88] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%86 = llvm.load %89 : !llvm.ptr -> i32
%90 = llvm.load %66 : !llvm.ptr -> i32
%91 = arith.constant 1 : i32
%92 = arith.addi %90, %91 : i32
%93 = arith.extsi %92 : i32 to i64
%94 = llvm.getelementptr %44[%93] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %86, %94 : i32, !llvm.ptr
%95 = llvm.load %66 : !llvm.ptr -> i32
%96 = arith.constant 1 : i32
%97 = arith.addi %95, %96 : i32
llvm.store %97, %66 : i32, !llvm.ptr
cf.br ^bb18
^bb20:
%99 = arith.constant 152 : i32
%100 = arith.extsi %99 : i32 to i64
%98 = func.call @memcpy(%49, %arg2, %100) : (!llvm.ptr, !llvm.ptr, i64) -> !llvm.ptr
%102 = arith.constant 152 : i32
%103 = arith.extsi %102 : i32 to i64
%101 = func.call @memcpy(%54, %arg3, %103) : (!llvm.ptr, !llvm.ptr, i64) -> !llvm.ptr
%104 = llvm.mlir.constant(1 : i64) : i64
%105 = llvm.alloca %104 x i64 : (i64) -> !llvm.ptr
llvm.store %arg4, %105 : i64, !llvm.ptr
%106 = arith.constant 1 : i32
%107 = llvm.mlir.constant(1 : i64) : i64
%108 = llvm.alloca %107 x i32 : (i64) -> !llvm.ptr
llvm.store %106, %108 : i32, !llvm.ptr
%109 = arith.constant 0 : i32
%110 = llvm.mlir.constant(1 : i64) : i64
%111 = llvm.alloca %110 x i32 : (i64) -> !llvm.ptr
llvm.store %109, %111 : i32, !llvm.ptr
cf.br ^bb21
^bb21:
%112 = llvm.load %111 : !llvm.ptr -> i32
%113 = arith.cmpi slt, %112, %38 : i32
cf.cond_br %113, ^bb22, ^bb23
^bb22:
%115 = llvm.load %111 : !llvm.ptr -> i32
%116 = arith.extsi %115 : i32 to i64
%117 = llvm.getelementptr %44[%116] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%114 = llvm.load %117 : !llvm.ptr -> i32
%118 = llvm.load %111 : !llvm.ptr -> i32
%119 = arith.extsi %118 : i32 to i64
%120 = arith.muli %119, %39 : i64
%121 = llvm.load %111 : !llvm.ptr -> i32
%122 = arith.constant 1 : i32
%123 = arith.addi %121, %122 : i32
%124 = arith.extsi %123 : i32 to i64
%125 = arith.muli %124, %39 : i64
%127 = arith.extsi %114 : i32 to i64
%128 = llvm.getelementptr %49[%127] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%126 = llvm.load %128 : !llvm.ptr -> i64
%129 = arith.cmpi sgt, %120, %126 : i64
cf.cond_br %129, ^bb24, ^bb25
^bb24:
%130 = llvm.load %105 : !llvm.ptr -> i64
%132 = arith.extsi %114 : i32 to i64
%133 = llvm.getelementptr %49[%132] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%131 = llvm.load %133 : !llvm.ptr -> i64
%134 = arith.subi %120, %131 : i64
%135 = arith.addi %130, %134 : i64
llvm.store %135, %105 : i64, !llvm.ptr
%136 = arith.extsi %114 : i32 to i64
%137 = llvm.getelementptr %49[%136] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %120, %137 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%139 = arith.extsi %114 : i32 to i64
%140 = llvm.getelementptr %54[%139] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%138 = llvm.load %140 : !llvm.ptr -> i64
%141 = arith.cmpi slt, %125, %138 : i64
cf.cond_br %141, ^bb27, ^bb28
^bb27:
%142 = arith.extsi %114 : i32 to i64
%143 = llvm.getelementptr %54[%142] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %125, %143 : i64, !llvm.ptr
cf.br ^bb29
^bb28:
cf.br ^bb29
^bb29:
%145 = arith.extsi %114 : i32 to i64
%146 = llvm.getelementptr %49[%145] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%144 = llvm.load %146 : !llvm.ptr -> i64
%148 = arith.extsi %114 : i32 to i64
%149 = llvm.getelementptr %54[%148] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%147 = llvm.load %149 : !llvm.ptr -> i64
%150 = arith.cmpi sge, %144, %147 : i64
cf.cond_br %150, ^bb30, ^bb31
^bb30:
%151 = arith.constant 0 : i32
llvm.store %151, %108 : i32, !llvm.ptr
cf.br ^bb23
^bb31:
cf.br ^bb32
^bb32:
%152 = llvm.load %111 : !llvm.ptr -> i32
%153 = arith.constant 1 : i32
%154 = arith.addi %152, %153 : i32
llvm.store %154, %111 : i32, !llvm.ptr
cf.br ^bb21
^bb23:
%155 = llvm.load %108 : !llvm.ptr -> i32
%156 = arith.constant 0 : i32
%157 = arith.cmpi ne, %155, %156 : i32
cf.cond_br %157, ^bb33, ^bb34
^bb33:
%159 = llvm.load %105 : !llvm.ptr -> i64
func.call @rec(%38, %44, %49, %54, %159) : (i32, !llvm.ptr, !llvm.ptr, !llvm.ptr, i64) -> ()
cf.br ^bb35
^bb34:
cf.br ^bb35
^bb35:
%160 = llvm.load %61 : !llvm.ptr -> i32
%161 = arith.constant 1 : i32
%162 = arith.addi %160, %161 : i32
llvm.store %162, %61 : i32, !llvm.ptr
cf.br ^bb12
^bb14:
func.call @free(%44) : (!llvm.ptr) -> ()
func.call @free(%49) : (!llvm.ptr) -> ()
func.call @free(%54) : (!llvm.ptr) -> ()
func.return
}
func.func @solve() -> f64 {
%166 = arith.constant 1 : i32
%167 = arith.extsi %166 : i32 to i64
%168 = llvm.mlir.constant(1 : i64) : i64
%169 = llvm.alloca %168 x i64 : (i64) -> !llvm.ptr
llvm.store %167, %169 : i64, !llvm.ptr
%170 = arith.constant 1 : i32
%171 = llvm.mlir.constant(1 : i64) : i64
%172 = llvm.alloca %171 x i32 : (i64) -> !llvm.ptr
llvm.store %170, %172 : i32, !llvm.ptr
cf.br ^bb36
^bb36:
%173 = llvm.load %172 : !llvm.ptr -> i32
%174 = llvm.mlir.addressof @MAX_N : !llvm.ptr
%175 = llvm.load %174 : !llvm.ptr -> i32
%176 = arith.cmpi sle, %173, %175 : i32
cf.cond_br %176, ^bb37, ^bb38
^bb37:
%178 = llvm.load %169 : !llvm.ptr -> i64
%179 = llvm.load %172 : !llvm.ptr -> i32
%180 = arith.extsi %179 : i32 to i64
%177 = func.call @lcm_ll(%178, %180) : (i64, i64) -> i64
llvm.store %177, %169 : i64, !llvm.ptr
%181 = llvm.load %172 : !llvm.ptr -> i32
%182 = arith.constant 1 : i32
%183 = arith.addi %181, %182 : i32
llvm.store %183, %172 : i32, !llvm.ptr
cf.br ^bb36
^bb38:
%185 = arith.constant 19 : i32
%186 = arith.constant 8 : i32
%187 = arith.extsi %185 : i32 to i64
%188 = arith.extsi %186 : i32 to i64
%184 = func.call @calloc(%187, %188) : (i64, i64) -> !llvm.ptr
%189 = llvm.mlir.addressof @g_scale : !llvm.ptr
llvm.store %184, %189 : !llvm.ptr, !llvm.ptr
%190 = arith.constant 1 : i32
%191 = llvm.mlir.constant(1 : i64) : i64
%192 = llvm.alloca %191 x i32 : (i64) -> !llvm.ptr
llvm.store %190, %192 : i32, !llvm.ptr
cf.br ^bb39
^bb39:
%193 = llvm.load %192 : !llvm.ptr -> i32
%194 = llvm.mlir.addressof @MAX_N : !llvm.ptr
%195 = llvm.load %194 : !llvm.ptr -> i32
%196 = arith.cmpi sle, %193, %195 : i32
cf.cond_br %196, ^bb40, ^bb41
^bb40:
%197 = llvm.load %169 : !llvm.ptr -> i64
%198 = llvm.load %192 : !llvm.ptr -> i32
%199 = arith.extsi %198 : i32 to i64
%200 = arith.divsi %197, %199 : i64
%201 = llvm.mlir.addressof @g_scale : !llvm.ptr
%202 = llvm.load %201 : !llvm.ptr -> !llvm.ptr
%203 = llvm.load %192 : !llvm.ptr -> i32
%204 = arith.extsi %203 : i32 to i64
%205 = llvm.getelementptr %202[%204] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %200, %205 : i64, !llvm.ptr
%206 = llvm.load %192 : !llvm.ptr -> i32
%207 = arith.constant 1 : i32
%208 = arith.addi %206, %207 : i32
llvm.store %208, %192 : i32, !llvm.ptr
cf.br ^bb39
^bb41:
%210 = arith.constant 19 : i32
%211 = arith.constant 8 : i32
%212 = arith.extsi %210 : i32 to i64
%213 = arith.extsi %211 : i32 to i64
%209 = func.call @calloc(%212, %213) : (i64, i64) -> !llvm.ptr
%215 = arith.constant 19 : i32
%216 = arith.constant 8 : i32
%217 = arith.extsi %215 : i32 to i64
%218 = arith.extsi %216 : i32 to i64
%214 = func.call @calloc(%217, %218) : (i64, i64) -> !llvm.ptr
%219 = arith.constant 0 : i32
%220 = llvm.mlir.constant(1 : i64) : i64
%221 = llvm.alloca %220 x i32 : (i64) -> !llvm.ptr
llvm.store %219, %221 : i32, !llvm.ptr
cf.br ^bb42
^bb42:
%222 = llvm.load %221 : !llvm.ptr -> i32
%223 = llvm.mlir.addressof @MAX_N : !llvm.ptr
%224 = llvm.load %223 : !llvm.ptr -> i32
%225 = arith.cmpi sle, %222, %224 : i32
cf.cond_br %225, ^bb43, ^bb44
^bb43:
%226 = arith.constant 0 : i32
%227 = llvm.load %221 : !llvm.ptr -> i32
%228 = arith.extsi %226 : i32 to i64
%229 = arith.extsi %227 : i32 to i64
%230 = llvm.getelementptr %209[%229] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %228, %230 : i64, !llvm.ptr
%231 = llvm.load %169 : !llvm.ptr -> i64
%232 = llvm.load %221 : !llvm.ptr -> i32
%233 = arith.extsi %232 : i32 to i64
%234 = llvm.getelementptr %214[%233] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %231, %234 : i64, !llvm.ptr
%235 = llvm.load %221 : !llvm.ptr -> i32
%236 = arith.constant 1 : i32
%237 = arith.addi %235, %236 : i32
llvm.store %237, %221 : i32, !llvm.ptr
cf.br ^bb42
^bb44:
%239 = arith.constant 19 : i32
%240 = arith.constant 4 : i32
%241 = arith.extsi %239 : i32 to i64
%242 = arith.extsi %240 : i32 to i64
%238 = func.call @calloc(%241, %242) : (i64, i64) -> !llvm.ptr
%243 = arith.constant 1 : i32
%244 = arith.constant 0 : i32
%245 = arith.extsi %244 : i32 to i64
%246 = llvm.getelementptr %238[%245] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %243, %246 : i32, !llvm.ptr
%247 = arith.constant 0 : i32
%248 = llvm.mlir.addressof @g_best_found : !llvm.ptr
llvm.store %247, %248 : i32, !llvm.ptr
%249 = arith.constant 0 : i32
%250 = arith.extsi %249 : i32 to i64
%251 = llvm.mlir.addressof @g_best : !llvm.ptr
llvm.store %250, %251 : i64, !llvm.ptr
%252 = arith.constant 0 : i32
%253 = llvm.mlir.addressof @g_exists_max : !llvm.ptr
llvm.store %252, %253 : i32, !llvm.ptr
%255 = arith.constant 1 : i32
%256 = arith.constant 0 : i32
%257 = arith.extsi %256 : i32 to i64
func.call @rec(%255, %238, %209, %214, %257) : (i32, !llvm.ptr, !llvm.ptr, !llvm.ptr, i64) -> ()
%258 = llvm.mlir.addressof @g_best : !llvm.ptr
%259 = llvm.load %258 : !llvm.ptr -> i64
%260 = arith.extsi %259 : i64 to i128
%261 = arith.constant 995705032704 : i32
%262 = arith.extsi %261 : i32 to i128
%264 = arith.trunci %260 : i128 to i64
%265 = arith.trunci %262 : i128 to i64
%263 = arith.muli %264, %265 : i64
%266 = arith.extsi %263 : i64 to i128
%267 = llvm.load %169 : !llvm.ptr -> i64
%268 = arith.extsi %267 : i64 to i128
%270 = arith.trunci %266 : i128 to i64
%271 = arith.trunci %268 : i128 to i64
%269 = arith.divsi %270, %271 : i64
%272 = arith.extsi %269 : i64 to i128
%273 = llvm.mlir.constant(1 : i64) : i64
%274 = llvm.alloca %273 x i128 : (i64) -> !llvm.ptr
llvm.store %272, %274 : i128, !llvm.ptr
%275 = llvm.load %169 : !llvm.ptr -> i64
%276 = arith.extsi %275 : i64 to i128
%278 = arith.trunci %266 : i128 to i64
%279 = arith.trunci %276 : i128 to i64
%277 = arith.remsi %278, %279 : i64
%280 = arith.extsi %277 : i64 to i128
%281 = arith.constant 2 : i32
%282 = arith.extsi %281 : i32 to i128
%284 = arith.trunci %282 : i128 to i64
%285 = arith.trunci %280 : i128 to i64
%283 = arith.muli %284, %285 : i64
%286 = llvm.load %169 : !llvm.ptr -> i64
%287 = arith.extsi %286 : i64 to i128
%289 = arith.trunci %287 : i128 to i64
%288 = arith.cmpi sge, %283, %289 : i64
cf.cond_br %288, ^bb45, ^bb46
^bb45:
%290 = llvm.load %274 : !llvm.ptr -> i128
%291 = arith.constant 1 : i32
%292 = arith.extsi %291 : i32 to i128
%294 = arith.trunci %290 : i128 to i64
%295 = arith.trunci %292 : i128 to i64
%293 = arith.addi %294, %295 : i64
%296 = arith.extsi %293 : i64 to i128
llvm.store %296, %274 : i128, !llvm.ptr
cf.br ^bb47
^bb46:
cf.br ^bb47
^bb47:
%298 = llvm.mlir.addressof @g_scale : !llvm.ptr
%299 = llvm.load %298 : !llvm.ptr -> !llvm.ptr
func.call @free(%299) : (!llvm.ptr) -> ()
func.call @free(%209) : (!llvm.ptr) -> ()
func.call @free(%214) : (!llvm.ptr) -> ()
func.call @free(%238) : (!llvm.ptr) -> ()
%303 = llvm.load %274 : !llvm.ptr -> i128
%304 = arith.trunci %303 : i128 to i64
%305 = arith.sitofp %304 : i64 to f64
%306 = arith.constant 1000000000000.0 : f32
%308 = arith.extf %306 : f32 to f64
%307 = arith.divf %305, %308 : f64
func.return %307 : f64
}
func.func @main() -> i32 {
%309 = llvm.mlir.addressof @str_0 : !llvm.ptr
%310 = func.call @solve() : () -> f64
%311 = llvm.call @printf(%309, %310) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%312 = arith.constant 0 : i32
func.return %312 : i32
}
}