Buckets of Water: sum over prime pairs p<q<1000 of the minimal pour count P(2^{p^5}-1, 2^{q^5}-1), modulo 1e9+7. P(a,b) = 2*(pen_p + pen_q) - 2 where pen_p/pen_q is the penultimate convergent of b/a. For Mersenne arguments the Euclidean step reduces to Euclid on the exponents, so CF terms are computed mod MOD.
# Project Euler 758
# Buckets of Water: sum over prime pairs p<q<1000 of the minimal pour
# count P(2^{p^5}-1, 2^{q^5}-1), modulo 1e9+7.
#
# P(a,b) = 2*(pen_p + pen_q) - 2 where pen_p/pen_q is the penultimate
# convergent of b/a. For Mersenne arguments the Euclidean step reduces
# to Euclid on the exponents, so CF terms are computed mod MOD.
import euler.nt { mod_pow }
const MOD: i64 = 1000000007
function mulmod(a0: i64, b0: i64) -> i64 {
let a: i64 = a0 % MOD
let b: i64 = b0 % MOD
return a * b % MOD
}
function invmod(a: i64) -> i64 {
return mod_pow(a % MOD, MOD - 2, MOD)
}
# S = 1 + ratio + ... + ratio^(m-1) (mod MOD), m >= 1.
function geom_sum_ratio(ratio0: i64, m: i64) -> i64 {
if m <= 0 {
return 0
}
let mut ratio: i64 = ratio0 % MOD
if ratio < 0 {
ratio = ratio + MOD
}
if ratio == 1 {
return m % MOD
}
let rm: i64 = mod_pow(ratio, m, MOD)
let mut num: i64 = (rm - 1) % MOD
if num < 0 {
num = num + MOD
}
return mulmod(num, invmod(ratio - 1))
}
# Continued-fraction terms of (2^e_large - 1)/(2^e_small - 1), each mod MOD.
# Writes into terms[] and returns the count.
function cf_terms_mersenne(e_small: i64, e_large: i64, terms: ptr<i64>, cap: i32) -> i32 {
let mut n: i32 = 0
let mut hi: i64 = e_large
let mut lo: i64 = e_small
while true {
let m: i64 = hi / lo
let r: i64 = hi % lo
let ratio: i64 = mod_pow(2, lo, MOD)
let shift: i64 = mod_pow(2, r, MOD)
let series: i64 = geom_sum_ratio(ratio, m)
let q_mod: i64 = mulmod(shift, series)
terms[n] = q_mod
n = n + 1
if r == 0 {
break
}
hi = lo
lo = r
}
return n
}
# Penultimate convergent (p, q) of the CF with the given terms, mod MOD.
function pen_p(terms: ptr<i64>, n: i32) -> i64 {
let mut p_m2: i64 = 0
let mut p_m1: i64 = 1
let mut last_p: i64 = 0
let mut prev_p: i64 = 0
for i in 0..n {
let a: i64 = terms[i]
let p: i64 = (mulmod(a, p_m1) + p_m2) % MOD
prev_p = last_p
last_p = p
p_m2 = p_m1
p_m1 = p
}
if n == 1 {
return last_p
}
return prev_p
}
function pen_q(terms: ptr<i64>, n: i32) -> i64 {
let mut q_m2: i64 = 1
let mut q_m1: i64 = 0
let mut last_q: i64 = 0
let mut prev_q: i64 = 0
for i in 0..n {
let a: i64 = terms[i]
let q: i64 = (mulmod(a, q_m1) + q_m2) % MOD
prev_q = last_q
last_q = q
q_m2 = q_m1
q_m1 = q
}
if n == 1 {
return last_q
}
return prev_q
}
function P_mersenne_exponents(ea: i64, eb: i64, terms: ptr<i64>) -> i64 {
let n: i32 = cf_terms_mersenne(ea, eb, terms, 256)
let p: i64 = pen_p(terms, n)
let q: i64 = pen_q(terms, n)
let s: i64 = (p + q) % MOD
let mut result: i64 = (2 * s - 2) % MOD
if result < 0 {
result = result + MOD
}
return result
}
function primes_below(n: i32, out: ptr<i32>) -> i32 {
let sieve: ptr<i8> = calloc((n as i64), 1)
for i in 0..n {
sieve[i] = 1
}
let mut p: i32 = 2
while (p as i64) * (p as i64) < (n as i64) {
if sieve[p] != 0 {
let mut k: i32 = p * p
while k < n {
sieve[k] = 0
k = k + p
}
}
p = p + 1
}
let mut count: i32 = 0
for i in 0..n {
if i >= 2 {
if sieve[i] != 0 {
out[count] = i
count = count + 1
}
}
}
free(sieve)
return count
}
function main() -> i32 {
let primes: ptr<i32> = calloc(1000, 4)
let np: i32 = primes_below(1000, primes)
let exps: ptr<i64> = calloc(1000, 8)
for i in 0..np {
let p: i64 = (primes[i] as i64)
let e: i64 = p * p * p * p * p
exps[i] = e
}
let terms: ptr<i64> = calloc(256, 8)
let mut total: i64 = 0
for i in 0..np {
for j in 0..np {
if j > i {
total = (total + P_mersenne_exponents(exps[i], exps[j], terms)) % MOD
}
}
}
printf("%lld\n", total)
free(primes)
free(exps)
free(terms)
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 mulmod_i64_i64(int64_t a0, int64_t b0);
int64_t invmod_i64(int64_t a);
int64_t geom_sum_ratio_i64_i64(int64_t ratio0, int64_t m);
int32_t cf_terms_mersenne_i64_i64_ptr_i64_i32(int64_t e_small, int64_t e_large, int64_t* terms, int32_t cap);
int64_t pen_p_ptr_i64_i32(int64_t* terms, int32_t n);
int64_t pen_q_ptr_i64_i32(int64_t* terms, int32_t n);
int64_t P_mersenne_exponents_i64_i64_ptr_i64(int64_t ea, int64_t eb, int64_t* terms);
int32_t primes_below_i32_ptr_i32(int32_t n, int32_t* out);
int32_t main(void);
static const int64_t MOD = 1000000007;
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 mulmod_i64_i64(int64_t a0, int64_t b0) {
int64_t a = FLOW_CHECKED_MOD((a0), (MOD));
int64_t b = FLOW_CHECKED_MOD((b0), (MOD));
return FLOW_CHECKED_MOD(((a * b)), (MOD));
}
int64_t invmod_i64(int64_t a) {
return mod_pow_i64_i64_i64(FLOW_CHECKED_MOD((a), (MOD)), (MOD - 2), MOD);
}
int64_t geom_sum_ratio_i64_i64(int64_t ratio0, int64_t m) {
if (m <= 0) {
return 0;
}
int64_t ratio = FLOW_CHECKED_MOD((ratio0), (MOD));
if (ratio < 0) {
ratio = (ratio + MOD);
}
if (ratio == 1) {
return FLOW_CHECKED_MOD((m), (MOD));
}
int64_t rm = mod_pow_i64_i64_i64(ratio, m, MOD);
int64_t num = FLOW_CHECKED_MOD(((rm - 1)), (MOD));
if (num < 0) {
num = (num + MOD);
}
return mulmod_i64_i64(num, invmod_i64((ratio - 1)));
}
int32_t cf_terms_mersenne_i64_i64_ptr_i64_i32(int64_t e_small, int64_t e_large, int64_t* terms, int32_t cap) {
int32_t n = 0;
int64_t hi = e_large;
int64_t lo = e_small;
while (1) {
int64_t m = FLOW_CHECKED_DIV((hi), (lo));
int64_t r = FLOW_CHECKED_MOD((hi), (lo));
int64_t ratio = mod_pow_i64_i64_i64(2, lo, MOD);
int64_t shift = mod_pow_i64_i64_i64(2, r, MOD);
int64_t series = geom_sum_ratio_i64_i64(ratio, m);
int64_t q_mod = mulmod_i64_i64(shift, series);
terms[n] = q_mod;
n = (n + 1);
if (r == 0) {
break;
}
hi = lo;
lo = r;
}
return n;
}
int64_t pen_p_ptr_i64_i32(int64_t* terms, int32_t n) {
int64_t p_m2 = 0;
int64_t p_m1 = 1;
int64_t last_p = 0;
int64_t prev_p = 0;
int32_t __flow_step_1 = 1;
for (int32_t i = 0; (0 <= n) ? i < n : i > n; i += (0 <= n) ? 1 : -1) {
int64_t a = terms[i];
int64_t p = FLOW_CHECKED_MOD(((mulmod_i64_i64(a, p_m1) + p_m2)), (MOD));
prev_p = last_p;
last_p = p;
p_m2 = p_m1;
p_m1 = p;
}
if (n == 1) {
return last_p;
}
return prev_p;
}
int64_t pen_q_ptr_i64_i32(int64_t* terms, int32_t n) {
int64_t q_m2 = 1;
int64_t q_m1 = 0;
int64_t last_q = 0;
int64_t prev_q = 0;
int32_t __flow_step_2 = 1;
for (int32_t i = 0; (0 <= n) ? i < n : i > n; i += (0 <= n) ? 1 : -1) {
int64_t a = terms[i];
int64_t q = FLOW_CHECKED_MOD(((mulmod_i64_i64(a, q_m1) + q_m2)), (MOD));
prev_q = last_q;
last_q = q;
q_m2 = q_m1;
q_m1 = q;
}
if (n == 1) {
return last_q;
}
return prev_q;
}
int64_t P_mersenne_exponents_i64_i64_ptr_i64(int64_t ea, int64_t eb, int64_t* terms) {
int32_t n = cf_terms_mersenne_i64_i64_ptr_i64_i32(ea, eb, terms, 256);
int64_t p = pen_p_ptr_i64_i32(terms, n);
int64_t q = pen_q_ptr_i64_i32(terms, n);
int64_t s = FLOW_CHECKED_MOD(((p + q)), (MOD));
int64_t result = FLOW_CHECKED_MOD((((2 * s) - 2)), (MOD));
if (result < 0) {
result = (result + MOD);
}
return result;
}
int32_t primes_below_i32_ptr_i32(int32_t n, int32_t* out) {
int8_t* sieve = (int8_t*)(calloc(((int64_t)(n)), 1));
int32_t __flow_step_3 = 1;
for (int32_t i = 0; (0 <= n) ? i < n : i > n; i += (0 <= n) ? 1 : -1) {
sieve[i] = 1;
}
int32_t p = 2;
while ((((int64_t)(p)) * ((int64_t)(p))) < ((int64_t)(n))) {
if (sieve[p] != 0) {
int32_t k = (p * p);
while (k < n) {
sieve[k] = 0;
k = (k + p);
}
}
p = (p + 1);
}
int32_t count = 0;
int32_t __flow_step_4 = 1;
for (int32_t i = 0; (0 <= n) ? i < n : i > n; i += (0 <= n) ? 1 : -1) {
if (i >= 2) {
if (sieve[i] != 0) {
out[count] = i;
count = (count + 1);
}
}
}
free(sieve);
return count;
}
int32_t main(void) {
int32_t* primes = (int32_t*)(calloc(1000, 4));
int32_t np = primes_below_i32_ptr_i32(1000, primes);
int64_t* exps = (int64_t*)(calloc(1000, 8));
int32_t __flow_step_5 = 1;
for (int32_t i = 0; (0 <= np) ? i < np : i > np; i += (0 <= np) ? 1 : -1) {
int64_t p = ((int64_t)(primes[i]));
int64_t e = ((((p * p) * p) * p) * p);
exps[i] = e;
}
int64_t* terms = (int64_t*)(calloc(256, 8));
int64_t total = 0;
int32_t __flow_step_6 = 1;
for (int32_t i = 0; (0 <= np) ? i < np : i > np; i += (0 <= np) ? 1 : -1) {
int32_t __flow_step_7 = 1;
for (int32_t j = 0; (0 <= np) ? j < np : j > np; j += (0 <= np) ? 1 : -1) {
if (j > i) {
total = FLOW_CHECKED_MOD(((total + P_mersenne_exponents_i64_i64_ptr_i64(exps[i], exps[j], terms))), (MOD));
}
}
}
printf("%lld\n", total);
free(primes);
free(exps);
free(terms);
return 0;
}