# Project Euler 588
# Quintinomial Coefficients
# Q(k) = # odd coeffs in (1+x+x^2+x^3+x^4)^k; sum Q(10^m) for m=1..18.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function build_trans(active: bool, trans: ptr<i64>) -> void {
let mut bit: i64 = 0
while bit < 2 {
let mut state: i64 = 0
while state < 16 {
let mut next_state: i64 = 0
let mut c: i64 = 0
while c < 4 {
if ((state >> c) & 1) != 0 {
let mut d: i64 = 0
let mut d_hi: i64 = 0
if active { d_hi = 4 }
while d <= d_hi {
let s: i64 = c + d
if (s & 1) == bit {
let nc: i64 = s >> 1
next_state = next_state ^ (1 << nc)
}
d = d + 1
}
}
c = c + 1
}
trans[bit * 16 + state] = next_state
state = state + 1
}
bit = bit + 1
}
}
function bit_length(k0: i64) -> i64 {
let mut k: i64 = k0
let mut n: i64 = 0
while k > 0 {
n = n + 1
k = k >> 1
}
return n
}
function Q(k: i64, t_active: ptr<i64>, t_inactive: ptr<i64>) -> i64 {
let L: i64 = bit_length(k) + 3
let dp: ptr<i64> = calloc(16, 8)
let ndp: ptr<i64> = calloc(16, 8)
if dp == null || ndp == null { return -1 }
dp[1] = 1
let mut i: i64 = 0
while i < L {
let mut trans: ptr<i64> = t_inactive
if ((k >> i) & 1) != 0 {
trans = t_active
}
let mut s: i64 = 0
while s < 16 {
ndp[s] = 0
s = s + 1
}
s = 0
while s < 16 {
let cnt: i64 = dp[s]
if cnt != 0 {
ndp[trans[s]] = ndp[trans[s]] + cnt
ndp[trans[16 + s]] = ndp[trans[16 + s]] + cnt
}
s = s + 1
}
s = 0
while s < 16 {
dp[s] = ndp[s]
s = s + 1
}
i = i + 1
}
let mut ans: i64 = 0
let mut s2: i64 = 0
while s2 < 16 {
if (s2 & 1) != 0 {
ans = ans + dp[s2]
}
s2 = s2 + 1
}
free(dp)
free(ndp)
return ans
}
function main() -> i32 {
let t_active: ptr<i64> = calloc(32, 8)
let t_inactive: ptr<i64> = calloc(32, 8)
if t_active == null || t_inactive == null { return 1 }
build_trans(true, t_active)
build_trans(false, t_inactive)
let mut total: i64 = 0
let mut k: i64 = 10
let mut m: i64 = 0
while m < 18 {
total = total + Q(k, t_active, t_inactive)
k = k * 10
m = m + 1
}
printf("%lld\n", total)
free(t_active)
free(t_inactive)
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; }
void build_trans_bool_ptr_i64(bool active, int64_t* trans);
int64_t bit_length_i64(int64_t k0);
int64_t Q_i64_ptr_i64_ptr_i64(int64_t k, int64_t* t_active, int64_t* t_inactive);
int32_t main(void);
void build_trans_bool_ptr_i64(bool active, int64_t* trans) {
int64_t bit = 0;
while (bit < 2) {
int64_t state = 0;
while (state < 16) {
int64_t next_state = 0;
int64_t c = 0;
while (c < 4) {
if ((FLOW_CHECKED_SHR((state), (c)) & 1) != 0) {
int64_t d = 0;
int64_t d_hi = 0;
if (active) {
d_hi = 4;
}
while (d <= d_hi) {
int64_t s = (c + d);
if ((s & 1) == bit) {
int64_t nc = FLOW_CHECKED_SHR((s), (1));
next_state = (next_state ^ FLOW_CHECKED_SHL((1), (nc)));
}
d = (d + 1);
}
}
c = (c + 1);
}
trans[((bit * 16) + state)] = next_state;
state = (state + 1);
}
bit = (bit + 1);
}
}
int64_t bit_length_i64(int64_t k0) {
int64_t k = k0;
int64_t n = 0;
while (k > 0) {
n = (n + 1);
k = FLOW_CHECKED_SHR((k), (1));
}
return n;
}
int64_t Q_i64_ptr_i64_ptr_i64(int64_t k, int64_t* t_active, int64_t* t_inactive) {
int64_t L = (bit_length_i64(k) + 3);
int64_t* dp = (int64_t*)(calloc(16, 8));
int64_t* ndp = (int64_t*)(calloc(16, 8));
if ((dp == NULL || ndp == NULL)) {
return (-1);
}
dp[1] = 1;
int64_t i = 0;
while (i < L) {
int64_t* trans = (int64_t*)(t_inactive);
if ((FLOW_CHECKED_SHR((k), (i)) & 1) != 0) {
trans = t_active;
}
int64_t s = 0;
while (s < 16) {
ndp[s] = 0;
s = (s + 1);
}
s = 0;
while (s < 16) {
int64_t cnt = dp[s];
if (cnt != 0) {
ndp[trans[s]] = (ndp[trans[s]] + cnt);
ndp[trans[(16 + s)]] = (ndp[trans[(16 + s)]] + cnt);
}
s = (s + 1);
}
s = 0;
while (s < 16) {
dp[s] = ndp[s];
s = (s + 1);
}
i = (i + 1);
}
int64_t ans = 0;
int64_t s2 = 0;
while (s2 < 16) {
if ((s2 & 1) != 0) {
ans = (ans + dp[s2]);
}
s2 = (s2 + 1);
}
free(dp);
free(ndp);
return ans;
}
int32_t main(void) {
int64_t* t_active = (int64_t*)(calloc(32, 8));
int64_t* t_inactive = (int64_t*)(calloc(32, 8));
if ((t_active == NULL || t_inactive == NULL)) {
return 1;
}
build_trans_bool_ptr_i64(1, t_active);
build_trans_bool_ptr_i64(0, t_inactive);
int64_t total = 0;
int64_t k = 10;
int64_t m = 0;
while (m < 18) {
total = (total + Q_i64_ptr_i64_ptr_i64(k, t_active, t_inactive));
k = (k * 10);
m = (m + 1);
}
printf("%lld\n", total);
free(t_active);
free(t_inactive);
return 0;
}