Problem 306
Paper-strip game: winning first-player positions for 10^6 squares. Losing lengths follow a period-34 pattern after a short prefix.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | O(n^3) |
| Space complexity | O(1) | O(n^2) |
| Approach | Flow solution | Game theory DP |
| Verdict | Optimal |
Flow source
# Project Euler 306
# Paper-strip game: winning first-player positions for 10^6 squares.
# Losing lengths follow a period-34 pattern after a short prefix.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let limit: i64 = 1000000
let init_vals: ptr<i64> = calloc(13, 8)
let last5: ptr<i64> = calloc(5, 8)
if init_vals == null || last5 == null { return 1 }
init_vals[0] = 1; init_vals[1] = 5; init_vals[2] = 9; init_vals[3] = 15
init_vals[4] = 21; init_vals[5] = 25; init_vals[6] = 29; init_vals[7] = 35
init_vals[8] = 39; init_vals[9] = 43; init_vals[10] = 55; init_vals[11] = 59
init_vals[12] = 63
let mut num_lost: i64 = 0
while true {
let mut current: i64 = last5[num_lost % 5] + 34
if num_lost < 13 {
current = init_vals[num_lost]
}
if current > limit {
printf("%lld\n", limit - num_lost)
free(init_vals); free(last5)
return 0
}
last5[num_lost % 5] = current
num_lost = num_lost + 1
}
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; }
int32_t main(void);
int32_t main(void) {
int64_t limit = 1000000;
int64_t* init_vals = (int64_t*)(calloc(13, 8));
int64_t* last5 = (int64_t*)(calloc(5, 8));
if ((init_vals == NULL || last5 == NULL)) {
return 1;
}
init_vals[0] = 1;
init_vals[1] = 5;
init_vals[2] = 9;
init_vals[3] = 15;
init_vals[4] = 21;
init_vals[5] = 25;
init_vals[6] = 29;
init_vals[7] = 35;
init_vals[8] = 39;
init_vals[9] = 43;
init_vals[10] = 55;
init_vals[11] = 59;
init_vals[12] = 63;
int64_t num_lost = 0;
while (1) {
int64_t current = (last5[FLOW_CHECKED_MOD((num_lost), (5))] + 34);
if (num_lost < 13) {
current = init_vals[num_lost];
}
if (current > limit) {
printf("%lld\n", (limit - num_lost));
free(init_vals);
free(last5);
return 0;
}
last5[FLOW_CHECKED_MOD((num_lost), (5))] = current;
num_lost = (num_lost + 1);
}
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 @main() -> i32 {
%0 = arith.constant 1000000 : i32
%1 = arith.extsi %0 : i32 to i64
%3 = arith.constant 13 : i32
%4 = arith.constant 8 : i32
%5 = arith.extsi %3 : i32 to i64
%6 = arith.extsi %4 : i32 to i64
%2 = func.call @calloc(%5, %6) : (i64, i64) -> !llvm.ptr
%8 = arith.constant 5 : i32
%9 = arith.constant 8 : i32
%10 = arith.extsi %8 : i32 to i64
%11 = arith.extsi %9 : i32 to i64
%7 = func.call @calloc(%10, %11) : (i64, i64) -> !llvm.ptr
%12 = llvm.mlir.zero : !llvm.ptr
%13 = llvm.icmp "eq" %2, %12 : !llvm.ptr
%14 = scf.if %13 -> (i1) {
%15 = arith.constant true
scf.yield %15 : i1
} else {
%16 = llvm.mlir.zero : !llvm.ptr
%17 = llvm.icmp "eq" %7, %16 : !llvm.ptr
scf.yield %17 : i1
}
cf.cond_br %14, ^bb0, ^bb1
^bb0:
%18 = arith.constant 1 : i32
func.return %18 : i32
^bb1:
cf.br ^bb2
^bb2:
%19 = arith.constant 1 : i32
%20 = arith.constant 0 : i32
%21 = arith.extsi %19 : i32 to i64
%22 = arith.extsi %20 : i32 to i64
%23 = llvm.getelementptr %2[%22] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %21, %23 : i64, !llvm.ptr
%24 = arith.constant 5 : i32
%25 = arith.constant 1 : i32
%26 = arith.extsi %24 : i32 to i64
%27 = arith.extsi %25 : i32 to i64
%28 = llvm.getelementptr %2[%27] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %26, %28 : i64, !llvm.ptr
%29 = arith.constant 9 : i32
%30 = arith.constant 2 : i32
%31 = arith.extsi %29 : i32 to i64
%32 = arith.extsi %30 : i32 to i64
%33 = llvm.getelementptr %2[%32] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %31, %33 : i64, !llvm.ptr
%34 = arith.constant 15 : i32
%35 = arith.constant 3 : i32
%36 = arith.extsi %34 : i32 to i64
%37 = arith.extsi %35 : i32 to i64
%38 = llvm.getelementptr %2[%37] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %36, %38 : i64, !llvm.ptr
%39 = arith.constant 21 : i32
%40 = arith.constant 4 : i32
%41 = arith.extsi %39 : i32 to i64
%42 = arith.extsi %40 : i32 to i64
%43 = llvm.getelementptr %2[%42] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %41, %43 : i64, !llvm.ptr
%44 = arith.constant 25 : i32
%45 = arith.constant 5 : i32
%46 = arith.extsi %44 : i32 to i64
%47 = arith.extsi %45 : i32 to i64
%48 = llvm.getelementptr %2[%47] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %46, %48 : i64, !llvm.ptr
%49 = arith.constant 29 : i32
%50 = arith.constant 6 : i32
%51 = arith.extsi %49 : i32 to i64
%52 = arith.extsi %50 : i32 to i64
%53 = llvm.getelementptr %2[%52] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %51, %53 : i64, !llvm.ptr
%54 = arith.constant 35 : i32
%55 = arith.constant 7 : i32
%56 = arith.extsi %54 : i32 to i64
%57 = arith.extsi %55 : i32 to i64
%58 = llvm.getelementptr %2[%57] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %56, %58 : i64, !llvm.ptr
%59 = arith.constant 39 : i32
%60 = arith.constant 8 : i32
%61 = arith.extsi %59 : i32 to i64
%62 = arith.extsi %60 : i32 to i64
%63 = llvm.getelementptr %2[%62] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %61, %63 : i64, !llvm.ptr
%64 = arith.constant 43 : i32
%65 = arith.constant 9 : i32
%66 = arith.extsi %64 : i32 to i64
%67 = arith.extsi %65 : i32 to i64
%68 = llvm.getelementptr %2[%67] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %66, %68 : i64, !llvm.ptr
%69 = arith.constant 55 : i32
%70 = arith.constant 10 : i32
%71 = arith.extsi %69 : i32 to i64
%72 = arith.extsi %70 : i32 to i64
%73 = llvm.getelementptr %2[%72] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %71, %73 : i64, !llvm.ptr
%74 = arith.constant 59 : i32
%75 = arith.constant 11 : i32
%76 = arith.extsi %74 : i32 to i64
%77 = arith.extsi %75 : i32 to i64
%78 = llvm.getelementptr %2[%77] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %76, %78 : i64, !llvm.ptr
%79 = arith.constant 63 : i32
%80 = arith.constant 12 : i32
%81 = arith.extsi %79 : i32 to i64
%82 = arith.extsi %80 : i32 to i64
%83 = llvm.getelementptr %2[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %81, %83 : i64, !llvm.ptr
%84 = arith.constant 0 : i32
%85 = arith.extsi %84 : i32 to i64
%86 = llvm.mlir.constant(1 : i64) : i64
%87 = llvm.alloca %86 x i64 : (i64) -> !llvm.ptr
llvm.store %85, %87 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%88 = arith.constant 1 : i1
cf.cond_br %88, ^bb4, ^bb5
^bb4:
%90 = llvm.load %87 : !llvm.ptr -> i64
%91 = arith.constant 5 : i32
%93 = arith.extsi %91 : i32 to i64
%92 = arith.remsi %90, %93 : i64
%94 = llvm.getelementptr %7[%92] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%89 = llvm.load %94 : !llvm.ptr -> i64
%95 = arith.constant 34 : i32
%97 = arith.extsi %95 : i32 to i64
%96 = arith.addi %89, %97 : i64
%98 = llvm.mlir.constant(1 : i64) : i64
%99 = llvm.alloca %98 x i64 : (i64) -> !llvm.ptr
llvm.store %96, %99 : i64, !llvm.ptr
%100 = llvm.load %87 : !llvm.ptr -> i64
%101 = arith.constant 13 : i32
%103 = arith.extsi %101 : i32 to i64
%102 = arith.cmpi slt, %100, %103 : i64
cf.cond_br %102, ^bb6, ^bb7
^bb6:
%105 = llvm.load %87 : !llvm.ptr -> i64
%106 = llvm.getelementptr %2[%105] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%104 = llvm.load %106 : !llvm.ptr -> i64
llvm.store %104, %99 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%107 = llvm.load %99 : !llvm.ptr -> i64
%108 = arith.cmpi sgt, %107, %1 : i64
cf.cond_br %108, ^bb9, ^bb10
^bb9:
%109 = llvm.mlir.addressof @str_0 : !llvm.ptr
%110 = llvm.load %87 : !llvm.ptr -> i64
%111 = arith.subi %1, %110 : i64
%112 = llvm.call @printf(%109, %111) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%2) : (!llvm.ptr) -> ()
func.call @free(%7) : (!llvm.ptr) -> ()
%115 = arith.constant 0 : i32
func.return %115 : i32
^bb10:
cf.br ^bb11
^bb11:
%116 = llvm.load %99 : !llvm.ptr -> i64
%117 = llvm.load %87 : !llvm.ptr -> i64
%118 = arith.constant 5 : i32
%120 = arith.extsi %118 : i32 to i64
%119 = arith.remsi %117, %120 : i64
%121 = llvm.getelementptr %7[%119] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %116, %121 : i64, !llvm.ptr
%122 = llvm.load %87 : !llvm.ptr -> i64
%123 = arith.constant 1 : i32
%125 = arith.extsi %123 : i32 to i64
%124 = arith.addi %122, %125 : i64
llvm.store %124, %87 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%126 = arith.constant 0 : i32
func.return %126 : i32
}
}