# Project Euler 908: Clock Sequence II
# Compute C(10^4) mod 1111211113.
# Port of the C reference solver: moduli generation, binomial coefficients, Mobius inversion.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function memset(p: ptr<void>, c: i32, n: i64) -> ptr<void>
}
const MOD: i64 = 1111211113
const N: i64 = 10000
# Global pairs buffer for moduli generation
let mut pairs_buf: ptr<i64> = null
let mut pairs_count: i64 = 0
let mut pairs_cap: i64 = 0
# Options per prime: stored as flat arrays (all opts for all primes in one buffer)
# opt_all_m[prime_idx * 64 + oi], opt_all_k[prime_idx * 64 + oi]
let mut opt_all_m: ptr<i64> = null
let mut opt_all_k: ptr<i64> = null
let mut opt_counts: ptr<i32> = null
function gcd64(a: i64, b: i64) -> i64 {
let mut x: i64 = a
let mut y: i64 = b
while y != 0 {
let t: i64 = x % y
x = y
y = t
}
return x
}
function add_pair(m: i64, k: i64) -> void {
if pairs_count >= pairs_cap {
let new_cap: i64 = 1024
if pairs_cap > 0 {
new_cap = pairs_cap * 2
}
let new_buf: ptr<i64> = calloc(new_cap * 2, 8)
let mut i: i64 = 0
while i < pairs_count {
new_buf[i * 2] = pairs_buf[i * 2]
new_buf[i * 2 + 1] = pairs_buf[i * 2 + 1]
i = i + 1
}
if pairs_buf != null {
free(pairs_buf)
}
pairs_buf = new_buf
pairs_cap = new_cap
}
pairs_buf[pairs_count * 2] = m
pairs_buf[pairs_count * 2 + 1] = k
pairs_count = pairs_count + 1
}
# k(p^e)
function k_prime_power(p: i64, e: i64) -> i64 {
if e <= 0 {
return 1
}
if p == 2 {
let mut result: i64 = 1
let mut i: i64 = 0
while i < e {
result = result * 2
i = i + 1
}
return result
}
let mut k: i64 = (p + 1) / 2
let mut exp: i64 = 2
while exp <= e {
if (exp % 2) == 0 {
k = p * k - (p - 1)
} else {
k = p * k - (p - 1) / 2
}
exp = exp + 1
}
return k
}
# DFS to generate all (m, k) pairs with k <= max_k
function dfs_moduli(start_idx: i64, num_primes: i64, primes: ptr<i64>,
m_cur: i64, k_cur: i64, max_k: i64) -> void {
add_pair(m_cur, k_cur)
let mut j: i64 = start_idx
while j < num_primes {
let p: i64 = primes[j]
let oc: i32 = opt_counts[j]
if oc == 0 {
j = j + 1
continue
}
# smallest k-factor for this prime
if k_cur * opt_all_k[j * 64 + 0] > max_k {
break
}
let mut oi: i64 = 0
while oi < (oc as i64) {
let mp: i64 = opt_all_m[j * 64 + oi]
let kp: i64 = opt_all_k[j * 64 + oi]
let k_new: i64 = k_cur * kp
if k_new > max_k {
break
}
let m_new: i128 = (m_cur as i128) * (mp as i128)
dfs_moduli(j + 1, num_primes, primes, m_new as i64, k_new, max_k)
oi = oi + 1
}
j = j + 1
}
}
# Generate moduli (m, k(m)) with k(m) <= max_k
# Returns pairs in pairs_buf, count in pairs_count
function generate_moduli(max_k: i64) -> void {
# Sieve primes up to 2*max_k
let limit: i64 = 2 * max_k
let is_comp: ptr<i8> = calloc(limit + 1, 1)
let primes: ptr<i64> = calloc(limit + 1, 8)
let mut pc: i64 = 0
let mut p: i64 = 2
while p <= limit {
if is_comp[p] == 0 {
primes[pc] = p
pc = pc + 1
let mut m: i64 = p * p
while m <= limit {
is_comp[m] = 1
m = m + p
}
}
p = p + 1
}
# Build options for each prime
opt_all_m = calloc(pc * 64, 8)
opt_all_k = calloc(pc * 64, 8)
opt_counts = calloc(pc, 4)
let mut j: i64 = 0
while j < pc {
let pr: i64 = primes[j]
let mut oc: i64 = 0
if pr == 2 {
let mut m: i64 = 2
let mut k: i64 = 2
while k <= max_k {
opt_all_m[j * 64 + oc] = m
opt_all_k[j * 64 + oc] = k
oc = oc + 1
m = m * 2
k = k * 2
}
} else {
let mut m: i64 = pr
let mut k: i64 = (pr + 1) / 2
let mut e: i64 = 1
while k <= max_k {
opt_all_m[j * 64 + oc] = m
opt_all_k[j * 64 + oc] = k
oc = oc + 1
e = e + 1
m = m * pr
if (e % 2) == 0 {
k = pr * k - (pr - 1)
} else {
k = pr * k - (pr - 1) / 2
}
}
}
opt_counts[j] = oc as i32
j = j + 1
}
pairs_buf = null
pairs_count = 0
pairs_cap = 0
dfs_moduli(0, pc, primes, 1, 1, max_k)
# Cleanup options
free(opt_all_m)
free(opt_all_k)
free(opt_counts)
free(primes)
free(is_comp)
}
# Prepare modular inverses 1..n mod mod
function prepare_inverses(n: i64, mod: i64) -> ptr<i64> {
let inv: ptr<i64> = calloc(n + 1, 8)
inv[1] = 1
let mut i: i64 = 2
while i <= n {
inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod
i = i + 1
}
return inv
}
# Compute B array
function compute_B(max_period: i64, mod: i64, B: ptr<i64>) -> void {
generate_moduli(max_period)
let inv: ptr<i64> = prepare_inverses(max_period, mod)
let mut i: i64 = 0
while i <= max_period {
B[i] = 0
i = i + 1
}
let mut idx: i64 = 0
while idx < pairs_count {
let m: i64 = pairs_buf[idx * 2]
let k: i64 = pairs_buf[idx * 2 + 1]
if k > max_period {
idx = idx + 1
continue
}
let n: i64 = m - k
if n < 0 {
idx = idx + 1
continue
}
let mut rmax: i64 = max_period - k
if rmax < 0 {
idx = idx + 1
continue
}
if n < rmax {
rmax = n
}
# r = 0
let idx0: i64 = k
B[idx0] = B[idx0] + 1
if B[idx0] >= mod {
B[idx0] = B[idx0] - mod
}
let mut c: i64 = 1
let mut r: i64 = 1
while r <= rmax {
c = ((c as i128) * (n - r + 1) % mod) as i64
c = ((c as i128) * inv[r] % mod) as i64
let bidx: i64 = idx0 + r
B[bidx] = B[bidx] + c
if B[bidx] >= mod {
B[bidx] = B[bidx] - mod
}
r = r + 1
}
idx = idx + 1
}
free(inv)
free(pairs_buf)
}
# Mobius via linear sieve
function mobius_upto(n: i64, mu: ptr<i8>) -> void {
let mut i: i64 = 0
while i <= n {
mu[i] = 0
i = i + 1
}
let primes: ptr<i64> = calloc(n + 1, 8)
let is_comp: ptr<i8> = calloc(n + 1, 1)
let mut pc: i64 = 0
mu[1] = 1
i = 2
while i <= n {
if is_comp[i] == 0 {
primes[pc] = i
pc = pc + 1
mu[i] = (0 - 1) as i8
}
let mut j: i64 = 0
while j < pc {
let p: i64 = primes[j]
let v: i64 = i * p
if v > n {
break
}
is_comp[v] = 1
if (i % p) == 0 {
mu[v] = 0
break
}
mu[v] = (0 - (mu[i] as i32)) as i8
j = j + 1
}
i = i + 1
}
free(primes)
free(is_comp)
}
# Compute A from B via Mobius
function compute_A_from_B(B: ptr<i64>, mu: ptr<i8>, mod: i64, A: ptr<i64>, n: i64) -> void {
let mut i: i64 = 0
while i <= n {
A[i] = 0
i = i + 1
}
let mut d: i64 = 1
while d <= n {
let md: i8 = mu[d]
if md == 0 {
d = d + 1
continue
}
if md == 1 {
let mut q: i64 = 1
while q <= n / d {
let p: i64 = d * q
A[p] = A[p] + B[q]
if A[p] >= mod {
A[p] = A[p] - mod
}
q = q + 1
}
} else {
let mut q: i64 = 1
while q <= n / d {
let p: i64 = d * q
A[p] = A[p] - B[q]
if A[p] < 0 {
A[p] = A[p] + mod
}
q = q + 1
}
}
d = d + 1
}
}
function main() -> i32 {
let B: ptr<i64> = calloc(N + 1, 8)
compute_B(N, MOD, B)
let mu: ptr<i8> = calloc(N + 1, 1)
mobius_upto(N, mu)
let A: ptr<i64> = calloc(N + 1, 8)
compute_A_from_B(B, mu, MOD, A, N)
# Prefix sum
let mut s: i64 = 0
let mut i: i64 = 1
while i <= N {
s = s + A[i]
s = s % MOD
i = i + 1
}
printf("%lld\n", s % MOD)
free(B)
free(mu)
free(A)
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 gcd64_i64_i64(int64_t a, int64_t b);
void add_pair_i64_i64(int64_t m, int64_t k);
int64_t k_prime_power_i64_i64(int64_t p, int64_t e);
void dfs_moduli_i64_i64_ptr_i64_i64_i64_i64(int64_t start_idx, int64_t num_primes, int64_t* primes, int64_t m_cur, int64_t k_cur, int64_t max_k);
void generate_moduli_i64(int64_t max_k);
int64_t* prepare_inverses_i64_i64(int64_t n, int64_t mod);
void compute_B_i64_i64_ptr_i64(int64_t max_period, int64_t mod, int64_t* B);
void mobius_upto_i64_ptr_i8(int64_t n, int8_t* mu);
void compute_A_from_B_ptr_i64_ptr_i8_i64_ptr_i64_i64(int64_t* B, int8_t* mu, int64_t mod, int64_t* A, int64_t n);
int32_t main(void);
static const int64_t MOD = 1111211113;
static const int64_t N = 10000;
/* Module statics */
static int64_t* pairs_buf = NULL;
static int64_t pairs_count = 0;
static int64_t pairs_cap = 0;
static int64_t* opt_all_m = NULL;
static int64_t* opt_all_k = NULL;
static int32_t* opt_counts = NULL;
int64_t gcd64_i64_i64(int64_t a, int64_t b) {
int64_t x = a;
int64_t y = b;
while (y != 0) {
int64_t t = FLOW_CHECKED_MOD((x), (y));
x = y;
y = t;
}
return x;
}
void add_pair_i64_i64(int64_t m, int64_t k) {
if (pairs_count >= pairs_cap) {
int64_t new_cap = 1024;
if (pairs_cap > 0) {
new_cap = (pairs_cap * 2);
}
int64_t* new_buf = (int64_t*)(calloc((new_cap * 2), 8));
int64_t i = 0;
while (i < pairs_count) {
new_buf[(i * 2)] = pairs_buf[(i * 2)];
new_buf[((i * 2) + 1)] = pairs_buf[((i * 2) + 1)];
i = (i + 1);
}
if (pairs_buf != NULL) {
free(pairs_buf);
}
pairs_buf = new_buf;
pairs_cap = new_cap;
}
pairs_buf[(pairs_count * 2)] = m;
pairs_buf[((pairs_count * 2) + 1)] = k;
pairs_count = (pairs_count + 1);
}
int64_t k_prime_power_i64_i64(int64_t p, int64_t e) {
if (e <= 0) {
return 1;
}
if (p == 2) {
int64_t result = 1;
int64_t i = 0;
while (i < e) {
result = (result * 2);
i = (i + 1);
}
return result;
}
int64_t k = FLOW_CHECKED_DIV(((p + 1)), (2));
int64_t exp = 2;
while (exp <= e) {
if (FLOW_CHECKED_MOD((exp), (2)) == 0) {
k = ((p * k) - (p - 1));
} else {
k = ((p * k) - FLOW_CHECKED_DIV(((p - 1)), (2)));
}
exp = (exp + 1);
}
return k;
}
void dfs_moduli_i64_i64_ptr_i64_i64_i64_i64(int64_t start_idx, int64_t num_primes, int64_t* primes, int64_t m_cur, int64_t k_cur, int64_t max_k) {
add_pair_i64_i64(m_cur, k_cur);
int64_t j = start_idx;
while (j < num_primes) {
int64_t p = primes[j];
int32_t oc = opt_counts[j];
if (oc == 0) {
j = (j + 1);
continue;
}
if ((k_cur * opt_all_k[((j * 64) + 0)]) > max_k) {
break;
}
int64_t oi = 0;
while (oi < ((int64_t)(oc))) {
int64_t mp = opt_all_m[((j * 64) + oi)];
int64_t kp = opt_all_k[((j * 64) + oi)];
int64_t k_new = (k_cur * kp);
if (k_new > max_k) {
break;
}
__int128 m_new = (((__int128)(m_cur)) * ((__int128)(mp)));
dfs_moduli_i64_i64_ptr_i64_i64_i64_i64((j + 1), num_primes, primes, ((int64_t)(m_new)), k_new, max_k);
oi = (oi + 1);
}
j = (j + 1);
}
}
void generate_moduli_i64(int64_t max_k) {
int64_t limit = (2 * max_k);
int8_t* is_comp = (int8_t*)(calloc((limit + 1), 1));
int64_t* primes = (int64_t*)(calloc((limit + 1), 8));
int64_t pc = 0;
int64_t p = 2;
while (p <= limit) {
if (is_comp[p] == 0) {
primes[pc] = p;
pc = (pc + 1);
int64_t m = (p * p);
while (m <= limit) {
is_comp[m] = 1;
m = (m + p);
}
}
p = (p + 1);
}
opt_all_m = calloc((pc * 64), 8);
opt_all_k = calloc((pc * 64), 8);
opt_counts = calloc(pc, 4);
int64_t j = 0;
while (j < pc) {
int64_t pr = primes[j];
int64_t oc = 0;
if (pr == 2) {
int64_t m = 2;
int64_t k = 2;
while (k <= max_k) {
opt_all_m[((j * 64) + oc)] = m;
opt_all_k[((j * 64) + oc)] = k;
oc = (oc + 1);
m = (m * 2);
k = (k * 2);
}
} else {
int64_t m = pr;
int64_t k = FLOW_CHECKED_DIV(((pr + 1)), (2));
int64_t e = 1;
while (k <= max_k) {
opt_all_m[((j * 64) + oc)] = m;
opt_all_k[((j * 64) + oc)] = k;
oc = (oc + 1);
e = (e + 1);
m = (m * pr);
if (FLOW_CHECKED_MOD((e), (2)) == 0) {
k = ((pr * k) - (pr - 1));
} else {
k = ((pr * k) - FLOW_CHECKED_DIV(((pr - 1)), (2)));
}
}
}
opt_counts[j] = ((int32_t)(oc));
j = (j + 1);
}
pairs_buf = NULL;
pairs_count = 0;
pairs_cap = 0;
dfs_moduli_i64_i64_ptr_i64_i64_i64_i64(0, pc, primes, 1, 1, max_k);
free(opt_all_m);
free(opt_all_k);
free(opt_counts);
free(primes);
free(is_comp);
}
int64_t* prepare_inverses_i64_i64(int64_t n, int64_t mod) {
int64_t* inv = (int64_t*)(calloc((n + 1), 8));
inv[1] = 1;
int64_t i = 2;
while (i <= n) {
inv[i] = FLOW_CHECKED_MOD(((mod - FLOW_CHECKED_MOD(((FLOW_CHECKED_DIV((mod), (i)) * inv[FLOW_CHECKED_MOD((mod), (i))])), (mod)))), (mod));
i = (i + 1);
}
return inv;
}
void compute_B_i64_i64_ptr_i64(int64_t max_period, int64_t mod, int64_t* B) {
generate_moduli_i64(max_period);
int64_t* inv = (int64_t*)(prepare_inverses_i64_i64(max_period, mod));
int64_t i = 0;
while (i <= max_period) {
B[i] = 0;
i = (i + 1);
}
int64_t idx = 0;
while (idx < pairs_count) {
int64_t m = pairs_buf[(idx * 2)];
int64_t k = pairs_buf[((idx * 2) + 1)];
if (k > max_period) {
idx = (idx + 1);
continue;
}
int64_t n = (m - k);
if (n < 0) {
idx = (idx + 1);
continue;
}
int64_t rmax = (max_period - k);
if (rmax < 0) {
idx = (idx + 1);
continue;
}
if (n < rmax) {
rmax = n;
}
int64_t idx0 = k;
B[idx0] = (B[idx0] + 1);
if (B[idx0] >= mod) {
B[idx0] = (B[idx0] - mod);
}
int64_t c = 1;
int64_t r = 1;
while (r <= rmax) {
c = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(c)) * ((n - r) + 1))), (mod))));
c = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(c)) * inv[r])), (mod))));
int64_t bidx = (idx0 + r);
B[bidx] = (B[bidx] + c);
if (B[bidx] >= mod) {
B[bidx] = (B[bidx] - mod);
}
r = (r + 1);
}
idx = (idx + 1);
}
free(inv);
free(pairs_buf);
}
void mobius_upto_i64_ptr_i8(int64_t n, int8_t* mu) {
int64_t i = 0;
while (i <= n) {
mu[i] = 0;
i = (i + 1);
}
int64_t* primes = (int64_t*)(calloc((n + 1), 8));
int8_t* is_comp = (int8_t*)(calloc((n + 1), 1));
int64_t pc = 0;
mu[1] = 1;
i = 2;
while (i <= n) {
if (is_comp[i] == 0) {
primes[pc] = i;
pc = (pc + 1);
mu[i] = ((int8_t)((0 - 1)));
}
int64_t j = 0;
while (j < pc) {
int64_t p = primes[j];
int64_t v = (i * p);
if (v > n) {
break;
}
is_comp[v] = 1;
if (FLOW_CHECKED_MOD((i), (p)) == 0) {
mu[v] = 0;
break;
}
mu[v] = ((int8_t)((0 - ((int32_t)(mu[i])))));
j = (j + 1);
}
i = (i + 1);
}
free(primes);
free(is_comp);
}
void compute_A_from_B_ptr_i64_ptr_i8_i64_ptr_i64_i64(int64_t* B, int8_t* mu, int64_t mod, int64_t* A, int64_t n) {
int64_t i = 0;
while (i <= n) {
A[i] = 0;
i = (i + 1);
}
int64_t d = 1;
while (d <= n) {
int8_t md = mu[d];
if (md == 0) {
d = (d + 1);
continue;
}
if (md == 1) {
int64_t q = 1;
while (q <= FLOW_CHECKED_DIV((n), (d))) {
int64_t p = (d * q);
A[p] = (A[p] + B[q]);
if (A[p] >= mod) {
A[p] = (A[p] - mod);
}
q = (q + 1);
}
} else {
int64_t q = 1;
while (q <= FLOW_CHECKED_DIV((n), (d))) {
int64_t p = (d * q);
A[p] = (A[p] - B[q]);
if (A[p] < 0) {
A[p] = (A[p] + mod);
}
q = (q + 1);
}
}
d = (d + 1);
}
}
int32_t main(void) {
int64_t* B = (int64_t*)(calloc((N + 1), 8));
compute_B_i64_i64_ptr_i64(N, MOD, B);
int8_t* mu = (int8_t*)(calloc((N + 1), 1));
mobius_upto_i64_ptr_i8(N, mu);
int64_t* A = (int64_t*)(calloc((N + 1), 8));
compute_A_from_B_ptr_i64_ptr_i8_i64_ptr_i64_i64(B, mu, MOD, A, N);
int64_t s = 0;
int64_t i = 1;
while (i <= N) {
s = (s + A[i]);
s = FLOW_CHECKED_MOD((s), (MOD));
i = (i + 1);
}
printf("%lld\n", FLOW_CHECKED_MOD((s), (MOD)));
free(B);
free(mu);
free(A);
return 0;
}