Average and Variance: S(10^8) mod 433494437. Sum over quadruples (a,b,c,d) whose average equals twice their variance. Lattice enumeration with prefix sums.
# Project Euler 791
# Average and Variance: S(10^8) mod 433494437.
# Sum over quadruples (a,b,c,d) whose average equals twice their variance.
# Lattice enumeration with prefix sums.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function sqrt(x: f64) -> f64
}
const MOD: i64 = 433494437
const LIM: i64 = 1152921504606846976
function isqrt_i64(n: i64) -> i64 {
if n <= 0 { return 0 }
let mut r: i64 = sqrt(n as f64) as i64
while r > 0 && r * r > n {
r = r - 1
}
while (r + 1) * (r + 1) <= n {
r = r + 1
}
return r
}
function main() -> i32 {
let n: i64 = 100000000
let N2: i64 = 2 * n
let R: i64 = isqrt_i64(2 * n) + 2
let s2: ptr<i64> = calloc(R + 1, 8)
let ps2: ptr<i64> = calloc(R + 1, 8)
let p1: ptr<i64> = calloc(R + 1, 8)
let p2: ptr<i64> = calloc(R + 1, 8)
let p3: ptr<i64> = calloc(R + 1, 8)
for i in 1..(R + 1) {
let ii: i64 = i * i
s2[i] = s2[i - 1] + ii
ps2[i] = ps2[i - 1] + s2[i]
p1[i] = p1[i - 1] + i
p2[i] = p2[i - 1] + ii
p3[i] = p3[i - 1] + ii * i
}
let mut total: i64 = 0
# Small U cases (U=0,1): brute force
for U in 0..2 {
for V in 0..(U + 1) {
for W in 0..(V + 1) {
let mut sgn: i32 = -1
while sgn <= 1 {
if W == 0 && sgn == -1 {
sgn = sgn + 2
} else {
let u: i64 = -(U as i64)
let v: i64 = -(V as i64)
let w: i64 = (sgn as i64) * (W as i64)
let m: i64 = (U as i64) * (U as i64) + (V as i64) * (V as i64) + (W as i64) * (W as i64)
if m != 0 {
let a: i64 = (m + u + v + w) / 2
let b: i64 = (m + u - v - w) / 2
let c: i64 = (m - u + v - w) / 2
let d: i64 = (m - u - v + w) / 2
if 1 <= a && a <= b && b <= c && c <= d && d <= n {
let v_add: i64 = 2 * m
total = total + v_add % MOD
if total >= LIM { total = total % MOD }
}
}
sgn = sgn + 2
}
}
}
}
}
# Main enumeration
let mut U: i64 = 2
while U <= R {
let U2: i64 = U * U
let rem: i64 = N2 - U2 - U
if rem < 0 { break }
let mut Wmax0: i64 = isqrt_i64(rem / 2)
if Wmax0 > U { Wmax0 = U }
let T: i64 = N2 - 2 * U2 - 2 * U
let mut sgn: i32 = -1
while sgn <= 1 {
let startW: i64 = if sgn == 1 { 0 } else { 1 }
if startW > Wmax0 {
sgn = sgn + 2
continue
}
let mut Wfull: i64
if T < 0 {
Wfull = -1
} else {
let rt: i64 = isqrt_i64(1 + 4 * T)
if sgn == 1 {
Wfull = (rt - 1) / 2
} else {
Wfull = (rt + 1) / 2
}
if Wfull > Wmax0 { Wfull = Wmax0 }
}
# Full segment: W in [startW..Wfull], V in [W..U]
if Wfull >= startW {
let A: i64 = startW
let B: i64 = Wfull
let num: i64 = B - A + 1
let sumW: i64 = p1[B] - (if A > 0 { p1[A - 1] } else { 0 })
let sumW2: i64 = p2[B] - (if A > 0 { p2[A - 1] } else { 0 })
let sumW3: i64 = p3[B] - (if A > 0 { p3[A - 1] } else { 0 })
let sumCnt: i64 = num * (U + 1) - sumW
let sumCntW2: i64 = (U + 1) * sumW2 - sumW3
let sumPrefix: i64
if B == 0 {
sumPrefix = 0
} else {
let mut lo: i64 = A - 1
let hi: i64 = B - 1
if lo < 0 { lo = 0 }
sumPrefix = ps2[hi] - (if lo > 0 { ps2[lo - 1] } else { 0 })
}
let sumSumV2: i64 = num * s2[U] - sumPrefix
let contrib: i128 = (2 as i128) * (U2 as i128) * (sumCnt as i128) + (2 as i128) * (sumCntW2 as i128) + (2 as i128) * (sumSumV2 as i128)
let mut cmod: i64 = (contrib % (MOD as i128)) as i64
if cmod < 0 { cmod = cmod + MOD }
total = total + cmod
if total >= LIM { total = total % MOD }
}
# Tail segment
let mut tail_start: i64 = Wfull + 1
if tail_start < startW { tail_start = startW }
if tail_start > Wmax0 {
sgn = sgn + 2
continue
}
let base_D: i64 = 4 * N2 + 1 - 4 * (U2 + U)
let mut W: i64 = tail_start
while W <= Wmax0 {
let D: i64 = base_D - 4 * (W * W + (sgn as i64) * W)
if D < 0 { break }
let mut Vmax: i64 = (isqrt_i64(D) - 1) / 2
if Vmax > U { Vmax = U }
if Vmax < W {
W = W + 1
continue
}
let cnt: i64 = Vmax - W + 1
let sumV2: i64 = s2[Vmax] - (if W > 0 { s2[W - 1] } else { 0 })
let W2: i64 = W * W
let contrib2: i128 = (cnt as i128) * (2 as i128) * ((U2 + W2) as i128) + (2 as i128) * (sumV2 as i128)
let mut cmod2: i64 = (contrib2 % (MOD as i128)) as i64
if cmod2 < 0 { cmod2 = cmod2 + MOD }
total = total + cmod2
if total >= LIM { total = total % MOD }
W = W + 1
}
sgn = sgn + 2
}
U = U + 1
}
free(s2)
free(ps2)
free(p1)
free(p2)
free(p3)
printf("%lld\n", total % MOD)
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 isqrt_i64_i64(int64_t n);
int32_t main(void);
static const int64_t MOD = 433494437;
static const int64_t LIM = 1152921504606846976;
int64_t isqrt_i64_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t r = ((int64_t)(sqrt(((double)(n)))));
while ((r > 0 && (r * r) > n)) {
r = (r - 1);
}
while (((r + 1) * (r + 1)) <= n) {
r = (r + 1);
}
return r;
}
int32_t main(void) {
int64_t n = 100000000;
int64_t N2 = (2 * n);
int64_t R = (isqrt_i64_i64((2 * n)) + 2);
int64_t* s2 = (int64_t*)(calloc((R + 1), 8));
int64_t* ps2 = (int64_t*)(calloc((R + 1), 8));
int64_t* p1 = (int64_t*)(calloc((R + 1), 8));
int64_t* p2 = (int64_t*)(calloc((R + 1), 8));
int64_t* p3 = (int64_t*)(calloc((R + 1), 8));
int32_t __flow_step_1 = 1;
for (int32_t i = 1; (1 <= (R + 1)) ? i < (R + 1) : i > (R + 1); i += (1 <= (R + 1)) ? 1 : -1) {
int64_t ii = (i * i);
s2[i] = (s2[(i - 1)] + ii);
ps2[i] = (ps2[(i - 1)] + s2[i]);
p1[i] = (p1[(i - 1)] + i);
p2[i] = (p2[(i - 1)] + ii);
p3[i] = (p3[(i - 1)] + (ii * i));
}
int64_t total = 0;
int32_t __flow_step_2 = 1;
for (int32_t U = 0; (0 <= 2) ? U < 2 : U > 2; U += (0 <= 2) ? 1 : -1) {
int32_t __flow_step_3 = 1;
for (int32_t V = 0; (0 <= (U + 1)) ? V < (U + 1) : V > (U + 1); V += (0 <= (U + 1)) ? 1 : -1) {
int32_t __flow_step_4 = 1;
for (int32_t W = 0; (0 <= (V + 1)) ? W < (V + 1) : W > (V + 1); W += (0 <= (V + 1)) ? 1 : -1) {
int32_t sgn = (-1);
while (sgn <= 1) {
if ((W == 0 && sgn == (-1))) {
sgn = (sgn + 2);
} else {
int64_t u = (-((int64_t)(U)));
int64_t v = (-((int64_t)(V)));
int64_t w = (((int64_t)(sgn)) * ((int64_t)(W)));
int64_t m = (((((int64_t)(U)) * ((int64_t)(U))) + (((int64_t)(V)) * ((int64_t)(V)))) + (((int64_t)(W)) * ((int64_t)(W))));
if (m != 0) {
int64_t a = FLOW_CHECKED_DIV(((((m + u) + v) + w)), (2));
int64_t b = FLOW_CHECKED_DIV(((((m + u) - v) - w)), (2));
int64_t c = FLOW_CHECKED_DIV(((((m - u) + v) - w)), (2));
int64_t d = FLOW_CHECKED_DIV(((((m - u) - v) + w)), (2));
if (((((1 <= a && a <= b) && b <= c) && c <= d) && d <= n)) {
int64_t v_add = (2 * m);
total = (total + FLOW_CHECKED_MOD((v_add), (MOD)));
if (total >= LIM) {
total = FLOW_CHECKED_MOD((total), (MOD));
}
}
}
sgn = (sgn + 2);
}
}
}
}
}
int64_t U = 2;
while (U <= R) {
int64_t U2 = (U * U);
int64_t rem = ((N2 - U2) - U);
if (rem < 0) {
break;
}
int64_t Wmax0 = isqrt_i64_i64(FLOW_CHECKED_DIV((rem), (2)));
if (Wmax0 > U) {
Wmax0 = U;
}
int64_t T = ((N2 - (2 * U2)) - (2 * U));
int32_t sgn = (-1);
while (sgn <= 1) {
int64_t startW = ((sgn == 1) ? (0) : (1));
if (startW > Wmax0) {
sgn = (sgn + 2);
continue;
}
int64_t Wfull;
if (T < 0) {
Wfull = (-1);
} else {
int64_t rt = isqrt_i64_i64((1 + (4 * T)));
if (sgn == 1) {
Wfull = FLOW_CHECKED_DIV(((rt - 1)), (2));
} else {
Wfull = FLOW_CHECKED_DIV(((rt + 1)), (2));
}
if (Wfull > Wmax0) {
Wfull = Wmax0;
}
}
if (Wfull >= startW) {
int64_t A = startW;
int64_t B = Wfull;
int64_t num = ((B - A) + 1);
int64_t sumW = (p1[B] - ((A > 0) ? (p1[(A - 1)]) : (0)));
int64_t sumW2 = (p2[B] - ((A > 0) ? (p2[(A - 1)]) : (0)));
int64_t sumW3 = (p3[B] - ((A > 0) ? (p3[(A - 1)]) : (0)));
int64_t sumCnt = ((num * (U + 1)) - sumW);
int64_t sumCntW2 = (((U + 1) * sumW2) - sumW3);
int64_t sumPrefix;
if (B == 0) {
sumPrefix = 0;
} else {
int64_t lo = (A - 1);
int64_t hi = (B - 1);
if (lo < 0) {
lo = 0;
}
sumPrefix = (ps2[hi] - ((lo > 0) ? (ps2[(lo - 1)]) : (0)));
}
int64_t sumSumV2 = ((num * s2[U]) - sumPrefix);
__int128 contrib = ((((((__int128)(2)) * ((__int128)(U2))) * ((__int128)(sumCnt))) + (((__int128)(2)) * ((__int128)(sumCntW2)))) + (((__int128)(2)) * ((__int128)(sumSumV2))));
int64_t cmod = ((int64_t)(FLOW_CHECKED_MOD((contrib), (((__int128)(MOD))))));
if (cmod < 0) {
cmod = (cmod + MOD);
}
total = (total + cmod);
if (total >= LIM) {
total = FLOW_CHECKED_MOD((total), (MOD));
}
}
int64_t tail_start = (Wfull + 1);
if (tail_start < startW) {
tail_start = startW;
}
if (tail_start > Wmax0) {
sgn = (sgn + 2);
continue;
}
int64_t base_D = (((4 * N2) + 1) - (4 * (U2 + U)));
int64_t W = tail_start;
while (W <= Wmax0) {
int64_t D = (base_D - (4 * ((W * W) + (((int64_t)(sgn)) * W))));
if (D < 0) {
break;
}
int64_t Vmax = FLOW_CHECKED_DIV(((isqrt_i64_i64(D) - 1)), (2));
if (Vmax > U) {
Vmax = U;
}
if (Vmax < W) {
W = (W + 1);
continue;
}
int64_t cnt = ((Vmax - W) + 1);
int64_t sumV2 = (s2[Vmax] - ((W > 0) ? (s2[(W - 1)]) : (0)));
int64_t W2 = (W * W);
__int128 contrib2 = (((((__int128)(cnt)) * ((__int128)(2))) * ((__int128)((U2 + W2)))) + (((__int128)(2)) * ((__int128)(sumV2))));
int64_t cmod2 = ((int64_t)(FLOW_CHECKED_MOD((contrib2), (((__int128)(MOD))))));
if (cmod2 < 0) {
cmod2 = (cmod2 + MOD);
}
total = (total + cmod2);
if (total >= LIM) {
total = FLOW_CHECKED_MOD((total), (MOD));
}
W = (W + 1);
}
sgn = (sgn + 2);
}
U = (U + 1);
}
free(s2);
free(ps2);
free(p1);
free(p2);
free(p3);
printf("%lld\n", FLOW_CHECKED_MOD((total), (MOD)));
return 0;
}