Problem 287
Minimal quadtree encoding length for D_24.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(1) | O(n) |
| Space complexity | O(1) | O(log n) |
| Approach | Flow solution | Recursive tree traversal |
| Verdict | Optimal |
Flow source
# Project Euler 287
# Minimal quadtree encoding length for D_24.
function is_black(x: i64, y: i64, size: i64) -> bool {
let middle: i64 = size >> 1
let threshold: i64 = middle * middle
let dx: i64 = x - middle
let dy: i64 = y - middle
return dx * dx + dy * dy <= threshold
}
function encode(from_x: i64, from_y: i64, to_x: i64, to_y: i64, is_first: bool, size: i64) -> i64 {
if from_x == to_x { return 2 }
let a: bool = is_black(from_x, from_y, size)
let b: bool = is_black(to_x, from_y, size)
let c: bool = is_black(to_x, to_y, size)
let d: bool = is_black(from_x, to_y, size)
if a == b && b == c && c == d && !is_first { return 2 }
if from_x + 1 == to_x { return 1 + 8 }
let half: i64 = (to_x - from_x + 1) / 2
return encode(from_x, from_y + half, to_x - half, to_y, false, size) + encode(from_x + half, from_y + half, to_x, to_y, false, size) + encode(from_x, from_y, to_x - half, to_y - half, false, size) + encode(from_x + half, from_y, to_x, to_y - half, false, size) + 1
}
function main() -> i32 {
let one: i64 = 1
let size: i64 = one << 24
printf("%lld\n", encode(0, 0, size - 1, size - 1, true, size))
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; }
bool is_black_i64_i64_i64(int64_t x, int64_t y, int64_t size);
int64_t encode_i64_i64_i64_i64_bool_i64(int64_t from_x, int64_t from_y, int64_t to_x, int64_t to_y, bool is_first, int64_t size);
int32_t main(void);
bool is_black_i64_i64_i64(int64_t x, int64_t y, int64_t size) {
int64_t middle = FLOW_CHECKED_SHR((size), (1));
int64_t threshold = (middle * middle);
int64_t dx = (x - middle);
int64_t dy = (y - middle);
return ((dx * dx) + (dy * dy)) <= threshold;
}
int64_t encode_i64_i64_i64_i64_bool_i64(int64_t from_x, int64_t from_y, int64_t to_x, int64_t to_y, bool is_first, int64_t size) {
if (from_x == to_x) {
return 2;
}
bool a = is_black_i64_i64_i64(from_x, from_y, size);
bool b = is_black_i64_i64_i64(to_x, from_y, size);
bool c = is_black_i64_i64_i64(to_x, to_y, size);
bool d = is_black_i64_i64_i64(from_x, to_y, size);
if ((((a == b && b == c) && c == d) && (!(is_first)))) {
return 2;
}
if ((from_x + 1) == to_x) {
return (1 + 8);
}
int64_t half = FLOW_CHECKED_DIV((((to_x - from_x) + 1)), (2));
return ((((encode_i64_i64_i64_i64_bool_i64(from_x, (from_y + half), (to_x - half), to_y, 0, size) + encode_i64_i64_i64_i64_bool_i64((from_x + half), (from_y + half), to_x, to_y, 0, size)) + encode_i64_i64_i64_i64_bool_i64(from_x, from_y, (to_x - half), (to_y - half), 0, size)) + encode_i64_i64_i64_i64_bool_i64((from_x + half), from_y, to_x, (to_y - half), 0, size)) + 1);
}
int32_t main(void) {
int64_t one = 1;
int64_t size = FLOW_CHECKED_SHL((one), (24));
printf("%lld\n", encode_i64_i64_i64_i64_bool_i64(0, 0, (size - 1), (size - 1), 1, size));
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 @is_black(%arg0: i64, %arg1: i64, %arg2: i64) -> i1 {
%0 = arith.constant 1 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.shrsi %arg2, %2 : i64
%3 = arith.muli %1, %1 : i64
%4 = arith.subi %arg0, %1 : i64
%5 = arith.subi %arg1, %1 : i64
%6 = arith.muli %4, %4 : i64
%7 = arith.muli %5, %5 : i64
%8 = arith.addi %6, %7 : i64
%9 = arith.cmpi sle, %8, %3 : i64
func.return %9 : i1
}
func.func @encode(%arg0: i64, %arg1: i64, %arg2: i64, %arg3: i64, %arg4: i1, %arg5: i64) -> i64 {
%10 = arith.cmpi eq, %arg0, %arg2 : i64
cf.cond_br %10, ^bb0, ^bb1
^bb0:
%11 = arith.constant 2 : i32
%12 = arith.extsi %11 : i32 to i64
func.return %12 : i64
^bb1:
cf.br ^bb2
^bb2:
%13 = func.call @is_black(%arg0, %arg1, %arg5) : (i64, i64, i64) -> i1
%14 = func.call @is_black(%arg2, %arg1, %arg5) : (i64, i64, i64) -> i1
%15 = func.call @is_black(%arg2, %arg3, %arg5) : (i64, i64, i64) -> i1
%16 = func.call @is_black(%arg0, %arg3, %arg5) : (i64, i64, i64) -> i1
%17 = arith.cmpi eq, %13, %14 : i1
%18 = scf.if %17 -> (i1) {
%19 = arith.cmpi eq, %14, %15 : i1
scf.yield %19 : i1
} else {
%20 = arith.constant false
scf.yield %20 : i1
}
%21 = scf.if %18 -> (i1) {
%22 = arith.cmpi eq, %15, %16 : i1
scf.yield %22 : i1
} else {
%23 = arith.constant false
scf.yield %23 : i1
}
%24 = scf.if %21 -> (i1) {
%26 = arith.constant 1 : i1
%25 = arith.xori %arg4, %26 : i1
scf.yield %25 : i1
} else {
%28 = arith.constant false
scf.yield %28 : i1
}
cf.cond_br %24, ^bb3, ^bb4
^bb3:
%29 = arith.constant 2 : i32
%30 = arith.extsi %29 : i32 to i64
func.return %30 : i64
^bb4:
cf.br ^bb5
^bb5:
%31 = arith.constant 1 : i32
%33 = arith.extsi %31 : i32 to i64
%32 = arith.addi %arg0, %33 : i64
%34 = arith.cmpi eq, %32, %arg2 : i64
cf.cond_br %34, ^bb6, ^bb7
^bb6:
%35 = arith.constant 1 : i32
%36 = arith.constant 8 : i32
%37 = arith.addi %35, %36 : i32
%38 = arith.extsi %37 : i32 to i64
func.return %38 : i64
^bb7:
cf.br ^bb8
^bb8:
%39 = arith.subi %arg2, %arg0 : i64
%40 = arith.constant 1 : i32
%42 = arith.extsi %40 : i32 to i64
%41 = arith.addi %39, %42 : i64
%43 = arith.constant 2 : i32
%45 = arith.extsi %43 : i32 to i64
%44 = arith.divsi %41, %45 : i64
%47 = arith.addi %arg1, %44 : i64
%48 = arith.subi %arg2, %44 : i64
%49 = arith.constant 0 : i1
%46 = func.call @encode(%arg0, %47, %48, %arg3, %49, %arg5) : (i64, i64, i64, i64, i1, i64) -> i64
%51 = arith.addi %arg0, %44 : i64
%52 = arith.addi %arg1, %44 : i64
%53 = arith.constant 0 : i1
%50 = func.call @encode(%51, %52, %arg2, %arg3, %53, %arg5) : (i64, i64, i64, i64, i1, i64) -> i64
%54 = arith.addi %46, %50 : i64
%56 = arith.subi %arg2, %44 : i64
%57 = arith.subi %arg3, %44 : i64
%58 = arith.constant 0 : i1
%55 = func.call @encode(%arg0, %arg1, %56, %57, %58, %arg5) : (i64, i64, i64, i64, i1, i64) -> i64
%59 = arith.addi %54, %55 : i64
%61 = arith.addi %arg0, %44 : i64
%62 = arith.subi %arg3, %44 : i64
%63 = arith.constant 0 : i1
%60 = func.call @encode(%61, %arg1, %arg2, %62, %63, %arg5) : (i64, i64, i64, i64, i1, i64) -> i64
%64 = arith.addi %59, %60 : i64
%65 = arith.constant 1 : i32
%67 = arith.extsi %65 : i32 to i64
%66 = arith.addi %64, %67 : i64
func.return %66 : i64
}
func.func @main() -> i32 {
%68 = arith.constant 1 : i32
%69 = arith.extsi %68 : i32 to i64
%70 = arith.constant 24 : i32
%72 = arith.extsi %70 : i32 to i64
%71 = arith.shli %69, %72 : i64
%73 = llvm.mlir.addressof @str_0 : !llvm.ptr
%75 = arith.constant 0 : i32
%76 = arith.constant 0 : i32
%77 = arith.constant 1 : i32
%79 = arith.extsi %77 : i32 to i64
%78 = arith.subi %71, %79 : i64
%80 = arith.constant 1 : i32
%82 = arith.extsi %80 : i32 to i64
%81 = arith.subi %71, %82 : i64
%83 = arith.constant 1 : i1
%84 = arith.extsi %75 : i32 to i64
%85 = arith.extsi %76 : i32 to i64
%74 = func.call @encode(%84, %85, %78, %81, %83, %71) : (i64, i64, i64, i64, i1, i64) -> i64
%86 = llvm.call @printf(%73, %74) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%87 = arith.constant 0 : i32
func.return %87 : i32
}
}