← All problems
Problem 750
Optimal Card Stacking: G(976). Positions generated by x=(x*3)%(N+1). DP over intervals [l,r] with cost = |pos[k]-pos[r]| for the split point k.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^3)O(n)
Space complexity O(n)O(1)
Approach Flow solution Direct hand evaluation or enumeration
Verdict Suboptimal
Flow source
# Project Euler 750
# Optimal Card Stacking: G(976).
# Positions generated by x=(x*3)%(N+1). DP over intervals [l,r]
# with cost = |pos[k]-pos[r]| for the split point k.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const N: i64 = 976
const INF: i64 = 4000000000000000000
function iabs(x: i64) -> i64 {
if x < 0 {
return 0 - x
}
return x
}
function main() -> i32 {
let mod: i64 = N + 1
let pos: ptr<i64> = calloc(N + 1, 8)
if pos == null {
return 1
}
let mut x: i64 = 1
let mut i: i64 = 1
while i <= N {
x = (x * 3) % mod
if x == 0 || x > N || pos[x] != 0 {
free(pos as ptr<void>)
printf("%lld\n", -1)
return 0
}
pos[x] = i
i = i + 1
}
let dp: ptr<i64> = calloc((N + 1) * (N + 1), 8)
if dp == null {
free(pos as ptr<void>)
return 1
}
let mut r: i64 = 2
while r <= N {
let mut l: i64 = r - 1
while l >= 1 {
let mut best: i64 = INF
let mut k: i64 = l
while k < r {
let cost: i64 = dp[l * (N + 1) + k] + dp[(k + 1) * (N + 1) + r] + iabs(pos[k] - pos[r])
if cost < best {
best = cost
}
k = k + 1
}
dp[l * (N + 1) + r] = best
l = l - 1
}
r = r + 1
}
let ans: i64 = dp[1 * (N + 1) + N]
free(dp as ptr<void>)
free(pos as ptr<void>)
printf("%lld\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; }
int64_t iabs_i64(int64_t x);
int32_t main(void);
static const int64_t N = 976;
static const int64_t INF = 4000000000000000000;
int64_t iabs_i64(int64_t x) {
if (x < 0) {
return (0 - x);
}
return x;
}
int32_t main(void) {
int64_t mod = (N + 1);
int64_t* pos = (int64_t*)(calloc((N + 1), 8));
if (pos == NULL) {
return 1;
}
int64_t x = 1;
int64_t i = 1;
while (i <= N) {
x = FLOW_CHECKED_MOD(((x * 3)), (mod));
if (((x == 0 || x > N) || pos[x] != 0)) {
free(((void*)(pos)));
printf("%lld\n", (-1));
return 0;
}
pos[x] = i;
i = (i + 1);
}
int64_t* dp = (int64_t*)(calloc(((N + 1) * (N + 1)), 8));
if (dp == NULL) {
free(((void*)(pos)));
return 1;
}
int64_t r = 2;
while (r <= N) {
int64_t l = (r - 1);
while (l >= 1) {
int64_t best = INF;
int64_t k = l;
while (k < r) {
int64_t cost = ((dp[((l * (N + 1)) + k)] + dp[(((k + 1) * (N + 1)) + r)]) + iabs_i64((pos[k] - pos[r])));
if (cost < best) {
best = cost;
}
k = (k + 1);
}
dp[((l * (N + 1)) + r)] = best;
l = (l - 1);
}
r = (r + 1);
}
int64_t ans = dp[((1 * (N + 1)) + N)];
free(((void*)(dp)));
free(((void*)(pos)));
printf("%lld\n", ans);
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) -> ()
// Constant: N
llvm.mlir.global internal constant @N(976 : i64) : i64
// Constant: INF
llvm.mlir.global internal constant @INF(4000000000000000000 : i64) : i64
func.func @iabs(%arg0: i64) -> i64 {
%0 = arith.constant 0 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.cmpi slt, %arg0, %2 : i64
cf.cond_br %1, ^bb0, ^bb1
^bb0:
%3 = arith.constant 0 : i32
%5 = arith.extsi %3 : i32 to i64
%4 = arith.subi %5, %arg0 : i64
func.return %4 : i64
^bb1:
cf.br ^bb2
^bb2:
func.return %arg0 : i64
}
func.func @main() -> i32 {
%6 = llvm.mlir.addressof @N : !llvm.ptr
%7 = llvm.load %6 : !llvm.ptr -> i64
%8 = arith.constant 1 : i32
%10 = arith.extsi %8 : i32 to i64
%9 = arith.addi %7, %10 : i64
%12 = llvm.mlir.addressof @N : !llvm.ptr
%13 = llvm.load %12 : !llvm.ptr -> i64
%14 = arith.constant 1 : i32
%16 = arith.extsi %14 : i32 to i64
%15 = arith.addi %13, %16 : i64
%17 = arith.constant 8 : i32
%18 = arith.extsi %17 : i32 to i64
%11 = func.call @calloc(%15, %18) : (i64, i64) -> !llvm.ptr
%19 = llvm.mlir.zero : !llvm.ptr
%20 = llvm.icmp "eq" %11, %19 : !llvm.ptr
cf.cond_br %20, ^bb3, ^bb4
^bb3:
%21 = arith.constant 1 : i32
func.return %21 : i32
^bb4:
cf.br ^bb5
^bb5:
%22 = arith.constant 1 : i32
%23 = arith.extsi %22 : i32 to i64
%24 = llvm.mlir.constant(1 : i64) : i64
%25 = llvm.alloca %24 x i64 : (i64) -> !llvm.ptr
llvm.store %23, %25 : i64, !llvm.ptr
%26 = arith.constant 1 : i32
%27 = arith.extsi %26 : i32 to i64
%28 = llvm.mlir.constant(1 : i64) : i64
%29 = llvm.alloca %28 x i64 : (i64) -> !llvm.ptr
llvm.store %27, %29 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%30 = llvm.load %29 : !llvm.ptr -> i64
%31 = llvm.mlir.addressof @N : !llvm.ptr
%32 = llvm.load %31 : !llvm.ptr -> i64
%33 = arith.cmpi sle, %30, %32 : i64
cf.cond_br %33, ^bb7, ^bb8
^bb7:
%34 = llvm.load %25 : !llvm.ptr -> i64
%35 = arith.constant 3 : i32
%37 = arith.extsi %35 : i32 to i64
%36 = arith.muli %34, %37 : i64
%38 = arith.remsi %36, %9 : i64
llvm.store %38, %25 : i64, !llvm.ptr
%39 = llvm.load %25 : !llvm.ptr -> i64
%40 = arith.constant 0 : i32
%42 = arith.extsi %40 : i32 to i64
%41 = arith.cmpi eq, %39, %42 : i64
%43 = scf.if %41 -> (i1) {
%44 = arith.constant true
scf.yield %44 : i1
} else {
%45 = llvm.load %25 : !llvm.ptr -> i64
%46 = llvm.mlir.addressof @N : !llvm.ptr
%47 = llvm.load %46 : !llvm.ptr -> i64
%48 = arith.cmpi sgt, %45, %47 : i64
scf.yield %48 : i1
}
%49 = scf.if %43 -> (i1) {
%50 = arith.constant true
scf.yield %50 : i1
} else {
%52 = llvm.load %25 : !llvm.ptr -> i64
%53 = llvm.getelementptr %11[%52] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%51 = llvm.load %53 : !llvm.ptr -> i64
%54 = arith.constant 0 : i32
%56 = arith.extsi %54 : i32 to i64
%55 = arith.cmpi ne, %51, %56 : i64
scf.yield %55 : i1
}
cf.cond_br %49, ^bb9, ^bb10
^bb9:
func.call @free(%11) : (!llvm.ptr) -> ()
%58 = llvm.mlir.addressof @str_0 : !llvm.ptr
%59 = arith.constant 1 : i32
%61 = arith.constant 0 : i32
%60 = arith.subi %61, %59 : i32
%62 = llvm.call @printf(%58, %60) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i32) -> i32
%63 = arith.constant 0 : i32
func.return %63 : i32
^bb10:
cf.br ^bb11
^bb11:
%64 = llvm.load %29 : !llvm.ptr -> i64
%65 = llvm.load %25 : !llvm.ptr -> i64
%66 = llvm.getelementptr %11[%65] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %64, %66 : i64, !llvm.ptr
%67 = llvm.load %29 : !llvm.ptr -> i64
%68 = arith.constant 1 : i32
%70 = arith.extsi %68 : i32 to i64
%69 = arith.addi %67, %70 : i64
llvm.store %69, %29 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%72 = llvm.mlir.addressof @N : !llvm.ptr
%73 = llvm.load %72 : !llvm.ptr -> i64
%74 = arith.constant 1 : i32
%76 = arith.extsi %74 : i32 to i64
%75 = arith.addi %73, %76 : i64
%77 = llvm.mlir.addressof @N : !llvm.ptr
%78 = llvm.load %77 : !llvm.ptr -> i64
%79 = arith.constant 1 : i32
%81 = arith.extsi %79 : i32 to i64
%80 = arith.addi %78, %81 : i64
%82 = arith.muli %75, %80 : i64
%83 = arith.constant 8 : i32
%84 = arith.extsi %83 : i32 to i64
%71 = func.call @calloc(%82, %84) : (i64, i64) -> !llvm.ptr
%85 = llvm.mlir.zero : !llvm.ptr
%86 = llvm.icmp "eq" %71, %85 : !llvm.ptr
cf.cond_br %86, ^bb12, ^bb13
^bb12:
func.call @free(%11) : (!llvm.ptr) -> ()
%88 = arith.constant 1 : i32
func.return %88 : i32
^bb13:
cf.br ^bb14
^bb14:
%89 = arith.constant 2 : i32
%90 = arith.extsi %89 : i32 to i64
%91 = llvm.mlir.constant(1 : i64) : i64
%92 = llvm.alloca %91 x i64 : (i64) -> !llvm.ptr
llvm.store %90, %92 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%93 = llvm.load %92 : !llvm.ptr -> i64
%94 = llvm.mlir.addressof @N : !llvm.ptr
%95 = llvm.load %94 : !llvm.ptr -> i64
%96 = arith.cmpi sle, %93, %95 : i64
cf.cond_br %96, ^bb16, ^bb17
^bb16:
%97 = llvm.load %92 : !llvm.ptr -> i64
%98 = arith.constant 1 : i32
%100 = arith.extsi %98 : i32 to i64
%99 = arith.subi %97, %100 : i64
%101 = llvm.mlir.constant(1 : i64) : i64
%102 = llvm.alloca %101 x i64 : (i64) -> !llvm.ptr
llvm.store %99, %102 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%103 = llvm.load %102 : !llvm.ptr -> i64
%104 = arith.constant 1 : i32
%106 = arith.extsi %104 : i32 to i64
%105 = arith.cmpi sge, %103, %106 : i64
cf.cond_br %105, ^bb19, ^bb20
^bb19:
%107 = llvm.mlir.addressof @INF : !llvm.ptr
%108 = llvm.load %107 : !llvm.ptr -> i64
%109 = llvm.mlir.constant(1 : i64) : i64
%110 = llvm.alloca %109 x i64 : (i64) -> !llvm.ptr
llvm.store %108, %110 : i64, !llvm.ptr
%111 = llvm.load %102 : !llvm.ptr -> i64
%112 = llvm.mlir.constant(1 : i64) : i64
%113 = llvm.alloca %112 x i64 : (i64) -> !llvm.ptr
llvm.store %111, %113 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%114 = llvm.load %113 : !llvm.ptr -> i64
%115 = llvm.load %92 : !llvm.ptr -> i64
%116 = arith.cmpi slt, %114, %115 : i64
cf.cond_br %116, ^bb22, ^bb23
^bb22:
%118 = llvm.load %102 : !llvm.ptr -> i64
%119 = llvm.mlir.addressof @N : !llvm.ptr
%120 = llvm.load %119 : !llvm.ptr -> i64
%121 = arith.constant 1 : i32
%123 = arith.extsi %121 : i32 to i64
%122 = arith.addi %120, %123 : i64
%124 = arith.muli %118, %122 : i64
%125 = llvm.load %113 : !llvm.ptr -> i64
%126 = arith.addi %124, %125 : i64
%127 = llvm.getelementptr %71[%126] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%117 = llvm.load %127 : !llvm.ptr -> i64
%129 = llvm.load %113 : !llvm.ptr -> i64
%130 = arith.constant 1 : i32
%132 = arith.extsi %130 : i32 to i64
%131 = arith.addi %129, %132 : i64
%133 = llvm.mlir.addressof @N : !llvm.ptr
%134 = llvm.load %133 : !llvm.ptr -> i64
%135 = arith.constant 1 : i32
%137 = arith.extsi %135 : i32 to i64
%136 = arith.addi %134, %137 : i64
%138 = arith.muli %131, %136 : i64
%139 = llvm.load %92 : !llvm.ptr -> i64
%140 = arith.addi %138, %139 : i64
%141 = llvm.getelementptr %71[%140] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%128 = llvm.load %141 : !llvm.ptr -> i64
%142 = arith.addi %117, %128 : i64
%145 = llvm.load %113 : !llvm.ptr -> i64
%146 = llvm.getelementptr %11[%145] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%144 = llvm.load %146 : !llvm.ptr -> i64
%148 = llvm.load %92 : !llvm.ptr -> i64
%149 = llvm.getelementptr %11[%148] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%147 = llvm.load %149 : !llvm.ptr -> i64
%150 = arith.subi %144, %147 : i64
%143 = func.call @iabs(%150) : (i64) -> i64
%151 = arith.addi %142, %143 : i64
%152 = llvm.load %110 : !llvm.ptr -> i64
%153 = arith.cmpi slt, %151, %152 : i64
cf.cond_br %153, ^bb24, ^bb25
^bb24:
llvm.store %151, %110 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%154 = llvm.load %113 : !llvm.ptr -> i64
%155 = arith.constant 1 : i32
%157 = arith.extsi %155 : i32 to i64
%156 = arith.addi %154, %157 : i64
llvm.store %156, %113 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%158 = llvm.load %110 : !llvm.ptr -> i64
%159 = llvm.load %102 : !llvm.ptr -> i64
%160 = llvm.mlir.addressof @N : !llvm.ptr
%161 = llvm.load %160 : !llvm.ptr -> i64
%162 = arith.constant 1 : i32
%164 = arith.extsi %162 : i32 to i64
%163 = arith.addi %161, %164 : i64
%165 = arith.muli %159, %163 : i64
%166 = llvm.load %92 : !llvm.ptr -> i64
%167 = arith.addi %165, %166 : i64
%168 = llvm.getelementptr %71[%167] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %158, %168 : i64, !llvm.ptr
%169 = llvm.load %102 : !llvm.ptr -> i64
%170 = arith.constant 1 : i32
%172 = arith.extsi %170 : i32 to i64
%171 = arith.subi %169, %172 : i64
llvm.store %171, %102 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%173 = llvm.load %92 : !llvm.ptr -> i64
%174 = arith.constant 1 : i32
%176 = arith.extsi %174 : i32 to i64
%175 = arith.addi %173, %176 : i64
llvm.store %175, %92 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%178 = arith.constant 1 : i32
%179 = llvm.mlir.addressof @N : !llvm.ptr
%180 = llvm.load %179 : !llvm.ptr -> i64
%181 = arith.constant 1 : i32
%183 = arith.extsi %181 : i32 to i64
%182 = arith.addi %180, %183 : i64
%185 = arith.extsi %178 : i32 to i64
%184 = arith.muli %185, %182 : i64
%186 = llvm.mlir.addressof @N : !llvm.ptr
%187 = llvm.load %186 : !llvm.ptr -> i64
%188 = arith.addi %184, %187 : i64
%189 = llvm.getelementptr %71[%188] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%177 = llvm.load %189 : !llvm.ptr -> i64
func.call @free(%71) : (!llvm.ptr) -> ()
func.call @free(%11) : (!llvm.ptr) -> ()
%192 = llvm.mlir.addressof @str_0 : !llvm.ptr
%193 = llvm.call @printf(%192, %177) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%194 = arith.constant 0 : i32
func.return %194 : i32
}
}