# Project Euler 412
# LC(10000, 5000) mod 76543217 (gnomon numbering count).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base0 % mod
let mut e: i64 = exp0
while e > 0 {
if e % 2 == 1 {
let t: i128 = (r as i128) * (b as i128) % (mod as i128)
r = t as i64
}
let t2: i128 = (b as i128) * (b as i128) % (mod as i128)
b = t2 as i64
e = e / 2
}
return r
}
function main() -> i32 {
let M: i64 = 10000
let N: i64 = 5000
let MOD: i64 = 76543217
let K: i64 = M - N
let max_small: i64 = 2 * M - 1
let fac: ptr<i64> = calloc(max_small + 1, 8)
let invfac: ptr<i64> = calloc(max_small + 1, 8)
if fac == null || invfac == null { return 1 }
fac[0] = 1
let mut i: i64 = 1
while i <= max_small {
fac[i] = ((fac[i - 1] as i128) * (i as i128) % (MOD as i128)) as i64
i = i + 1
}
invfac[max_small] = modpow(fac[max_small], MOD - 2, MOD)
i = max_small
while i > 0 {
invfac[i - 1] = ((invfac[i] as i128) * (i as i128) % (MOD as i128)) as i64
i = i - 1
}
let mut v_a: i64 = 1
i = 1
while i < N {
v_a = ((v_a as i128) * (fac[i] as i128) % (MOD as i128)) as i64
i = i + 1
}
let mut v_b: i64 = 1
i = 1
while i < K {
v_b = ((v_b as i128) * (fac[i] as i128) % (MOD as i128)) as i64
i = i + 1
}
let mut v_ab: i64 = 1
let mut t: i64 = 0
while t < K {
v_ab = ((v_ab as i128) * (fac[2 * N + t] as i128) % (MOD as i128)) as i64
v_ab = ((v_ab as i128) * (invfac[N + t] as i128) % (MOD as i128)) as i64
t = t + 1
}
let mut d_inv: i64 = 1
let mut a: i64 = K
while a < M {
d_inv = ((d_inv as i128) * (invfac[a] as i128) % (MOD as i128)) as i64
a = a + 1
}
let mut b: i64 = M + N
while b < 2 * M {
d_inv = ((d_inv as i128) * (invfac[b] as i128) % (MOD as i128)) as i64
b = b + 1
}
let cells: i64 = M * M - N * N
let mut total_fact: i64 = 1
i = 2
while i <= cells {
total_fact = ((total_fact as i128) * (i as i128) % (MOD as i128)) as i64
i = i + 1
}
let mut res: i64 = total_fact
res = ((res as i128) * (v_a as i128) % (MOD as i128)) as i64
res = ((res as i128) * (v_b as i128) % (MOD as i128)) as i64
res = ((res as i128) * (v_ab as i128) % (MOD as i128)) as i64
res = ((res as i128) * (d_inv as i128) % (MOD as i128)) as i64
printf("%lld\n", res)
free(invfac)
free(fac)
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 modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int32_t main(void);
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base0), (mod));
int64_t e = exp0;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
__int128 t = FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))));
r = ((int64_t)(t));
}
__int128 t2 = FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))));
b = ((int64_t)(t2));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int32_t main(void) {
int64_t M = 10000;
int64_t N = 5000;
int64_t MOD = 76543217;
int64_t K = (M - N);
int64_t max_small = ((2 * M) - 1);
int64_t* fac = (int64_t*)(calloc((max_small + 1), 8));
int64_t* invfac = (int64_t*)(calloc((max_small + 1), 8));
if ((fac == NULL || invfac == NULL)) {
return 1;
}
fac[0] = 1;
int64_t i = 1;
while (i <= max_small) {
fac[i] = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(fac[(i - 1)])) * ((__int128)(i)))), (((__int128)(MOD))))));
i = (i + 1);
}
invfac[max_small] = modpow_i64_i64_i64(fac[max_small], (MOD - 2), MOD);
i = max_small;
while (i > 0) {
invfac[(i - 1)] = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(invfac[i])) * ((__int128)(i)))), (((__int128)(MOD))))));
i = (i - 1);
}
int64_t v_a = 1;
i = 1;
while (i < N) {
v_a = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(v_a)) * ((__int128)(fac[i])))), (((__int128)(MOD))))));
i = (i + 1);
}
int64_t v_b = 1;
i = 1;
while (i < K) {
v_b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(v_b)) * ((__int128)(fac[i])))), (((__int128)(MOD))))));
i = (i + 1);
}
int64_t v_ab = 1;
int64_t t = 0;
while (t < K) {
v_ab = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(v_ab)) * ((__int128)(fac[((2 * N) + t)])))), (((__int128)(MOD))))));
v_ab = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(v_ab)) * ((__int128)(invfac[(N + t)])))), (((__int128)(MOD))))));
t = (t + 1);
}
int64_t d_inv = 1;
int64_t a = K;
while (a < M) {
d_inv = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(d_inv)) * ((__int128)(invfac[a])))), (((__int128)(MOD))))));
a = (a + 1);
}
int64_t b = (M + N);
while (b < (2 * M)) {
d_inv = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(d_inv)) * ((__int128)(invfac[b])))), (((__int128)(MOD))))));
b = (b + 1);
}
int64_t cells = ((M * M) - (N * N));
int64_t total_fact = 1;
i = 2;
while (i <= cells) {
total_fact = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(total_fact)) * ((__int128)(i)))), (((__int128)(MOD))))));
i = (i + 1);
}
int64_t res = total_fact;
res = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(res)) * ((__int128)(v_a)))), (((__int128)(MOD))))));
res = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(res)) * ((__int128)(v_b)))), (((__int128)(MOD))))));
res = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(res)) * ((__int128)(v_ab)))), (((__int128)(MOD))))));
res = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(res)) * ((__int128)(d_inv)))), (((__int128)(MOD))))));
printf("%lld\n", res);
free(invfac);
free(fac);
return 0;
}