# Project Euler 408
# Admissible paths (0,0)->(n,n) avoiding square-sum obstacles, mod 10^9+7.
import euler.nt { isqrt }
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 {
r = ((r as i128) * (b as i128) % (mod as i128)) as i64
}
b = ((b as i128) * (b as i128) % (mod as i128)) as i64
e = e / 2
}
return r
}
function main() -> i32 {
let N: i64 = 10000000
let MOD: i64 = 1000000007
let m: i64 = isqrt(N)
let max_sum: i64 = isqrt(2 * N)
# Mark which numbers are perfect squares up to 2N
let is_sq: ptr<i8> = calloc(2 * N + 1, 1)
if is_sq == null { return 1 }
let mut i: i64 = 1
while i <= max_sum {
is_sq[i * i] = 1
i = i + 1
}
let xs: ptr<i64> = calloc(20000, 8)
let ys: ptr<i64> = calloc(20000, 8)
if xs == null || ys == null { return 1 }
let mut k: i64 = 0
let mut a: i64 = 1
while a <= m {
let a2: i64 = a * a
let mut b: i64 = 1
while b <= m {
let b2: i64 = b * b
if is_sq[a2 + b2] != 0 {
xs[k] = a2
ys[k] = b2
k = k + 1
}
b = b + 1
}
a = a + 1
}
# sort by x+y, then x (shell sort)
let mut gap: i64 = k / 2
while gap > 0 {
i = gap
while i < k {
let mut j: i64 = i
while j >= gap {
let s1: i64 = xs[j] + ys[j]
let s0: i64 = xs[j - gap] + ys[j - gap]
if s1 < s0 || (s1 == s0 && xs[j] < xs[j - gap]) {
let tx: i64 = xs[j]
xs[j] = xs[j - gap]
xs[j - gap] = tx
let ty: i64 = ys[j]
ys[j] = ys[j - gap]
ys[j - gap] = ty
j = j - gap
} else {
break
}
}
i = i + 1
}
gap = gap / 2
}
let limit: i64 = 2 * N
let fact: ptr<i64> = calloc(limit + 1, 8)
let invfact: ptr<i64> = calloc(limit + 1, 8)
if fact == null || invfact == null { return 1 }
fact[0] = 1
i = 1
while i <= limit {
fact[i] = ((fact[i - 1] as i128) * (i as i128) % (MOD as i128)) as i64
i = i + 1
}
invfact[limit] = modpow(fact[limit], MOD - 2, MOD)
i = limit
while i > 0 {
invfact[i - 1] = ((invfact[i] as i128) * (i as i128) % (MOD as i128)) as i64
i = i - 1
}
let ways: ptr<i64> = calloc(k, 8)
if ways == null { return 1 }
i = 0
while i < k {
let xi: i64 = xs[i]
let yi: i64 = ys[i]
let mut total: i64 = ((fact[xi + yi] as i128) * (invfact[xi] as i128) % (MOD as i128) * (invfact[yi] as i128) % (MOD as i128)) as i64
let mut j: i64 = 0
while j < i {
let xj: i64 = xs[j]
let yj: i64 = ys[j]
if xj <= xi && yj <= yi {
let dx: i64 = xi - xj
let dy: i64 = yi - yj
let w: i64 = ((fact[dx + dy] as i128) * (invfact[dx] as i128) % (MOD as i128) * (invfact[dy] as i128) % (MOD as i128)) as i64
total = (total - ((ways[j] as i128) * (w as i128) % (MOD as i128)) as i64) % MOD
}
j = j + 1
}
if total < 0 { total = total + MOD }
ways[i] = total
i = i + 1
}
let mut total_paths: i64 = ((fact[2 * N] as i128) * (invfact[N] as i128) % (MOD as i128) * (invfact[N] as i128) % (MOD as i128)) as i64
i = 0
while i < k {
let xi: i64 = xs[i]
let yi: i64 = ys[i]
let w: i64 = ((fact[N - xi + N - yi] as i128) * (invfact[N - xi] as i128) % (MOD as i128) * (invfact[N - yi] as i128) % (MOD as i128)) as i64
total_paths = (total_paths - ((ways[i] as i128) * (w as i128) % (MOD as i128)) as i64) % MOD
i = i + 1
}
if total_paths < 0 { total_paths = total_paths + MOD }
printf("%lld\n", total_paths)
free(ways)
free(invfact)
free(fact)
free(ys)
free(xs)
free(is_sq)
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 gcd_i64_i64(int64_t a0, int64_t b0);
int64_t lcm_i64_i64(int64_t a, int64_t b);
int64_t isqrt_i64(int64_t n);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t n);
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int32_t main(void);
int64_t gcd_i64_i64(int64_t a0, int64_t b0) {
int64_t a = a0;
int64_t b = b0;
while (b != 0) {
int64_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
return a;
}
int64_t lcm_i64_i64(int64_t a, int64_t b) {
if ((a == 0 || b == 0)) {
return 0;
}
return (FLOW_CHECKED_DIV((a), (gcd_i64_i64(a, b))) * b);
}
int64_t isqrt_i64(int64_t n) {
if (n < 2) {
return n;
}
int64_t x = n;
int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
while (y < x) {
x = y;
y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
}
return x;
}
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
int64_t b = FLOW_CHECKED_MOD((b0), (mod));
int64_t result = 0;
while (b > 0) {
if (FLOW_CHECKED_MOD((b), (2)) == 1) {
result = FLOW_CHECKED_MOD(((result + a)), (mod));
}
a = FLOW_CHECKED_MOD(((a * 2)), (mod));
b = FLOW_CHECKED_DIV((b), (2));
}
return result;
}
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
if (mod == 1) {
return 0;
}
int64_t result = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
result = mulmod_i64_i64_i64(result, b, mod);
}
b = mulmod_i64_i64_i64(b, b, mod);
e = FLOW_CHECKED_DIV((e), (2));
}
return result;
}
bool is_prime_i64(int64_t n) {
if (n < 2) {
return 0;
}
if (n < 4) {
return 1;
}
if ((FLOW_CHECKED_MOD((n), (2)) == 0 || FLOW_CHECKED_MOD((n), (3)) == 0)) {
return 0;
}
int64_t i = 5;
while ((i * i) <= n) {
if ((FLOW_CHECKED_MOD((n), (i)) == 0 || FLOW_CHECKED_MOD((n), ((i + 2))) == 0)) {
return 0;
}
i = (i + 6);
}
return 1;
}
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) {
r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))))));
}
b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))))));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int32_t main(void) {
int64_t N = 10000000;
int64_t MOD = 1000000007;
int64_t m = isqrt_i64(N);
int64_t max_sum = isqrt_i64((2 * N));
int8_t* is_sq = (int8_t*)(calloc(((2 * N) + 1), 1));
if (is_sq == NULL) {
return 1;
}
int64_t i = 1;
while (i <= max_sum) {
is_sq[(i * i)] = 1;
i = (i + 1);
}
int64_t* xs = (int64_t*)(calloc(20000, 8));
int64_t* ys = (int64_t*)(calloc(20000, 8));
if ((xs == NULL || ys == NULL)) {
return 1;
}
int64_t k = 0;
int64_t a = 1;
while (a <= m) {
int64_t a2 = (a * a);
int64_t b = 1;
while (b <= m) {
int64_t b2 = (b * b);
if (is_sq[(a2 + b2)] != 0) {
xs[k] = a2;
ys[k] = b2;
k = (k + 1);
}
b = (b + 1);
}
a = (a + 1);
}
int64_t gap = FLOW_CHECKED_DIV((k), (2));
while (gap > 0) {
i = gap;
while (i < k) {
int64_t j = i;
while (j >= gap) {
int64_t s1 = (xs[j] + ys[j]);
int64_t s0 = (xs[(j - gap)] + ys[(j - gap)]);
if ((s1 < s0 || (s1 == s0 && xs[j] < xs[(j - gap)]))) {
int64_t tx = xs[j];
xs[j] = xs[(j - gap)];
xs[(j - gap)] = tx;
int64_t ty = ys[j];
ys[j] = ys[(j - gap)];
ys[(j - gap)] = ty;
j = (j - gap);
} else {
break;
}
}
i = (i + 1);
}
gap = FLOW_CHECKED_DIV((gap), (2));
}
int64_t limit = (2 * N);
int64_t* fact = (int64_t*)(calloc((limit + 1), 8));
int64_t* invfact = (int64_t*)(calloc((limit + 1), 8));
if ((fact == NULL || invfact == NULL)) {
return 1;
}
fact[0] = 1;
i = 1;
while (i <= limit) {
fact[i] = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(fact[(i - 1)])) * ((__int128)(i)))), (((__int128)(MOD))))));
i = (i + 1);
}
invfact[limit] = modpow_i64_i64_i64(fact[limit], (MOD - 2), MOD);
i = limit;
while (i > 0) {
invfact[(i - 1)] = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(invfact[i])) * ((__int128)(i)))), (((__int128)(MOD))))));
i = (i - 1);
}
int64_t* ways = (int64_t*)(calloc(k, 8));
if (ways == NULL) {
return 1;
}
i = 0;
while (i < k) {
int64_t xi = xs[i];
int64_t yi = ys[i];
int64_t total = ((int64_t)(FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((((__int128)(fact[(xi + yi)])) * ((__int128)(invfact[xi])))), (((__int128)(MOD)))) * ((__int128)(invfact[yi])))), (((__int128)(MOD))))));
int64_t j = 0;
while (j < i) {
int64_t xj = xs[j];
int64_t yj = ys[j];
if ((xj <= xi && yj <= yi)) {
int64_t dx = (xi - xj);
int64_t dy = (yi - yj);
int64_t w = ((int64_t)(FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((((__int128)(fact[(dx + dy)])) * ((__int128)(invfact[dx])))), (((__int128)(MOD)))) * ((__int128)(invfact[dy])))), (((__int128)(MOD))))));
total = FLOW_CHECKED_MOD(((total - ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(ways[j])) * ((__int128)(w)))), (((__int128)(MOD)))))))), (MOD));
}
j = (j + 1);
}
if (total < 0) {
total = (total + MOD);
}
ways[i] = total;
i = (i + 1);
}
int64_t total_paths = ((int64_t)(FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((((__int128)(fact[(2 * N)])) * ((__int128)(invfact[N])))), (((__int128)(MOD)))) * ((__int128)(invfact[N])))), (((__int128)(MOD))))));
i = 0;
while (i < k) {
int64_t xi = xs[i];
int64_t yi = ys[i];
int64_t w = ((int64_t)(FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((((__int128)(fact[(((N - xi) + N) - yi)])) * ((__int128)(invfact[(N - xi)])))), (((__int128)(MOD)))) * ((__int128)(invfact[(N - yi)])))), (((__int128)(MOD))))));
total_paths = FLOW_CHECKED_MOD(((total_paths - ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(ways[i])) * ((__int128)(w)))), (((__int128)(MOD)))))))), (MOD));
i = (i + 1);
}
if (total_paths < 0) {
total_paths = (total_paths + MOD);
}
printf("%lld\n", total_paths);
free(ways);
free(invfact);
free(fact);
free(ys);
free(xs);
free(is_sq);
return 0;
}