Problem 191
Prize strings of length 30: no 3 consecutive A, at most one L.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^3) | O(n * s) |
| Space complexity | O(n) | O(s) |
| Approach | Flow solution | DP over states |
| Verdict | Unknown |
Flow source
# Project Euler 191
# Prize strings of length 30: no 3 consecutive A, at most one L.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
# dp[day+1][late][abs_streak]
let dp: ptr<i64> = calloc(31 * 2 * 3, 8)
if dp == null { return 1 }
dp[0 * 2 * 3 + 0 * 3 + 0] = 1
let mut day: i32 = 0
while day < 30 {
let mut late: i32 = 0
while late < 2 {
let mut astr: i32 = 0
while astr < 3 {
let v: i64 = dp[day * 2 * 3 + late * 3 + astr]
if v != 0 {
# O
dp[(day + 1) * 2 * 3 + late * 3 + 0] = dp[(day + 1) * 2 * 3 + late * 3 + 0] + v
# L
if late == 0 {
dp[(day + 1) * 2 * 3 + 1 * 3 + 0] = dp[(day + 1) * 2 * 3 + 1 * 3 + 0] + v
}
# A
if astr < 2 {
dp[(day + 1) * 2 * 3 + late * 3 + (astr + 1)] = dp[(day + 1) * 2 * 3 + late * 3 + (astr + 1)] + v
}
}
astr = astr + 1
}
late = late + 1
}
day = day + 1
}
let mut total: i64 = 0
let mut late2: i32 = 0
while late2 < 2 {
let mut astr2: i32 = 0
while astr2 < 3 {
total = total + dp[30 * 2 * 3 + late2 * 3 + astr2]
astr2 = astr2 + 1
}
late2 = late2 + 1
}
printf("%lld\n", total)
free(dp)
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* dp = (int64_t*)(calloc(((31 * 2) * 3), 8));
if (dp == NULL) {
return 1;
}
dp[((((0 * 2) * 3) + (0 * 3)) + 0)] = 1;
int32_t day = 0;
while (day < 30) {
int32_t late = 0;
while (late < 2) {
int32_t astr = 0;
while (astr < 3) {
int64_t v = dp[((((day * 2) * 3) + (late * 3)) + astr)];
if (v != 0) {
dp[(((((day + 1) * 2) * 3) + (late * 3)) + 0)] = (dp[(((((day + 1) * 2) * 3) + (late * 3)) + 0)] + v);
if (late == 0) {
dp[(((((day + 1) * 2) * 3) + (1 * 3)) + 0)] = (dp[(((((day + 1) * 2) * 3) + (1 * 3)) + 0)] + v);
}
if (astr < 2) {
dp[(((((day + 1) * 2) * 3) + (late * 3)) + (astr + 1))] = (dp[(((((day + 1) * 2) * 3) + (late * 3)) + (astr + 1))] + v);
}
}
astr = (astr + 1);
}
late = (late + 1);
}
day = (day + 1);
}
int64_t total = 0;
int32_t late2 = 0;
while (late2 < 2) {
int32_t astr2 = 0;
while (astr2 < 3) {
total = (total + dp[((((30 * 2) * 3) + (late2 * 3)) + astr2)]);
astr2 = (astr2 + 1);
}
late2 = (late2 + 1);
}
printf("%lld\n", total);
free(dp);
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 {
%1 = arith.constant 31 : i32
%2 = arith.constant 2 : i32
%3 = arith.muli %1, %2 : i32
%4 = arith.constant 3 : i32
%5 = arith.muli %3, %4 : i32
%6 = arith.constant 8 : i32
%7 = arith.extsi %5 : i32 to i64
%8 = arith.extsi %6 : i32 to i64
%0 = func.call @calloc(%7, %8) : (i64, i64) -> !llvm.ptr
%9 = llvm.mlir.zero : !llvm.ptr
%10 = llvm.icmp "eq" %0, %9 : !llvm.ptr
cf.cond_br %10, ^bb0, ^bb1
^bb0:
%11 = arith.constant 1 : i32
func.return %11 : i32
^bb1:
cf.br ^bb2
^bb2:
%12 = arith.constant 1 : i32
%13 = arith.constant 0 : i32
%14 = arith.constant 2 : i32
%15 = arith.muli %13, %14 : i32
%16 = arith.constant 3 : i32
%17 = arith.muli %15, %16 : i32
%18 = arith.constant 0 : i32
%19 = arith.constant 3 : i32
%20 = arith.muli %18, %19 : i32
%21 = arith.addi %17, %20 : i32
%22 = arith.constant 0 : i32
%23 = arith.addi %21, %22 : i32
%24 = arith.extsi %12 : i32 to i64
%25 = arith.extsi %23 : i32 to i64
%26 = llvm.getelementptr %0[%25] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %24, %26 : i64, !llvm.ptr
%27 = arith.constant 0 : i32
%28 = llvm.mlir.constant(1 : i64) : i64
%29 = llvm.alloca %28 x i32 : (i64) -> !llvm.ptr
llvm.store %27, %29 : i32, !llvm.ptr
cf.br ^bb3
^bb3:
%30 = llvm.load %29 : !llvm.ptr -> i32
%31 = arith.constant 30 : i32
%32 = arith.cmpi slt, %30, %31 : i32
cf.cond_br %32, ^bb4, ^bb5
^bb4:
%33 = arith.constant 0 : i32
%34 = llvm.mlir.constant(1 : i64) : i64
%35 = llvm.alloca %34 x i32 : (i64) -> !llvm.ptr
llvm.store %33, %35 : i32, !llvm.ptr
cf.br ^bb6
^bb6:
%36 = llvm.load %35 : !llvm.ptr -> i32
%37 = arith.constant 2 : i32
%38 = arith.cmpi slt, %36, %37 : i32
cf.cond_br %38, ^bb7, ^bb8
^bb7:
%39 = arith.constant 0 : i32
%40 = llvm.mlir.constant(1 : i64) : i64
%41 = llvm.alloca %40 x i32 : (i64) -> !llvm.ptr
llvm.store %39, %41 : i32, !llvm.ptr
cf.br ^bb9
^bb9:
%42 = llvm.load %41 : !llvm.ptr -> i32
%43 = arith.constant 3 : i32
%44 = arith.cmpi slt, %42, %43 : i32
cf.cond_br %44, ^bb10, ^bb11
^bb10:
%46 = llvm.load %29 : !llvm.ptr -> i32
%47 = arith.constant 2 : i32
%48 = arith.muli %46, %47 : i32
%49 = arith.constant 3 : i32
%50 = arith.muli %48, %49 : i32
%51 = llvm.load %35 : !llvm.ptr -> i32
%52 = arith.constant 3 : i32
%53 = arith.muli %51, %52 : i32
%54 = arith.addi %50, %53 : i32
%55 = llvm.load %41 : !llvm.ptr -> i32
%56 = arith.addi %54, %55 : i32
%57 = arith.extsi %56 : i32 to i64
%58 = llvm.getelementptr %0[%57] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%45 = llvm.load %58 : !llvm.ptr -> i64
%59 = arith.constant 0 : i32
%61 = arith.extsi %59 : i32 to i64
%60 = arith.cmpi ne, %45, %61 : i64
cf.cond_br %60, ^bb12, ^bb13
^bb12:
%63 = llvm.load %29 : !llvm.ptr -> i32
%64 = arith.constant 1 : i32
%65 = arith.addi %63, %64 : i32
%66 = arith.constant 2 : i32
%67 = arith.muli %65, %66 : i32
%68 = arith.constant 3 : i32
%69 = arith.muli %67, %68 : i32
%70 = llvm.load %35 : !llvm.ptr -> i32
%71 = arith.constant 3 : i32
%72 = arith.muli %70, %71 : i32
%73 = arith.addi %69, %72 : i32
%74 = arith.constant 0 : i32
%75 = arith.addi %73, %74 : i32
%76 = arith.extsi %75 : i32 to i64
%77 = llvm.getelementptr %0[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%62 = llvm.load %77 : !llvm.ptr -> i64
%78 = arith.addi %62, %45 : i64
%79 = llvm.load %29 : !llvm.ptr -> i32
%80 = arith.constant 1 : i32
%81 = arith.addi %79, %80 : i32
%82 = arith.constant 2 : i32
%83 = arith.muli %81, %82 : i32
%84 = arith.constant 3 : i32
%85 = arith.muli %83, %84 : i32
%86 = llvm.load %35 : !llvm.ptr -> i32
%87 = arith.constant 3 : i32
%88 = arith.muli %86, %87 : i32
%89 = arith.addi %85, %88 : i32
%90 = arith.constant 0 : i32
%91 = arith.addi %89, %90 : i32
%92 = arith.extsi %91 : i32 to i64
%93 = llvm.getelementptr %0[%92] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %78, %93 : i64, !llvm.ptr
%94 = llvm.load %35 : !llvm.ptr -> i32
%95 = arith.constant 0 : i32
%96 = arith.cmpi eq, %94, %95 : i32
cf.cond_br %96, ^bb15, ^bb16
^bb15:
%98 = llvm.load %29 : !llvm.ptr -> i32
%99 = arith.constant 1 : i32
%100 = arith.addi %98, %99 : i32
%101 = arith.constant 2 : i32
%102 = arith.muli %100, %101 : i32
%103 = arith.constant 3 : i32
%104 = arith.muli %102, %103 : i32
%105 = arith.constant 1 : i32
%106 = arith.constant 3 : i32
%107 = arith.muli %105, %106 : i32
%108 = arith.addi %104, %107 : i32
%109 = arith.constant 0 : i32
%110 = arith.addi %108, %109 : i32
%111 = arith.extsi %110 : i32 to i64
%112 = llvm.getelementptr %0[%111] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%97 = llvm.load %112 : !llvm.ptr -> i64
%113 = arith.addi %97, %45 : i64
%114 = llvm.load %29 : !llvm.ptr -> i32
%115 = arith.constant 1 : i32
%116 = arith.addi %114, %115 : i32
%117 = arith.constant 2 : i32
%118 = arith.muli %116, %117 : i32
%119 = arith.constant 3 : i32
%120 = arith.muli %118, %119 : i32
%121 = arith.constant 1 : i32
%122 = arith.constant 3 : i32
%123 = arith.muli %121, %122 : i32
%124 = arith.addi %120, %123 : i32
%125 = arith.constant 0 : i32
%126 = arith.addi %124, %125 : i32
%127 = arith.extsi %126 : i32 to i64
%128 = llvm.getelementptr %0[%127] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %113, %128 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%129 = llvm.load %41 : !llvm.ptr -> i32
%130 = arith.constant 2 : i32
%131 = arith.cmpi slt, %129, %130 : i32
cf.cond_br %131, ^bb18, ^bb19
^bb18:
%133 = llvm.load %29 : !llvm.ptr -> i32
%134 = arith.constant 1 : i32
%135 = arith.addi %133, %134 : i32
%136 = arith.constant 2 : i32
%137 = arith.muli %135, %136 : i32
%138 = arith.constant 3 : i32
%139 = arith.muli %137, %138 : i32
%140 = llvm.load %35 : !llvm.ptr -> i32
%141 = arith.constant 3 : i32
%142 = arith.muli %140, %141 : i32
%143 = arith.addi %139, %142 : i32
%144 = llvm.load %41 : !llvm.ptr -> i32
%145 = arith.constant 1 : i32
%146 = arith.addi %144, %145 : i32
%147 = arith.addi %143, %146 : i32
%148 = arith.extsi %147 : i32 to i64
%149 = llvm.getelementptr %0[%148] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%132 = llvm.load %149 : !llvm.ptr -> i64
%150 = arith.addi %132, %45 : i64
%151 = llvm.load %29 : !llvm.ptr -> i32
%152 = arith.constant 1 : i32
%153 = arith.addi %151, %152 : i32
%154 = arith.constant 2 : i32
%155 = arith.muli %153, %154 : i32
%156 = arith.constant 3 : i32
%157 = arith.muli %155, %156 : i32
%158 = llvm.load %35 : !llvm.ptr -> i32
%159 = arith.constant 3 : i32
%160 = arith.muli %158, %159 : i32
%161 = arith.addi %157, %160 : i32
%162 = llvm.load %41 : !llvm.ptr -> i32
%163 = arith.constant 1 : i32
%164 = arith.addi %162, %163 : i32
%165 = arith.addi %161, %164 : i32
%166 = arith.extsi %165 : i32 to i64
%167 = llvm.getelementptr %0[%166] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %150, %167 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%168 = llvm.load %41 : !llvm.ptr -> i32
%169 = arith.constant 1 : i32
%170 = arith.addi %168, %169 : i32
llvm.store %170, %41 : i32, !llvm.ptr
cf.br ^bb9
^bb11:
%171 = llvm.load %35 : !llvm.ptr -> i32
%172 = arith.constant 1 : i32
%173 = arith.addi %171, %172 : i32
llvm.store %173, %35 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%174 = llvm.load %29 : !llvm.ptr -> i32
%175 = arith.constant 1 : i32
%176 = arith.addi %174, %175 : i32
llvm.store %176, %29 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%177 = arith.constant 0 : i32
%178 = arith.extsi %177 : i32 to i64
%179 = llvm.mlir.constant(1 : i64) : i64
%180 = llvm.alloca %179 x i64 : (i64) -> !llvm.ptr
llvm.store %178, %180 : i64, !llvm.ptr
%181 = arith.constant 0 : i32
%182 = llvm.mlir.constant(1 : i64) : i64
%183 = llvm.alloca %182 x i32 : (i64) -> !llvm.ptr
llvm.store %181, %183 : i32, !llvm.ptr
cf.br ^bb21
^bb21:
%184 = llvm.load %183 : !llvm.ptr -> i32
%185 = arith.constant 2 : i32
%186 = arith.cmpi slt, %184, %185 : i32
cf.cond_br %186, ^bb22, ^bb23
^bb22:
%187 = arith.constant 0 : i32
%188 = llvm.mlir.constant(1 : i64) : i64
%189 = llvm.alloca %188 x i32 : (i64) -> !llvm.ptr
llvm.store %187, %189 : i32, !llvm.ptr
cf.br ^bb24
^bb24:
%190 = llvm.load %189 : !llvm.ptr -> i32
%191 = arith.constant 3 : i32
%192 = arith.cmpi slt, %190, %191 : i32
cf.cond_br %192, ^bb25, ^bb26
^bb25:
%193 = llvm.load %180 : !llvm.ptr -> i64
%195 = arith.constant 30 : i32
%196 = arith.constant 2 : i32
%197 = arith.muli %195, %196 : i32
%198 = arith.constant 3 : i32
%199 = arith.muli %197, %198 : i32
%200 = llvm.load %183 : !llvm.ptr -> i32
%201 = arith.constant 3 : i32
%202 = arith.muli %200, %201 : i32
%203 = arith.addi %199, %202 : i32
%204 = llvm.load %189 : !llvm.ptr -> i32
%205 = arith.addi %203, %204 : i32
%206 = arith.extsi %205 : i32 to i64
%207 = llvm.getelementptr %0[%206] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%194 = llvm.load %207 : !llvm.ptr -> i64
%208 = arith.addi %193, %194 : i64
llvm.store %208, %180 : i64, !llvm.ptr
%209 = llvm.load %189 : !llvm.ptr -> i32
%210 = arith.constant 1 : i32
%211 = arith.addi %209, %210 : i32
llvm.store %211, %189 : i32, !llvm.ptr
cf.br ^bb24
^bb26:
%212 = llvm.load %183 : !llvm.ptr -> i32
%213 = arith.constant 1 : i32
%214 = arith.addi %212, %213 : i32
llvm.store %214, %183 : i32, !llvm.ptr
cf.br ^bb21
^bb23:
%215 = llvm.mlir.addressof @str_0 : !llvm.ptr
%216 = llvm.load %180 : !llvm.ptr -> i64
%217 = llvm.call @printf(%215, %216) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%0) : (!llvm.ptr) -> ()
%219 = arith.constant 0 : i32
func.return %219 : i32
}
}