# Project Euler 922: Staircase Game
# R(m, w) mod 10^9+7, compute R(8, 64).
# Uses FWT for XOR convolution and polynomial exponentiation.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function memset(p: ptr<void>, c: i32, n: i64) -> ptr<void>
function memcpy(dst: ptr<void>, src: ptr<void>, n: i64) -> ptr<void>
}
const MOD: i64 = 1000000007
const XOR_SIZE: i32 = 64
const W: i32 = 64
const M: i32 = 8
const DMAX: i32 = 62
const DIFF_COUNT: i32 = 125
const MAX_POLY: i32 = 1024
# counts[DIFF_COUNT][XOR_SIZE] flat
let mut g_counts: ptr<i64> = null
# Qhat[XOR_SIZE][MAX_POLY] flat
let mut g_qhat: ptr<i64> = null
# vec[XOR_SIZE]
let mut g_vec: ptr<i64> = null
# Polynomial buffers: 3 polynomials each with MAX_POLY coeffs
let mut g_poly_coeffs: ptr<i64> = null
let mut g_poly_len: ptr<i32> = null
let mut g_poly_offset: ptr<i64> = null
# tmp buffer for poly_mul
let mut g_pmul_tmp: ptr<i64> = null
function powmod(a0: i64, b0: i64, m: i64) -> i64 {
let mut r: i64 = 1
let mut a: i64 = a0 % m
if a < 0 { a = a + m }
let mut b: i64 = b0
while b > 0 {
if (b & 1) == 1 {
let aw: i128 = r as i128
let bw: i128 = a as i128
let mw: i128 = m as i128
r = ((aw * bw) % mw) as i64
}
let aw2: i128 = a as i128
let bw2: i128 = a as i128
let mw2: i128 = m as i128
a = ((aw2 * bw2) % mw2) as i64
b = b >> 1
}
return r
}
function fwt_xor(arr: ptr<i64>, n: i32, inverse: i32) -> void {
let mut step: i32 = 1
while step < n {
let jump: i32 = step * 2
let mut i: i32 = 0
while i < n {
let mut j: i32 = i
while j < i + step {
let x: i64 = arr[j]
let y: i64 = arr[j + step]
let mut s: i64 = x + y
if s >= MOD { s = s - MOD }
let mut d: i64 = x - y
if d < 0 { d = d + MOD }
arr[j] = s
arr[j + step] = d
j = j + 1
}
i = i + jump
}
step = jump
}
if inverse != 0 {
let inv_n: i64 = powmod(n as i64, MOD - 2, MOD)
let mut i: i32 = 0
while i < n {
let aw: i128 = arr[i] as i128
let bw: i128 = inv_n as i128
let mw: i128 = MOD as i128
arr[i] = ((aw * bw) % mw) as i64
i = i + 1
}
}
}
# poly_mul: res_idx = a_idx * b_idx
function poly_mul(res_idx: i32, a_idx: i32, b_idx: i32) -> void {
let big_idx: i32 = a_idx
let small_idx: i32 = b_idx
if g_poly_len[a_idx] < g_poly_len[b_idx] {
big_idx = b_idx
small_idx = a_idx
}
let new_len: i32 = g_poly_len[big_idx] + g_poly_len[small_idx] - 1
memset(g_pmul_tmp as ptr<void>, 0, (new_len as i64) * 8)
let mut i: i32 = 0
while i < g_poly_len[big_idx] {
if g_poly_coeffs[big_idx * MAX_POLY + i] != 0 {
let mut j: i32 = 0
while j < g_poly_len[small_idx] {
if g_poly_coeffs[small_idx * MAX_POLY + j] != 0 {
let aw: i128 = g_pmul_tmp[i + j] as i128
let bw: i128 = g_poly_coeffs[big_idx * MAX_POLY + i] as i128
let cw: i128 = g_poly_coeffs[small_idx * MAX_POLY + j] as i128
let mw: i128 = MOD as i128
g_pmul_tmp[i + j] = ((aw + bw * cw) % mw) as i64
}
j = j + 1
}
}
i = i + 1
}
g_poly_len[res_idx] = new_len
g_poly_offset[res_idx] = g_poly_offset[a_idx] + g_poly_offset[b_idx]
memcpy(g_poly_coeffs + res_idx * MAX_POLY, g_pmul_tmp, (new_len as i64) * 8)
}
# poly_pow: res_idx = base_idx ^ exp
function poly_pow(res_idx: i32, base_idx: i32, exp: i32) -> void {
# Copy base_in to buffer 1 first (before overwriting buffer 0)
let base: i32 = 1
g_poly_len[base] = g_poly_len[base_idx]
g_poly_offset[base] = g_poly_offset[base_idx]
memcpy(g_poly_coeffs + base * MAX_POLY, g_poly_coeffs + base_idx * MAX_POLY, (g_poly_len[base_idx] as i64) * 8)
# result = {1} in buffer 0
let res: i32 = 0
g_poly_len[res] = 1
g_poly_offset[res] = 0
g_poly_coeffs[res * MAX_POLY + 0] = 1
let tmp: i32 = 2
let mut e: i32 = exp
while e > 0 {
if (e & 1) == 1 {
poly_mul(tmp, res, base)
# res = tmp
g_poly_len[res] = g_poly_len[tmp]
g_poly_offset[res] = g_poly_offset[tmp]
memcpy(g_poly_coeffs + res * MAX_POLY, g_poly_coeffs + tmp * MAX_POLY, (g_poly_len[tmp] as i64) * 8)
}
e = e >> 1
if e != 0 {
poly_mul(tmp, base, base)
# base = tmp
g_poly_len[base] = g_poly_len[tmp]
g_poly_offset[base] = g_poly_offset[tmp]
memcpy(g_poly_coeffs + base * MAX_POLY, g_poly_coeffs + tmp * MAX_POLY, (g_poly_len[tmp] as i64) * 8)
}
}
# res_idx = res
g_poly_len[res_idx] = g_poly_len[res]
g_poly_offset[res_idx] = g_poly_offset[res]
memcpy(g_poly_coeffs + res_idx * MAX_POLY, g_poly_coeffs + res * MAX_POLY, (g_poly_len[res] as i64) * 8)
}
function main() -> i32 {
let w: i32 = W
let m: i32 = M
let dmax: i32 = w - 2
let diff_count: i32 = 2 * dmax + 1
# Allocate arrays
g_counts = calloc((diff_count * XOR_SIZE) as i64, 8)
g_qhat = calloc((XOR_SIZE * MAX_POLY) as i64, 8)
g_vec = calloc(XOR_SIZE as i64, 8)
g_poly_coeffs = calloc((3 * MAX_POLY) as i64, 8)
g_poly_len = calloc(3, 4)
g_poly_offset = calloc(3, 8)
g_pmul_tmp = calloc(MAX_POLY as i64, 8)
# Build counts
let mut k: i32 = 1
while k < w - 1 {
let limit: i32 = w - k
if limit >= 2 {
let g: i32 = k - 1
let tmax: i32 = limit - 2
let mut t: i32 = 0
while t <= tmax {
let c: i32 = (limit - t) / 2
if c > 0 {
let idx1: i32 = (dmax + t) * XOR_SIZE + g
g_counts[idx1] = (g_counts[idx1] + c as i64) % MOD
if t != 0 {
let idx2: i32 = (dmax - t) * XOR_SIZE + g
g_counts[idx2] = (g_counts[idx2] + c as i64) % MOD
}
}
t = t + 1
}
}
k = k + 1
}
# FWT each row
let mut d: i32 = 0
while d < diff_count {
fwt_xor(g_counts + d * XOR_SIZE, XOR_SIZE, 0)
d = d + 1
}
let final_offset: i32 = m * dmax
let final_len: i32 = 2 * final_offset + 1
# For each t, build polynomial and raise to power m
let mut t: i32 = 0
while t < XOR_SIZE {
# poly = counts[:, t]
g_poly_len[0] = diff_count
g_poly_offset[0] = dmax
let mut di: i32 = 0
while di < diff_count {
g_poly_coeffs[0 * MAX_POLY + di] = g_counts[di * XOR_SIZE + t]
di = di + 1
}
poly_pow(1, 0, m)
let mut i: i32 = 0
while i < g_poly_len[1] {
if i < final_len {
g_qhat[t * MAX_POLY + i] = g_poly_coeffs[1 * MAX_POLY + i]
}
i = i + 1
}
t = t + 1
}
# Compute answer
let mut ans: i64 = 0
let mut idx: i32 = 0
while idx < final_len {
let mut ti: i32 = 0
while ti < XOR_SIZE {
g_vec[ti] = g_qhat[ti * MAX_POLY + idx]
ti = ti + 1
}
fwt_xor(g_vec, XOR_SIZE, 1)
let total_diff: i32 = idx - final_offset
if total_diff > 0 {
let mut s: i64 = 0
let mut ti2: i32 = 0
while ti2 < XOR_SIZE {
s = s + g_vec[ti2]
if s >= MOD { s = s - MOD }
ti2 = ti2 + 1
}
ans = ans + s
if ans >= MOD { ans = ans - MOD }
} else {
if total_diff == 0 {
let mut s: i64 = 0
let mut ti3: i32 = 1
while ti3 < XOR_SIZE {
s = s + g_vec[ti3]
if s >= MOD { s = s - MOD }
ti3 = ti3 + 1
}
ans = ans + s
if ans >= MOD { ans = ans - MOD }
}
}
idx = idx + 1
}
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 powmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t m);
void fwt_xor_ptr_i64_i32_i32(int64_t* arr, int32_t n, int32_t inverse);
void poly_mul_i32_i32_i32(int32_t res_idx, int32_t a_idx, int32_t b_idx);
void poly_pow_i32_i32_i32(int32_t res_idx, int32_t base_idx, int32_t exp);
int32_t main(void);
static const int64_t MOD = 1000000007;
static const int32_t XOR_SIZE = 64;
static const int32_t W = 64;
static const int32_t M = 8;
static const int32_t DMAX = 62;
static const int32_t DIFF_COUNT = 125;
static const int32_t MAX_POLY = 1024;
/* Module statics */
static int64_t* g_counts = NULL;
static int64_t* g_qhat = NULL;
static int64_t* g_vec = NULL;
static int64_t* g_poly_coeffs = NULL;
static int32_t* g_poly_len = NULL;
static int64_t* g_poly_offset = NULL;
static int64_t* g_pmul_tmp = NULL;
int64_t powmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t m) {
int64_t r = 1;
int64_t a = FLOW_CHECKED_MOD((a0), (m));
if (a < 0) {
a = (a + m);
}
int64_t b = b0;
while (b > 0) {
if ((b & 1) == 1) {
__int128 aw = ((__int128)(r));
__int128 bw = ((__int128)(a));
__int128 mw = ((__int128)(m));
r = ((int64_t)(FLOW_CHECKED_MOD(((aw * bw)), (mw))));
}
__int128 aw2 = ((__int128)(a));
__int128 bw2 = ((__int128)(a));
__int128 mw2 = ((__int128)(m));
a = ((int64_t)(FLOW_CHECKED_MOD(((aw2 * bw2)), (mw2))));
b = FLOW_CHECKED_SHR((b), (1));
}
return r;
}
void fwt_xor_ptr_i64_i32_i32(int64_t* arr, int32_t n, int32_t inverse) {
int32_t step = 1;
while (step < n) {
int32_t jump = (step * 2);
int32_t i = 0;
while (i < n) {
int32_t j = i;
while (j < (i + step)) {
int64_t x = arr[j];
int64_t y = arr[(j + step)];
int64_t s = (x + y);
if (s >= MOD) {
s = (s - MOD);
}
int64_t d = (x - y);
if (d < 0) {
d = (d + MOD);
}
arr[j] = s;
arr[(j + step)] = d;
j = (j + 1);
}
i = (i + jump);
}
step = jump;
}
if (inverse != 0) {
int64_t inv_n = powmod_i64_i64_i64(((int64_t)(n)), (MOD - 2), MOD);
int32_t i = 0;
while (i < n) {
__int128 aw = ((__int128)(arr[i]));
__int128 bw = ((__int128)(inv_n));
__int128 mw = ((__int128)(MOD));
arr[i] = ((int64_t)(FLOW_CHECKED_MOD(((aw * bw)), (mw))));
i = (i + 1);
}
}
}
void poly_mul_i32_i32_i32(int32_t res_idx, int32_t a_idx, int32_t b_idx) {
int32_t big_idx = a_idx;
int32_t small_idx = b_idx;
if (g_poly_len[a_idx] < g_poly_len[b_idx]) {
big_idx = b_idx;
small_idx = a_idx;
}
int32_t new_len = ((g_poly_len[big_idx] + g_poly_len[small_idx]) - 1);
memset(((void*)(g_pmul_tmp)), 0, (((int64_t)(new_len)) * 8));
int32_t i = 0;
while (i < g_poly_len[big_idx]) {
if (g_poly_coeffs[((big_idx * MAX_POLY) + i)] != 0) {
int32_t j = 0;
while (j < g_poly_len[small_idx]) {
if (g_poly_coeffs[((small_idx * MAX_POLY) + j)] != 0) {
__int128 aw = ((__int128)(g_pmul_tmp[(i + j)]));
__int128 bw = ((__int128)(g_poly_coeffs[((big_idx * MAX_POLY) + i)]));
__int128 cw = ((__int128)(g_poly_coeffs[((small_idx * MAX_POLY) + j)]));
__int128 mw = ((__int128)(MOD));
g_pmul_tmp[(i + j)] = ((int64_t)(FLOW_CHECKED_MOD(((aw + (bw * cw))), (mw))));
}
j = (j + 1);
}
}
i = (i + 1);
}
g_poly_len[res_idx] = new_len;
g_poly_offset[res_idx] = (g_poly_offset[a_idx] + g_poly_offset[b_idx]);
memcpy((g_poly_coeffs + (res_idx * MAX_POLY)), g_pmul_tmp, (((int64_t)(new_len)) * 8));
}
void poly_pow_i32_i32_i32(int32_t res_idx, int32_t base_idx, int32_t exp) {
int32_t base = 1;
g_poly_len[base] = g_poly_len[base_idx];
g_poly_offset[base] = g_poly_offset[base_idx];
memcpy((g_poly_coeffs + (base * MAX_POLY)), (g_poly_coeffs + (base_idx * MAX_POLY)), (((int64_t)(g_poly_len[base_idx])) * 8));
int32_t res = 0;
g_poly_len[res] = 1;
g_poly_offset[res] = 0;
g_poly_coeffs[((res * MAX_POLY) + 0)] = 1;
int32_t tmp = 2;
int32_t e = exp;
while (e > 0) {
if ((e & 1) == 1) {
poly_mul_i32_i32_i32(tmp, res, base);
g_poly_len[res] = g_poly_len[tmp];
g_poly_offset[res] = g_poly_offset[tmp];
memcpy((g_poly_coeffs + (res * MAX_POLY)), (g_poly_coeffs + (tmp * MAX_POLY)), (((int64_t)(g_poly_len[tmp])) * 8));
}
e = FLOW_CHECKED_SHR((e), (1));
if (e != 0) {
poly_mul_i32_i32_i32(tmp, base, base);
g_poly_len[base] = g_poly_len[tmp];
g_poly_offset[base] = g_poly_offset[tmp];
memcpy((g_poly_coeffs + (base * MAX_POLY)), (g_poly_coeffs + (tmp * MAX_POLY)), (((int64_t)(g_poly_len[tmp])) * 8));
}
}
g_poly_len[res_idx] = g_poly_len[res];
g_poly_offset[res_idx] = g_poly_offset[res];
memcpy((g_poly_coeffs + (res_idx * MAX_POLY)), (g_poly_coeffs + (res * MAX_POLY)), (((int64_t)(g_poly_len[res])) * 8));
}
int32_t main(void) {
int32_t w = W;
int32_t m = M;
int32_t dmax = (w - 2);
int32_t diff_count = ((2 * dmax) + 1);
g_counts = calloc(((int64_t)((diff_count * XOR_SIZE))), 8);
g_qhat = calloc(((int64_t)((XOR_SIZE * MAX_POLY))), 8);
g_vec = calloc(((int64_t)(XOR_SIZE)), 8);
g_poly_coeffs = calloc(((int64_t)((3 * MAX_POLY))), 8);
g_poly_len = calloc(3, 4);
g_poly_offset = calloc(3, 8);
g_pmul_tmp = calloc(((int64_t)(MAX_POLY)), 8);
int32_t k = 1;
while (k < (w - 1)) {
int32_t limit = (w - k);
if (limit >= 2) {
int32_t g = (k - 1);
int32_t tmax = (limit - 2);
int32_t t = 0;
while (t <= tmax) {
int32_t c = FLOW_CHECKED_DIV(((limit - t)), (2));
if (c > 0) {
int32_t idx1 = (((dmax + t) * XOR_SIZE) + g);
g_counts[idx1] = FLOW_CHECKED_MOD(((g_counts[idx1] + ((int64_t)(c)))), (MOD));
if (t != 0) {
int32_t idx2 = (((dmax - t) * XOR_SIZE) + g);
g_counts[idx2] = FLOW_CHECKED_MOD(((g_counts[idx2] + ((int64_t)(c)))), (MOD));
}
}
t = (t + 1);
}
}
k = (k + 1);
}
int32_t d = 0;
while (d < diff_count) {
fwt_xor_ptr_i64_i32_i32((g_counts + (d * XOR_SIZE)), XOR_SIZE, 0);
d = (d + 1);
}
int32_t final_offset = (m * dmax);
int32_t final_len = ((2 * final_offset) + 1);
int32_t t = 0;
while (t < XOR_SIZE) {
g_poly_len[0] = diff_count;
g_poly_offset[0] = dmax;
int32_t di = 0;
while (di < diff_count) {
g_poly_coeffs[((0 * MAX_POLY) + di)] = g_counts[((di * XOR_SIZE) + t)];
di = (di + 1);
}
poly_pow_i32_i32_i32(1, 0, m);
int32_t i = 0;
while (i < g_poly_len[1]) {
if (i < final_len) {
g_qhat[((t * MAX_POLY) + i)] = g_poly_coeffs[((1 * MAX_POLY) + i)];
}
i = (i + 1);
}
t = (t + 1);
}
int64_t ans = 0;
int32_t idx = 0;
while (idx < final_len) {
int32_t ti = 0;
while (ti < XOR_SIZE) {
g_vec[ti] = g_qhat[((ti * MAX_POLY) + idx)];
ti = (ti + 1);
}
fwt_xor_ptr_i64_i32_i32(g_vec, XOR_SIZE, 1);
int32_t total_diff = (idx - final_offset);
if (total_diff > 0) {
int64_t s = 0;
int32_t ti2 = 0;
while (ti2 < XOR_SIZE) {
s = (s + g_vec[ti2]);
if (s >= MOD) {
s = (s - MOD);
}
ti2 = (ti2 + 1);
}
ans = (ans + s);
if (ans >= MOD) {
ans = (ans - MOD);
}
} else {
if (total_diff == 0) {
int64_t s = 0;
int32_t ti3 = 1;
while (ti3 < XOR_SIZE) {
s = (s + g_vec[ti3]);
if (s >= MOD) {
s = (s - MOD);
}
ti3 = (ti3 + 1);
}
ans = (ans + s);
if (ans >= MOD) {
ans = (ans - MOD);
}
}
}
idx = (idx + 1);
}
printf("%lld\n", ans);
return 0;
}