Problem 600
Integer sided equiangular hexagons with perimeter <= 55106. H(n) = # ways to write n-6 as 1a+2b+3c+4d+6e (coin DP).
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | O(n * m) |
| Space complexity | O(n) | O(n) |
| Approach | Flow solution | Dynamic programming or generating function |
| Verdict | Unknown |
Flow source
# Project Euler 600
# Integer sided equiangular hexagons with perimeter <= 55106.
# H(n) = # ways to write n-6 as 1a+2b+3c+4d+6e (coin DP).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function H(n: i64) -> i64 {
let m: i64 = n - 6
if m < 0 { return 0 }
let dp: ptr<i64> = calloc(m + 1, 8)
if dp == null { return -1 }
dp[0] = 1
let coins: ptr<i64> = calloc(5, 8)
coins[0] = 1
coins[1] = 2
coins[2] = 3
coins[3] = 4
coins[4] = 6
let mut ci: i64 = 0
while ci < 5 {
let c: i64 = coins[ci]
let mut i: i64 = c
while i <= m {
dp[i] = dp[i] + dp[i - c]
i = i + 1
}
ci = ci + 1
}
let ans: i64 = dp[m]
free(coins)
free(dp)
return ans
}
function main() -> i32 {
printf("%lld\n", H(55106))
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 H_i64(int64_t n);
int32_t main(void);
int64_t H_i64(int64_t n) {
int64_t m = (n - 6);
if (m < 0) {
return 0;
}
int64_t* dp = (int64_t*)(calloc((m + 1), 8));
if (dp == NULL) {
return (-1);
}
dp[0] = 1;
int64_t* coins = (int64_t*)(calloc(5, 8));
coins[0] = 1;
coins[1] = 2;
coins[2] = 3;
coins[3] = 4;
coins[4] = 6;
int64_t ci = 0;
while (ci < 5) {
int64_t c = coins[ci];
int64_t i = c;
while (i <= m) {
dp[i] = (dp[i] + dp[(i - c)]);
i = (i + 1);
}
ci = (ci + 1);
}
int64_t ans = dp[m];
free(coins);
free(dp);
return ans;
}
int32_t main(void) {
printf("%lld\n", H_i64(55106));
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 @H(%arg0: i64) -> i64 {
%0 = arith.constant 6 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.subi %arg0, %2 : i64
%3 = arith.constant 0 : i32
%5 = arith.extsi %3 : i32 to i64
%4 = arith.cmpi slt, %1, %5 : i64
cf.cond_br %4, ^bb0, ^bb1
^bb0:
%6 = arith.constant 0 : i32
%7 = arith.extsi %6 : i32 to i64
func.return %7 : i64
^bb1:
cf.br ^bb2
^bb2:
%9 = arith.constant 1 : i32
%11 = arith.extsi %9 : i32 to i64
%10 = arith.addi %1, %11 : i64
%12 = arith.constant 8 : i32
%13 = arith.extsi %12 : i32 to i64
%8 = func.call @calloc(%10, %13) : (i64, i64) -> !llvm.ptr
%14 = llvm.mlir.zero : !llvm.ptr
%15 = llvm.icmp "eq" %8, %14 : !llvm.ptr
cf.cond_br %15, ^bb3, ^bb4
^bb3:
%16 = arith.constant 1 : i32
%18 = arith.constant 0 : i32
%17 = arith.subi %18, %16 : i32
%19 = arith.extsi %17 : i32 to i64
func.return %19 : i64
^bb4:
cf.br ^bb5
^bb5:
%20 = arith.constant 1 : i32
%21 = arith.constant 0 : i32
%22 = arith.extsi %20 : i32 to i64
%23 = arith.extsi %21 : i32 to i64
%24 = llvm.getelementptr %8[%23] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %22, %24 : i64, !llvm.ptr
%26 = arith.constant 5 : i32
%27 = arith.constant 8 : i32
%28 = arith.extsi %26 : i32 to i64
%29 = arith.extsi %27 : i32 to i64
%25 = func.call @calloc(%28, %29) : (i64, i64) -> !llvm.ptr
%30 = arith.constant 1 : i32
%31 = arith.constant 0 : i32
%32 = arith.extsi %30 : i32 to i64
%33 = arith.extsi %31 : i32 to i64
%34 = llvm.getelementptr %25[%33] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %32, %34 : i64, !llvm.ptr
%35 = arith.constant 2 : i32
%36 = arith.constant 1 : i32
%37 = arith.extsi %35 : i32 to i64
%38 = arith.extsi %36 : i32 to i64
%39 = llvm.getelementptr %25[%38] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %37, %39 : i64, !llvm.ptr
%40 = arith.constant 3 : i32
%41 = arith.constant 2 : i32
%42 = arith.extsi %40 : i32 to i64
%43 = arith.extsi %41 : i32 to i64
%44 = llvm.getelementptr %25[%43] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %42, %44 : i64, !llvm.ptr
%45 = arith.constant 4 : i32
%46 = arith.constant 3 : i32
%47 = arith.extsi %45 : i32 to i64
%48 = arith.extsi %46 : i32 to i64
%49 = llvm.getelementptr %25[%48] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %47, %49 : i64, !llvm.ptr
%50 = arith.constant 6 : i32
%51 = arith.constant 4 : i32
%52 = arith.extsi %50 : i32 to i64
%53 = arith.extsi %51 : i32 to i64
%54 = llvm.getelementptr %25[%53] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %52, %54 : i64, !llvm.ptr
%55 = arith.constant 0 : i32
%56 = arith.extsi %55 : i32 to i64
%57 = llvm.mlir.constant(1 : i64) : i64
%58 = llvm.alloca %57 x i64 : (i64) -> !llvm.ptr
llvm.store %56, %58 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%59 = llvm.load %58 : !llvm.ptr -> i64
%60 = arith.constant 5 : i32
%62 = arith.extsi %60 : i32 to i64
%61 = arith.cmpi slt, %59, %62 : i64
cf.cond_br %61, ^bb7, ^bb8
^bb7:
%64 = llvm.load %58 : !llvm.ptr -> i64
%65 = llvm.getelementptr %25[%64] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%63 = llvm.load %65 : !llvm.ptr -> i64
%66 = llvm.mlir.constant(1 : i64) : i64
%67 = llvm.alloca %66 x i64 : (i64) -> !llvm.ptr
llvm.store %63, %67 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%68 = llvm.load %67 : !llvm.ptr -> i64
%69 = arith.cmpi sle, %68, %1 : i64
cf.cond_br %69, ^bb10, ^bb11
^bb10:
%71 = llvm.load %67 : !llvm.ptr -> i64
%72 = llvm.getelementptr %8[%71] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%70 = llvm.load %72 : !llvm.ptr -> i64
%74 = llvm.load %67 : !llvm.ptr -> i64
%75 = arith.subi %74, %63 : i64
%76 = llvm.getelementptr %8[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%73 = llvm.load %76 : !llvm.ptr -> i64
%77 = arith.addi %70, %73 : i64
%78 = llvm.load %67 : !llvm.ptr -> i64
%79 = llvm.getelementptr %8[%78] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %77, %79 : i64, !llvm.ptr
%80 = llvm.load %67 : !llvm.ptr -> i64
%81 = arith.constant 1 : i32
%83 = arith.extsi %81 : i32 to i64
%82 = arith.addi %80, %83 : i64
llvm.store %82, %67 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%84 = llvm.load %58 : !llvm.ptr -> i64
%85 = arith.constant 1 : i32
%87 = arith.extsi %85 : i32 to i64
%86 = arith.addi %84, %87 : i64
llvm.store %86, %58 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%89 = llvm.getelementptr %8[%1] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%88 = llvm.load %89 : !llvm.ptr -> i64
func.call @free(%25) : (!llvm.ptr) -> ()
func.call @free(%8) : (!llvm.ptr) -> ()
func.return %88 : i64
}
func.func @main() -> i32 {
%92 = llvm.mlir.addressof @str_0 : !llvm.ptr
%94 = arith.constant 55106 : i32
%95 = arith.extsi %94 : i32 to i64
%93 = func.call @H(%95) : (i64) -> i64
%96 = llvm.call @printf(%92, %93) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%97 = arith.constant 0 : i32
func.return %97 : i32
}
}