# Project Euler 550
# Divisor Game - nimber XOR convolution via FWHT.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 987654321
function mod_pow(a0: i64, e0: i64, mod: i64) -> i64 {
let mut r: i64 = 1 % mod
let mut a: i64 = a0 % mod
if a < 0 { a = a + mod }
let mut e: i64 = e0
while e > 0 {
if (e & 1) != 0 {
r = ((r as i128) * (a as i128) % (mod as i128)) as i64
}
a = ((a as i128) * (a as i128) % (mod as i128)) as i64
e = e >> 1
}
return r
}
function mod_inv(a0: i64, mod: i64) -> i64 {
let mut old_r: i64 = a0 % mod
let mut r: i64 = mod
let mut old_s: i64 = 1
let mut s: i64 = 0
if old_r < 0 { old_r = old_r + mod }
while r != 0 {
let q: i64 = old_r / r
let t: i64 = old_r - q * r
old_r = r
r = t
let t2: i64 = old_s - q * s
old_s = s
s = t2
}
if old_s < 0 { old_s = old_s + mod }
return old_s % mod
}
function fwht_xor(a: ptr<i64>, n: i64, mod: i64) -> void {
let mut h: i64 = 1
while h < n {
let step: i64 = h << 1
let mut i: i64 = 0
while i < n {
let mut j: i64 = i
while j < i + h {
let x: i64 = a[j]
let y: i64 = a[j + h]
let mut s: i64 = x + y
if s >= mod { s = s - mod }
let mut d: i64 = x - y
if d < 0 { d = d + mod }
a[j] = s
a[j + h] = d
j = j + 1
}
i = i + step
}
h = h << 1
}
}
function omega_counts_up_to(n: i64, counts: ptr<i64>) -> i64 {
let spf: ptr<i32> = calloc(n + 1, 4)
let omega: ptr<i8> = calloc(n + 1, 1)
let primes: ptr<i32> = calloc(700000, 4)
if spf == null || omega == null || primes == null {
free(spf); free(omega); free(primes)
return -1
}
let mut z: i64 = 0
while z < 32 {
counts[z] = 0
z = z + 1
}
let mut np: i64 = 0
let mut max_om: i64 = 0
let mut i: i64 = 2
while i <= n {
let si: i32 = spf[i]
let oi: i64
if si == 0 {
spf[i] = i as i32
primes[np] = i as i32
np = np + 1
omega[i] = 1
oi = 1
} else {
oi = omega[i] as i64
}
counts[oi] = counts[oi] + 1
if oi > max_om { max_om = oi }
let mut j: i64 = 0
while j < np {
let p: i64 = primes[j] as i64
let ip: i64 = i * p
if ip > n { break }
spf[ip] = p as i32
omega[ip] = (oi + 1) as i8
if p == (si as i64) { break }
j = j + 1
}
i = i + 1
}
free(spf)
free(omega)
free(primes)
return max_om
}
function compute_h_sequence(tmax: i64, h: ptr<i64>) -> i64 {
if tmax <= 0 { return 0 }
h[1] = 0
let mut max_g: i64 = 0
let reachable: ptr<i64> = calloc(64, 8)
let mut t: i64 = 2
while t <= tmax {
let mut z: i64 = 0
while z < 64 {
reachable[z] = 0
z = z + 1
}
let mut i: i64 = 1
while i < t {
let mut j: i64 = 1
while j < t {
let v: i64 = h[i] ^ h[j]
let idx: i64 = (v >> 6) & 63
let bit: i64 = (1 as i64) << (v & 63)
reachable[idx] = reachable[idx] | bit
j = j + 1
}
i = i + 1
}
let mut mex: i64 = 0
while mex < 4096 {
let idx: i64 = (mex >> 6) & 63
let bit: i64 = (1 as i64) << (mex & 63)
if (reachable[idx] & bit) != 0 {
mex = mex + 1
} else {
break
}
}
h[t] = mex
if mex > max_g { max_g = mex }
t = t + 1
}
free(reachable)
return max_g
}
function f(n: i64, k: i64) -> i64 {
let counts: ptr<i64> = calloc(32, 8)
let max_om: i64 = omega_counts_up_to(n, counts)
if max_om < 0 { free(counts); return 0 }
let h: ptr<i64> = calloc(32, 8)
let max_g: i64 = compute_h_sequence(max_om, h)
let mut size: i64 = 1
while size <= max_g { size = size << 1 }
let arr: ptr<i64> = calloc(size, 8)
if arr == null { free(counts); free(h); return 0 }
let mut t: i64 = 1
while t <= max_om {
arr[h[t]] = (arr[h[t]] + counts[t]) % MOD
t = t + 1
}
fwht_xor(arr, size, MOD)
let mut s: i64 = 0
let mut i: i64 = 0
while i < size {
s = (s + mod_pow(arr[i], k, MOD)) % MOD
i = i + 1
}
let losing: i64 = ((s as i128) * (mod_inv(size, MOD) as i128) % (MOD as i128)) as i64
let total: i64 = mod_pow(n - 1, k, MOD)
let mut ans: i64 = (total - losing) % MOD
if ans < 0 { ans = ans + MOD }
free(arr)
free(counts)
free(h)
return ans
}
function main() -> i32 {
printf("%lld\n", f(10000000, 1000000000000))
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 mod_pow_i64_i64_i64(int64_t a0, int64_t e0, int64_t mod);
int64_t mod_inv_i64_i64(int64_t a0, int64_t mod);
void fwht_xor_ptr_i64_i64_i64(int64_t* a, int64_t n, int64_t mod);
int64_t omega_counts_up_to_i64_ptr_i64(int64_t n, int64_t* counts);
int64_t compute_h_sequence_i64_ptr_i64(int64_t tmax, int64_t* h);
int64_t f_i64_i64(int64_t n, int64_t k);
int32_t main(void);
static const int64_t MOD = 987654321;
int64_t mod_pow_i64_i64_i64(int64_t a0, int64_t e0, int64_t mod) {
int64_t r = FLOW_CHECKED_MOD((1), (mod));
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
if (a < 0) {
a = (a + mod);
}
int64_t e = e0;
while (e > 0) {
if ((e & 1) != 0) {
r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(a)))), (((__int128)(mod))))));
}
a = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(a)) * ((__int128)(a)))), (((__int128)(mod))))));
e = FLOW_CHECKED_SHR((e), (1));
}
return r;
}
int64_t mod_inv_i64_i64(int64_t a0, int64_t mod) {
int64_t old_r = FLOW_CHECKED_MOD((a0), (mod));
int64_t r = mod;
int64_t old_s = 1;
int64_t s = 0;
if (old_r < 0) {
old_r = (old_r + mod);
}
while (r != 0) {
int64_t q = FLOW_CHECKED_DIV((old_r), (r));
int64_t t = (old_r - (q * r));
old_r = r;
r = t;
int64_t t2 = (old_s - (q * s));
old_s = s;
s = t2;
}
if (old_s < 0) {
old_s = (old_s + mod);
}
return FLOW_CHECKED_MOD((old_s), (mod));
}
void fwht_xor_ptr_i64_i64_i64(int64_t* a, int64_t n, int64_t mod) {
int64_t h = 1;
while (h < n) {
int64_t step = FLOW_CHECKED_SHL((h), (1));
int64_t i = 0;
while (i < n) {
int64_t j = i;
while (j < (i + h)) {
int64_t x = a[j];
int64_t y = a[(j + h)];
int64_t s = (x + y);
if (s >= mod) {
s = (s - mod);
}
int64_t d = (x - y);
if (d < 0) {
d = (d + mod);
}
a[j] = s;
a[(j + h)] = d;
j = (j + 1);
}
i = (i + step);
}
h = FLOW_CHECKED_SHL((h), (1));
}
}
int64_t omega_counts_up_to_i64_ptr_i64(int64_t n, int64_t* counts) {
int32_t* spf = (int32_t*)(calloc((n + 1), 4));
int8_t* omega = (int8_t*)(calloc((n + 1), 1));
int32_t* primes = (int32_t*)(calloc(700000, 4));
if (((spf == NULL || omega == NULL) || primes == NULL)) {
free(spf);
free(omega);
free(primes);
return (-1);
}
int64_t z = 0;
while (z < 32) {
counts[z] = 0;
z = (z + 1);
}
int64_t np = 0;
int64_t max_om = 0;
int64_t i = 2;
while (i <= n) {
int32_t si = spf[i];
int64_t oi;
if (si == 0) {
spf[i] = ((int32_t)(i));
primes[np] = ((int32_t)(i));
np = (np + 1);
omega[i] = 1;
oi = 1;
} else {
oi = ((int64_t)(omega[i]));
}
counts[oi] = (counts[oi] + 1);
if (oi > max_om) {
max_om = oi;
}
int64_t j = 0;
while (j < np) {
int64_t p = ((int64_t)(primes[j]));
int64_t ip = (i * p);
if (ip > n) {
break;
}
spf[ip] = ((int32_t)(p));
omega[ip] = ((int8_t)((oi + 1)));
if (p == ((int64_t)(si))) {
break;
}
j = (j + 1);
}
i = (i + 1);
}
free(spf);
free(omega);
free(primes);
return max_om;
}
int64_t compute_h_sequence_i64_ptr_i64(int64_t tmax, int64_t* h) {
if (tmax <= 0) {
return 0;
}
h[1] = 0;
int64_t max_g = 0;
int64_t* reachable = (int64_t*)(calloc(64, 8));
int64_t t = 2;
while (t <= tmax) {
int64_t z = 0;
while (z < 64) {
reachable[z] = 0;
z = (z + 1);
}
int64_t i = 1;
while (i < t) {
int64_t j = 1;
while (j < t) {
int64_t v = (h[i] ^ h[j]);
int64_t idx = (FLOW_CHECKED_SHR((v), (6)) & 63);
int64_t bit = FLOW_CHECKED_SHL((((int64_t)(1))), ((v & 63)));
reachable[idx] = (reachable[idx] | bit);
j = (j + 1);
}
i = (i + 1);
}
int64_t mex = 0;
while (mex < 4096) {
int64_t idx = (FLOW_CHECKED_SHR((mex), (6)) & 63);
int64_t bit = FLOW_CHECKED_SHL((((int64_t)(1))), ((mex & 63)));
if ((reachable[idx] & bit) != 0) {
mex = (mex + 1);
} else {
break;
}
}
h[t] = mex;
if (mex > max_g) {
max_g = mex;
}
t = (t + 1);
}
free(reachable);
return max_g;
}
int64_t f_i64_i64(int64_t n, int64_t k) {
int64_t* counts = (int64_t*)(calloc(32, 8));
int64_t max_om = omega_counts_up_to_i64_ptr_i64(n, counts);
if (max_om < 0) {
free(counts);
return 0;
}
int64_t* h = (int64_t*)(calloc(32, 8));
int64_t max_g = compute_h_sequence_i64_ptr_i64(max_om, h);
int64_t size = 1;
while (size <= max_g) {
size = FLOW_CHECKED_SHL((size), (1));
}
int64_t* arr = (int64_t*)(calloc(size, 8));
if (arr == NULL) {
free(counts);
free(h);
return 0;
}
int64_t t = 1;
while (t <= max_om) {
arr[h[t]] = FLOW_CHECKED_MOD(((arr[h[t]] + counts[t])), (MOD));
t = (t + 1);
}
fwht_xor_ptr_i64_i64_i64(arr, size, MOD);
int64_t s = 0;
int64_t i = 0;
while (i < size) {
s = FLOW_CHECKED_MOD(((s + mod_pow_i64_i64_i64(arr[i], k, MOD))), (MOD));
i = (i + 1);
}
int64_t losing = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(s)) * ((__int128)(mod_inv_i64_i64(size, MOD))))), (((__int128)(MOD))))));
int64_t total = mod_pow_i64_i64_i64((n - 1), k, MOD);
int64_t ans = FLOW_CHECKED_MOD(((total - losing)), (MOD));
if (ans < 0) {
ans = (ans + MOD);
}
free(arr);
free(counts);
free(h);
return ans;
}
int32_t main(void) {
printf("%lld\n", f_i64_i64(10000000, 1000000000000));
return 0;
}