# Project Euler 487
# Sums of Power Sums — sum_p S_10000(10^12) mod p over primes in [2e9, 2e9+2000].
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 {
let t: i128 = (r as i128) * (b as i128) % (mod as i128)
r = t as i64
}
let t2: i128 = (b as i128) * (b as i128) % (mod as i128)
b = t2 as i64
e = e / 2
}
return r
}
function sieve_primes(limit: i64, out: ptr<i64>) -> i64 {
let is_p: ptr<i8> = calloc(limit + 1, 1)
let mut i: i64 = 0
while i <= limit {
is_p[i] = 1
i = i + 1
}
is_p[0] = 0
is_p[1] = 0
i = 2
while i * i <= limit {
if is_p[i] != 0 {
let mut j: i64 = i * i
while j <= limit {
is_p[j] = 0
j = j + i
}
}
i = i + 1
}
let mut n: i64 = 0
i = 2
while i <= limit {
if is_p[i] != 0 {
out[n] = i
n = n + 1
}
i = i + 1
}
free(is_p)
return n
}
function primes_in_interval(L: i64, R: i64, out: ptr<i64>) -> i64 {
let mut lim: i64 = 1
while lim * lim <= R {
lim = lim + 1
}
lim = lim + 1
let small: ptr<i64> = calloc(lim + 16, 8)
let ns: i64 = sieve_primes(lim, small)
let mut nout: i64 = 0
let mut x: i64 = L
while x <= R {
let mut ok: i64 = 1
let mut i: i64 = 0
while i < ns {
let p: i64 = small[i]
if p * p > x { break }
if x % p == 0 {
ok = 0
break
}
i = i + 1
}
if ok != 0 {
out[nout] = x
nout = nout + 1
}
x = x + 1
}
free(small)
return nout
}
function inv_factorials(max_d: i64, p: i64, inv_fact: ptr<i64>) -> void {
let fact: ptr<i64> = calloc(max_d + 1, 8)
fact[0] = 1
let mut i: i64 = 1
while i <= max_d {
fact[i] = (fact[i - 1] * i) % p
i = i + 1
}
inv_fact[max_d] = modpow(fact[max_d], p - 2, p)
i = max_d
while i > 0 {
inv_fact[i - 1] = (inv_fact[i] * i) % p
i = i - 1
}
free(fact)
}
function lagrange_eval(y: ptr<i64>, n: i64, d: i64, p: i64, inv_fact: ptr<i64>) -> i64 {
if n <= d { return y[n] }
let pre: ptr<i64> = calloc(d + 2, 8)
let suf: ptr<i64> = calloc(d + 2, 8)
pre[0] = 1
let mut i: i64 = 0
while i <= d {
pre[i + 1] = (pre[i] * ((n - i) % p + p) % p) % p
i = i + 1
}
suf[d + 1] = 1
i = d
while i >= 0 {
suf[i] = (suf[i + 1] * ((n - i) % p + p) % p) % p
i = i - 1
}
let mut res: i64 = 0
i = 0
while i <= d {
let num: i64 = (pre[i] * suf[i + 1]) % p
let mut term: i64 = (y[i] * num) % p
term = (term * inv_fact[i]) % p
term = (term * inv_fact[d - i]) % p
if ((d - i) & 1) != 0 {
res = res - term
} else {
res = res + term
}
i = i + 1
}
free(suf)
free(pre)
let mut r: i64 = res % p
if r < 0 { r = r + p }
return r
}
function S_k_mod_prime(k: i64, n: i64, p: i64) -> i64 {
let d1: i64 = k + 1
let d2: i64 = k + 2
let n0: i64 = n % p
let inv_fact: ptr<i64> = calloc(d2 + 1, 8)
inv_factorials(d2, p, inv_fact)
let yk: ptr<i64> = calloc(d1 + 1, 8)
let yk1: ptr<i64> = calloc(d2 + 1, 8)
let mut s0: i64 = 0
let mut s1: i64 = 0
let mut i: i64 = 1
while i <= d2 {
let pk: i64 = modpow(i, k, p)
s0 = (s0 + pk) % p
if i <= d1 { yk[i] = s0 }
s1 = (s1 + (pk * i) % p) % p
yk1[i] = s1
i = i + 1
}
let fk: i64 = lagrange_eval(yk, n0, d1, p, inv_fact)
let fk1: i64 = lagrange_eval(yk1, n0, d2, p, inv_fact)
let mut ans: i64 = (((n + 1) % p) * fk - fk1) % p
if ans < 0 { ans = ans + p }
free(yk1)
free(yk)
free(inv_fact)
return ans
}
function main() -> i32 {
let L: i64 = 2000000000
let R: i64 = L + 2000
let n: i64 = 1000000000000
let k: i64 = 10000
let primes: ptr<i64> = calloc(256, 8)
let np: i64 = primes_in_interval(L, R, primes)
let mut total: i64 = 0
let mut i: i64 = 0
while i < np {
total = total + S_k_mod_prime(k, n, primes[i])
i = i + 1
}
printf("%lld\n", total)
free(primes)
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 mod);
int64_t sieve_primes_i64_ptr_i64(int64_t limit, int64_t* out);
int64_t primes_in_interval_i64_i64_ptr_i64(int64_t L, int64_t R, int64_t* out);
void inv_factorials_i64_i64_ptr_i64(int64_t max_d, int64_t p, int64_t* inv_fact);
int64_t lagrange_eval_ptr_i64_i64_i64_i64_ptr_i64(int64_t* y, int64_t n, int64_t d, int64_t p, int64_t* inv_fact);
int64_t S_k_mod_prime_i64_i64_i64(int64_t k, int64_t n, int64_t p);
int32_t main(void);
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) {
__int128 t = FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))));
r = ((int64_t)(t));
}
__int128 t2 = FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))));
b = ((int64_t)(t2));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t sieve_primes_i64_ptr_i64(int64_t limit, int64_t* out) {
int8_t* is_p = (int8_t*)(calloc((limit + 1), 1));
int64_t i = 0;
while (i <= limit) {
is_p[i] = 1;
i = (i + 1);
}
is_p[0] = 0;
is_p[1] = 0;
i = 2;
while ((i * i) <= limit) {
if (is_p[i] != 0) {
int64_t j = (i * i);
while (j <= limit) {
is_p[j] = 0;
j = (j + i);
}
}
i = (i + 1);
}
int64_t n = 0;
i = 2;
while (i <= limit) {
if (is_p[i] != 0) {
out[n] = i;
n = (n + 1);
}
i = (i + 1);
}
free(is_p);
return n;
}
int64_t primes_in_interval_i64_i64_ptr_i64(int64_t L, int64_t R, int64_t* out) {
int64_t lim = 1;
while ((lim * lim) <= R) {
lim = (lim + 1);
}
lim = (lim + 1);
int64_t* small = (int64_t*)(calloc((lim + 16), 8));
int64_t ns = sieve_primes_i64_ptr_i64(lim, small);
int64_t nout = 0;
int64_t x = L;
while (x <= R) {
int64_t ok = 1;
int64_t i = 0;
while (i < ns) {
int64_t p = small[i];
if ((p * p) > x) {
break;
}
if (FLOW_CHECKED_MOD((x), (p)) == 0) {
ok = 0;
break;
}
i = (i + 1);
}
if (ok != 0) {
out[nout] = x;
nout = (nout + 1);
}
x = (x + 1);
}
free(small);
return nout;
}
void inv_factorials_i64_i64_ptr_i64(int64_t max_d, int64_t p, int64_t* inv_fact) {
int64_t* fact = (int64_t*)(calloc((max_d + 1), 8));
fact[0] = 1;
int64_t i = 1;
while (i <= max_d) {
fact[i] = FLOW_CHECKED_MOD(((fact[(i - 1)] * i)), (p));
i = (i + 1);
}
inv_fact[max_d] = modpow_i64_i64_i64(fact[max_d], (p - 2), p);
i = max_d;
while (i > 0) {
inv_fact[(i - 1)] = FLOW_CHECKED_MOD(((inv_fact[i] * i)), (p));
i = (i - 1);
}
free(fact);
}
int64_t lagrange_eval_ptr_i64_i64_i64_i64_ptr_i64(int64_t* y, int64_t n, int64_t d, int64_t p, int64_t* inv_fact) {
if (n <= d) {
return y[n];
}
int64_t* pre = (int64_t*)(calloc((d + 2), 8));
int64_t* suf = (int64_t*)(calloc((d + 2), 8));
pre[0] = 1;
int64_t i = 0;
while (i <= d) {
pre[(i + 1)] = FLOW_CHECKED_MOD((FLOW_CHECKED_MOD(((pre[i] * (FLOW_CHECKED_MOD(((n - i)), (p)) + p))), (p))), (p));
i = (i + 1);
}
suf[(d + 1)] = 1;
i = d;
while (i >= 0) {
suf[i] = FLOW_CHECKED_MOD((FLOW_CHECKED_MOD(((suf[(i + 1)] * (FLOW_CHECKED_MOD(((n - i)), (p)) + p))), (p))), (p));
i = (i - 1);
}
int64_t res = 0;
i = 0;
while (i <= d) {
int64_t num = FLOW_CHECKED_MOD(((pre[i] * suf[(i + 1)])), (p));
int64_t term = FLOW_CHECKED_MOD(((y[i] * num)), (p));
term = FLOW_CHECKED_MOD(((term * inv_fact[i])), (p));
term = FLOW_CHECKED_MOD(((term * inv_fact[(d - i)])), (p));
if (((d - i) & 1) != 0) {
res = (res - term);
} else {
res = (res + term);
}
i = (i + 1);
}
free(suf);
free(pre);
int64_t r = FLOW_CHECKED_MOD((res), (p));
if (r < 0) {
r = (r + p);
}
return r;
}
int64_t S_k_mod_prime_i64_i64_i64(int64_t k, int64_t n, int64_t p) {
int64_t d1 = (k + 1);
int64_t d2 = (k + 2);
int64_t n0 = FLOW_CHECKED_MOD((n), (p));
int64_t* inv_fact = (int64_t*)(calloc((d2 + 1), 8));
inv_factorials_i64_i64_ptr_i64(d2, p, inv_fact);
int64_t* yk = (int64_t*)(calloc((d1 + 1), 8));
int64_t* yk1 = (int64_t*)(calloc((d2 + 1), 8));
int64_t s0 = 0;
int64_t s1 = 0;
int64_t i = 1;
while (i <= d2) {
int64_t pk = modpow_i64_i64_i64(i, k, p);
s0 = FLOW_CHECKED_MOD(((s0 + pk)), (p));
if (i <= d1) {
yk[i] = s0;
}
s1 = FLOW_CHECKED_MOD(((s1 + FLOW_CHECKED_MOD(((pk * i)), (p)))), (p));
yk1[i] = s1;
i = (i + 1);
}
int64_t fk = lagrange_eval_ptr_i64_i64_i64_i64_ptr_i64(yk, n0, d1, p, inv_fact);
int64_t fk1 = lagrange_eval_ptr_i64_i64_i64_i64_ptr_i64(yk1, n0, d2, p, inv_fact);
int64_t ans = FLOW_CHECKED_MOD((((FLOW_CHECKED_MOD(((n + 1)), (p)) * fk) - fk1)), (p));
if (ans < 0) {
ans = (ans + p);
}
free(yk1);
free(yk);
free(inv_fact);
return ans;
}
int32_t main(void) {
int64_t L = 2000000000;
int64_t R = (L + 2000);
int64_t n = 1000000000000;
int64_t k = 10000;
int64_t* primes = (int64_t*)(calloc(256, 8));
int64_t np = primes_in_interval_i64_i64_ptr_i64(L, R, primes);
int64_t total = 0;
int64_t i = 0;
while (i < np) {
total = (total + S_k_mod_prime_i64_i64_i64(k, n, primes[i]));
i = (i + 1);
}
printf("%lld\n", total);
free(primes);
return 0;
}