# Project Euler 486
# Palindrome-containing Strings — D(10^18) via period-6 free counts + CRT.
function modinv(a0: i64, m: i64) -> i64 {
let mut a: i64 = a0 % m
let mut b: i64 = m
let mut x0: i64 = 1
let mut x1: i64 = 0
while b != 0 {
let q: i64 = a / b
let t: i64 = a - q * b
a = b
b = t
let tx: i64 = x0 - q * x1
x0 = x1
x1 = tx
}
if x0 < 0 { x0 = x0 + m }
return x0
}
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 * b) % mod
}
b = (b * b) % mod
e = e / 2
}
return r
}
function main() -> i32 {
let m: i64 = 87654321
let L: i64 = 1000000000000000000
let B6: i64 = 85
let o64: i64 = 1216562
let period_q: i64 = m * o64
let inv200: i64 = modinv(200, m)
let inv_o: i64 = modinv(o64, m)
let PREFIX: array<i64, 6> = [0, 32, 64, 96, 130, 166]
let pow2: array<i64, 6> = [0, 0, 0, 0, 0, 0]
let C: array<i64, 6> = [0, 0, 0, 0, 0, 0]
let Rlist: array<i64, 6> = [0, 0, 0, 0, 0, 0]
let mut base: i64 = 0
let mut r: i64 = 0
while r < 6 {
pow2[r] = modpow(2, r + 7, m)
C[r] = (1 + B6 + PREFIX[r]) % m
let qmax: i64 = (L - r - 6) / 6
let N: i64 = qmax + 1
let Q: i64 = N / period_q
base = base + Q * o64
Rlist[r] = N - Q * period_q
r = r + 1
}
let mut extra: i64 = 0
let mut pow64: i64 = 1
let mut k: i64 = 0
while k < o64 {
r = 0
while r < 6 {
let Lval: i64 = (pow2[r] * pow64) % m
let mut q0: i64 = ((Lval - C[r]) % m + m) % m
q0 = (q0 * inv200) % m
let mut diff: i64 = q0 - k
if diff < 0 { diff = diff + m }
let t: i64 = (diff * inv_o) % m
let q_res: i64 = k + o64 * t
if q_res < Rlist[r] {
extra = extra + 1
}
r = r + 1
}
pow64 = (pow64 * 64) % m
k = k + 1
}
# F5(5)=8, 8 % m != 0, so no add5
printf("%lld\n", base + extra)
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 modinv_i64_i64(int64_t a0, int64_t m);
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int32_t main(void);
int64_t modinv_i64_i64(int64_t a0, int64_t m) {
int64_t a = FLOW_CHECKED_MOD((a0), (m));
int64_t b = m;
int64_t x0 = 1;
int64_t x1 = 0;
while (b != 0) {
int64_t q = FLOW_CHECKED_DIV((a), (b));
int64_t t = (a - (q * b));
a = b;
b = t;
int64_t tx = (x0 - (q * x1));
x0 = x1;
x1 = tx;
}
if (x0 < 0) {
x0 = (x0 + m);
}
return x0;
}
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 = FLOW_CHECKED_MOD(((r * b)), (mod));
}
b = FLOW_CHECKED_MOD(((b * b)), (mod));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int32_t main(void) {
int64_t m = 87654321;
int64_t L = 1000000000000000000;
int64_t B6 = 85;
int64_t o64 = 1216562;
int64_t period_q = (m * o64);
int64_t inv200 = modinv_i64_i64(200, m);
int64_t inv_o = modinv_i64_i64(o64, m);
int64_t PREFIX[6] = { 0, 32, 64, 96, 130, 166 };
int64_t pow2[6] = { 0, 0, 0, 0, 0, 0 };
int64_t C[6] = { 0, 0, 0, 0, 0, 0 };
int64_t Rlist[6] = { 0, 0, 0, 0, 0, 0 };
int64_t base = 0;
int64_t r = 0;
while (r < 6) {
pow2[r] = modpow_i64_i64_i64(2, (r + 7), m);
C[r] = FLOW_CHECKED_MOD((((1 + B6) + (((unsigned)(r) < 6) ? PREFIX[r] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(r), 6), flow_fault_handler("array index out of bounds"), PREFIX[0])))), (m));
int64_t qmax = FLOW_CHECKED_DIV((((L - r) - 6)), (6));
int64_t N = (qmax + 1);
int64_t Q = FLOW_CHECKED_DIV((N), (period_q));
base = (base + (Q * o64));
Rlist[r] = (N - (Q * period_q));
r = (r + 1);
}
int64_t extra = 0;
int64_t pow64 = 1;
int64_t k = 0;
while (k < o64) {
r = 0;
while (r < 6) {
int64_t Lval = FLOW_CHECKED_MOD((((((unsigned)(r) < 6) ? pow2[r] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(r), 6), flow_fault_handler("array index out of bounds"), pow2[0])) * pow64)), (m));
int64_t q0 = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((Lval - (((unsigned)(r) < 6) ? C[r] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(r), 6), flow_fault_handler("array index out of bounds"), C[0])))), (m)) + m)), (m));
q0 = FLOW_CHECKED_MOD(((q0 * inv200)), (m));
int64_t diff = (q0 - k);
if (diff < 0) {
diff = (diff + m);
}
int64_t t = FLOW_CHECKED_MOD(((diff * inv_o)), (m));
int64_t q_res = (k + (o64 * t));
if (q_res < (((unsigned)(r) < 6) ? Rlist[r] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(r), 6), flow_fault_handler("array index out of bounds"), Rlist[0]))) {
extra = (extra + 1);
}
r = (r + 1);
}
pow64 = FLOW_CHECKED_MOD(((pow64 * 64)), (m));
k = (k + 1);
}
printf("%lld\n", (base + extra));
return 0;
}