# Project Euler 875
# Quadruple Congruence: Q(12345678) mod 1001961001.
# Pure Flow port of the native C solver.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function malloc(n: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function memset(s: ptr<void>, c: i64, n: i64) -> ptr<void>
}
const MOD: i64 = 1001961001
const TARGET_N: i64 = 12345678
let mut spf: ptr<i32> = null
let mut primes: ptr<i32> = null
let mut nprimes: i32 = 0
let mut f_arr: ptr<i32> = null
function mulmod(a: i64, b: i64, m: i64) -> i64 {
let r: i128 = ((a as i128) * (b as i128)) % (m as i128)
return r as i64
}
function powmod(a0: i64, e0: i64, m: i64) -> i64 {
let mut r: i64 = 1 % m
let mut a: i64 = a0 % m
let mut e: i64 = e0
while e > 0 {
if e % 2 == 1 { r = mulmod(r, a, m) }
a = mulmod(a, a, m)
e = e / 2
}
return r
}
function q_prime_power_mod(p: i64, k: i32, mod: i64) -> i64 {
if p == 2 {
if k == 1 { return 128 % mod }
let r: i64 = 8 % mod
let mut g: i64 = 0
let mut cur: i64 = 1
let mut i: i32 = 0
while i < k - 1 {
g = g + cur
if g >= mod { g = g - mod }
cur = mulmod(cur, r, mod)
i = i + 1
}
let term1: i64 = powmod(2, 7 * (k as i64), mod)
let term2: i64 = mulmod(powmod(2, 4 * (k as i64) + 3, mod), g, mod)
return (term1 + term2) % mod
}
# Odd prime
if k == 1 {
let p2: i64 = mulmod(p, p, mod)
let p3: i64 = mulmod(p2, p, mod)
let p4: i64 = mulmod(p2, p2, mod)
let p7: i64 = mulmod(p4, p3, mod)
return (p7 + p4 + mod - p3) % mod
}
let r: i64 = powmod(p, 3, mod)
let mut g: i64 = 0
let mut cur: i64 = 1
let mut i: i32 = 0
while i < k {
g = g + cur
if g >= mod { g = g - mod }
cur = mulmod(cur, r, mod)
i = i + 1
}
let term1: i64 = powmod(p, 7 * (k as i64), mod)
let termp: i64 = powmod(p, 4 * (k as i64) - 1, mod)
let term2: i64 = mulmod(mulmod((p - 1) % mod, termp, mod), g, mod)
return (term1 + term2) % mod
}
function smallest_prime_factors(n: i64) -> void {
spf = calloc(n + 1, 4) as ptr<i32>
primes = malloc((n + 1) * 4) as ptr<i32>
nprimes = 0
spf[1] = 1
let mut i: i64 = 2
while i <= n {
if spf[i] == 0 {
spf[i] = i as i32
primes[nprimes] = i as i32
nprimes = nprimes + 1
}
let si: i64 = spf[i] as i64
let mut j: i32 = 0
while j < nprimes {
let p: i64 = primes[j] as i64
let ip: i64 = i * p
if ip > n { break }
spf[ip] = p as i32
if p == si { break }
j = j + 1
}
i = i + 1
}
}
function compute_Q(N: i64, mod: i64) -> i64 {
smallest_prime_factors(N)
f_arr = calloc(N + 1, 4) as ptr<i32>
f_arr[1] = 1
let mut total: i64 = 1 % mod
let mut n: i64 = 2
while n <= N {
let p: i64 = spf[n] as i64
let m: i64 = n / p
let val: i64 = 0
if (spf[m] as i64) != p {
let qp: i64 = 0
if m == 1 {
qp = q_prime_power_mod(p, 1, mod)
} else {
qp = f_arr[p] as i64
}
val = mulmod(f_arr[m] as i64, qp, mod)
} else {
let mut ppow: i64 = p * p
let mut k: i32 = 2
let mut mm: i64 = m / p
while mm > 1 && (spf[mm] as i64) == p {
ppow = ppow * p
k = k + 1
mm = mm / p
}
let rest: i64 = mm
let qp: i64 = 0
if rest == 1 {
qp = q_prime_power_mod(p, k, mod)
} else {
qp = f_arr[ppow] as i64
}
val = mulmod(f_arr[rest] as i64, qp, mod)
}
f_arr[n] = val as i32
total = total + val
if total >= mod { total = total - mod }
n = n + 1
}
free(spf as ptr<void>)
free(primes as ptr<void>)
free(f_arr as ptr<void>)
return total
}
function main() -> i32 {
printf("%lld\n", compute_Q(TARGET_N, MOD))
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 mulmod_i64_i64_i64(int64_t a, int64_t b, int64_t m);
int64_t powmod_i64_i64_i64(int64_t a0, int64_t e0, int64_t m);
int64_t q_prime_power_mod_i64_i32_i64(int64_t p, int32_t k, int64_t mod);
void smallest_prime_factors_i64(int64_t n);
int64_t compute_Q_i64_i64(int64_t N, int64_t mod);
int32_t main(void);
static const int64_t MOD = 1001961001;
static const int64_t TARGET_N = 12345678;
/* Module statics */
static int32_t* spf = NULL;
static int32_t* primes = NULL;
static int32_t nprimes = 0;
static int32_t* f_arr = NULL;
int64_t mulmod_i64_i64_i64(int64_t a, int64_t b, int64_t m) {
__int128 r = FLOW_CHECKED_MOD(((((__int128)(a)) * ((__int128)(b)))), (((__int128)(m))));
return ((int64_t)(r));
}
int64_t powmod_i64_i64_i64(int64_t a0, int64_t e0, int64_t m) {
int64_t r = FLOW_CHECKED_MOD((1), (m));
int64_t a = FLOW_CHECKED_MOD((a0), (m));
int64_t e = e0;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = mulmod_i64_i64_i64(r, a, m);
}
a = mulmod_i64_i64_i64(a, a, m);
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t q_prime_power_mod_i64_i32_i64(int64_t p, int32_t k, int64_t mod) {
if (p == 2) {
if (k == 1) {
return FLOW_CHECKED_MOD((128), (mod));
}
int64_t r = FLOW_CHECKED_MOD((8), (mod));
int64_t g = 0;
int64_t cur = 1;
int32_t i = 0;
while (i < (k - 1)) {
g = (g + cur);
if (g >= mod) {
g = (g - mod);
}
cur = mulmod_i64_i64_i64(cur, r, mod);
i = (i + 1);
}
int64_t term1 = powmod_i64_i64_i64(2, (7 * ((int64_t)(k))), mod);
int64_t term2 = mulmod_i64_i64_i64(powmod_i64_i64_i64(2, ((4 * ((int64_t)(k))) + 3), mod), g, mod);
return FLOW_CHECKED_MOD(((term1 + term2)), (mod));
}
if (k == 1) {
int64_t p2 = mulmod_i64_i64_i64(p, p, mod);
int64_t p3 = mulmod_i64_i64_i64(p2, p, mod);
int64_t p4 = mulmod_i64_i64_i64(p2, p2, mod);
int64_t p7 = mulmod_i64_i64_i64(p4, p3, mod);
return FLOW_CHECKED_MOD(((((p7 + p4) + mod) - p3)), (mod));
}
int64_t r = powmod_i64_i64_i64(p, 3, mod);
int64_t g = 0;
int64_t cur = 1;
int32_t i = 0;
while (i < k) {
g = (g + cur);
if (g >= mod) {
g = (g - mod);
}
cur = mulmod_i64_i64_i64(cur, r, mod);
i = (i + 1);
}
int64_t term1 = powmod_i64_i64_i64(p, (7 * ((int64_t)(k))), mod);
int64_t termp = powmod_i64_i64_i64(p, ((4 * ((int64_t)(k))) - 1), mod);
int64_t term2 = mulmod_i64_i64_i64(mulmod_i64_i64_i64(FLOW_CHECKED_MOD(((p - 1)), (mod)), termp, mod), g, mod);
return FLOW_CHECKED_MOD(((term1 + term2)), (mod));
}
void smallest_prime_factors_i64(int64_t n) {
spf = ((int32_t*)(calloc((n + 1), 4)));
primes = ((int32_t*)(malloc(((n + 1) * 4))));
nprimes = 0;
spf[1] = 1;
int64_t i = 2;
while (i <= n) {
if (spf[i] == 0) {
spf[i] = ((int32_t)(i));
primes[nprimes] = ((int32_t)(i));
nprimes = (nprimes + 1);
}
int64_t si = ((int64_t)(spf[i]));
int32_t j = 0;
while (j < nprimes) {
int64_t p = ((int64_t)(primes[j]));
int64_t ip = (i * p);
if (ip > n) {
break;
}
spf[ip] = ((int32_t)(p));
if (p == si) {
break;
}
j = (j + 1);
}
i = (i + 1);
}
}
int64_t compute_Q_i64_i64(int64_t N, int64_t mod) {
smallest_prime_factors_i64(N);
f_arr = ((int32_t*)(calloc((N + 1), 4)));
f_arr[1] = 1;
int64_t total = FLOW_CHECKED_MOD((1), (mod));
int64_t n = 2;
while (n <= N) {
int64_t p = ((int64_t)(spf[n]));
int64_t m = FLOW_CHECKED_DIV((n), (p));
int64_t val = 0;
if (((int64_t)(spf[m])) != p) {
int64_t qp = 0;
if (m == 1) {
qp = q_prime_power_mod_i64_i32_i64(p, 1, mod);
} else {
qp = ((int64_t)(f_arr[p]));
}
val = mulmod_i64_i64_i64(((int64_t)(f_arr[m])), qp, mod);
} else {
int64_t ppow = (p * p);
int32_t k = 2;
int64_t mm = FLOW_CHECKED_DIV((m), (p));
while ((mm > 1 && ((int64_t)(spf[mm])) == p)) {
ppow = (ppow * p);
k = (k + 1);
mm = FLOW_CHECKED_DIV((mm), (p));
}
int64_t rest = mm;
int64_t qp = 0;
if (rest == 1) {
qp = q_prime_power_mod_i64_i32_i64(p, k, mod);
} else {
qp = ((int64_t)(f_arr[ppow]));
}
val = mulmod_i64_i64_i64(((int64_t)(f_arr[rest])), qp, mod);
}
f_arr[n] = ((int32_t)(val));
total = (total + val);
if (total >= mod) {
total = (total - mod);
}
n = (n + 1);
}
free(((void*)(spf)));
free(((void*)(primes)));
free(((void*)(f_arr)));
return total;
}
int32_t main(void) {
printf("%lld\n", compute_Q_i64_i64(TARGET_N, MOD));
return 0;
}