# Project Euler 945
# XOR-Equation C: count pairs 0<=a<=b<=N where (a XOR-product b) has no even-position bits.
# Uses GF(2) polynomial arithmetic and digit DP.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
# 16-bit compaction lookup tables
let mut even16: ptr<i64> = null as ptr<i64>
let mut odd16: ptr<i64> = null as ptr<i64>
function clz64(x: i64) -> i32 {
if x == 0 { return 64 }
if x < 0 { return 0 }
let mut n: i32 = 0
let mut v: i64 = x
while v != 0 {
v = v >> 1
n = n + 1
}
return 64 - n
}
function build_tables() -> void {
even16 = calloc(65536, 8)
odd16 = calloc(65536, 8)
for x in 0..65536 {
let mut e: i64 = 0
let mut o: i64 = 0
for i in 0..8 {
let b: i64 = 1 << (2 * i)
if ((x as i64) & b) != 0 { e = e | (1 << i) }
if ((x as i64) & (b << 1)) != 0 { o = o | (1 << i) }
}
even16[x] = e
odd16[x] = o
}
}
# Split x into (E, O) where E = even-position bits compacted, O = odd-position bits compacted.
function split_u(x: i64, E: ptr<i64>, O: ptr<i64>) -> void {
let mut e: i64 = 0
let mut o: i64 = 0
let mut shift: i32 = 0
let mut val: i64 = x
while val != 0 {
let chunk: i64 = val & 0xFFFF
e = e | (even16[chunk] << shift)
o = o | (odd16[chunk] << shift)
val = val >> 16
if val < 0 { val = val & 0xFFFFFFFFFFFF }
shift = shift + 8
}
E[0] = e
O[0] = o
}
# Polynomial remainder a mod b over GF(2)
function gf2_mod(a: i64, b: i64) -> i64 {
if b == 0 { return a }
let db: i32 = 63 - clz64(b)
let mut val: i64 = a
while val != 0 {
let da: i32 = 63 - clz64(val)
if da < db { break }
val = val ^ (b << (da - db))
}
return val
}
# Polynomial GCD over GF(2)
function gf2_gcd(a: i64, b: i64) -> i64 {
let mut x: i64 = a
let mut y: i64 = b
while y != 0 {
let t: i64 = gf2_mod(x, y)
x = y
y = t
}
return x
}
# Ordered solution count for [0, 2^bits - 1]
function ordered_full(bits: i32) -> i64 {
let mut sign: i64 = 1
if (bits & 1) != 0 { sign = -1 }
let num: i64 = (1 << (bits + 1)) * (3 * (bits as i64) + 4) + sign
return num / 9
}
# For b = 2^k + y, count a in [0, 2^k - 1] satisfying the condition
function count_a_for_upper(k: i32, y: i64) -> i64 {
let m: i32 = k / 2
let Y0: ptr<i64> = calloc(1, 8)
let Y1: ptr<i64> = calloc(1, 8)
split_u(y, Y0, Y1)
let result: i64 = 0
if (k & 1) == 0 {
# k even: k = 2m
if Y1[0] == 0 {
result = 1 << m
} else {
let P: i64 = Y0[0] ^ (1 << m)
let g: i64 = gf2_gcd(P, Y1[0] << 1)
result = 1 << (63 - clz64(g))
}
} else {
# k odd: k = 2m+1
if Y0[0] == 0 {
result = 1 << (m + 1)
} else {
let uQ: i64 = ((1 << m) ^ Y1[0]) << 1
let g: i64 = gf2_gcd(Y0[0], uQ)
result = 1 << (63 - clz64(g))
}
}
free(Y0)
free(Y1)
return result
}
# Ordered count of pairs (a,b) with 0<=a,b<=N
function ordered_S(N: i64) -> i64 {
if N < 0 { return 0 }
if N == 0 { return 1 }
let bits: i32 = 64 - clz64(N)
let all_ones: i64 = (1 << bits) - 1
if N == all_ones {
return ordered_full(bits)
}
let k: i32 = bits - 1
let M: i64 = 1 << k
let r: i64 = N - M
let base: i64 = ordered_full(k)
let mut cross: i64 = 0
let mut y: i64 = 0
while y <= r {
cross = cross + count_a_for_upper(k, y)
y = y + 1
}
return base + 2 * cross
}
function main() -> i32 {
build_tables()
let mut N: i64 = 1
let mut i: i32 = 0
while i < 7 {
N = N * 10
i = i + 1
}
let s: i64 = ordered_S(N)
printf("%lld\n", (s + 1) / 2)
free(even16)
free(odd16)
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; }
int32_t clz64_i64(int64_t x);
void build_tables(void);
void split_u_i64_ptr_i64_ptr_i64(int64_t x, int64_t* E, int64_t* O);
int64_t gf2_mod_i64_i64(int64_t a, int64_t b);
int64_t gf2_gcd_i64_i64(int64_t a, int64_t b);
int64_t ordered_full_i32(int32_t bits);
int64_t count_a_for_upper_i32_i64(int32_t k, int64_t y);
int64_t ordered_S_i64(int64_t N);
int32_t main(void);
/* Module statics */
static int64_t* even16 = ((int64_t*)(NULL));
static int64_t* odd16 = ((int64_t*)(NULL));
int32_t clz64_i64(int64_t x) {
if (x == 0) {
return 64;
}
if (x < 0) {
return 0;
}
int32_t n = 0;
int64_t v = x;
while (v != 0) {
v = FLOW_CHECKED_SHR((v), (1));
n = (n + 1);
}
return (64 - n);
}
void build_tables(void) {
even16 = calloc(65536, 8);
odd16 = calloc(65536, 8);
int32_t __flow_step_1 = 1;
for (int32_t x = 0; (0 <= 65536) ? x < 65536 : x > 65536; x += (0 <= 65536) ? 1 : -1) {
int64_t e = 0;
int64_t o = 0;
int32_t __flow_step_2 = 1;
for (int32_t i = 0; (0 <= 8) ? i < 8 : i > 8; i += (0 <= 8) ? 1 : -1) {
int64_t b = FLOW_CHECKED_SHL((1), ((2 * i)));
if ((((int64_t)(x)) & b) != 0) {
e = (e | FLOW_CHECKED_SHL((1), (i)));
}
if ((((int64_t)(x)) & FLOW_CHECKED_SHL((b), (1))) != 0) {
o = (o | FLOW_CHECKED_SHL((1), (i)));
}
}
even16[x] = e;
odd16[x] = o;
}
}
void split_u_i64_ptr_i64_ptr_i64(int64_t x, int64_t* E, int64_t* O) {
int64_t e = 0;
int64_t o = 0;
int32_t shift = 0;
int64_t val = x;
while (val != 0) {
int64_t chunk = (val & 65535);
e = (e | FLOW_CHECKED_SHL((even16[chunk]), (shift)));
o = (o | FLOW_CHECKED_SHL((odd16[chunk]), (shift)));
val = FLOW_CHECKED_SHR((val), (16));
if (val < 0) {
val = (val & 281474976710655);
}
shift = (shift + 8);
}
E[0] = e;
O[0] = o;
}
int64_t gf2_mod_i64_i64(int64_t a, int64_t b) {
if (b == 0) {
return a;
}
int32_t db = (63 - clz64_i64(b));
int64_t val = a;
while (val != 0) {
int32_t da = (63 - clz64_i64(val));
if (da < db) {
break;
}
val = (val ^ FLOW_CHECKED_SHL((b), ((da - db))));
}
return val;
}
int64_t gf2_gcd_i64_i64(int64_t a, int64_t b) {
int64_t x = a;
int64_t y = b;
while (y != 0) {
int64_t t = gf2_mod_i64_i64(x, y);
x = y;
y = t;
}
return x;
}
int64_t ordered_full_i32(int32_t bits) {
int64_t sign = 1;
if ((bits & 1) != 0) {
sign = (-1);
}
int64_t num = ((FLOW_CHECKED_SHL((1), ((bits + 1))) * ((3 * ((int64_t)(bits))) + 4)) + sign);
return FLOW_CHECKED_DIV((num), (9));
}
int64_t count_a_for_upper_i32_i64(int32_t k, int64_t y) {
int32_t m = FLOW_CHECKED_DIV((k), (2));
int64_t* Y0 = (int64_t*)(calloc(1, 8));
int64_t* Y1 = (int64_t*)(calloc(1, 8));
split_u_i64_ptr_i64_ptr_i64(y, Y0, Y1);
int64_t result = 0;
if ((k & 1) == 0) {
if (Y1[0] == 0) {
result = FLOW_CHECKED_SHL((1), (m));
} else {
int64_t P = (Y0[0] ^ FLOW_CHECKED_SHL((1), (m)));
int64_t g = gf2_gcd_i64_i64(P, FLOW_CHECKED_SHL((Y1[0]), (1)));
result = FLOW_CHECKED_SHL((1), ((63 - clz64_i64(g))));
}
} else {
if (Y0[0] == 0) {
result = FLOW_CHECKED_SHL((1), ((m + 1)));
} else {
int64_t uQ = FLOW_CHECKED_SHL(((FLOW_CHECKED_SHL((1), (m)) ^ Y1[0])), (1));
int64_t g = gf2_gcd_i64_i64(Y0[0], uQ);
result = FLOW_CHECKED_SHL((1), ((63 - clz64_i64(g))));
}
}
free(Y0);
free(Y1);
return result;
}
int64_t ordered_S_i64(int64_t N) {
if (N < 0) {
return 0;
}
if (N == 0) {
return 1;
}
int32_t bits = (64 - clz64_i64(N));
int64_t all_ones = (FLOW_CHECKED_SHL((1), (bits)) - 1);
if (N == all_ones) {
return ordered_full_i32(bits);
}
int32_t k = (bits - 1);
int64_t M = FLOW_CHECKED_SHL((1), (k));
int64_t r = (N - M);
int64_t base = ordered_full_i32(k);
int64_t cross = 0;
int64_t y = 0;
while (y <= r) {
cross = (cross + count_a_for_upper_i32_i64(k, y));
y = (y + 1);
}
return (base + (2 * cross));
}
int32_t main(void) {
build_tables();
int64_t N = 1;
int32_t i = 0;
while (i < 7) {
N = (N * 10);
i = (i + 1);
}
int64_t s = ordered_S_i64(N);
printf("%lld\n", FLOW_CHECKED_DIV(((s + 1)), (2)));
free(even16);
free(odd16);
return 0;
}