P(n) = sum_{d<=n} A(d) * M(floor(n/d)) mod 1020340567, n = 10^7. A(1)=2, A(d)=2^(d-1). M is the Mertens function. Floor-division grouping. Pure Flow port of the native C solver. Uses i128 for mod_pow intermediates.
# Project Euler 802: Iterated Composition.
# P(n) = sum_{d<=n} A(d) * M(floor(n/d)) mod 1020340567, n = 10^7.
# A(1)=2, A(d)=2^(d-1). M is the Mertens function. Floor-division grouping.
# Pure Flow port of the native C solver. Uses i128 for mod_pow intermediates.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 1020340567
const N_DEFAULT: i64 = 10000000
function mod_pow(a0: i64, e0: i64, modv: i64) -> i64 {
let mut r: i128 = 1 % (modv as i128)
let mut a: i128 = a0 % (modv as i128)
let mut e: i64 = e0
while e > 0 {
if e % 2 == 1 {
r = r * a % (modv as i128)
}
a = a * a % (modv as i128)
e = e / 2
}
return r as i64
}
# Build sorted ascending distinct values of floor(n / k) for k=1..n.
function build_floor_div_queries(n: i64, qs: ptr<i64>) -> i64 {
let mut cnt: i64 = 0
let mut k: i64 = 1
while k <= n {
let q: i64 = n / k
qs[cnt] = q
cnt = cnt + 1
k = n / q + 1
}
# qs is strictly decreasing; reverse to ascending.
let mut i: i64 = 0
let mut j: i64 = cnt - 1
while i < j {
let t: i64 = qs[i]
qs[i] = qs[j]
qs[j] = t
i = i + 1
j = j - 1
}
return cnt
}
# Compute Mertens M(t) at the selected ascending query points via linear sieve.
function mertens_at_points(n: i64, points: ptr<i64>, npoints: i64, res: ptr<i64>) -> void {
if npoints == 0 {
return
}
let mu: ptr<i8> = calloc(n + 1, 1) as ptr<i8>
let lp: ptr<i64> = calloc(n + 1, 8) as ptr<i64>
let primes: ptr<i64> = calloc(n, 8) as ptr<i64>
let mut nprimes: i64 = 0
mu[1] = 1
let mut mertens: i64 = 1
let mut idx: i64 = 0
if points[0] == 1 {
res[0] = 1
idx = 1
}
let mut i: i64 = 2
while i <= n {
if lp[i] == 0 {
lp[i] = i
primes[nprimes] = i
nprimes = nprimes + 1
mu[i] = -1
}
let li: i64 = lp[i]
let mui: i8 = mu[i]
let mut pi: i64 = 0
while pi < nprimes {
let p: i64 = primes[pi]
if p > li {
pi = nprimes
} else {
let ip: i64 = i * p
if ip > n {
pi = nprimes
} else {
lp[ip] = p
if p == li {
mu[ip] = 0
pi = nprimes
} else {
mu[ip] = -mui
}
pi = pi + 1
}
}
}
mertens = mertens + (mu[i] as i64)
if idx < npoints && i == points[idx] {
res[idx] = mertens
idx = idx + 1
}
i = i + 1
}
free(mu)
free(lp)
free(primes)
}
# Sum_{d=l..r} A(d) mod MOD, A(1)=2, A(d)=2^(d-1) for d>=2.
function sum_A(l: i64, r: i64) -> i64 {
if l == 1 {
return mod_pow(2, r, MOD)
}
let mut v: i64 = (mod_pow(2, r, MOD) - mod_pow(2, l - 1, MOD)) % MOD
if v < 0 {
v = v + MOD
}
return v
}
function P_mod(n: i64) -> i64 {
if n <= 0 {
return 0
}
let qs: ptr<i64> = calloc(2 * 64000, 8) as ptr<i64>
let nq: i64 = build_floor_div_queries(n, qs)
let mertens: ptr<i64> = calloc(nq, 8) as ptr<i64>
mertens_at_points(n, qs, nq, mertens)
let mut ans: i64 = 0
let mut l: i64 = 1
while l <= n {
let q: i64 = n / l
let r: i64 = n / q
# binary search for q in ascending qs
let mut lo: i64 = 0
let mut hi: i64 = nq - 1
let mut found: i64 = -1
while lo <= hi {
let mid: i64 = (lo + hi) / 2
if qs[mid] == q {
found = mid
lo = hi + 1
} else {
if qs[mid] < q {
lo = mid + 1
} else {
hi = mid - 1
}
}
}
let m: i64 = mertens[found]
let s: i64 = sum_A(l, r)
let term: i128 = (s as i128) * (m as i128) % (MOD as i128)
ans = (ans + (term as i64)) % MOD
if ans < 0 {
ans = ans + MOD
}
l = r + 1
}
free(qs)
free(mertens)
return ans
}
function main() -> i32 {
printf("%lld\n", P_mod(N_DEFAULT))
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 modv);
int64_t build_floor_div_queries_i64_ptr_i64(int64_t n, int64_t* qs);
void mertens_at_points_i64_ptr_i64_i64_ptr_i64(int64_t n, int64_t* points, int64_t npoints, int64_t* res);
int64_t sum_A_i64_i64(int64_t l, int64_t r);
int64_t P_mod_i64(int64_t n);
int32_t main(void);
static const int64_t MOD = 1020340567;
static const int64_t N_DEFAULT = 10000000;
int64_t mod_pow_i64_i64_i64(int64_t a0, int64_t e0, int64_t modv) {
__int128 r = FLOW_CHECKED_MOD((1), (((__int128)(modv))));
__int128 a = FLOW_CHECKED_MOD((a0), (((__int128)(modv))));
int64_t e = e0;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = FLOW_CHECKED_MOD(((r * a)), (((__int128)(modv))));
}
a = FLOW_CHECKED_MOD(((a * a)), (((__int128)(modv))));
e = FLOW_CHECKED_DIV((e), (2));
}
return ((int64_t)(r));
}
int64_t build_floor_div_queries_i64_ptr_i64(int64_t n, int64_t* qs) {
int64_t cnt = 0;
int64_t k = 1;
while (k <= n) {
int64_t q = FLOW_CHECKED_DIV((n), (k));
qs[cnt] = q;
cnt = (cnt + 1);
k = (FLOW_CHECKED_DIV((n), (q)) + 1);
}
int64_t i = 0;
int64_t j = (cnt - 1);
while (i < j) {
int64_t t = qs[i];
qs[i] = qs[j];
qs[j] = t;
i = (i + 1);
j = (j - 1);
}
return cnt;
}
void mertens_at_points_i64_ptr_i64_i64_ptr_i64(int64_t n, int64_t* points, int64_t npoints, int64_t* res) {
if (npoints == 0) {
return;
}
int8_t* mu = (int8_t*)(((int8_t*)(calloc((n + 1), 1))));
int64_t* lp = (int64_t*)(((int64_t*)(calloc((n + 1), 8))));
int64_t* primes = (int64_t*)(((int64_t*)(calloc(n, 8))));
int64_t nprimes = 0;
mu[1] = 1;
int64_t mertens = 1;
int64_t idx = 0;
if (points[0] == 1) {
res[0] = 1;
idx = 1;
}
int64_t i = 2;
while (i <= n) {
if (lp[i] == 0) {
lp[i] = i;
primes[nprimes] = i;
nprimes = (nprimes + 1);
mu[i] = (-1);
}
int64_t li = lp[i];
int8_t mui = mu[i];
int64_t pi = 0;
while (pi < nprimes) {
int64_t p = primes[pi];
if (p > li) {
pi = nprimes;
} else {
int64_t ip = (i * p);
if (ip > n) {
pi = nprimes;
} else {
lp[ip] = p;
if (p == li) {
mu[ip] = 0;
pi = nprimes;
} else {
mu[ip] = (-mui);
}
pi = (pi + 1);
}
}
}
mertens = (mertens + ((int64_t)(mu[i])));
if ((idx < npoints && i == points[idx])) {
res[idx] = mertens;
idx = (idx + 1);
}
i = (i + 1);
}
free(mu);
free(lp);
free(primes);
}
int64_t sum_A_i64_i64(int64_t l, int64_t r) {
if (l == 1) {
return mod_pow_i64_i64_i64(2, r, MOD);
}
int64_t v = FLOW_CHECKED_MOD(((mod_pow_i64_i64_i64(2, r, MOD) - mod_pow_i64_i64_i64(2, (l - 1), MOD))), (MOD));
if (v < 0) {
v = (v + MOD);
}
return v;
}
int64_t P_mod_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t* qs = (int64_t*)(((int64_t*)(calloc((2 * 64000), 8))));
int64_t nq = build_floor_div_queries_i64_ptr_i64(n, qs);
int64_t* mertens = (int64_t*)(((int64_t*)(calloc(nq, 8))));
mertens_at_points_i64_ptr_i64_i64_ptr_i64(n, qs, nq, mertens);
int64_t ans = 0;
int64_t l = 1;
while (l <= n) {
int64_t q = FLOW_CHECKED_DIV((n), (l));
int64_t r = FLOW_CHECKED_DIV((n), (q));
int64_t lo = 0;
int64_t hi = (nq - 1);
int64_t found = (-1);
while (lo <= hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
if (qs[mid] == q) {
found = mid;
lo = (hi + 1);
} else {
if (qs[mid] < q) {
lo = (mid + 1);
} else {
hi = (mid - 1);
}
}
}
int64_t m = mertens[found];
int64_t s = sum_A_i64_i64(l, r);
__int128 term = FLOW_CHECKED_MOD(((((__int128)(s)) * ((__int128)(m)))), (((__int128)(MOD))));
ans = FLOW_CHECKED_MOD(((ans + ((int64_t)(term)))), (MOD));
if (ans < 0) {
ans = (ans + MOD);
}
l = (r + 1);
}
free(qs);
free(mertens);
return ans;
}
int32_t main(void) {
printf("%lld\n", P_mod_i64(N_DEFAULT));
return 0;
}