# Project Euler 458
# Permutations of Project — T(10^12) mod 10^9 via transfer matrix.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function mat_mul(a: ptr<i64>, b: ptr<i64>, out: ptr<i64>, n: i32, mod: i64) -> void {
let mut i: i32 = 0
while i < n {
let mut j: i32 = 0
while j < n {
let mut s: i64 = 0
let mut k: i32 = 0
while k < n {
let t: i128 = (a[i * n + k] as i128) * (b[k * n + j] as i128)
s = ((s as i128 + t) % (mod as i128)) as i64
if s < 0 { s = s + mod }
k = k + 1
}
out[i * n + j] = s
j = j + 1
}
i = i + 1
}
}
function mat_pow(base: ptr<i64>, exp0: i64, n: i32, mod: i64, out: ptr<i64>) -> void {
let tmp: ptr<i64> = calloc((n * n) as i64, 8)
let cur: ptr<i64> = calloc((n * n) as i64, 8)
if tmp == null || cur == null { return }
let mut i: i32 = 0
while i < n * n {
out[i] = 0
cur[i] = base[i]
i = i + 1
}
i = 0
while i < n {
out[i * n + i] = 1
i = i + 1
}
let mut exp: i64 = exp0
while exp > 0 {
if exp % 2 == 1 {
mat_mul(out, cur, tmp, n, mod)
i = 0
while i < n * n {
out[i] = tmp[i]
i = i + 1
}
}
mat_mul(cur, cur, tmp, n, mod)
i = 0
while i < n * n {
cur[i] = tmp[i]
i = i + 1
}
exp = exp / 2
}
free(tmp)
free(cur)
}
function main() -> i32 {
let N: i32 = 8
let MOD: i64 = 1000000000
let mat: ptr<i64> = calloc((N * N) as i64, 8)
let res: ptr<i64> = calloc((N * N) as i64, 8)
if mat == null || res == null { return 1 }
mat[1 * N + 0] = 7
let mut c: i32 = 1
while c <= 6 {
mat[1 * N + c] = 1
c = c + 1
}
mat[2 * N + 1] = 6
c = 2
while c <= 6 {
mat[2 * N + c] = 1
c = c + 1
}
mat[3 * N + 2] = 5
c = 3
while c <= 6 {
mat[3 * N + c] = 1
c = c + 1
}
mat[4 * N + 3] = 4
c = 4
while c <= 6 {
mat[4 * N + c] = 1
c = c + 1
}
mat[5 * N + 4] = 3
c = 5
while c <= 6 {
mat[5 * N + c] = 1
c = c + 1
}
mat[6 * N + 5] = 2
mat[6 * N + 6] = 1
mat[7 * N + 6] = 1
mat[7 * N + 7] = 7
mat_pow(mat, 1000000000000, N, MOD, res)
let with_project: i64 = res[7 * N + 0]
let mut all_strings: i64 = res[7 * N + 7]
if all_strings < with_project {
all_strings = all_strings + MOD
}
printf("%lld\n", all_strings - with_project)
free(res)
free(mat)
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 mat_mul_ptr_i64_ptr_i64_ptr_i64_i32_i64(int64_t* a, int64_t* b, int64_t* out, int32_t n, int64_t mod);
void mat_pow_ptr_i64_i64_i32_i64_ptr_i64(int64_t* base, int64_t exp0, int32_t n, int64_t mod, int64_t* out);
int32_t main(void);
void mat_mul_ptr_i64_ptr_i64_ptr_i64_i32_i64(int64_t* a, int64_t* b, int64_t* out, int32_t n, int64_t mod) {
int32_t i = 0;
while (i < n) {
int32_t j = 0;
while (j < n) {
int64_t s = 0;
int32_t k = 0;
while (k < n) {
__int128 t = (((__int128)(a[((i * n) + k)])) * ((__int128)(b[((k * n) + j)])));
s = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(s)) + t)), (((__int128)(mod))))));
if (s < 0) {
s = (s + mod);
}
k = (k + 1);
}
out[((i * n) + j)] = s;
j = (j + 1);
}
i = (i + 1);
}
}
void mat_pow_ptr_i64_i64_i32_i64_ptr_i64(int64_t* base, int64_t exp0, int32_t n, int64_t mod, int64_t* out) {
int64_t* tmp = (int64_t*)(calloc(((int64_t)((n * n))), 8));
int64_t* cur = (int64_t*)(calloc(((int64_t)((n * n))), 8));
if ((tmp == NULL || cur == NULL)) {
return;
}
int32_t i = 0;
while (i < (n * n)) {
out[i] = 0;
cur[i] = base[i];
i = (i + 1);
}
i = 0;
while (i < n) {
out[((i * n) + i)] = 1;
i = (i + 1);
}
int64_t exp = exp0;
while (exp > 0) {
if (FLOW_CHECKED_MOD((exp), (2)) == 1) {
mat_mul_ptr_i64_ptr_i64_ptr_i64_i32_i64(out, cur, tmp, n, mod);
i = 0;
while (i < (n * n)) {
out[i] = tmp[i];
i = (i + 1);
}
}
mat_mul_ptr_i64_ptr_i64_ptr_i64_i32_i64(cur, cur, tmp, n, mod);
i = 0;
while (i < (n * n)) {
cur[i] = tmp[i];
i = (i + 1);
}
exp = FLOW_CHECKED_DIV((exp), (2));
}
free(tmp);
free(cur);
}
int32_t main(void) {
int32_t N = 8;
int64_t MOD = 1000000000;
int64_t* mat = (int64_t*)(calloc(((int64_t)((N * N))), 8));
int64_t* res = (int64_t*)(calloc(((int64_t)((N * N))), 8));
if ((mat == NULL || res == NULL)) {
return 1;
}
mat[((1 * N) + 0)] = 7;
int32_t c = 1;
while (c <= 6) {
mat[((1 * N) + c)] = 1;
c = (c + 1);
}
mat[((2 * N) + 1)] = 6;
c = 2;
while (c <= 6) {
mat[((2 * N) + c)] = 1;
c = (c + 1);
}
mat[((3 * N) + 2)] = 5;
c = 3;
while (c <= 6) {
mat[((3 * N) + c)] = 1;
c = (c + 1);
}
mat[((4 * N) + 3)] = 4;
c = 4;
while (c <= 6) {
mat[((4 * N) + c)] = 1;
c = (c + 1);
}
mat[((5 * N) + 4)] = 3;
c = 5;
while (c <= 6) {
mat[((5 * N) + c)] = 1;
c = (c + 1);
}
mat[((6 * N) + 5)] = 2;
mat[((6 * N) + 6)] = 1;
mat[((7 * N) + 6)] = 1;
mat[((7 * N) + 7)] = 7;
mat_pow_ptr_i64_i64_i32_i64_ptr_i64(mat, 1000000000000, N, MOD, res);
int64_t with_project = res[((7 * N) + 0)];
int64_t all_strings = res[((7 * N) + 7)];
if (all_strings < with_project) {
all_strings = (all_strings + MOD);
}
printf("%lld\n", (all_strings - with_project));
free(res);
free(mat);
return 0;
}