n = 17^p*a + 19^p*b + 23^p*c with a,b,c >= 1 is reachable iff n - s lies in the numerical semigroup S = <17^p, 19^p, 23^p>, where s = 17^p + 19^p + 23^p. So G(p) = sum(1..s-1) plus, for each gap m of S, the value s + m. Gaps come from the Apery set of S modulo x = 17^p, computed with the Bocker-Liptak round-robin algorithm: one cyclic relaxation pass per remaining generator (valid here since the generators are pairwise coprime).
# Project Euler 718: Unreachable Numbers
# n = 17^p*a + 19^p*b + 23^p*c with a,b,c >= 1 is reachable iff
# n - s lies in the numerical semigroup S = <17^p, 19^p, 23^p>,
# where s = 17^p + 19^p + 23^p. So G(p) = sum(1..s-1) plus, for each
# gap m of S, the value s + m. Gaps come from the Apery set of S
# modulo x = 17^p, computed with the Bocker-Liptak round-robin
# algorithm: one cyclic relaxation pass per remaining generator
# (valid here since the generators are pairwise coprime).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function ipow(b: i64, e: i64) -> i64 {
let mut r: i64 = 1
for i in 0..e {
r = r * b
}
return r
}
function solve(p: i64, m: i64) -> i64 {
let x: i64 = ipow(17, p)
let y: i64 = ipow(19, p)
let z: i64 = ipow(23, p)
# ap[r] = smallest element of S congruent to r mod x
let ap: ptr<i64> = calloc(x, 8)
if ap == null {
return -1
}
let inf: i64 = 4000000000000000000
for i in 1..x {
ap[i] = inf
}
ap[0] = 0
# Round-robin pass for generator y, then z. Start each cycle at
# residue 0, whose value 0 is the global minimum.
let mut r: i64 = 0
for i in 0..x {
let nr: i64 = (r + y) % x
let cand: i64 = ap[r] + y
if cand < ap[nr] {
ap[nr] = cand
}
r = nr
}
r = 0
for i in 0..x {
let nr: i64 = (r + z) % x
let cand: i64 = ap[r] + z
if cand < ap[nr] {
ap[nr] = cand
}
r = nr
}
# Sum unreachable values modulo m. All n in 1..s-1 are unreachable,
# then one unreachable value s + gap per gap of S. Residue class rr
# contributes gaps rr, rr + x, ..., ap[rr] - x.
let s: i64 = x + y + z
let mut total: i64 = (s % m) * ((s - 1) % m) % m
let inv2: i64 = (m + 1) / 2
total = total * inv2 % m
for rr in 0..x {
let k: i64 = (ap[rr] - rr) / x
if k > 0 {
let tri: i64 = k * (k - 1) / 2
let big: i128 = ((x as i128) * (tri as i128)) % (m as i128)
let mut term: i64 = ((k % m) * (s % m)) % m
term = (term + ((k % m) * (rr % m)) % m) % m
term = (term + (big as i64)) % m
total = (total + term) % m
}
}
free(ap)
return total
}
function main() -> i32 {
printf("%lld\n", solve(6, 1000000007))
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 ipow_i64_i64(int64_t b, int64_t e);
int64_t solve_i64_i64(int64_t p, int64_t m);
int32_t main(void);
int64_t ipow_i64_i64(int64_t b, int64_t e) {
int64_t r = 1;
int32_t __flow_step_1 = 1;
for (int32_t i = 0; (0 <= e) ? i < e : i > e; i += (0 <= e) ? 1 : -1) {
r = (r * b);
}
return r;
}
int64_t solve_i64_i64(int64_t p, int64_t m) {
int64_t x = ipow_i64_i64(17, p);
int64_t y = ipow_i64_i64(19, p);
int64_t z = ipow_i64_i64(23, p);
int64_t* ap = (int64_t*)(calloc(x, 8));
if (ap == NULL) {
return (-1);
}
int64_t inf = 4000000000000000000;
int32_t __flow_step_2 = 1;
for (int32_t i = 1; (1 <= x) ? i < x : i > x; i += (1 <= x) ? 1 : -1) {
ap[i] = inf;
}
ap[0] = 0;
int64_t r = 0;
int32_t __flow_step_3 = 1;
for (int32_t i = 0; (0 <= x) ? i < x : i > x; i += (0 <= x) ? 1 : -1) {
int64_t nr = FLOW_CHECKED_MOD(((r + y)), (x));
int64_t cand = (ap[r] + y);
if (cand < ap[nr]) {
ap[nr] = cand;
}
r = nr;
}
r = 0;
int32_t __flow_step_4 = 1;
for (int32_t i = 0; (0 <= x) ? i < x : i > x; i += (0 <= x) ? 1 : -1) {
int64_t nr = FLOW_CHECKED_MOD(((r + z)), (x));
int64_t cand = (ap[r] + z);
if (cand < ap[nr]) {
ap[nr] = cand;
}
r = nr;
}
int64_t s = ((x + y) + z);
int64_t total = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((s), (m)) * FLOW_CHECKED_MOD(((s - 1)), (m)))), (m));
int64_t inv2 = FLOW_CHECKED_DIV(((m + 1)), (2));
total = FLOW_CHECKED_MOD(((total * inv2)), (m));
int32_t __flow_step_5 = 1;
for (int32_t rr = 0; (0 <= x) ? rr < x : rr > x; rr += (0 <= x) ? 1 : -1) {
int64_t k = FLOW_CHECKED_DIV(((ap[rr] - rr)), (x));
if (k > 0) {
int64_t tri = FLOW_CHECKED_DIV(((k * (k - 1))), (2));
__int128 big = FLOW_CHECKED_MOD(((((__int128)(x)) * ((__int128)(tri)))), (((__int128)(m))));
int64_t term = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((k), (m)) * FLOW_CHECKED_MOD((s), (m)))), (m));
term = FLOW_CHECKED_MOD(((term + FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((k), (m)) * FLOW_CHECKED_MOD((rr), (m)))), (m)))), (m));
term = FLOW_CHECKED_MOD(((term + ((int64_t)(big)))), (m));
total = FLOW_CHECKED_MOD(((total + term)), (m));
}
}
free(ap);
return total;
}
int32_t main(void) {
printf("%lld\n", solve_i64_i64(6, 1000000007));
return 0;
}