# Project Euler 889
# Rational Blancmange: F(10^18+31, 10^14+31, 62) mod 1000062031.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 1000062031
function mulmod(a0: i64, b0: i64, m: i64) -> i64 {
let a_w: i128 = a0 as i128
let b_w: i128 = b0 as i128
let m_w: i128 = m as i128
let r: i128 = (a_w * b_w) % m_w
return r as i64
}
function powmod(a0: i64, e0: i64, m: i64) -> i64 {
let mut r: i64 = 1 % m
let mut a: i64 = a0 % m
let mut e: i64 = e0
while e > 0 {
if (e & 1) == 1 { r = mulmod(r, a, m) }
a = mulmod(a, a, m)
e = e >> 1
}
return r
}
function popcount_u64(x0: i64) -> i32 {
let mut x: i64 = x0
let mut count: i32 = 0
while x != 0 {
count = count + 1
x = x & (x - 1)
}
return count
}
function bitlen_u64(x0: i64) -> i32 {
if x0 == 0 { return 0 }
let mut x: i64 = x0
let mut n: i32 = 0
while x != 0 {
n = n + 1
x = x >> 1
}
return n
}
function ctz_u64(x0: i64) -> i32 {
if x0 == 0 { return 64 }
let mut x: i64 = x0
let mut n: i32 = 0
while (x & 1) == 0 {
n = n + 1
x = x >> 1
}
return n
}
function binom_small(n: i32, k: i32) -> i64 {
if k < 0 || k > n { return 0 }
let mut kk: i32 = k
if kk > n - kk { kk = n - kk }
let mut r: i128 = 1
let mut i: i32 = 0
while i < kk {
r = r * ((n - i) as i128) / ((i + 1) as i128)
i = i + 1
}
return r as i64
}
function bit_positions_of_N(t: i64, r: i32, positions: ptr<i64>) -> i32 {
let mut n: i32 = 0
let mut u: i32 = 0
while u <= r {
let mut c: i64 = binom_small(r, u)
let base: i64 = t * (u as i64)
while c > 0 {
let b: i32 = ctz_u64(c)
positions[n] = base + (b as i64)
n = n + 1
c = c & (c - 1)
}
u = u + 1
}
# insertion sort
let mut i: i32 = 1
while i < n {
let key: i64 = positions[i]
let mut j: i32 = i - 1
while j >= 0 && positions[j] > key {
positions[j + 1] = positions[j]
j = j - 1
}
positions[j + 1] = key
i = i + 1
}
return n
}
function fast_F_mod(k: i64, t: i64, r: i32, mod: i64) -> i64 {
let positions: ptr<i64> = calloc(4000, 8)
let n: i32 = bit_positions_of_N(t, r, positions)
let max_pos: i64 = positions[n - 1]
let pow2_k: i64 = powmod(2, k, mod)
let vals_low: ptr<i64> = calloc(n as i64, 8)
let vals_high: ptr<i64> = calloc(n as i64, 8)
let mut i: i32 = 0
while i < n {
vals_low[i] = powmod(2, positions[i], mod)
vals_high[i] = mulmod(pow2_k, vals_low[i], mod)
i = i + 1
}
let prefix_low: ptr<i64> = calloc(((n + 1) as i64), 8)
let prefix_high: ptr<i64> = calloc(((n + 1) as i64), 8)
prefix_low[0] = 0
prefix_high[0] = 0
i = 0
while i < n {
prefix_low[i + 1] = (prefix_low[i] + vals_low[i]) % mod
prefix_high[i + 1] = (prefix_high[i] + vals_high[i]) % mod
i = i + 1
}
let total_low: i64 = prefix_low[n]
let mut ans: i64 = 0
i = 0
while i < n {
let p: i64 = positions[i]
let v_low: i64 = vals_low[i]
let v_high: i64 = vals_high[i]
let km: i64 = k % mod
let pm: i64 = p % mod
let term: i64 = mulmod((km + mod - pm) % mod, v_high, mod)
let term2: i64 = (term + mod - mulmod(pm, v_low, mod)) % mod
ans = (ans + term2) % mod
i = i + 1
}
# Corrections
i = 1
while i < n {
let p0: i64 = positions[i]
let sum_high_le: i64 = prefix_high[i + 1]
let sum_low_gt: i64 = (total_low + mod - prefix_low[i + 1]) % mod
let S: i64 = (sum_high_le + mod - sum_low_gt) % mod
let pow2_p0_plus1: i64 = mulmod(vals_low[i], 2, mod)
let q_times: i64 = mulmod((pow2_k + 1) % mod, pow2_p0_plus1, mod)
let delta: i64 = (q_times + mod - mulmod(S, 2, mod)) % mod
ans = (ans + delta) % mod
i = i + 1
}
free(positions)
free(vals_low)
free(vals_high)
free(prefix_low)
free(prefix_high)
return ans
}
function main() -> i32 {
let k: i64 = 1000000000000000031
let t: i64 = 100000000000031
let r: i32 = 62
let ans: i64 = fast_F_mod(k, t, r, MOD)
printf("%lld\n", ans)
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 mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t m);
int64_t powmod_i64_i64_i64(int64_t a0, int64_t e0, int64_t m);
int32_t popcount_u64_i64(int64_t x0);
int32_t bitlen_u64_i64(int64_t x0);
int32_t ctz_u64_i64(int64_t x0);
int64_t binom_small_i32_i32(int32_t n, int32_t k);
int32_t bit_positions_of_N_i64_i32_ptr_i64(int64_t t, int32_t r, int64_t* positions);
int64_t fast_F_mod_i64_i64_i32_i64(int64_t k, int64_t t, int32_t r, int64_t mod);
int32_t main(void);
static const int64_t MOD = 1000062031;
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t m) {
__int128 a_w = ((__int128)(a0));
__int128 b_w = ((__int128)(b0));
__int128 m_w = ((__int128)(m));
__int128 r = FLOW_CHECKED_MOD(((a_w * b_w)), (m_w));
return ((int64_t)(r));
}
int64_t powmod_i64_i64_i64(int64_t a0, int64_t e0, int64_t m) {
int64_t r = FLOW_CHECKED_MOD((1), (m));
int64_t a = FLOW_CHECKED_MOD((a0), (m));
int64_t e = e0;
while (e > 0) {
if ((e & 1) == 1) {
r = mulmod_i64_i64_i64(r, a, m);
}
a = mulmod_i64_i64_i64(a, a, m);
e = FLOW_CHECKED_SHR((e), (1));
}
return r;
}
int32_t popcount_u64_i64(int64_t x0) {
int64_t x = x0;
int32_t count = 0;
while (x != 0) {
count = (count + 1);
x = (x & (x - 1));
}
return count;
}
int32_t bitlen_u64_i64(int64_t x0) {
if (x0 == 0) {
return 0;
}
int64_t x = x0;
int32_t n = 0;
while (x != 0) {
n = (n + 1);
x = FLOW_CHECKED_SHR((x), (1));
}
return n;
}
int32_t ctz_u64_i64(int64_t x0) {
if (x0 == 0) {
return 64;
}
int64_t x = x0;
int32_t n = 0;
while ((x & 1) == 0) {
n = (n + 1);
x = FLOW_CHECKED_SHR((x), (1));
}
return n;
}
int64_t binom_small_i32_i32(int32_t n, int32_t k) {
if ((k < 0 || k > n)) {
return 0;
}
int32_t kk = k;
if (kk > (n - kk)) {
kk = (n - kk);
}
__int128 r = 1;
int32_t i = 0;
while (i < kk) {
r = FLOW_CHECKED_DIV(((r * ((__int128)((n - i))))), (((__int128)((i + 1)))));
i = (i + 1);
}
return ((int64_t)(r));
}
int32_t bit_positions_of_N_i64_i32_ptr_i64(int64_t t, int32_t r, int64_t* positions) {
int32_t n = 0;
int32_t u = 0;
while (u <= r) {
int64_t c = binom_small_i32_i32(r, u);
int64_t base = (t * ((int64_t)(u)));
while (c > 0) {
int32_t b = ctz_u64_i64(c);
positions[n] = (base + ((int64_t)(b)));
n = (n + 1);
c = (c & (c - 1));
}
u = (u + 1);
}
int32_t i = 1;
while (i < n) {
int64_t key = positions[i];
int32_t j = (i - 1);
while ((j >= 0 && positions[j] > key)) {
positions[(j + 1)] = positions[j];
j = (j - 1);
}
positions[(j + 1)] = key;
i = (i + 1);
}
return n;
}
int64_t fast_F_mod_i64_i64_i32_i64(int64_t k, int64_t t, int32_t r, int64_t mod) {
int64_t* positions = (int64_t*)(calloc(4000, 8));
int32_t n = bit_positions_of_N_i64_i32_ptr_i64(t, r, positions);
int64_t max_pos = positions[(n - 1)];
int64_t pow2_k = powmod_i64_i64_i64(2, k, mod);
int64_t* vals_low = (int64_t*)(calloc(((int64_t)(n)), 8));
int64_t* vals_high = (int64_t*)(calloc(((int64_t)(n)), 8));
int32_t i = 0;
while (i < n) {
vals_low[i] = powmod_i64_i64_i64(2, positions[i], mod);
vals_high[i] = mulmod_i64_i64_i64(pow2_k, vals_low[i], mod);
i = (i + 1);
}
int64_t* prefix_low = (int64_t*)(calloc(((int64_t)((n + 1))), 8));
int64_t* prefix_high = (int64_t*)(calloc(((int64_t)((n + 1))), 8));
prefix_low[0] = 0;
prefix_high[0] = 0;
i = 0;
while (i < n) {
prefix_low[(i + 1)] = FLOW_CHECKED_MOD(((prefix_low[i] + vals_low[i])), (mod));
prefix_high[(i + 1)] = FLOW_CHECKED_MOD(((prefix_high[i] + vals_high[i])), (mod));
i = (i + 1);
}
int64_t total_low = prefix_low[n];
int64_t ans = 0;
i = 0;
while (i < n) {
int64_t p = positions[i];
int64_t v_low = vals_low[i];
int64_t v_high = vals_high[i];
int64_t km = FLOW_CHECKED_MOD((k), (mod));
int64_t pm = FLOW_CHECKED_MOD((p), (mod));
int64_t term = mulmod_i64_i64_i64(FLOW_CHECKED_MOD((((km + mod) - pm)), (mod)), v_high, mod);
int64_t term2 = FLOW_CHECKED_MOD((((term + mod) - mulmod_i64_i64_i64(pm, v_low, mod))), (mod));
ans = FLOW_CHECKED_MOD(((ans + term2)), (mod));
i = (i + 1);
}
i = 1;
while (i < n) {
int64_t p0 = positions[i];
int64_t sum_high_le = prefix_high[(i + 1)];
int64_t sum_low_gt = FLOW_CHECKED_MOD((((total_low + mod) - prefix_low[(i + 1)])), (mod));
int64_t S = FLOW_CHECKED_MOD((((sum_high_le + mod) - sum_low_gt)), (mod));
int64_t pow2_p0_plus1 = mulmod_i64_i64_i64(vals_low[i], 2, mod);
int64_t q_times = mulmod_i64_i64_i64(FLOW_CHECKED_MOD(((pow2_k + 1)), (mod)), pow2_p0_plus1, mod);
int64_t delta = FLOW_CHECKED_MOD((((q_times + mod) - mulmod_i64_i64_i64(S, 2, mod))), (mod));
ans = FLOW_CHECKED_MOD(((ans + delta)), (mod));
i = (i + 1);
}
free(positions);
free(vals_low);
free(vals_high);
free(prefix_low);
free(prefix_high);
return ans;
}
int32_t main(void) {
int64_t k = 1000000000000000031;
int64_t t = 100000000000031;
int32_t r = 62;
int64_t ans = fast_F_mod_i64_i64_i32_i64(k, t, r, MOD);
printf("%lld\n", ans);
return 0;
}