# Project Euler 648: Skipping Squares
# F(1000) mod 10^9 using polynomial recurrence.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 1000000000
const N: i64 = 1000
# Multiply two polynomials (little-endian), truncate to out_len coefficients, mod MOD
function mul_trunc(a: ptr<i64>, b: ptr<i64>, out_len: i64) -> ptr<i64> {
let out: ptr<i64> = calloc(out_len, 8)
for i in 0..out_len {
let mut s: i64 = 0
for j in 0..(i + 1) {
if j < out_len && (i - j) < out_len {
s = (s + a[j] * b[i - j]) % MOD
}
}
out[i] = s
}
return out
}
function main() -> i32 {
# a[k] = coefficients of f(rho) = sum S_m
let a: ptr<i64> = calloc(N + 1, 8)
# S_1 = 1 - rho
let S: ptr<i64> = calloc(N + 1, 8)
S[0] = 1
S[1] = MOD - 1
let mut S_offset: i64 = 0
# Add S_1 into f
for i in 0..(N + 1) {
a[i] = (a[i] + S[i]) % MOD
}
# v_prev2 = v_0 = [0, 0, ...]
let v_prev2: ptr<i64> = calloc(N + 1, 8)
# v_prev1 = v_1 = S = 1 - rho
let v_prev1: ptr<i64> = calloc(N + 1, 8)
for i in 0..(N + 1) { v_prev1[i] = S[i] }
let vk: ptr<i64> = calloc(N + 1, 8)
for k in 2..(2 * N + 1) {
# Compute v_k = v_{k-2} + rho*(v_{k-1} - v_{k-2})
# v[0] = v_prev2[0]
# v[i+1] = v_prev2[i+1] + v_prev1[i] - v_prev2[i]
vk[0] = v_prev2[0]
for i in 0..N {
let mut val: i64 = v_prev2[i + 1] + v_prev1[i] - v_prev2[i]
val = val % MOD
if val < 0 { val = val + MOD }
vk[i + 1] = val
}
# Shift: v_prev2 = v_prev1, v_prev1 = vk
for i in 0..(N + 1) { v_prev2[i] = v_prev1[i] }
for i in 0..(N + 1) { v_prev1[i] = vk[i] }
if k % 2 != 0 { continue }
# n = k/2, b_n = v_k
# S_new = S * b_n (truncated)
# b_n has no constant term, so factor = b[1..maxdeg]
let maxdeg_factor: i64 = N - S_offset
let out_len: i64 = N - S_offset # degrees S_offset+1 .. N
if out_len <= 0 { continue }
# left = S[S_offset .. S_offset + out_len]
# right = b[1 .. 1 + out_len] (but need to be careful with lengths)
let left: ptr<i64> = calloc(out_len, 8)
let right: ptr<i64> = calloc(out_len, 8)
for i in 0..out_len {
left[i] = S[S_offset + i]
}
for i in 0..out_len {
if i + 1 <= N {
right[i] = vk[i + 1]
}
}
let prod: ptr<i64> = mul_trunc(left, right, out_len)
S_offset = S_offset + 1
# Rebuild S
let new_S: ptr<i64> = calloc(N + 1, 8)
for i in 0..out_len {
new_S[S_offset + i] = prod[i]
}
for i in 0..(N + 1) { S[i] = new_S[i] }
# Add S_{n+1} into f
for i in 0..out_len {
a[S_offset + i] = (a[S_offset + i] + prod[i]) % MOD
}
free(prod)
free(right)
free(left)
free(new_S)
}
# F(N) = sum a[0..N]
let mut ans: i64 = 0
for i in 0..(N + 1) {
ans = (ans + a[i]) % MOD
}
printf("%lld\n", ans)
free(vk)
free(v_prev1)
free(v_prev2)
free(S)
free(a)
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* mul_trunc_ptr_i64_ptr_i64_i64(int64_t* a, int64_t* b, int64_t out_len);
int32_t main(void);
static const int64_t MOD = 1000000000;
static const int64_t N = 1000;
int64_t* mul_trunc_ptr_i64_ptr_i64_i64(int64_t* a, int64_t* b, int64_t out_len) {
int64_t* out = (int64_t*)(calloc(out_len, 8));
int32_t __flow_step_1 = 1;
for (int32_t i = 0; (0 <= out_len) ? i < out_len : i > out_len; i += (0 <= out_len) ? 1 : -1) {
int64_t s = 0;
int32_t __flow_step_2 = 1;
for (int32_t j = 0; (0 <= (i + 1)) ? j < (i + 1) : j > (i + 1); j += (0 <= (i + 1)) ? 1 : -1) {
if ((j < out_len && (i - j) < out_len)) {
s = FLOW_CHECKED_MOD(((s + (a[j] * b[(i - j)]))), (MOD));
}
}
out[i] = s;
}
return out;
}
int32_t main(void) {
int64_t* a = (int64_t*)(calloc((N + 1), 8));
int64_t* S = (int64_t*)(calloc((N + 1), 8));
S[0] = 1;
S[1] = (MOD - 1);
int64_t S_offset = 0;
int32_t __flow_step_3 = 1;
for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
a[i] = FLOW_CHECKED_MOD(((a[i] + S[i])), (MOD));
}
int64_t* v_prev2 = (int64_t*)(calloc((N + 1), 8));
int64_t* v_prev1 = (int64_t*)(calloc((N + 1), 8));
int32_t __flow_step_4 = 1;
for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
v_prev1[i] = S[i];
}
int64_t* vk = (int64_t*)(calloc((N + 1), 8));
int32_t __flow_step_5 = 1;
for (int32_t k = 2; (2 <= ((2 * N) + 1)) ? k < ((2 * N) + 1) : k > ((2 * N) + 1); k += (2 <= ((2 * N) + 1)) ? 1 : -1) {
vk[0] = v_prev2[0];
int32_t __flow_step_6 = 1;
for (int32_t i = 0; (0 <= N) ? i < N : i > N; i += (0 <= N) ? 1 : -1) {
int64_t val = ((v_prev2[(i + 1)] + v_prev1[i]) - v_prev2[i]);
val = FLOW_CHECKED_MOD((val), (MOD));
if (val < 0) {
val = (val + MOD);
}
vk[(i + 1)] = val;
}
int32_t __flow_step_7 = 1;
for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
v_prev2[i] = v_prev1[i];
}
int32_t __flow_step_8 = 1;
for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
v_prev1[i] = vk[i];
}
if (FLOW_CHECKED_MOD((k), (2)) != 0) {
continue;
}
int64_t maxdeg_factor = (N - S_offset);
int64_t out_len = (N - S_offset);
if (out_len <= 0) {
continue;
}
int64_t* left = (int64_t*)(calloc(out_len, 8));
int64_t* right = (int64_t*)(calloc(out_len, 8));
int32_t __flow_step_9 = 1;
for (int32_t i = 0; (0 <= out_len) ? i < out_len : i > out_len; i += (0 <= out_len) ? 1 : -1) {
left[i] = S[(S_offset + i)];
}
int32_t __flow_step_10 = 1;
for (int32_t i = 0; (0 <= out_len) ? i < out_len : i > out_len; i += (0 <= out_len) ? 1 : -1) {
if ((i + 1) <= N) {
right[i] = vk[(i + 1)];
}
}
int64_t* prod = (int64_t*)(mul_trunc_ptr_i64_ptr_i64_i64(left, right, out_len));
S_offset = (S_offset + 1);
int64_t* new_S = (int64_t*)(calloc((N + 1), 8));
int32_t __flow_step_11 = 1;
for (int32_t i = 0; (0 <= out_len) ? i < out_len : i > out_len; i += (0 <= out_len) ? 1 : -1) {
new_S[(S_offset + i)] = prod[i];
}
int32_t __flow_step_12 = 1;
for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
S[i] = new_S[i];
}
int32_t __flow_step_13 = 1;
for (int32_t i = 0; (0 <= out_len) ? i < out_len : i > out_len; i += (0 <= out_len) ? 1 : -1) {
a[(S_offset + i)] = FLOW_CHECKED_MOD(((a[(S_offset + i)] + prod[i])), (MOD));
}
free(prod);
free(right);
free(left);
free(new_S);
}
int64_t ans = 0;
int32_t __flow_step_14 = 1;
for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
ans = FLOW_CHECKED_MOD(((ans + a[i])), (MOD));
}
printf("%lld\n", ans);
free(vk);
free(v_prev1);
free(v_prev2);
free(S);
free(a);
return 0;
}