# Project Euler 950
# Pirate Treasure. Exact integer arithmetic with i128.
# Compute sum_{k=1..6} T(10^16, 10^k+1, 1/sqrt(10^k+1)) mod 10^9.
const MOD9: i128 = 1000000000
function isqrt128(x: i128) -> i128 {
if x <= 0 { return 0 }
# Check if fits in i64 path
if x < (1 as i128) << 63 {
let mut r: i128 = x
let mut s: i128 = 0
let mut b: i128 = (1 as i128) << 62
while b > r { b = b >> 2 }
while b > 0 {
if r >= s + b {
r = r - (s + b)
s = (s >> 1) + b
} else {
s = s >> 1
}
b = b >> 2
}
return s
}
# Newton's method for large values
let mut bits: i64 = 0
let mut tmp: i128 = x
while tmp > 0 {
bits = bits + 1
tmp = tmp >> 1
}
let mut r: i128 = (1 as i128) << (bits / 2 + 1)
while true {
let next: i128 = (r + x / r) / 2
if next >= r { break }
r = next
}
while r * r > x { r = r - 1 }
while (r + 1) * (r + 1) <= x { r = r + 1 }
return r
}
function floor_div_sqrt(d: i128, D: i128) -> i128 {
if d <= 0 { return 0 }
let dd: i128 = d * d
let mut t: i128 = isqrt128(dd / D)
while (t + 1) * (t + 1) * D <= dd { t = t + 1 }
while t * t * D > dd { t = t - 1 }
return t
}
function ceil_div_sqrt(d: i128, D: i128) -> i128 {
if d <= 0 { return 0 }
return floor_div_sqrt(d, D) + 1
}
function initial_prefix_sum(N: i128, C: i128) -> i128 {
if N <= 0 { return 0 }
let limit: i128 = 2 * C + 2
let mut M: i128 = N
if M > limit { M = limit }
if M > 2 * C { M = 2 * C }
if M <= 0 { return 0 }
let m: i128 = M / 2
let mut s: i128 = 2 * (m * C - (m * (m - 1)) / 2)
if M % 2 == 1 {
s = s + (C - m)
}
return s
}
function next_reset(L: i128, C: i128, D: i128) -> i128 {
if C == 0 {
return 2 * L
}
let mut t: i128 = 1
while t <= C {
let y: i128 = C / t
let x: i128 = 2 * L - 2 * y
let d: i128 = x - L
if d > 0 {
let s: i128 = ceil_div_sqrt(d, D)
if C / s == y {
return x
}
}
t = C / y + 1
}
return 2 * L
}
function T_func(N: i128, C: i128, D: i128) -> i128 {
if N <= 0 { return 0 }
let start_reset: i128 = 2 * C + 2
if N <= start_reset {
return initial_prefix_sum(N, C)
}
let mut total: i128 = initial_prefix_sum(start_reset, C)
let mut L: i128 = start_reset
let mut cL: i128 = 0
while L < N {
let x: i128 = next_reset(L, C, D)
if x > N {
let d: i128 = N - L + 1
total = total + (d - 1) * cL + (d - 1) * d / 2
break
}
let d: i128 = x - L
if d > 1 {
total = total + (d - 1) * cL + (d - 1) * d / 2
}
let required_votes: i128 = (x + 1) / 2
let free_votes: i128 = d
let mut need_bribes: i128 = required_votes - free_votes
if need_bribes < 0 { need_bribes = 0 }
let s: i128 = ceil_div_sqrt(d, D)
let cost: i128 = need_bribes * s
cL = C - cost
total = total + cL
L = x
}
return total
}
function main() -> i32 {
let mut Nval: i128 = 1
for i in 0..16 {
Nval = Nval * 10
}
let mut acc: i128 = 0
for k in 1..7 {
let mut C: i128 = 1
for j in 0..k {
C = C * 10
}
C = C + 1
acc = acc + T_func(Nval, C, C)
}
let mut result: i128 = acc % MOD9
if result < 0 { result = result + MOD9 }
printf("%lld\n", result as i64)
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; }
__int128 isqrt128_i128(__int128 x);
__int128 floor_div_sqrt_i128_i128(__int128 d, __int128 D);
__int128 ceil_div_sqrt_i128_i128(__int128 d, __int128 D);
__int128 initial_prefix_sum_i128_i128(__int128 N, __int128 C);
__int128 next_reset_i128_i128_i128(__int128 L, __int128 C, __int128 D);
__int128 T_func_i128_i128_i128(__int128 N, __int128 C, __int128 D);
int32_t main(void);
static const __int128 MOD9 = 1000000000;
__int128 isqrt128_i128(__int128 x) {
if (x <= 0) {
return 0;
}
if (x < FLOW_CHECKED_SHL((((__int128)(1))), (63))) {
__int128 r = x;
__int128 s = 0;
__int128 b = FLOW_CHECKED_SHL((((__int128)(1))), (62));
while (b > r) {
b = FLOW_CHECKED_SHR((b), (2));
}
while (b > 0) {
if (r >= (s + b)) {
r = (r - (s + b));
s = (FLOW_CHECKED_SHR((s), (1)) + b);
} else {
s = FLOW_CHECKED_SHR((s), (1));
}
b = FLOW_CHECKED_SHR((b), (2));
}
return s;
}
int64_t bits = 0;
__int128 tmp = x;
while (tmp > 0) {
bits = (bits + 1);
tmp = FLOW_CHECKED_SHR((tmp), (1));
}
__int128 r = FLOW_CHECKED_SHL((((__int128)(1))), ((FLOW_CHECKED_DIV((bits), (2)) + 1)));
while (1) {
__int128 next = FLOW_CHECKED_DIV(((r + FLOW_CHECKED_DIV((x), (r)))), (2));
if (next >= r) {
break;
}
r = next;
}
while ((r * r) > x) {
r = (r - 1);
}
while (((r + 1) * (r + 1)) <= x) {
r = (r + 1);
}
return r;
}
__int128 floor_div_sqrt_i128_i128(__int128 d, __int128 D) {
if (d <= 0) {
return 0;
}
__int128 dd = (d * d);
__int128 t = isqrt128_i128(FLOW_CHECKED_DIV((dd), (D)));
while ((((t + 1) * (t + 1)) * D) <= dd) {
t = (t + 1);
}
while (((t * t) * D) > dd) {
t = (t - 1);
}
return t;
}
__int128 ceil_div_sqrt_i128_i128(__int128 d, __int128 D) {
if (d <= 0) {
return 0;
}
return (floor_div_sqrt_i128_i128(d, D) + 1);
}
__int128 initial_prefix_sum_i128_i128(__int128 N, __int128 C) {
if (N <= 0) {
return 0;
}
__int128 limit = ((2 * C) + 2);
__int128 M = N;
if (M > limit) {
M = limit;
}
if (M > (2 * C)) {
M = (2 * C);
}
if (M <= 0) {
return 0;
}
__int128 m = FLOW_CHECKED_DIV((M), (2));
__int128 s = (2 * ((m * C) - FLOW_CHECKED_DIV(((m * (m - 1))), (2))));
if (FLOW_CHECKED_MOD((M), (2)) == 1) {
s = (s + (C - m));
}
return s;
}
__int128 next_reset_i128_i128_i128(__int128 L, __int128 C, __int128 D) {
if (C == 0) {
return (2 * L);
}
__int128 t = 1;
while (t <= C) {
__int128 y = FLOW_CHECKED_DIV((C), (t));
__int128 x = ((2 * L) - (2 * y));
__int128 d = (x - L);
if (d > 0) {
__int128 s = ceil_div_sqrt_i128_i128(d, D);
if (FLOW_CHECKED_DIV((C), (s)) == y) {
return x;
}
}
t = (FLOW_CHECKED_DIV((C), (y)) + 1);
}
return (2 * L);
}
__int128 T_func_i128_i128_i128(__int128 N, __int128 C, __int128 D) {
if (N <= 0) {
return 0;
}
__int128 start_reset = ((2 * C) + 2);
if (N <= start_reset) {
return initial_prefix_sum_i128_i128(N, C);
}
__int128 total = initial_prefix_sum_i128_i128(start_reset, C);
__int128 L = start_reset;
__int128 cL = 0;
while (L < N) {
__int128 x = next_reset_i128_i128_i128(L, C, D);
if (x > N) {
__int128 d = ((N - L) + 1);
total = ((total + ((d - 1) * cL)) + FLOW_CHECKED_DIV((((d - 1) * d)), (2)));
break;
}
__int128 d = (x - L);
if (d > 1) {
total = ((total + ((d - 1) * cL)) + FLOW_CHECKED_DIV((((d - 1) * d)), (2)));
}
__int128 required_votes = FLOW_CHECKED_DIV(((x + 1)), (2));
__int128 free_votes = d;
__int128 need_bribes = (required_votes - free_votes);
if (need_bribes < 0) {
need_bribes = 0;
}
__int128 s = ceil_div_sqrt_i128_i128(d, D);
__int128 cost = (need_bribes * s);
cL = (C - cost);
total = (total + cL);
L = x;
}
return total;
}
int32_t main(void) {
__int128 Nval = 1;
int32_t __flow_step_1 = 1;
for (int32_t i = 0; (0 <= 16) ? i < 16 : i > 16; i += (0 <= 16) ? 1 : -1) {
Nval = (Nval * 10);
}
__int128 acc = 0;
int32_t __flow_step_2 = 1;
for (int32_t k = 1; (1 <= 7) ? k < 7 : k > 7; k += (1 <= 7) ? 1 : -1) {
__int128 C = 1;
int32_t __flow_step_3 = 1;
for (int32_t j = 0; (0 <= k) ? j < k : j > k; j += (0 <= k) ? 1 : -1) {
C = (C * 10);
}
C = (C + 1);
acc = (acc + T_func_i128_i128_i128(Nval, C, C));
}
__int128 result = FLOW_CHECKED_MOD((acc), (MOD9));
if (result < 0) {
result = (result + MOD9);
}
printf("%lld\n", ((int64_t)(result)));
return 0;
}