# Project Euler 590
# Sets with a Given LCM — HL(50000) mod 1e9 (CRT: 0 mod 512, value mod 5^9).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD2: i64 = 512
const MOD5: i64 = 1953125
const PHI5: i64 = 1562500
function modpow(base0: i64, exp0: i64, modv: i64) -> i64 {
let mut result: i64 = 1
let mut base: i64 = base0 % modv
let mut exp: i64 = exp0
while exp > 0 {
if (exp & 1) != 0 {
result = ((result as i128) * (base as i128) % (modv as i128)) as i64
}
base = ((base as i128) * (base as i128) % (modv as i128)) as i64
exp = exp >> 1
}
return result
}
function modinv_coprime(a0: i64, m: i64) -> i64 {
let mut a: i64 = a0 % m
if a < 0 { a = a + m }
let mut mm: i64 = m
let mut x0: i64 = 1
let mut x1: i64 = 0
while mm != 0 {
let q: i64 = a / mm
let na: i64 = mm
mm = a - q * mm
a = na
let nx: i64 = x1
x1 = x0 - q * x1
x0 = nx
}
let mut inv: i64 = x0 % m
if inv < 0 { inv = inv + m }
return inv
}
function sieve(limit: i64, primes: ptr<i64>) -> i64 {
let s: ptr<i8> = calloc(limit + 1, 1)
let mut i: i64 = 0
while i <= limit { s[i] = 1; i = i + 1 }
s[0] = 0
s[1] = 0
i = 2
while i * i <= limit {
if s[i] != 0 {
let mut j: i64 = i * i
while j <= limit { s[j] = 0; j = j + i }
}
i = i + 1
}
let mut m: i64 = 0
i = 2
while i <= limit {
if s[i] != 0 { primes[m] = i; m = m + 1 }
i = i + 1
}
free(s)
return m
}
function max_power_exp(p: i64, n: i64) -> i64 {
let mut e: i64 = 1
let mut pp: i64 = p
while pp * p <= n {
pp = pp * p
e = e + 1
}
return e
}
# C(n,k) mod 5^9 via stripping factors of 5
function binom_mod5(n: i64, k: i64) -> i64 {
if k < 0 || k > n { return 0 }
if k == 0 || k == n { return 1 }
if k > n - k { k = n - k }
let mut num: i64 = 1
let mut den: i64 = 1
let mut val: i64 = 0
let mut i: i64 = 1
while i <= k {
let mut a: i64 = n - k + i
let mut b: i64 = i
while a % 5 == 0 {
a = a / 5
val = val + 1
}
while b % 5 == 0 {
b = b / 5
val = val - 1
}
num = ((num as i128) * (a as i128) % (MOD5 as i128)) as i64
den = ((den as i128) * (b as i128) % (MOD5 as i128)) as i64
i = i + 1
}
if val >= 9 { return 0 }
let inv: i64 = modinv_coprime(den, MOD5)
let mut res: i64 = ((num as i128) * (inv as i128) % (MOD5 as i128)) as i64
let mut p: i64 = 0
while p < val {
res = (res * 5) % MOD5
p = p + 1
}
return res
}
function HL_50000() -> i64 {
let N: i64 = 50000
let primes: ptr<i64> = calloc(10000, 8)
let np: i64 = sieve(N, primes)
let mut r: i64 = 0
let counts: ptr<i64> = calloc(32, 8)
let mut i: i64 = 0
while i < np {
let ae: i64 = max_power_exp(primes[i], N)
if ae == 1 {
r = r + 1
} else {
counts[ae] = counts[ae] + 1
}
i = i + 1
}
let CAP: i64 = 4000003
let mut keys: ptr<i64> = calloc(CAP, 8)
let mut vals: ptr<i64> = calloc(CAP, 8)
let mut used: ptr<i8> = calloc(CAP, 1)
let mut h: i64 = 1 % CAP
used[h] = 1
keys[h] = 1
vals[h] = 1
let mut ae: i64 = 2
while ae < 32 {
let c: i64 = counts[ae]
if c > 0 {
let terms_mul: ptr<i64> = calloc(c + 1, 8)
let terms_w: ptr<i64> = calloc(c + 1, 8)
let mut t: i64 = 0
while t <= c {
let mul: i64 = (modpow(ae, t, PHI5) * modpow(ae + 1, c - t, PHI5)) % PHI5
let mut w: i64 = binom_mod5(c, t)
if (t & 1) != 0 {
w = (MOD5 - w) % MOD5
}
terms_mul[t] = mul
terms_w[t] = w
t = t + 1
}
let nkeys: ptr<i64> = calloc(CAP, 8)
let nvals: ptr<i64> = calloc(CAP, 8)
let nused: ptr<i8> = calloc(CAP, 1)
i = 0
while i < CAP {
if used[i] != 0 {
let x_prev: i64 = keys[i]
let w_prev: i64 = vals[i]
t = 0
while t <= c {
let new_x: i64 = (x_prev * terms_mul[t]) % PHI5
let add: i64 = ((w_prev as i128) * (terms_w[t] as i128) % (MOD5 as i128)) as i64
let mut hh: i64 = new_x % CAP
if hh < 0 { hh = -hh }
while nused[hh] != 0 && nkeys[hh] != new_x {
hh = hh + 1
if hh >= CAP { hh = 0 }
}
if nused[hh] == 0 {
nused[hh] = 1
nkeys[hh] = new_x
nvals[hh] = add
} else {
nvals[hh] = (nvals[hh] + add) % MOD5
}
t = t + 1
}
}
i = i + 1
}
free(keys); free(vals); free(used)
free(terms_mul); free(terms_w)
keys = nkeys
vals = nvals
used = nused
}
ae = ae + 1
}
let coeffs: ptr<i64> = calloc(r + 1, 8)
let mut k: i64 = 0
while k <= r {
let ck: i64 = binom_mod5(r, k)
if ((r - k) & 1) == 0 {
coeffs[k] = ck
} else {
coeffs[k] = (MOD5 - ck) % MOD5
}
k = k + 1
}
let mut total_mod5: i64 = 0
i = 0
while i < CAP {
if used[i] != 0 {
let w: i64 = vals[i]
if w != 0 {
let x_mod: i64 = keys[i]
let mut p: i64 = modpow(2, x_mod, MOD5)
let mut acc: i64 = 0
k = 0
while k <= r {
acc = acc + coeffs[k] * p
if (k & 63) == 63 { acc = acc % MOD5 }
p = ((p as i128) * (p as i128) % (MOD5 as i128)) as i64
k = k + 1
}
total_mod5 = (total_mod5 + ((w as i128) * ((acc % MOD5) as i128) % (MOD5 as i128)) as i64) % MOD5
}
}
i = i + 1
}
let inv: i64 = modinv_coprime(MOD2, MOD5)
let tt: i64 = ((total_mod5 as i128) * (inv as i128) % (MOD5 as i128)) as i64
let ans: i64 = (MOD2 * tt) % 1000000000
free(primes); free(counts); free(keys); free(vals); free(used); free(coeffs)
return ans
}
function main() -> i32 {
printf("%lld\n", HL_50000())
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 modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t modv);
int64_t modinv_coprime_i64_i64(int64_t a0, int64_t m);
int64_t sieve_i64_ptr_i64(int64_t limit, int64_t* primes);
int64_t max_power_exp_i64_i64(int64_t p, int64_t n);
int64_t binom_mod5_i64_i64(int64_t n, int64_t k);
int64_t HL_50000(void);
int32_t main(void);
static const int64_t MOD2 = 512;
static const int64_t MOD5 = 1953125;
static const int64_t PHI5 = 1562500;
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t modv) {
int64_t result = 1;
int64_t base = FLOW_CHECKED_MOD((base0), (modv));
int64_t exp = exp0;
while (exp > 0) {
if ((exp & 1) != 0) {
result = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(result)) * ((__int128)(base)))), (((__int128)(modv))))));
}
base = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(base)) * ((__int128)(base)))), (((__int128)(modv))))));
exp = FLOW_CHECKED_SHR((exp), (1));
}
return result;
}
int64_t modinv_coprime_i64_i64(int64_t a0, int64_t m) {
int64_t a = FLOW_CHECKED_MOD((a0), (m));
if (a < 0) {
a = (a + m);
}
int64_t mm = m;
int64_t x0 = 1;
int64_t x1 = 0;
while (mm != 0) {
int64_t q = FLOW_CHECKED_DIV((a), (mm));
int64_t na = mm;
mm = (a - (q * mm));
a = na;
int64_t nx = x1;
x1 = (x0 - (q * x1));
x0 = nx;
}
int64_t inv = FLOW_CHECKED_MOD((x0), (m));
if (inv < 0) {
inv = (inv + m);
}
return inv;
}
int64_t sieve_i64_ptr_i64(int64_t limit, int64_t* primes) {
int8_t* s = (int8_t*)(calloc((limit + 1), 1));
int64_t i = 0;
while (i <= limit) {
s[i] = 1;
i = (i + 1);
}
s[0] = 0;
s[1] = 0;
i = 2;
while ((i * i) <= limit) {
if (s[i] != 0) {
int64_t j = (i * i);
while (j <= limit) {
s[j] = 0;
j = (j + i);
}
}
i = (i + 1);
}
int64_t m = 0;
i = 2;
while (i <= limit) {
if (s[i] != 0) {
primes[m] = i;
m = (m + 1);
}
i = (i + 1);
}
free(s);
return m;
}
int64_t max_power_exp_i64_i64(int64_t p, int64_t n) {
int64_t e = 1;
int64_t pp = p;
while ((pp * p) <= n) {
pp = (pp * p);
e = (e + 1);
}
return e;
}
int64_t binom_mod5_i64_i64(int64_t n, int64_t k) {
if ((k < 0 || k > n)) {
return 0;
}
if ((k == 0 || k == n)) {
return 1;
}
if (k > (n - k)) {
k = (n - k);
}
int64_t num = 1;
int64_t den = 1;
int64_t val = 0;
int64_t i = 1;
while (i <= k) {
int64_t a = ((n - k) + i);
int64_t b = i;
while (FLOW_CHECKED_MOD((a), (5)) == 0) {
a = FLOW_CHECKED_DIV((a), (5));
val = (val + 1);
}
while (FLOW_CHECKED_MOD((b), (5)) == 0) {
b = FLOW_CHECKED_DIV((b), (5));
val = (val - 1);
}
num = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(num)) * ((__int128)(a)))), (((__int128)(MOD5))))));
den = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(den)) * ((__int128)(b)))), (((__int128)(MOD5))))));
i = (i + 1);
}
if (val >= 9) {
return 0;
}
int64_t inv = modinv_coprime_i64_i64(den, MOD5);
int64_t res = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(num)) * ((__int128)(inv)))), (((__int128)(MOD5))))));
int64_t p = 0;
while (p < val) {
res = FLOW_CHECKED_MOD(((res * 5)), (MOD5));
p = (p + 1);
}
return res;
}
int64_t HL_50000(void) {
int64_t N = 50000;
int64_t* primes = (int64_t*)(calloc(10000, 8));
int64_t np = sieve_i64_ptr_i64(N, primes);
int64_t r = 0;
int64_t* counts = (int64_t*)(calloc(32, 8));
int64_t i = 0;
while (i < np) {
int64_t ae = max_power_exp_i64_i64(primes[i], N);
if (ae == 1) {
r = (r + 1);
} else {
counts[ae] = (counts[ae] + 1);
}
i = (i + 1);
}
int64_t CAP = 4000003;
int64_t* keys = (int64_t*)(calloc(CAP, 8));
int64_t* vals = (int64_t*)(calloc(CAP, 8));
int8_t* used = (int8_t*)(calloc(CAP, 1));
int64_t h = FLOW_CHECKED_MOD((1), (CAP));
used[h] = 1;
keys[h] = 1;
vals[h] = 1;
int64_t ae = 2;
while (ae < 32) {
int64_t c = counts[ae];
if (c > 0) {
int64_t* terms_mul = (int64_t*)(calloc((c + 1), 8));
int64_t* terms_w = (int64_t*)(calloc((c + 1), 8));
int64_t t = 0;
while (t <= c) {
int64_t mul = FLOW_CHECKED_MOD(((modpow_i64_i64_i64(ae, t, PHI5) * modpow_i64_i64_i64((ae + 1), (c - t), PHI5))), (PHI5));
int64_t w = binom_mod5_i64_i64(c, t);
if ((t & 1) != 0) {
w = FLOW_CHECKED_MOD(((MOD5 - w)), (MOD5));
}
terms_mul[t] = mul;
terms_w[t] = w;
t = (t + 1);
}
int64_t* nkeys = (int64_t*)(calloc(CAP, 8));
int64_t* nvals = (int64_t*)(calloc(CAP, 8));
int8_t* nused = (int8_t*)(calloc(CAP, 1));
i = 0;
while (i < CAP) {
if (used[i] != 0) {
int64_t x_prev = keys[i];
int64_t w_prev = vals[i];
t = 0;
while (t <= c) {
int64_t new_x = FLOW_CHECKED_MOD(((x_prev * terms_mul[t])), (PHI5));
int64_t add = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(w_prev)) * ((__int128)(terms_w[t])))), (((__int128)(MOD5))))));
int64_t hh = FLOW_CHECKED_MOD((new_x), (CAP));
if (hh < 0) {
hh = (-hh);
}
while ((nused[hh] != 0 && nkeys[hh] != new_x)) {
hh = (hh + 1);
if (hh >= CAP) {
hh = 0;
}
}
if (nused[hh] == 0) {
nused[hh] = 1;
nkeys[hh] = new_x;
nvals[hh] = add;
} else {
nvals[hh] = FLOW_CHECKED_MOD(((nvals[hh] + add)), (MOD5));
}
t = (t + 1);
}
}
i = (i + 1);
}
free(keys);
free(vals);
free(used);
free(terms_mul);
free(terms_w);
keys = nkeys;
vals = nvals;
used = nused;
}
ae = (ae + 1);
}
int64_t* coeffs = (int64_t*)(calloc((r + 1), 8));
int64_t k = 0;
while (k <= r) {
int64_t ck = binom_mod5_i64_i64(r, k);
if (((r - k) & 1) == 0) {
coeffs[k] = ck;
} else {
coeffs[k] = FLOW_CHECKED_MOD(((MOD5 - ck)), (MOD5));
}
k = (k + 1);
}
int64_t total_mod5 = 0;
i = 0;
while (i < CAP) {
if (used[i] != 0) {
int64_t w = vals[i];
if (w != 0) {
int64_t x_mod = keys[i];
int64_t p = modpow_i64_i64_i64(2, x_mod, MOD5);
int64_t acc = 0;
k = 0;
while (k <= r) {
acc = (acc + (coeffs[k] * p));
if ((k & 63) == 63) {
acc = FLOW_CHECKED_MOD((acc), (MOD5));
}
p = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(p)) * ((__int128)(p)))), (((__int128)(MOD5))))));
k = (k + 1);
}
total_mod5 = FLOW_CHECKED_MOD(((total_mod5 + ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(w)) * ((__int128)(FLOW_CHECKED_MOD((acc), (MOD5)))))), (((__int128)(MOD5)))))))), (MOD5));
}
}
i = (i + 1);
}
int64_t inv = modinv_coprime_i64_i64(MOD2, MOD5);
int64_t tt = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(total_mod5)) * ((__int128)(inv)))), (((__int128)(MOD5))))));
int64_t ans = FLOW_CHECKED_MOD(((MOD2 * tt)), (1000000000));
free(primes);
free(counts);
free(keys);
free(vals);
free(used);
free(coeffs);
return ans;
}
int32_t main(void) {
printf("%lld\n", HL_50000());
return 0;
}