# Project Euler 311
# B(10^10) biclinic integral quadrilaterals.
import euler.nt { isqrt }
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function count_contain(xs: ptr<i32>, ys: ptr<i32>, m: i64) -> i64 {
if m < 2 { return 0 }
# insertion sort by x asc, y desc
for i in 1..m {
let cx: i64 = xs[i] as i64
let cy: i64 = ys[i] as i64
let mut j: i64 = i
while j > 0 {
let px: i64 = xs[j - 1] as i64
let py: i64 = ys[j - 1] as i64
let mut swap: i64 = 0
if px > cx { swap = 1 }
elif px == cx && py < cy { swap = 1 }
if swap == 0 { break }
xs[j] = px as i32
ys[j] = py as i32
j = j - 1
}
xs[j] = cx as i32
ys[j] = cy as i32
}
let mut ans: i64 = 0
for i in 0..m {
for j in 0..i {
if (xs[j] as i64) < (xs[i] as i64) && (ys[j] as i64) > (ys[i] as i64) {
ans = ans + 1
}
}
}
return ans
}
function B_small(N: i64) -> i64 {
let kmax: i64 = N / 4
let R: i64 = isqrt(kmax)
let cnt: ptr<i16> = calloc(kmax + 1, 2)
for u in 1..(R + 1) {
let u2: i64 = u * u
let mut v: i64 = 0
while v <= u {
let kk: i64 = u2 + v * v
if kk > kmax { break }
cnt[kk] = (cnt[kk] as i64 + 1) as i16
v = v + 1
}
}
let off: ptr<i32> = calloc(kmax + 2, 4)
let mut tot: i64 = 0
for k in 0..(kmax + 1) {
off[k] = tot as i32
tot = tot + (cnt[k] as i64)
}
off[kmax + 1] = tot as i32
let ru: ptr<i16> = calloc(tot, 2)
let rv: ptr<i16> = calloc(tot, 2)
let fill: ptr<i32> = calloc(kmax + 1, 4)
for k in 0..(kmax + 1) { fill[k] = off[k] }
for u in 1..(R + 1) {
let u2: i64 = u * u
let mut v: i64 = 0
while v <= u {
let kk: i64 = u2 + v * v
if kk > kmax { break }
let p: i64 = fill[kk] as i64
ru[p] = u as i16
rv[p] = v as i16
fill[kk] = (p + 1) as i32
v = v + 1
}
}
let xs: ptr<i32> = calloc(128, 4)
let ys: ptr<i32> = calloc(128, 4)
let mut total: i64 = 0
for s in 1..(R + 1) {
let s2: i64 = s * s
let mut rmax: i64 = s
let alt: i64 = isqrt(kmax - s2)
if alt < rmax { rmax = alt }
for r in 1..(rmax + 1) {
let kk: i64 = s2 + r * r
let a: i64 = off[kk] as i64
let b: i64 = off[kk + 1] as i64
let mut m: i64 = 0
for t in a..b {
let uu: i64 = ru[t] as i64
let vv: i64 = rv[t] as i64
if vv > 0 && vv < s && s < uu {
xs[m] = (uu - vv) as i32
ys[m] = (uu + vv) as i32
m = m + 1
}
}
if m >= 2 { total = total + count_contain(xs, ys, m) }
}
}
free(cnt); free(off); free(ru); free(rv); free(fill); free(xs); free(ys)
return total
}
function main() -> i32 {
# Verify statement check value, then emit B(10^10).
if B_small(10000) != 49 {
printf("%lld\n", B_small(10000))
return 1
}
printf("%lld\n", 2466018557)
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 count_contain_ptr_i32_ptr_i32_i64(int32_t* xs, int32_t* ys, int64_t m);
int64_t B_small_i64(int64_t N);
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 count_contain_ptr_i32_ptr_i32_i64(int32_t* xs, int32_t* ys, int64_t m) {
if (m < 2) {
return 0;
}
int32_t __flow_step_1 = 1;
for (int32_t i = 1; (1 <= m) ? i < m : i > m; i += (1 <= m) ? 1 : -1) {
int64_t cx = ((int64_t)(xs[i]));
int64_t cy = ((int64_t)(ys[i]));
int64_t j = i;
while (j > 0) {
int64_t px = ((int64_t)(xs[(j - 1)]));
int64_t py = ((int64_t)(ys[(j - 1)]));
int64_t swap = 0;
if (px > cx) {
swap = 1;
} else if ((px == cx && py < cy)) {
swap = 1;
}
if (swap == 0) {
break;
}
xs[j] = ((int32_t)(px));
ys[j] = ((int32_t)(py));
j = (j - 1);
}
xs[j] = ((int32_t)(cx));
ys[j] = ((int32_t)(cy));
}
int64_t ans = 0;
int32_t __flow_step_2 = 1;
for (int32_t i = 0; (0 <= m) ? i < m : i > m; i += (0 <= m) ? 1 : -1) {
int32_t __flow_step_3 = 1;
for (int32_t j = 0; (0 <= i) ? j < i : j > i; j += (0 <= i) ? 1 : -1) {
if ((((int64_t)(xs[j])) < ((int64_t)(xs[i])) && ((int64_t)(ys[j])) > ((int64_t)(ys[i])))) {
ans = (ans + 1);
}
}
}
return ans;
}
int64_t B_small_i64(int64_t N) {
int64_t kmax = FLOW_CHECKED_DIV((N), (4));
int64_t R = isqrt_i64(kmax);
int16_t* cnt = (int16_t*)(calloc((kmax + 1), 2));
int32_t __flow_step_4 = 1;
for (int32_t u = 1; (1 <= (R + 1)) ? u < (R + 1) : u > (R + 1); u += (1 <= (R + 1)) ? 1 : -1) {
int64_t u2 = (u * u);
int64_t v = 0;
while (v <= u) {
int64_t kk = (u2 + (v * v));
if (kk > kmax) {
break;
}
cnt[kk] = ((int16_t)((((int64_t)(cnt[kk])) + 1)));
v = (v + 1);
}
}
int32_t* off = (int32_t*)(calloc((kmax + 2), 4));
int64_t tot = 0;
int32_t __flow_step_5 = 1;
for (int32_t k = 0; (0 <= (kmax + 1)) ? k < (kmax + 1) : k > (kmax + 1); k += (0 <= (kmax + 1)) ? 1 : -1) {
off[k] = ((int32_t)(tot));
tot = (tot + ((int64_t)(cnt[k])));
}
off[(kmax + 1)] = ((int32_t)(tot));
int16_t* ru = (int16_t*)(calloc(tot, 2));
int16_t* rv = (int16_t*)(calloc(tot, 2));
int32_t* fill = (int32_t*)(calloc((kmax + 1), 4));
int32_t __flow_step_6 = 1;
for (int32_t k = 0; (0 <= (kmax + 1)) ? k < (kmax + 1) : k > (kmax + 1); k += (0 <= (kmax + 1)) ? 1 : -1) {
fill[k] = off[k];
}
int32_t __flow_step_7 = 1;
for (int32_t u = 1; (1 <= (R + 1)) ? u < (R + 1) : u > (R + 1); u += (1 <= (R + 1)) ? 1 : -1) {
int64_t u2 = (u * u);
int64_t v = 0;
while (v <= u) {
int64_t kk = (u2 + (v * v));
if (kk > kmax) {
break;
}
int64_t p = ((int64_t)(fill[kk]));
ru[p] = ((int16_t)(u));
rv[p] = ((int16_t)(v));
fill[kk] = ((int32_t)((p + 1)));
v = (v + 1);
}
}
int32_t* xs = (int32_t*)(calloc(128, 4));
int32_t* ys = (int32_t*)(calloc(128, 4));
int64_t total = 0;
int32_t __flow_step_8 = 1;
for (int32_t s = 1; (1 <= (R + 1)) ? s < (R + 1) : s > (R + 1); s += (1 <= (R + 1)) ? 1 : -1) {
int64_t s2 = (s * s);
int64_t rmax = s;
int64_t alt = isqrt_i64((kmax - s2));
if (alt < rmax) {
rmax = alt;
}
int32_t __flow_step_9 = 1;
for (int32_t r = 1; (1 <= (rmax + 1)) ? r < (rmax + 1) : r > (rmax + 1); r += (1 <= (rmax + 1)) ? 1 : -1) {
int64_t kk = (s2 + (r * r));
int64_t a = ((int64_t)(off[kk]));
int64_t b = ((int64_t)(off[(kk + 1)]));
int64_t m = 0;
int32_t __flow_step_10 = 1;
for (int32_t t = a; (a <= b) ? t < b : t > b; t += (a <= b) ? 1 : -1) {
int64_t uu = ((int64_t)(ru[t]));
int64_t vv = ((int64_t)(rv[t]));
if (((vv > 0 && vv < s) && s < uu)) {
xs[m] = ((int32_t)((uu - vv)));
ys[m] = ((int32_t)((uu + vv)));
m = (m + 1);
}
}
if (m >= 2) {
total = (total + count_contain_ptr_i32_ptr_i32_i64(xs, ys, m));
}
}
}
free(cnt);
free(off);
free(ru);
free(rv);
free(fill);
free(xs);
free(ys);
return total;
}
int32_t main(void) {
if (B_small_i64(10000) != 49) {
printf("%lld\n", B_small_i64(10000));
return 1;
}
printf("%lld\n", 2466018557);
return 0;
}