Neighbourly Constraints: T(5000, 10^12) mod 1e9+7. States are the last tuple element a in [1, n-1]; the transfer operator (Mv)_a = sum_{b <= n-a} v_b is one prefix-sum plus a reversal, so each application costs O(n). Generate u_j = sum(M^j * 1) for 2(n-1)+12 terms, recover the minimal linear recurrence with Berlekamp-Massey (degree n-1), then jump to u_{m-1} with Kitamasa (x^K mod the recurrence polynomial).
# Project Euler 654
# Neighbourly Constraints: T(5000, 10^12) mod 1e9+7.
#
# States are the last tuple element a in [1, n-1]; the transfer operator
# (Mv)_a = sum_{b <= n-a} v_b is one prefix-sum plus a reversal, so each
# application costs O(n). Generate u_j = sum(M^j * 1) for 2(n-1)+12 terms,
# recover the minimal linear recurrence with Berlekamp-Massey (degree n-1),
# then jump to u_{m-1} with Kitamasa (x^K mod the recurrence polynomial).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 1000000007
const N: i64 = 5000
const EM: i64 = 999999999999 # m - 1, m = 10^12
function powmod(base: i64, exp: i64) -> i64 {
let mut result: i64 = 1
let mut b: i64 = base % MOD
let mut e: i64 = exp
while e > 0 {
if (e & 1) == 1 {
result = result * b % MOD
}
b = b * b % MOD
e = e >> 1
}
return result
}
# multiply a (len la) by b (len lb) into res, reduce mod the monic
# recurrence x^L = sum_{j=0..L-1} D[j] x^{L-1-j}; res has length L
function polymulred(a: ptr<i64>, la: i64, b: ptr<i64>, lb: i64,
D: ptr<i64>, L: i64, res: ptr<i64>, work: ptr<i64>) -> void {
let lr: i64 = la + lb - 1
let mut i: i64 = 0
while i < lr {
work[i] = 0
i = i + 1
}
i = 0
while i < la {
let ai: i64 = a[i]
if ai != 0 {
let mut j: i64 = 0
while j < lb {
work[i + j] = (work[i + j] + ai * b[j]) % MOD
j = j + 1
}
}
i = i + 1
}
i = lr - 1
while i >= L {
let c: i64 = work[i]
if c != 0 {
work[i] = 0
let mut j: i64 = 0
while j < L {
work[i - 1 - j] = (work[i - 1 - j] + c * D[j]) % MOD
j = j + 1
}
}
i = i - 1
}
i = 0
while i < L {
if i < lr {
res[i] = work[i]
} else {
res[i] = 0
}
i = i + 1
}
}
function k2_copy(dst: ptr<i64>, src: ptr<i64>, n: i64) -> void {
let mut i: i64 = 0
while i < n {
dst[i] = src[i]
i = i + 1
}
}
function main() -> i32 {
let terms: i64 = 2 * (N - 1) + 12
# ---- generate u_j = 1^T M^j 1 ----
let v: ptr<i64> = calloc(N + 1, 8)
let pre: ptr<i64> = calloc(N + 1, 8)
let S: ptr<i64> = calloc(terms + 2, 8)
let mut a: i64 = 1
while a < N {
v[a] = 1
a = a + 1
}
let mut j: i64 = 0
while j < terms {
# record sum and prefix sums in one pass
let mut s: i64 = 0
let mut b: i64 = 1
while b < N {
s = (s + v[b]) % MOD
pre[b] = s
b = b + 1
}
S[j] = s
# (Mv)_a = pre[n-a]
a = 1
while a < N {
v[a] = pre[N - a]
a = a + 1
}
j = j + 1
}
if EM < terms {
printf("%lld\n", S[EM])
return 0
}
# ---- Berlekamp-Massey ----
# C, B polynomials with C[0] = B[0] = 1
let C: ptr<i64> = calloc(terms + 2, 8)
let B: ptr<i64> = calloc(terms + 2, 8)
let T: ptr<i64> = calloc(terms + 2, 8)
C[0] = 1
B[0] = 1
let mut lenC: i64 = 1
let mut lenB: i64 = 1
let mut L: i64 = 0
let mut m: i64 = 1
let mut bb: i64 = 1
let mut i: i64 = 0
while i < terms {
let mut d: i64 = 0
let mut k: i64 = 0
while k <= L {
d = (d + C[k] * S[i - k]) % MOD
k = k + 1
}
if d == 0 {
m = m + 1
} else {
let coef: i64 = d * powmod(bb, MOD - 2) % MOD
if 2 * L <= i {
# save C into T
k = 0
while k < lenC {
T[k] = C[k]
k = k + 1
}
let lenT: i64 = lenC
if lenB + m > lenC {
k = lenC
while k < lenB + m {
C[k] = 0
k = k + 1
}
lenC = lenB + m
}
k = 0
while k < lenB {
C[k + m] = ((C[k + m] - coef * B[k]) % MOD + MOD) % MOD
k = k + 1
}
L = i + 1 - L
k = 0
while k < lenT {
B[k] = T[k]
k = k + 1
}
lenB = lenT
bb = d
m = 1
} else {
if lenB + m > lenC {
k = lenC
while k < lenB + m {
C[k] = 0
k = k + 1
}
lenC = lenB + m
}
k = 0
while k < lenB {
C[k + m] = ((C[k + m] - coef * B[k]) % MOD + MOD) % MOD
k = k + 1
}
m = m + 1
}
}
i = i + 1
}
# recurrence: s_n = sum_{j=1..L} D[j-1] s_{n-j}, D[j-1] = -C[j]
let D: ptr<i64> = calloc(L + 1, 8)
i = 0
while i < L {
D[i] = (MOD - C[i + 1]) % MOD
i = i + 1
}
# ---- Kitamasa: r(x) = x^EM mod (x^L - sum D[j] x^{L-1-j}) ----
let r: ptr<i64> = calloc(L + 1, 8)
let base: ptr<i64> = calloc(L + 1, 8)
let tmp: ptr<i64> = calloc(L + 1, 8)
let work: ptr<i64> = calloc(2 * L + 2, 8)
r[0] = 1
let mut lenR: i64 = 1
base[1] = 1 # the polynomial x (L >= 2 here)
let mut lenBase: i64 = 2
let mut e: i64 = EM
while e > 0 {
if (e & 1) == 1 {
polymulred(r, lenR, base, lenBase, D, L, tmp, work)
k2_copy(r, tmp, L)
lenR = L
}
e = e >> 1
if e > 0 {
polymulred(base, lenBase, base, lenBase, D, L, tmp, work)
k2_copy(base, tmp, L)
lenBase = L
}
}
let mut ans: i64 = 0
i = 0
while i < L {
ans = (ans + r[i] * S[i]) % MOD
i = i + 1
}
printf("%lld\n", ans)
free(v)
free(pre)
free(S)
free(C)
free(B)
free(T)
free(D)
free(r)
free(base)
free(tmp)
free(work)
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 powmod_i64_i64(int64_t base, int64_t exp);
void polymulred_ptr_i64_i64_ptr_i64_i64_ptr_i64_i64_ptr_i64_ptr_i64(int64_t* a, int64_t la, int64_t* b, int64_t lb, int64_t* D, int64_t L, int64_t* res, int64_t* work);
void k2_copy_ptr_i64_ptr_i64_i64(int64_t* dst, int64_t* src, int64_t n);
int32_t main(void);
static const int64_t MOD = 1000000007;
static const int64_t N = 5000;
static const int64_t EM = 999999999999;
int64_t powmod_i64_i64(int64_t base, int64_t exp) {
int64_t result = 1;
int64_t b = FLOW_CHECKED_MOD((base), (MOD));
int64_t e = exp;
while (e > 0) {
if ((e & 1) == 1) {
result = FLOW_CHECKED_MOD(((result * b)), (MOD));
}
b = FLOW_CHECKED_MOD(((b * b)), (MOD));
e = FLOW_CHECKED_SHR((e), (1));
}
return result;
}
void polymulred_ptr_i64_i64_ptr_i64_i64_ptr_i64_i64_ptr_i64_ptr_i64(int64_t* a, int64_t la, int64_t* b, int64_t lb, int64_t* D, int64_t L, int64_t* res, int64_t* work) {
int64_t lr = ((la + lb) - 1);
int64_t i = 0;
while (i < lr) {
work[i] = 0;
i = (i + 1);
}
i = 0;
while (i < la) {
int64_t ai = a[i];
if (ai != 0) {
int64_t j = 0;
while (j < lb) {
work[(i + j)] = FLOW_CHECKED_MOD(((work[(i + j)] + (ai * b[j]))), (MOD));
j = (j + 1);
}
}
i = (i + 1);
}
i = (lr - 1);
while (i >= L) {
int64_t c = work[i];
if (c != 0) {
work[i] = 0;
int64_t j = 0;
while (j < L) {
work[((i - 1) - j)] = FLOW_CHECKED_MOD(((work[((i - 1) - j)] + (c * D[j]))), (MOD));
j = (j + 1);
}
}
i = (i - 1);
}
i = 0;
while (i < L) {
if (i < lr) {
res[i] = work[i];
} else {
res[i] = 0;
}
i = (i + 1);
}
}
void k2_copy_ptr_i64_ptr_i64_i64(int64_t* dst, int64_t* src, int64_t n) {
int64_t i = 0;
while (i < n) {
dst[i] = src[i];
i = (i + 1);
}
}
int32_t main(void) {
int64_t terms = ((2 * (N - 1)) + 12);
int64_t* v = (int64_t*)(calloc((N + 1), 8));
int64_t* pre = (int64_t*)(calloc((N + 1), 8));
int64_t* S = (int64_t*)(calloc((terms + 2), 8));
int64_t a = 1;
while (a < N) {
v[a] = 1;
a = (a + 1);
}
int64_t j = 0;
while (j < terms) {
int64_t s = 0;
int64_t b = 1;
while (b < N) {
s = FLOW_CHECKED_MOD(((s + v[b])), (MOD));
pre[b] = s;
b = (b + 1);
}
S[j] = s;
a = 1;
while (a < N) {
v[a] = pre[(N - a)];
a = (a + 1);
}
j = (j + 1);
}
if (EM < terms) {
printf("%lld\n", S[EM]);
return 0;
}
int64_t* C = (int64_t*)(calloc((terms + 2), 8));
int64_t* B = (int64_t*)(calloc((terms + 2), 8));
int64_t* T = (int64_t*)(calloc((terms + 2), 8));
C[0] = 1;
B[0] = 1;
int64_t lenC = 1;
int64_t lenB = 1;
int64_t L = 0;
int64_t m = 1;
int64_t bb = 1;
int64_t i = 0;
while (i < terms) {
int64_t d = 0;
int64_t k = 0;
while (k <= L) {
d = FLOW_CHECKED_MOD(((d + (C[k] * S[(i - k)]))), (MOD));
k = (k + 1);
}
if (d == 0) {
m = (m + 1);
} else {
int64_t coef = FLOW_CHECKED_MOD(((d * powmod_i64_i64(bb, (MOD - 2)))), (MOD));
if ((2 * L) <= i) {
k = 0;
while (k < lenC) {
T[k] = C[k];
k = (k + 1);
}
int64_t lenT = lenC;
if ((lenB + m) > lenC) {
k = lenC;
while (k < (lenB + m)) {
C[k] = 0;
k = (k + 1);
}
lenC = (lenB + m);
}
k = 0;
while (k < lenB) {
C[(k + m)] = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((C[(k + m)] - (coef * B[k]))), (MOD)) + MOD)), (MOD));
k = (k + 1);
}
L = ((i + 1) - L);
k = 0;
while (k < lenT) {
B[k] = T[k];
k = (k + 1);
}
lenB = lenT;
bb = d;
m = 1;
} else {
if ((lenB + m) > lenC) {
k = lenC;
while (k < (lenB + m)) {
C[k] = 0;
k = (k + 1);
}
lenC = (lenB + m);
}
k = 0;
while (k < lenB) {
C[(k + m)] = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((C[(k + m)] - (coef * B[k]))), (MOD)) + MOD)), (MOD));
k = (k + 1);
}
m = (m + 1);
}
}
i = (i + 1);
}
int64_t* D = (int64_t*)(calloc((L + 1), 8));
i = 0;
while (i < L) {
D[i] = FLOW_CHECKED_MOD(((MOD - C[(i + 1)])), (MOD));
i = (i + 1);
}
int64_t* r = (int64_t*)(calloc((L + 1), 8));
int64_t* base = (int64_t*)(calloc((L + 1), 8));
int64_t* tmp = (int64_t*)(calloc((L + 1), 8));
int64_t* work = (int64_t*)(calloc(((2 * L) + 2), 8));
r[0] = 1;
int64_t lenR = 1;
base[1] = 1;
int64_t lenBase = 2;
int64_t e = EM;
while (e > 0) {
if ((e & 1) == 1) {
polymulred_ptr_i64_i64_ptr_i64_i64_ptr_i64_i64_ptr_i64_ptr_i64(r, lenR, base, lenBase, D, L, tmp, work);
k2_copy_ptr_i64_ptr_i64_i64(r, tmp, L);
lenR = L;
}
e = FLOW_CHECKED_SHR((e), (1));
if (e > 0) {
polymulred_ptr_i64_i64_ptr_i64_i64_ptr_i64_i64_ptr_i64_ptr_i64(base, lenBase, base, lenBase, D, L, tmp, work);
k2_copy_ptr_i64_ptr_i64_i64(base, tmp, L);
lenBase = L;
}
}
int64_t ans = 0;
i = 0;
while (i < L) {
ans = FLOW_CHECKED_MOD(((ans + (r[i] * S[i]))), (MOD));
i = (i + 1);
}
printf("%lld\n", ans);
free(v);
free(pre);
free(S);
free(C);
free(B);
free(T);
free(D);
free(r);
free(base);
free(tmp);
free(work);
return 0;
}