# Project Euler 438
# Sum of sum|ai| over integer coefficient tuples for floor-root condition, n=7.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function comb(n: i64, k0: i64) -> i64 {
let mut k: i64 = k0
if k < 0 || k > n { return 0 }
if k > n - k { k = n - k }
let mut r: i64 = 1
let mut i: i64 = 1
while i <= k {
r = r * (n - k + i) / i
i = i + 1
}
return r
}
function ipow(base: i64, e: i64) -> i64 {
let mut r: i64 = 1
let mut i: i64 = 0
while i < e {
r = r * base
i = i + 1
}
return r
}
function sum_abs_range(lo: i64, hi: i64) -> i64 {
if lo > hi { return 0 }
if hi < 0 {
let cnt: i64 = hi - lo + 1
return 0 - (lo + hi) * cnt / 2
}
if lo > 0 {
let cnt: i64 = hi - lo + 1
return (lo + hi) * cnt / 2
}
return (0 - lo) * ((0 - lo) + 1) / 2 + hi * (hi + 1) / 2
}
function floor_div(a: i64, b: i64) -> i64 {
let mut q: i64 = a / b
let r: i64 = a % b
if r != 0 && ((a < 0) != (b < 0)) { q = q - 1 }
return q
}
function first_sign(Bf: ptr<i64>, base: i64, n: i64) -> i64 {
for dd in 1..(n + 1) {
let v: i64 = Bf[base + dd]
if v != 0 {
if v > 0 { return 1 }
return 0 - 1
}
}
return 0
}
function apply_val(i: i64, val: i64, n: i64, P: ptr<i64>, Bf: ptr<i64>, MMAX: i64, IMAX: i64, DMAX: i64) -> void {
let mut kp: i64 = i + 1
while kp <= n {
let mut m: i64 = 1
while m <= kp + 1 {
let mut d: i64 = 0
while d <= n {
let pidx: i64 = ((kp * MMAX + m) * IMAX + i) * DMAX + d
let bix: i64 = (kp * MMAX + m) * DMAX + d
Bf[bix] = Bf[bix] + val * P[pidx]
d = d + 1
}
m = m + 1
}
kp = kp + 1
}
}
function dfs438(k: i64, prefix: i64, n: i64, P: ptr<i64>, A0: ptr<i64>, Bf: ptr<i64>, MMAX: i64, IMAX: i64, DMAX: i64) -> i64 {
let mut lb: i64 = 0 - 1000000000000000
let mut ub: i64 = 1000000000000000
let mut m: i64 = 1
while m <= k + 1 {
let A0v: i64 = A0[k * MMAX + m]
let bbase: i64 = (k * MMAX + m) * DMAX
let b0: i64 = Bf[bbase]
if A0v > 0 {
let num: i64 = 0 - b0
let den: i64 = A0v
let mut x0: i64 = floor_div(num, den)
if num % den != 0 {
x0 = x0 + 1
} else {
if first_sign(Bf, bbase, n) <= 0 { x0 = x0 + 1 }
}
if x0 > lb { lb = x0 }
} else {
let den: i64 = 0 - A0v
let num: i64 = b0
let mut x0: i64 = floor_div(num, den)
if num % den == 0 {
if first_sign(Bf, bbase, n) <= 0 { x0 = x0 - 1 }
}
if x0 < ub { ub = x0 }
}
m = m + 1
}
if lb > ub { return 0 }
if k == n {
return (ub - lb + 1) * prefix + sum_abs_range(lb, ub)
}
let mut total: i64 = 0
let mut ak: i64 = lb
while ak <= ub {
apply_val(k, ak, n, P, Bf, MMAX, IMAX, DMAX)
let mut ap: i64 = ak
if ap < 0 { ap = 0 - ap }
total = total + dfs438(k + 1, prefix + ap, n, P, A0, Bf, MMAX, IMAX, DMAX)
apply_val(k, 0 - ak, n, P, Bf, MMAX, IMAX, DMAX)
ak = ak + 1
}
return total
}
function main() -> i32 {
let n: i64 = 7
let MMAX: i64 = n + 2
let IMAX: i64 = n + 1
let DMAX: i64 = n + 1
let P: ptr<i64> = calloc((n + 1) * MMAX * IMAX * DMAX, 8)
let A0: ptr<i64> = calloc((n + 1) * MMAX, 8)
let Bf: ptr<i64> = calloc((n + 1) * MMAX * DMAX, 8)
if P == null || A0 == null || Bf == null { return 1 }
let mut k: i64 = 1
while k <= n {
let r: i64 = n - k
let mut fact: i64 = 1
let mut t: i64 = 2
while t <= r {
fact = fact * t
t = t + 1
}
let mut m: i64 = 1
while m <= k + 1 {
let mut s: i64 = 1
if ((k + 1 - m) & 1) != 0 { s = 0 - 1 }
A0[k * MMAX + m] = s * fact
let mut i: i64 = 0
while i <= k {
let p: i64 = n - i
let mut d: i64 = 0
while d <= n {
let mut acc: i64 = 0
let mut j: i64 = 0
while j <= r {
let mut cjj: i64 = comb(r, j)
if ((r - j) & 1) != 0 { cjj = 0 - cjj }
if d <= p {
let mut cd: i64 = comb(p, d) * ipow(m + j, p - d)
if (d & 1) != 0 { cd = 0 - cd }
acc = acc + cjj * cd
}
j = j + 1
}
P[((k * MMAX + m) * IMAX + i) * DMAX + d] = s * acc
d = d + 1
}
i = i + 1
}
m = m + 1
}
k = k + 1
}
k = 1
while k <= n {
let mut m: i64 = 1
while m <= k + 1 {
let mut d: i64 = 0
while d <= n {
Bf[(k * MMAX + m) * DMAX + d] = P[((k * MMAX + m) * IMAX + 0) * DMAX + d]
d = d + 1
}
m = m + 1
}
k = k + 1
}
let ans: i64 = dfs438(1, 0, n, P, A0, Bf, MMAX, IMAX, DMAX)
printf("%lld\n", ans)
free(Bf)
free(A0)
free(P)
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 comb_i64_i64(int64_t n, int64_t k0);
int64_t ipow_i64_i64(int64_t base, int64_t e);
int64_t sum_abs_range_i64_i64(int64_t lo, int64_t hi);
int64_t floor_div_i64_i64(int64_t a, int64_t b);
int64_t first_sign_ptr_i64_i64_i64(int64_t* Bf, int64_t base, int64_t n);
void apply_val_i64_i64_i64_ptr_i64_ptr_i64_i64_i64_i64(int64_t i, int64_t val, int64_t n, int64_t* P, int64_t* Bf, int64_t MMAX, int64_t IMAX, int64_t DMAX);
int64_t dfs438_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_i64_i64_i64(int64_t k, int64_t prefix, int64_t n, int64_t* P, int64_t* A0, int64_t* Bf, int64_t MMAX, int64_t IMAX, int64_t DMAX);
int32_t main(void);
int64_t comb_i64_i64(int64_t n, int64_t k0) {
int64_t k = k0;
if ((k < 0 || k > n)) {
return 0;
}
if (k > (n - k)) {
k = (n - k);
}
int64_t r = 1;
int64_t i = 1;
while (i <= k) {
r = FLOW_CHECKED_DIV(((r * ((n - k) + i))), (i));
i = (i + 1);
}
return r;
}
int64_t ipow_i64_i64(int64_t base, int64_t e) {
int64_t r = 1;
int64_t i = 0;
while (i < e) {
r = (r * base);
i = (i + 1);
}
return r;
}
int64_t sum_abs_range_i64_i64(int64_t lo, int64_t hi) {
if (lo > hi) {
return 0;
}
if (hi < 0) {
int64_t cnt = ((hi - lo) + 1);
return (0 - FLOW_CHECKED_DIV((((lo + hi) * cnt)), (2)));
}
if (lo > 0) {
int64_t cnt = ((hi - lo) + 1);
return FLOW_CHECKED_DIV((((lo + hi) * cnt)), (2));
}
return (FLOW_CHECKED_DIV((((0 - lo) * ((0 - lo) + 1))), (2)) + FLOW_CHECKED_DIV(((hi * (hi + 1))), (2)));
}
int64_t floor_div_i64_i64(int64_t a, int64_t b) {
int64_t q = FLOW_CHECKED_DIV((a), (b));
int64_t r = FLOW_CHECKED_MOD((a), (b));
if ((r != 0 && a < 0 != b < 0)) {
q = (q - 1);
}
return q;
}
int64_t first_sign_ptr_i64_i64_i64(int64_t* Bf, int64_t base, int64_t n) {
int32_t __flow_step_1 = 1;
for (int32_t dd = 1; (1 <= (n + 1)) ? dd < (n + 1) : dd > (n + 1); dd += (1 <= (n + 1)) ? 1 : -1) {
int64_t v = Bf[(base + dd)];
if (v != 0) {
if (v > 0) {
return 1;
}
return (0 - 1);
}
}
return 0;
}
void apply_val_i64_i64_i64_ptr_i64_ptr_i64_i64_i64_i64(int64_t i, int64_t val, int64_t n, int64_t* P, int64_t* Bf, int64_t MMAX, int64_t IMAX, int64_t DMAX) {
int64_t kp = (i + 1);
while (kp <= n) {
int64_t m = 1;
while (m <= (kp + 1)) {
int64_t d = 0;
while (d <= n) {
int64_t pidx = ((((((kp * MMAX) + m) * IMAX) + i) * DMAX) + d);
int64_t bix = ((((kp * MMAX) + m) * DMAX) + d);
Bf[bix] = (Bf[bix] + (val * P[pidx]));
d = (d + 1);
}
m = (m + 1);
}
kp = (kp + 1);
}
}
int64_t dfs438_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_i64_i64_i64(int64_t k, int64_t prefix, int64_t n, int64_t* P, int64_t* A0, int64_t* Bf, int64_t MMAX, int64_t IMAX, int64_t DMAX) {
int64_t lb = (0 - 1000000000000000);
int64_t ub = 1000000000000000;
int64_t m = 1;
while (m <= (k + 1)) {
int64_t A0v = A0[((k * MMAX) + m)];
int64_t bbase = (((k * MMAX) + m) * DMAX);
int64_t b0 = Bf[bbase];
if (A0v > 0) {
int64_t num = (0 - b0);
int64_t den = A0v;
int64_t x0 = floor_div_i64_i64(num, den);
if (FLOW_CHECKED_MOD((num), (den)) != 0) {
x0 = (x0 + 1);
} else {
if (first_sign_ptr_i64_i64_i64(Bf, bbase, n) <= 0) {
x0 = (x0 + 1);
}
}
if (x0 > lb) {
lb = x0;
}
} else {
int64_t den = (0 - A0v);
int64_t num = b0;
int64_t x0 = floor_div_i64_i64(num, den);
if (FLOW_CHECKED_MOD((num), (den)) == 0) {
if (first_sign_ptr_i64_i64_i64(Bf, bbase, n) <= 0) {
x0 = (x0 - 1);
}
}
if (x0 < ub) {
ub = x0;
}
}
m = (m + 1);
}
if (lb > ub) {
return 0;
}
if (k == n) {
return ((((ub - lb) + 1) * prefix) + sum_abs_range_i64_i64(lb, ub));
}
int64_t total = 0;
int64_t ak = lb;
while (ak <= ub) {
apply_val_i64_i64_i64_ptr_i64_ptr_i64_i64_i64_i64(k, ak, n, P, Bf, MMAX, IMAX, DMAX);
int64_t ap = ak;
if (ap < 0) {
ap = (0 - ap);
}
total = (total + dfs438_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_i64_i64_i64((k + 1), (prefix + ap), n, P, A0, Bf, MMAX, IMAX, DMAX));
apply_val_i64_i64_i64_ptr_i64_ptr_i64_i64_i64_i64(k, (0 - ak), n, P, Bf, MMAX, IMAX, DMAX);
ak = (ak + 1);
}
return total;
}
int32_t main(void) {
int64_t n = 7;
int64_t MMAX = (n + 2);
int64_t IMAX = (n + 1);
int64_t DMAX = (n + 1);
int64_t* P = (int64_t*)(calloc(((((n + 1) * MMAX) * IMAX) * DMAX), 8));
int64_t* A0 = (int64_t*)(calloc(((n + 1) * MMAX), 8));
int64_t* Bf = (int64_t*)(calloc((((n + 1) * MMAX) * DMAX), 8));
if (((P == NULL || A0 == NULL) || Bf == NULL)) {
return 1;
}
int64_t k = 1;
while (k <= n) {
int64_t r = (n - k);
int64_t fact = 1;
int64_t t = 2;
while (t <= r) {
fact = (fact * t);
t = (t + 1);
}
int64_t m = 1;
while (m <= (k + 1)) {
int64_t s = 1;
if ((((k + 1) - m) & 1) != 0) {
s = (0 - 1);
}
A0[((k * MMAX) + m)] = (s * fact);
int64_t i = 0;
while (i <= k) {
int64_t p = (n - i);
int64_t d = 0;
while (d <= n) {
int64_t acc = 0;
int64_t j = 0;
while (j <= r) {
int64_t cjj = comb_i64_i64(r, j);
if (((r - j) & 1) != 0) {
cjj = (0 - cjj);
}
if (d <= p) {
int64_t cd = (comb_i64_i64(p, d) * ipow_i64_i64((m + j), (p - d)));
if ((d & 1) != 0) {
cd = (0 - cd);
}
acc = (acc + (cjj * cd));
}
j = (j + 1);
}
P[((((((k * MMAX) + m) * IMAX) + i) * DMAX) + d)] = (s * acc);
d = (d + 1);
}
i = (i + 1);
}
m = (m + 1);
}
k = (k + 1);
}
k = 1;
while (k <= n) {
int64_t m = 1;
while (m <= (k + 1)) {
int64_t d = 0;
while (d <= n) {
Bf[((((k * MMAX) + m) * DMAX) + d)] = P[((((((k * MMAX) + m) * IMAX) + 0) * DMAX) + d)];
d = (d + 1);
}
m = (m + 1);
}
k = (k + 1);
}
int64_t ans = dfs438_i64_i64_i64_ptr_i64_ptr_i64_ptr_i64_i64_i64_i64(1, 0, n, P, A0, Bf, MMAX, IMAX, DMAX);
printf("%lld\n", ans);
free(Bf);
free(A0);
free(P);
return 0;
}