# Project Euler 411
# sum_{k=1..30} S(k^5): longest nondecreasing path through modular stations.
import euler.nt { gcd }
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base0 % mod
let mut e: i64 = exp0
while e > 0 {
if e % 2 == 1 {
r = ((r as i128) * (b as i128) % (mod as i128)) as i64
}
b = ((b as i128) * (b as i128) % (mod as i128)) as i64
e = e / 2
}
return r
}
function phi_of(n0: i64) -> i64 {
let mut n: i64 = n0
let mut result: i64 = n0
let mut p: i64 = 2
while p * p <= n {
if n % p == 0 {
while n % p == 0 { n = n / p }
result = result / p * (p - 1)
}
if p == 2 { p = 3 } else { p = p + 2 }
}
if n > 1 { result = result / n * (n - 1) }
return result
}
function mult_order(a: i64, n: i64) -> i64 {
if n == 1 { return 1 }
let mut order: i64 = phi_of(n)
let mut x: i64 = order
let mut p: i64 = 2
while p * p <= x {
if x % p == 0 {
while x % p == 0 { x = x / p }
while order % p == 0 && modpow(a, order / p, n) == 1 {
order = order / p
}
}
if p == 2 { p = 3 } else { p = p + 2 }
}
if x > 1 {
while order % x == 0 && modpow(a, order / x, n) == 1 {
order = order / x
}
}
return order
}
function lcm64(a: i64, b: i64) -> i64 {
return a / gcd(a, b) * b
}
function compute_S(n: i64) -> i64 {
let mut n2: i64 = n
let mut v2: i64 = 0
while n2 % 2 == 0 { n2 = n2 / 2; v2 = v2 + 1 }
let mut n3: i64 = n
let mut v3: i64 = 0
while n3 % 3 == 0 { n3 = n3 / 3; v3 = v3 + 1 }
let mut preperiod: i64 = v2
if v3 > preperiod { preperiod = v3 }
let ord2: i64 = 1
if n2 > 1 { ord2 = mult_order(2, n2) }
let ord3: i64 = 1
if n3 > 1 { ord3 = mult_order(3, n3) }
let period: i64 = lcm64(ord2, ord3)
let total: i64 = preperiod + period
let counts: ptr<i32> = calloc(n + 1, 4)
if counts == null { return -1 }
let mut x: i64 = 1 % n
let mut t: i64 = 0
while t < total {
counts[x] = (counts[x] as i64 + 1) as i32
x = (x * 2) % n
t = t + 1
}
# prefix: counts[i] = start index, counts[i+1]-counts[i] = length
let mut running: i64 = 0
let mut i: i64 = 0
while i < n {
let c: i64 = counts[i] as i64
counts[i] = running as i32
running = running + c
i = i + 1
}
counts[n] = running as i32
let pos: ptr<i32> = calloc(n, 4)
let ys: ptr<i32> = calloc(total, 4)
if pos == null || ys == null { return -1 }
i = 0
while i < n {
pos[i] = counts[i]
i = i + 1
}
x = 1 % n
let mut y: i64 = 1 % n
t = 0
while t < total {
let idx: i64 = pos[x] as i64
ys[idx] = y as i32
pos[x] = (idx + 1) as i32
x = (x * 2) % n
y = (y * 3) % n
t = t + 1
}
# For each x bucket, sort ys and run patience LIS across increasing x
let tails: ptr<i32> = calloc(total + 1, 4)
let mut len: i64 = 0
let mut xx: i64 = 0
while xx < n {
let start: i64 = counts[xx] as i64
let end: i64 = counts[xx + 1] as i64
let segn: i64 = end - start
if segn == 1 {
let v: i64 = ys[start] as i64
let mut lo: i64 = 0
let mut hi: i64 = len
while lo < hi {
let mid: i64 = (lo + hi) / 2
if (tails[mid] as i64) <= v { lo = mid + 1 } else { hi = mid }
}
tails[lo] = v as i32
if lo == len { len = len + 1 }
} elif segn > 1 {
# insertion sort segment (usually small)
let mut a: i64 = start + 1
while a < end {
let key: i32 = ys[a]
let mut b: i64 = a
while b > start && (ys[b - 1] as i64) > (key as i64) {
ys[b] = ys[b - 1]
b = b - 1
}
ys[b] = key
a = a + 1
}
a = start
while a < end {
let v2: i64 = ys[a] as i64
let mut lo2: i64 = 0
let mut hi2: i64 = len
while lo2 < hi2 {
let mid2: i64 = (lo2 + hi2) / 2
if (tails[mid2] as i64) <= v2 { lo2 = mid2 + 1 } else { hi2 = mid2 }
}
tails[lo2] = v2 as i32
if lo2 == len { len = len + 1 }
a = a + 1
}
}
xx = xx + 1
}
free(tails)
free(ys)
free(pos)
free(counts)
return len
}
function ipow(a: i64, e: i64) -> i64 {
let mut r: i64 = 1
let mut i: i64 = 0
while i < e { r = r * a; i = i + 1 }
return r
}
function main() -> i32 {
let mut total: i64 = 0
let mut k: i64 = 1
while k <= 30 {
let n: i64 = ipow(k, 5)
total = total + compute_S(n)
k = k + 1
}
printf("%lld\n", total)
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 gcd_i64_i64(int64_t a0, int64_t b0);
int64_t lcm_i64_i64(int64_t a, int64_t b);
int64_t isqrt_i64(int64_t n);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t n);
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t phi_of_i64(int64_t n0);
int64_t mult_order_i64_i64(int64_t a, int64_t n);
int64_t lcm64_i64_i64(int64_t a, int64_t b);
int64_t compute_S_i64(int64_t n);
int64_t ipow_i64_i64(int64_t a, int64_t e);
int32_t main(void);
int64_t gcd_i64_i64(int64_t a0, int64_t b0) {
int64_t a = a0;
int64_t b = b0;
while (b != 0) {
int64_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
return a;
}
int64_t lcm_i64_i64(int64_t a, int64_t b) {
if ((a == 0 || b == 0)) {
return 0;
}
return (FLOW_CHECKED_DIV((a), (gcd_i64_i64(a, b))) * b);
}
int64_t isqrt_i64(int64_t n) {
if (n < 2) {
return n;
}
int64_t x = n;
int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
while (y < x) {
x = y;
y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
}
return x;
}
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
int64_t b = FLOW_CHECKED_MOD((b0), (mod));
int64_t result = 0;
while (b > 0) {
if (FLOW_CHECKED_MOD((b), (2)) == 1) {
result = FLOW_CHECKED_MOD(((result + a)), (mod));
}
a = FLOW_CHECKED_MOD(((a * 2)), (mod));
b = FLOW_CHECKED_DIV((b), (2));
}
return result;
}
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
if (mod == 1) {
return 0;
}
int64_t result = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
result = mulmod_i64_i64_i64(result, b, mod);
}
b = mulmod_i64_i64_i64(b, b, mod);
e = FLOW_CHECKED_DIV((e), (2));
}
return result;
}
bool is_prime_i64(int64_t n) {
if (n < 2) {
return 0;
}
if (n < 4) {
return 1;
}
if ((FLOW_CHECKED_MOD((n), (2)) == 0 || FLOW_CHECKED_MOD((n), (3)) == 0)) {
return 0;
}
int64_t i = 5;
while ((i * i) <= n) {
if ((FLOW_CHECKED_MOD((n), (i)) == 0 || FLOW_CHECKED_MOD((n), ((i + 2))) == 0)) {
return 0;
}
i = (i + 6);
}
return 1;
}
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base0), (mod));
int64_t e = exp0;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))))));
}
b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))))));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t phi_of_i64(int64_t n0) {
int64_t n = n0;
int64_t result = n0;
int64_t p = 2;
while ((p * p) <= n) {
if (FLOW_CHECKED_MOD((n), (p)) == 0) {
while (FLOW_CHECKED_MOD((n), (p)) == 0) {
n = FLOW_CHECKED_DIV((n), (p));
}
result = (FLOW_CHECKED_DIV((result), (p)) * (p - 1));
}
if (p == 2) {
p = 3;
} else {
p = (p + 2);
}
}
if (n > 1) {
result = (FLOW_CHECKED_DIV((result), (n)) * (n - 1));
}
return result;
}
int64_t mult_order_i64_i64(int64_t a, int64_t n) {
if (n == 1) {
return 1;
}
int64_t order = phi_of_i64(n);
int64_t x = order;
int64_t p = 2;
while ((p * p) <= x) {
if (FLOW_CHECKED_MOD((x), (p)) == 0) {
while (FLOW_CHECKED_MOD((x), (p)) == 0) {
x = FLOW_CHECKED_DIV((x), (p));
}
while ((FLOW_CHECKED_MOD((order), (p)) == 0 && modpow_i64_i64_i64(a, FLOW_CHECKED_DIV((order), (p)), n) == 1)) {
order = FLOW_CHECKED_DIV((order), (p));
}
}
if (p == 2) {
p = 3;
} else {
p = (p + 2);
}
}
if (x > 1) {
while ((FLOW_CHECKED_MOD((order), (x)) == 0 && modpow_i64_i64_i64(a, FLOW_CHECKED_DIV((order), (x)), n) == 1)) {
order = FLOW_CHECKED_DIV((order), (x));
}
}
return order;
}
int64_t lcm64_i64_i64(int64_t a, int64_t b) {
return (FLOW_CHECKED_DIV((a), (gcd_i64_i64(a, b))) * b);
}
int64_t compute_S_i64(int64_t n) {
int64_t n2 = n;
int64_t v2 = 0;
while (FLOW_CHECKED_MOD((n2), (2)) == 0) {
n2 = FLOW_CHECKED_DIV((n2), (2));
v2 = (v2 + 1);
}
int64_t n3 = n;
int64_t v3 = 0;
while (FLOW_CHECKED_MOD((n3), (3)) == 0) {
n3 = FLOW_CHECKED_DIV((n3), (3));
v3 = (v3 + 1);
}
int64_t preperiod = v2;
if (v3 > preperiod) {
preperiod = v3;
}
int64_t ord2 = 1;
if (n2 > 1) {
ord2 = mult_order_i64_i64(2, n2);
}
int64_t ord3 = 1;
if (n3 > 1) {
ord3 = mult_order_i64_i64(3, n3);
}
int64_t period = lcm64_i64_i64(ord2, ord3);
int64_t total = (preperiod + period);
int32_t* counts = (int32_t*)(calloc((n + 1), 4));
if (counts == NULL) {
return (-1);
}
int64_t x = FLOW_CHECKED_MOD((1), (n));
int64_t t = 0;
while (t < total) {
counts[x] = ((int32_t)((((int64_t)(counts[x])) + 1)));
x = FLOW_CHECKED_MOD(((x * 2)), (n));
t = (t + 1);
}
int64_t running = 0;
int64_t i = 0;
while (i < n) {
int64_t c = ((int64_t)(counts[i]));
counts[i] = ((int32_t)(running));
running = (running + c);
i = (i + 1);
}
counts[n] = ((int32_t)(running));
int32_t* pos = (int32_t*)(calloc(n, 4));
int32_t* ys = (int32_t*)(calloc(total, 4));
if ((pos == NULL || ys == NULL)) {
return (-1);
}
i = 0;
while (i < n) {
pos[i] = counts[i];
i = (i + 1);
}
x = FLOW_CHECKED_MOD((1), (n));
int64_t y = FLOW_CHECKED_MOD((1), (n));
t = 0;
while (t < total) {
int64_t idx = ((int64_t)(pos[x]));
ys[idx] = ((int32_t)(y));
pos[x] = ((int32_t)((idx + 1)));
x = FLOW_CHECKED_MOD(((x * 2)), (n));
y = FLOW_CHECKED_MOD(((y * 3)), (n));
t = (t + 1);
}
int32_t* tails = (int32_t*)(calloc((total + 1), 4));
int64_t len = 0;
int64_t xx = 0;
while (xx < n) {
int64_t start = ((int64_t)(counts[xx]));
int64_t end = ((int64_t)(counts[(xx + 1)]));
int64_t segn = (end - start);
if (segn == 1) {
int64_t v = ((int64_t)(ys[start]));
int64_t lo = 0;
int64_t hi = len;
while (lo < hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
if (((int64_t)(tails[mid])) <= v) {
lo = (mid + 1);
} else {
hi = mid;
}
}
tails[lo] = ((int32_t)(v));
if (lo == len) {
len = (len + 1);
}
} else if (segn > 1) {
int64_t a = (start + 1);
while (a < end) {
int32_t key = ys[a];
int64_t b = a;
while ((b > start && ((int64_t)(ys[(b - 1)])) > ((int64_t)(key)))) {
ys[b] = ys[(b - 1)];
b = (b - 1);
}
ys[b] = key;
a = (a + 1);
}
a = start;
while (a < end) {
int64_t v2 = ((int64_t)(ys[a]));
int64_t lo2 = 0;
int64_t hi2 = len;
while (lo2 < hi2) {
int64_t mid2 = FLOW_CHECKED_DIV(((lo2 + hi2)), (2));
if (((int64_t)(tails[mid2])) <= v2) {
lo2 = (mid2 + 1);
} else {
hi2 = mid2;
}
}
tails[lo2] = ((int32_t)(v2));
if (lo2 == len) {
len = (len + 1);
}
a = (a + 1);
}
}
xx = (xx + 1);
}
free(tails);
free(ys);
free(pos);
free(counts);
return len;
}
int64_t ipow_i64_i64(int64_t a, int64_t e) {
int64_t r = 1;
int64_t i = 0;
while (i < e) {
r = (r * a);
i = (i + 1);
}
return r;
}
int32_t main(void) {
int64_t total = 0;
int64_t k = 1;
while (k <= 30) {
int64_t n = ipow_i64_i64(k, 5);
total = (total + compute_S_i64(n));
k = (k + 1);
}
printf("%lld\n", total);
return 0;
}