# Project Euler 992
# Journey counting on a path graph modulo a prime.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function malloc(n: i64) -> ptr<void>
}
const MOD: i64 = 987898789
function mod_pow(a0: i64, e0: i64, mod: i64) -> i64 {
let mut r: i64 = 1 % mod
let mut a: i64 = a0 % mod
if a < 0 { a = a + mod }
let mut e: i64 = e0
while e > 0 {
if (e & 1) != 0 {
r = ((r as i128) * (a as i128) % (mod as i128)) as i64
}
a = ((a as i128) * (a as i128) % (mod as i128)) as i64
e = e >> 1
}
return r
}
function mod_inv(a: i64, mod: i64) -> i64 {
return mod_pow(a, mod - 2, mod)
}
# Global combinatorics arrays
let mut g_fact: ptr<i64> = null
let mut g_inv_fact: ptr<i64> = null
function build_combinatorics(limit: i64, mod: i64) -> void {
g_fact[0] = 1 % mod
let mut i: i64 = 1
while i <= limit {
g_fact[i] = ((g_fact[i - 1] as i128) * (i as i128) % (mod as i128)) as i64
i = i + 1
}
g_inv_fact[limit] = mod_inv(g_fact[limit], mod)
i = limit
while i >= 1 {
g_inv_fact[i - 1] = ((g_inv_fact[i] as i128) * (i as i128) % (mod as i128)) as i64
i = i - 1
}
}
function comb(n: i64, r: i64) -> i64 {
if r < 0 || r > n { return 0 }
return ((g_fact[n] as i128) * (g_inv_fact[r] as i128) % (MOD as i128) * (g_inv_fact[n - r] as i128) % (MOD as i128)) as i64
}
function endpoint_count(n: i64, k: i64, end: i64) -> i64 {
if n == 0 { return 1 }
# right[i] for 0 <= i < n
let right: ptr<i64> = malloc(n * 8)
if end == 0 {
right[0] = k - 1
} else {
right[0] = k
}
if n >= 2 {
if end == 1 {
right[1] = 2 - 1
} else {
right[1] = 2
}
}
let mut i: i64 = 2
while i < n {
let mut val: i64 = 1 + right[i - 2]
if end == i { val = val - 1 }
right[i] = val
i = i + 1
}
let mut ways: i64 = 1
let mut v: i64 = 1
while v < n {
let mut out_degree: i64 = k + v
if end == v { out_degree = out_degree - 1 }
if v < end {
ways = ((ways as i128) * (comb(out_degree - 1, right[v] - 1) as i128) % (MOD as i128)) as i64
} else {
if v == end {
ways = ((ways as i128) * (comb(out_degree, right[v]) as i128) % (MOD as i128)) as i64
} else {
ways = ((ways as i128) * (comb(out_degree - 1, right[v]) as i128) % (MOD as i128)) as i64
}
}
v = v + 1
}
free(right)
return ways
}
function journey_count(n: i64, k: i64) -> i64 {
let mut total: i64 = 0
let mut end: i64 = 0
while end <= n {
total = total + endpoint_count(n, k, end)
if total >= MOD { total = total - MOD }
end = end + 1
}
return total
}
function main() -> i32 {
let n: i64 = 500
let ks: array<i64, 5> = [1, 10, 100, 1000, 10000]
let mut max_k: i64 = 0
let mut i: i64 = 0
while i < 5 {
if ks[i] > max_k { max_k = ks[i] }
i = i + 1
}
g_fact = malloc((max_k + n + 1) * 8)
g_inv_fact = malloc((max_k + n + 1) * 8)
build_combinatorics(max_k + n, MOD)
let mut answer: i64 = 0
i = 0
while i < 5 {
answer = answer + journey_count(n, ks[i])
if answer >= MOD { answer = answer - MOD }
i = i + 1
}
printf("%lld\n", answer)
free(g_fact)
free(g_inv_fact)
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 mod_pow_i64_i64_i64(int64_t a0, int64_t e0, int64_t mod);
int64_t mod_inv_i64_i64(int64_t a, int64_t mod);
void build_combinatorics_i64_i64(int64_t limit, int64_t mod);
int64_t comb_i64_i64(int64_t n, int64_t r);
int64_t endpoint_count_i64_i64_i64(int64_t n, int64_t k, int64_t end);
int64_t journey_count_i64_i64(int64_t n, int64_t k);
int32_t main(void);
static const int64_t MOD = 987898789;
/* Module statics */
static int64_t* g_fact = NULL;
static int64_t* g_inv_fact = NULL;
int64_t mod_pow_i64_i64_i64(int64_t a0, int64_t e0, int64_t mod) {
int64_t r = FLOW_CHECKED_MOD((1), (mod));
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
if (a < 0) {
a = (a + mod);
}
int64_t e = e0;
while (e > 0) {
if ((e & 1) != 0) {
r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(a)))), (((__int128)(mod))))));
}
a = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(a)) * ((__int128)(a)))), (((__int128)(mod))))));
e = FLOW_CHECKED_SHR((e), (1));
}
return r;
}
int64_t mod_inv_i64_i64(int64_t a, int64_t mod) {
return mod_pow_i64_i64_i64(a, (mod - 2), mod);
}
void build_combinatorics_i64_i64(int64_t limit, int64_t mod) {
g_fact[0] = FLOW_CHECKED_MOD((1), (mod));
int64_t i = 1;
while (i <= limit) {
g_fact[i] = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(g_fact[(i - 1)])) * ((__int128)(i)))), (((__int128)(mod))))));
i = (i + 1);
}
g_inv_fact[limit] = mod_inv_i64_i64(g_fact[limit], mod);
i = limit;
while (i >= 1) {
g_inv_fact[(i - 1)] = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(g_inv_fact[i])) * ((__int128)(i)))), (((__int128)(mod))))));
i = (i - 1);
}
}
int64_t comb_i64_i64(int64_t n, int64_t r) {
if ((r < 0 || r > n)) {
return 0;
}
return ((int64_t)(FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((((__int128)(g_fact[n])) * ((__int128)(g_inv_fact[r])))), (((__int128)(MOD)))) * ((__int128)(g_inv_fact[(n - r)])))), (((__int128)(MOD))))));
}
int64_t endpoint_count_i64_i64_i64(int64_t n, int64_t k, int64_t end) {
if (n == 0) {
return 1;
}
int64_t* right = (int64_t*)(malloc((n * 8)));
if (end == 0) {
right[0] = (k - 1);
} else {
right[0] = k;
}
if (n >= 2) {
if (end == 1) {
right[1] = (2 - 1);
} else {
right[1] = 2;
}
}
int64_t i = 2;
while (i < n) {
int64_t val = (1 + right[(i - 2)]);
if (end == i) {
val = (val - 1);
}
right[i] = val;
i = (i + 1);
}
int64_t ways = 1;
int64_t v = 1;
while (v < n) {
int64_t out_degree = (k + v);
if (end == v) {
out_degree = (out_degree - 1);
}
if (v < end) {
ways = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(ways)) * ((__int128)(comb_i64_i64((out_degree - 1), (right[v] - 1)))))), (((__int128)(MOD))))));
} else {
if (v == end) {
ways = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(ways)) * ((__int128)(comb_i64_i64(out_degree, right[v]))))), (((__int128)(MOD))))));
} else {
ways = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(ways)) * ((__int128)(comb_i64_i64((out_degree - 1), right[v]))))), (((__int128)(MOD))))));
}
}
v = (v + 1);
}
free(right);
return ways;
}
int64_t journey_count_i64_i64(int64_t n, int64_t k) {
int64_t total = 0;
int64_t end = 0;
while (end <= n) {
total = (total + endpoint_count_i64_i64_i64(n, k, end));
if (total >= MOD) {
total = (total - MOD);
}
end = (end + 1);
}
return total;
}
int32_t main(void) {
int64_t n = 500;
int64_t ks[5] = { 1, 10, 100, 1000, 10000 };
int64_t max_k = 0;
int64_t i = 0;
while (i < 5) {
if ((((unsigned)(i) < 5) ? ks[i] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(i), 5), flow_fault_handler("array index out of bounds"), ks[0])) > max_k) {
max_k = (((unsigned)(i) < 5) ? ks[i] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(i), 5), flow_fault_handler("array index out of bounds"), ks[0]));
}
i = (i + 1);
}
g_fact = malloc((((max_k + n) + 1) * 8));
g_inv_fact = malloc((((max_k + n) + 1) * 8));
build_combinatorics_i64_i64((max_k + n), MOD);
int64_t answer = 0;
i = 0;
while (i < 5) {
answer = (answer + journey_count_i64_i64(n, (((unsigned)(i) < 5) ? ks[i] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(i), 5), flow_fault_handler("array index out of bounds"), ks[0]))));
if (answer >= MOD) {
answer = (answer - MOD);
}
i = (i + 1);
}
printf("%lld\n", answer);
free(g_fact);
free(g_inv_fact);
return 0;
}