← All problems
Problem 460
An Ant on the Move — minimal excess climb DP + cruise at height h=d/2.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)?
Space complexity O(n)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 460
# An Ant on the Move — minimal excess climb DP + cruise at height h=d/2.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function log(x: f64) -> f64
function sqrt(x: f64) -> f64
function hypot(x: f64, y: f64) -> f64
function floor(x: f64) -> f64
}
function min_step_excess(y0: i64, y1: i64, h: i64, logs: ptr<f64>) -> f64 {
let dy: f64 = (y1 - y0) as f64
let v: f64 = dy / (logs[y1] - logs[y0])
let hh: f64 = h as f64
let denom: f64 = sqrt(hh * hh - v * v)
let dx_star: f64 = dy * v / denom
let k0: i64 = floor(dx_star) as i64
let mut best: f64 = 1.0e300
let mut t: i64 = 0
while t < 2 {
let dx: i64 = k0 + t
if dx >= 0 {
let val: f64 = hypot(dx as f64, dy) / v - (dx as f64) / hh
if val < best { best = val }
}
t = t + 1
}
return best
}
function best_climb_excess(h: i64) -> f64 {
let logs: ptr<f64> = calloc(h + 1, 8)
let dp: ptr<f64> = calloc(h + 1, 8)
if logs == null || dp == null { return 0.0 }
let mut y: i64 = 1
while y <= h {
logs[y] = log(y as f64)
dp[y] = 1.0e300
y = y + 1
}
dp[1] = 0.0
y = 2
while y <= h {
let M: i64 = (64 * h) / y + 2
let mut y0_min: i64 = y - M
if y0_min < 1 { y0_min = 1 }
let mut best: f64 = 1.0e300
let mut y0: i64 = y0_min
while y0 < y {
let cand: f64 = dp[y0] + min_step_excess(y0, y, h, logs)
if cand < best { best = cand }
y0 = y0 + 1
}
dp[y] = best
y = y + 1
}
let ans: f64 = dp[h]
free(dp)
free(logs)
return ans
}
function main() -> i32 {
let d: i64 = 10000
let h: i64 = d / 2
let E: f64 = best_climb_excess(h)
let ans: f64 = 2.0 * E + (d as f64) / (h as f64)
printf("%.9f\n", ans)
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; }
double hypot(double x, double y);
double min_step_excess_i64_i64_i64_ptr_f64(int64_t y0, int64_t y1, int64_t h, double* logs);
double best_climb_excess_i64(int64_t h);
int32_t main(void);
double min_step_excess_i64_i64_i64_ptr_f64(int64_t y0, int64_t y1, int64_t h, double* logs) {
double dy = ((double)((y1 - y0)));
double v = (dy / (logs[y1] - logs[y0]));
double hh = ((double)(h));
double denom = sqrt(((hh * hh) - (v * v)));
double dx_star = ((dy * v) / denom);
int64_t k0 = ((int64_t)(floor(dx_star)));
double best = 1.0e300;
int64_t t = 0;
while (t < 2) {
int64_t dx = (k0 + t);
if (dx >= 0) {
double val = ((hypot(((double)(dx)), dy) / v) - (((double)(dx)) / hh));
if (val < best) {
best = val;
}
}
t = (t + 1);
}
return best;
}
double best_climb_excess_i64(int64_t h) {
double* logs = (double*)(calloc((h + 1), 8));
double* dp = (double*)(calloc((h + 1), 8));
if ((logs == NULL || dp == NULL)) {
return 0.0;
}
int64_t y = 1;
while (y <= h) {
logs[y] = log(((double)(y)));
dp[y] = 1.0e300;
y = (y + 1);
}
dp[1] = 0.0;
y = 2;
while (y <= h) {
int64_t M = (FLOW_CHECKED_DIV(((64 * h)), (y)) + 2);
int64_t y0_min = (y - M);
if (y0_min < 1) {
y0_min = 1;
}
double best = 1.0e300;
int64_t y0 = y0_min;
while (y0 < y) {
double cand = (dp[y0] + min_step_excess_i64_i64_i64_ptr_f64(y0, y, h, logs));
if (cand < best) {
best = cand;
}
y0 = (y0 + 1);
}
dp[y] = best;
y = (y + 1);
}
double ans = dp[h];
free(dp);
free(logs);
return ans;
}
int32_t main(void) {
int64_t d = 10000;
int64_t h = FLOW_CHECKED_DIV((d), (2));
double E = best_climb_excess_i64(h);
double ans = ((2.0 * E) + (((double)(d)) / ((double)(h))));
printf("%.9f\n", ans);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.9f\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 @log(f64) -> f64
func.func private @sqrt(f64) -> f64
func.func private @hypot(f64, f64) -> f64
func.func private @floor(f64) -> f64
func.func @min_step_excess(%arg0: i64, %arg1: i64, %arg2: i64, %arg3: !llvm.ptr) -> f64 {
%0 = arith.subi %arg1, %arg0 : i64
%1 = arith.sitofp %0 : i64 to f64
%3 = llvm.getelementptr %arg3[%arg1] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%2 = llvm.load %3 : !llvm.ptr -> f64
%5 = llvm.getelementptr %arg3[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%4 = llvm.load %5 : !llvm.ptr -> f64
%6 = arith.subf %2, %4 : f64
%7 = arith.divf %1, %6 : f64
%8 = arith.sitofp %arg2 : i64 to f64
%9 = arith.mulf %8, %8 : f64
%10 = arith.mulf %7, %7 : f64
%11 = arith.subf %9, %10 : f64
%12 = math.sqrt %11 : f64
%13 = arith.mulf %1, %7 : f64
%14 = arith.divf %13, %12 : f64
%15 = func.call @floor(%14) : (f64) -> f64
%16 = arith.fptosi %15 : f64 to i64
%17 = arith.constant 1000000000000000052504760255204420248704468581108159154915854115511802457988908195786371375080447864043704443832883878176942523235360430575644792184786706982848387200926575803737830233794788090059368953234970799945081119038967640880074652742780142494579258788820056842838115669472196386865459400540160 : f32
%18 = arith.extf %17 : f32 to f64
%19 = llvm.mlir.constant(1 : i64) : i64
%20 = llvm.alloca %19 x f64 : (i64) -> !llvm.ptr
llvm.store %18, %20 : f64, !llvm.ptr
%21 = arith.constant 0 : i32
%22 = arith.extsi %21 : i32 to i64
%23 = llvm.mlir.constant(1 : i64) : i64
%24 = llvm.alloca %23 x i64 : (i64) -> !llvm.ptr
llvm.store %22, %24 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%25 = llvm.load %24 : !llvm.ptr -> i64
%26 = arith.constant 2 : i32
%28 = arith.extsi %26 : i32 to i64
%27 = arith.cmpi slt, %25, %28 : i64
cf.cond_br %27, ^bb1, ^bb2
^bb1:
%29 = llvm.load %24 : !llvm.ptr -> i64
%30 = arith.addi %16, %29 : i64
%31 = arith.constant 0 : i32
%33 = arith.extsi %31 : i32 to i64
%32 = arith.cmpi sge, %30, %33 : i64
cf.cond_br %32, ^bb3, ^bb4
^bb3:
%35 = arith.sitofp %30 : i64 to f64
%34 = func.call @hypot(%35, %1) : (f64, f64) -> f64
%36 = arith.divf %34, %7 : f64
%37 = arith.sitofp %30 : i64 to f64
%38 = arith.divf %37, %8 : f64
%39 = arith.subf %36, %38 : f64
%40 = llvm.load %20 : !llvm.ptr -> f64
%41 = arith.cmpf olt, %39, %40 : f64
cf.cond_br %41, ^bb6, ^bb7
^bb6:
llvm.store %39, %20 : f64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%42 = llvm.load %24 : !llvm.ptr -> i64
%43 = arith.constant 1 : i32
%45 = arith.extsi %43 : i32 to i64
%44 = arith.addi %42, %45 : i64
llvm.store %44, %24 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%46 = llvm.load %20 : !llvm.ptr -> f64
func.return %46 : f64
}
func.func @best_climb_excess(%arg0: i64) -> f64 {
%48 = arith.constant 1 : i32
%50 = arith.extsi %48 : i32 to i64
%49 = arith.addi %arg0, %50 : i64
%51 = arith.constant 8 : i32
%52 = arith.extsi %51 : i32 to i64
%47 = func.call @calloc(%49, %52) : (i64, i64) -> !llvm.ptr
%54 = arith.constant 1 : i32
%56 = arith.extsi %54 : i32 to i64
%55 = arith.addi %arg0, %56 : i64
%57 = arith.constant 8 : i32
%58 = arith.extsi %57 : i32 to i64
%53 = func.call @calloc(%55, %58) : (i64, i64) -> !llvm.ptr
%59 = llvm.mlir.zero : !llvm.ptr
%60 = llvm.icmp "eq" %47, %59 : !llvm.ptr
%61 = scf.if %60 -> (i1) {
%62 = arith.constant true
scf.yield %62 : i1
} else {
%63 = llvm.mlir.zero : !llvm.ptr
%64 = llvm.icmp "eq" %53, %63 : !llvm.ptr
scf.yield %64 : i1
}
cf.cond_br %61, ^bb9, ^bb10
^bb9:
%65 = arith.constant 0.0 : f32
%66 = arith.extf %65 : f32 to f64
func.return %66 : f64
^bb10:
cf.br ^bb11
^bb11:
%67 = arith.constant 1 : 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 ^bb12
^bb12:
%71 = llvm.load %70 : !llvm.ptr -> i64
%72 = arith.cmpi sle, %71, %arg0 : i64
cf.cond_br %72, ^bb13, ^bb14
^bb13:
%73 = llvm.load %70 : !llvm.ptr -> i64
%74 = arith.sitofp %73 : i64 to f64
%75 = math.log %74 : f64
%76 = llvm.load %70 : !llvm.ptr -> i64
%77 = llvm.getelementptr %47[%76] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %75, %77 : f64, !llvm.ptr
%78 = arith.constant 1000000000000000052504760255204420248704468581108159154915854115511802457988908195786371375080447864043704443832883878176942523235360430575644792184786706982848387200926575803737830233794788090059368953234970799945081119038967640880074652742780142494579258788820056842838115669472196386865459400540160 : f32
%79 = llvm.load %70 : !llvm.ptr -> i64
%80 = arith.extf %78 : f32 to f64
%81 = llvm.getelementptr %53[%79] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %80, %81 : f64, !llvm.ptr
%82 = llvm.load %70 : !llvm.ptr -> i64
%83 = arith.constant 1 : i32
%85 = arith.extsi %83 : i32 to i64
%84 = arith.addi %82, %85 : i64
llvm.store %84, %70 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%86 = arith.constant 0.0 : f32
%87 = arith.constant 1 : i32
%88 = arith.extf %86 : f32 to f64
%89 = arith.extsi %87 : i32 to i64
%90 = llvm.getelementptr %53[%89] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %88, %90 : f64, !llvm.ptr
%91 = arith.constant 2 : i32
%92 = arith.extsi %91 : i32 to i64
llvm.store %92, %70 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%93 = llvm.load %70 : !llvm.ptr -> i64
%94 = arith.cmpi sle, %93, %arg0 : i64
cf.cond_br %94, ^bb16, ^bb17
^bb16:
%95 = arith.constant 64 : i32
%97 = arith.extsi %95 : i32 to i64
%96 = arith.muli %97, %arg0 : i64
%98 = llvm.load %70 : !llvm.ptr -> i64
%99 = arith.divsi %96, %98 : i64
%100 = arith.constant 2 : i32
%102 = arith.extsi %100 : i32 to i64
%101 = arith.addi %99, %102 : i64
%103 = llvm.load %70 : !llvm.ptr -> i64
%104 = arith.subi %103, %101 : i64
%105 = llvm.mlir.constant(1 : i64) : i64
%106 = llvm.alloca %105 x i64 : (i64) -> !llvm.ptr
llvm.store %104, %106 : i64, !llvm.ptr
%107 = llvm.load %106 : !llvm.ptr -> i64
%108 = arith.constant 1 : i32
%110 = arith.extsi %108 : i32 to i64
%109 = arith.cmpi slt, %107, %110 : i64
cf.cond_br %109, ^bb18, ^bb19
^bb18:
%111 = arith.constant 1 : i32
%112 = arith.extsi %111 : i32 to i64
llvm.store %112, %106 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%113 = arith.constant 1000000000000000052504760255204420248704468581108159154915854115511802457988908195786371375080447864043704443832883878176942523235360430575644792184786706982848387200926575803737830233794788090059368953234970799945081119038967640880074652742780142494579258788820056842838115669472196386865459400540160 : f32
%114 = arith.extf %113 : f32 to f64
%115 = llvm.mlir.constant(1 : i64) : i64
%116 = llvm.alloca %115 x f64 : (i64) -> !llvm.ptr
llvm.store %114, %116 : f64, !llvm.ptr
%117 = llvm.load %106 : !llvm.ptr -> i64
%118 = llvm.mlir.constant(1 : i64) : i64
%119 = llvm.alloca %118 x i64 : (i64) -> !llvm.ptr
llvm.store %117, %119 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%120 = llvm.load %119 : !llvm.ptr -> i64
%121 = llvm.load %70 : !llvm.ptr -> i64
%122 = arith.cmpi slt, %120, %121 : i64
cf.cond_br %122, ^bb22, ^bb23
^bb22:
%124 = llvm.load %119 : !llvm.ptr -> i64
%125 = llvm.getelementptr %53[%124] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%123 = llvm.load %125 : !llvm.ptr -> f64
%127 = llvm.load %119 : !llvm.ptr -> i64
%128 = llvm.load %70 : !llvm.ptr -> i64
%126 = func.call @min_step_excess(%127, %128, %arg0, %47) : (i64, i64, i64, !llvm.ptr) -> f64
%129 = arith.addf %123, %126 : f64
%130 = llvm.load %116 : !llvm.ptr -> f64
%131 = arith.cmpf olt, %129, %130 : f64
cf.cond_br %131, ^bb24, ^bb25
^bb24:
llvm.store %129, %116 : f64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%132 = llvm.load %119 : !llvm.ptr -> i64
%133 = arith.constant 1 : i32
%135 = arith.extsi %133 : i32 to i64
%134 = arith.addi %132, %135 : i64
llvm.store %134, %119 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%136 = llvm.load %116 : !llvm.ptr -> f64
%137 = llvm.load %70 : !llvm.ptr -> i64
%138 = llvm.getelementptr %53[%137] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %136, %138 : f64, !llvm.ptr
%139 = llvm.load %70 : !llvm.ptr -> i64
%140 = arith.constant 1 : i32
%142 = arith.extsi %140 : i32 to i64
%141 = arith.addi %139, %142 : i64
llvm.store %141, %70 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%144 = llvm.getelementptr %53[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%143 = llvm.load %144 : !llvm.ptr -> f64
func.call @free(%53) : (!llvm.ptr) -> ()
func.call @free(%47) : (!llvm.ptr) -> ()
func.return %143 : f64
}
func.func @main() -> i32 {
%147 = arith.constant 10000 : i32
%148 = arith.extsi %147 : i32 to i64
%149 = arith.constant 2 : i32
%151 = arith.extsi %149 : i32 to i64
%150 = arith.divsi %148, %151 : i64
%152 = func.call @best_climb_excess(%150) : (i64) -> f64
%153 = arith.constant 2.0 : f32
%155 = arith.extf %153 : f32 to f64
%154 = arith.mulf %155, %152 : f64
%156 = arith.sitofp %148 : i64 to f64
%157 = arith.sitofp %150 : i64 to f64
%158 = arith.divf %156, %157 : f64
%159 = arith.addf %154, %158 : f64
%160 = llvm.mlir.addressof @str_0 : !llvm.ptr
%161 = llvm.call @printf(%160, %159) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%162 = arith.constant 0 : i32
func.return %162 : i32
}
}