# Project Euler 478
# Mixtures
# E(10^7) mod 11^8 via Möbius / Mertens direction counts.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 214358881
const PHI_MOD: i64 = 389743420
const N: i64 = 10000000
let mut MERTENS: ptr<i32> = null
function modpow(base0: i64, exp0: i64, m: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base0 % m
let mut e: i64 = exp0
while e > 0 {
if e % 2 == 1 {
r = (r * b) % m
}
b = (b * b) % m
e = e / 2
}
return r
}
function F(s: i64, n: i64) -> i64 {
let mut ret: i64 = 0
let mut i: i64 = 1
while i * s <= n {
let j: i64 = n / (n / i) + 1
let t: i64 = n / i
let d: i64 = t / s
let mut expr: i64 = (2 * (t % PHI_MOD) % PHI_MOD + 2 - s * (1 + d)) % PHI_MOD
if expr < 0 { expr = expr + PHI_MOD }
let g: i64 = expr * d % PHI_MOD / 2
let dm: i64 = (MERTENS[j - 1] as i64) - (MERTENS[i - 1] as i64)
ret = ret + g * dm % PHI_MOD
i = j
}
ret = ret % PHI_MOD
if ret < 0 { ret = ret + PHI_MOD }
return ret
}
function main() -> i32 {
let lp: ptr<i32> = calloc(N + 1, 4)
let mu: ptr<i32> = calloc(N + 1, 4)
let phi: ptr<i32> = calloc(N + 1, 4)
MERTENS = calloc(N + 1, 4)
let primes: ptr<i32> = calloc(N / 5 + 10, 4)
if lp == null || mu == null || phi == null || MERTENS == null || primes == null { return 1 }
mu[1] = 1
phi[1] = 1
let mut pc: i64 = 0
let mut i: i64 = 2
while i <= N {
if lp[i] == 0 {
lp[i] = i as i32
primes[pc] = i as i32
pc = pc + 1
mu[i] = -1
phi[i] = (i - 1) as i32
}
let mut j: i64 = 0
while j < pc {
let p: i64 = primes[j] as i64
if p > (lp[i] as i64) { break }
if i * p > N { break }
lp[i * p] = p as i32
if p == (lp[i] as i64) {
mu[i * p] = 0
phi[i * p] = (phi[i] as i64 * p) as i32
break
} else {
mu[i * p] = (0 - (mu[i] as i64)) as i32
phi[i * p] = (phi[i] as i64 * (p - 1)) as i32
}
j = j + 1
}
i = i + 1
}
i = 1
while i <= N {
MERTENS[i] = (MERTENS[i - 1] as i64 + mu[i] as i64) as i32
i = i + 1
}
let mut total: i64 = 0
i = 1
while i <= N {
let x: i64 = N / i + 1
let cube: i64 = x * x % PHI_MOD * x % PHI_MOD
let mut term: i64 = (cube - 1) % PHI_MOD
if term < 0 { term = term + PHI_MOD }
total = total + (mu[i] as i64) * term % PHI_MOD
i = i + 1
}
total = total % PHI_MOD
if total < 0 { total = total + PHI_MOD }
let mut ans: i64 = modpow(2, total, MOD)
let e1: i64 = (total - 1) / 2
let pow_e1: i64 = modpow(2, e1, MOD)
let mut b: i64 = 1
while b <= N {
let M: i64 = 6 * (phi[b] as i64)
let f: i64 = F(b, N)
let e2: i64 = e1 - f + PHI_MOD
let mut term2: i64 = (pow_e1 - modpow(2, e2, MOD)) % MOD
if term2 < 0 { term2 = term2 + MOD }
ans = ans - (M % MOD) * term2 % MOD
ans = ans % MOD
if ans < 0 { ans = ans + MOD }
b = b + 1
}
ans = ans - 1
ans = ans % MOD
if ans < 0 { ans = ans + MOD }
printf("%lld\n", ans)
free(primes)
free(MERTENS)
MERTENS = null
free(phi)
free(mu)
free(lp)
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 modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t m);
int64_t F_i64_i64(int64_t s, int64_t n);
int32_t main(void);
static const int64_t MOD = 214358881;
static const int64_t PHI_MOD = 389743420;
static const int64_t N = 10000000;
/* Module statics */
static int32_t* MERTENS = NULL;
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t m) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base0), (m));
int64_t e = exp0;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = FLOW_CHECKED_MOD(((r * b)), (m));
}
b = FLOW_CHECKED_MOD(((b * b)), (m));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t F_i64_i64(int64_t s, int64_t n) {
int64_t ret = 0;
int64_t i = 1;
while ((i * s) <= n) {
int64_t j = (FLOW_CHECKED_DIV((n), (FLOW_CHECKED_DIV((n), (i)))) + 1);
int64_t t = FLOW_CHECKED_DIV((n), (i));
int64_t d = FLOW_CHECKED_DIV((t), (s));
int64_t expr = FLOW_CHECKED_MOD((((FLOW_CHECKED_MOD(((2 * FLOW_CHECKED_MOD((t), (PHI_MOD)))), (PHI_MOD)) + 2) - (s * (1 + d)))), (PHI_MOD));
if (expr < 0) {
expr = (expr + PHI_MOD);
}
int64_t g = FLOW_CHECKED_DIV((FLOW_CHECKED_MOD(((expr * d)), (PHI_MOD))), (2));
int64_t dm = (((int64_t)(MERTENS[(j - 1)])) - ((int64_t)(MERTENS[(i - 1)])));
ret = (ret + FLOW_CHECKED_MOD(((g * dm)), (PHI_MOD)));
i = j;
}
ret = FLOW_CHECKED_MOD((ret), (PHI_MOD));
if (ret < 0) {
ret = (ret + PHI_MOD);
}
return ret;
}
int32_t main(void) {
int32_t* lp = (int32_t*)(calloc((N + 1), 4));
int32_t* mu = (int32_t*)(calloc((N + 1), 4));
int32_t* phi = (int32_t*)(calloc((N + 1), 4));
MERTENS = calloc((N + 1), 4);
int32_t* primes = (int32_t*)(calloc((FLOW_CHECKED_DIV((N), (5)) + 10), 4));
if (((((lp == NULL || mu == NULL) || phi == NULL) || MERTENS == NULL) || primes == NULL)) {
return 1;
}
mu[1] = 1;
phi[1] = 1;
int64_t pc = 0;
int64_t i = 2;
while (i <= N) {
if (lp[i] == 0) {
lp[i] = ((int32_t)(i));
primes[pc] = ((int32_t)(i));
pc = (pc + 1);
mu[i] = (-1);
phi[i] = ((int32_t)((i - 1)));
}
int64_t j = 0;
while (j < pc) {
int64_t p = ((int64_t)(primes[j]));
if (p > ((int64_t)(lp[i]))) {
break;
}
if ((i * p) > N) {
break;
}
lp[(i * p)] = ((int32_t)(p));
if (p == ((int64_t)(lp[i]))) {
mu[(i * p)] = 0;
phi[(i * p)] = ((int32_t)((((int64_t)(phi[i])) * p)));
break;
} else {
mu[(i * p)] = ((int32_t)((0 - ((int64_t)(mu[i])))));
phi[(i * p)] = ((int32_t)((((int64_t)(phi[i])) * (p - 1))));
}
j = (j + 1);
}
i = (i + 1);
}
i = 1;
while (i <= N) {
MERTENS[i] = ((int32_t)((((int64_t)(MERTENS[(i - 1)])) + ((int64_t)(mu[i])))));
i = (i + 1);
}
int64_t total = 0;
i = 1;
while (i <= N) {
int64_t x = (FLOW_CHECKED_DIV((N), (i)) + 1);
int64_t cube = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((x * x)), (PHI_MOD)) * x)), (PHI_MOD));
int64_t term = FLOW_CHECKED_MOD(((cube - 1)), (PHI_MOD));
if (term < 0) {
term = (term + PHI_MOD);
}
total = (total + FLOW_CHECKED_MOD(((((int64_t)(mu[i])) * term)), (PHI_MOD)));
i = (i + 1);
}
total = FLOW_CHECKED_MOD((total), (PHI_MOD));
if (total < 0) {
total = (total + PHI_MOD);
}
int64_t ans = modpow_i64_i64_i64(2, total, MOD);
int64_t e1 = FLOW_CHECKED_DIV(((total - 1)), (2));
int64_t pow_e1 = modpow_i64_i64_i64(2, e1, MOD);
int64_t b = 1;
while (b <= N) {
int64_t M = (6 * ((int64_t)(phi[b])));
int64_t f = F_i64_i64(b, N);
int64_t e2 = ((e1 - f) + PHI_MOD);
int64_t term2 = FLOW_CHECKED_MOD(((pow_e1 - modpow_i64_i64_i64(2, e2, MOD))), (MOD));
if (term2 < 0) {
term2 = (term2 + MOD);
}
ans = (ans - FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((M), (MOD)) * term2)), (MOD)));
ans = FLOW_CHECKED_MOD((ans), (MOD));
if (ans < 0) {
ans = (ans + MOD);
}
b = (b + 1);
}
ans = (ans - 1);
ans = FLOW_CHECKED_MOD((ans), (MOD));
if (ans < 0) {
ans = (ans + MOD);
}
printf("%lld\n", ans);
free(primes);
free(MERTENS);
MERTENS = NULL;
free(phi);
free(mu);
free(lp);
return 0;
}