3-Like Numbers — F(10^5) mod 10^9+7. Automaton with state (a,b,c,cur) tracking counts mod 3 of digit-sum classes 0,1,2 and the current running sum mod 3. Matrix exponentiation.
# Project Euler 706
# 3-Like Numbers — F(10^5) mod 10^9+7.
#
# Automaton with state (a,b,c,cur) tracking counts mod 3 of digit-sum
# classes 0,1,2 and the current running sum mod 3. Matrix 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 N: i64 = 81
const MOD: i64 = 1000000007
function idx(a: i32, b: i32, c: i32, cur: i32) -> i32 {
return ((a * 3 + b) * 3 + c) * 3 + cur
}
function mat_mul(c: ptr<i64>, a: ptr<i64>, b: ptr<i64>) -> void {
let t: ptr<i64> = calloc(N * N, 8)
memset(t, 0, N * N * 8)
let mut i: i32 = 0
while i < 81 {
let mut k: i32 = 0
while k < 81 {
if a[(i as i64) * N + (k as i64)] != 0 {
let mut j: i32 = 0
while j < 81 {
if b[(k as i64) * N + (j as i64)] != 0 {
let val: i64 = t[(i as i64) * N + (j as i64)] + a[(i as i64) * N + (k as i64)] * b[(k as i64) * N + (j as i64)]
t[(i as i64) * N + (j as i64)] = val % MOD
}
j = j + 1
}
}
k = k + 1
}
i = i + 1
}
memcpy(c, t, N * N * 8)
free(t)
}
function mat_pow(r: ptr<i64>, a0: ptr<i64>, e: i64) -> void {
let a: ptr<i64> = calloc(N * N, 8)
memcpy(a, a0, N * N * 8)
memset(r, 0, N * N * 8)
let mut i: i32 = 0
while i < 81 {
r[(i as i64) * N + (i as i64)] = 1
i = i + 1
}
let mut e2: i64 = e
while e2 > 0 {
if (e2 & 1) == 1 {
mat_mul(r, a, r)
}
e2 = e2 >> 1
if e2 > 0 {
mat_mul(a, a, a)
}
}
free(a)
}
function mat_vec(out: ptr<i64>, m: ptr<i64>, v: ptr<i64>) -> void {
let mut i: i32 = 0
while i < 81 {
let mut s: i64 = 0
let mut j: i32 = 0
while j < 81 {
if m[(i as i64) * N + (j as i64)] != 0 {
s = (s + m[(i as i64) * N + (j as i64)] * v[j]) % MOD
}
j = j + 1
}
out[i] = s
i = i + 1
}
}
function build(m: ptr<i64>, w0: i64, w1: i64, w2: i64) -> void {
memset(m, 0, N * N * 8)
let w: array<i64, 3> = [w0, w1, w2]
let mut a: i32 = 0
while a < 3 {
let mut b: i32 = 0
while b < 3 {
let mut c: i32 = 0
while c < 3 {
let mut cur: i32 = 0
while cur < 3 {
let frm: i32 = idx(a, b, c, cur)
let mut r: i32 = 0
while r < 3 {
let nxt: i32 = (cur + r) % 3
let mut a2: i32 = a
let mut b2: i32 = b
let mut c2: i32 = c
if nxt == 0 {
a2 = (a2 + 1) % 3
}
else {
if nxt == 1 {
b2 = (b2 + 1) % 3
}
else {
c2 = (c2 + 1) % 3
}
}
let to_idx: i32 = idx(a2, b2, c2, nxt)
m[(to_idx as i64) * N + (frm as i64)] = (m[(to_idx as i64) * N + (frm as i64)] + w[r]) % MOD
r = r + 1
}
cur = cur + 1
}
c = c + 1
}
b = b + 1
}
a = a + 1
}
}
function main() -> i32 {
let d: i64 = 100000
let lead: ptr<i64> = calloc(N * N, 8)
let step: ptr<i64> = calloc(N * N, 8)
build(lead, 3, 3, 3)
build(step, 4, 3, 3)
let good: ptr<i8> = calloc(N, 1)
let mut a: i32 = 0
while a < 3 {
let mut b: i32 = 0
while b < 3 {
let mut c: i32 = 0
while c < 3 {
let mut k: i32 = 0
if a == 2 { k = k + 1 }
if b == 2 { k = k + 1 }
if c == 2 { k = k + 1 }
if k % 3 == 0 {
let mut cur: i32 = 0
while cur < 3 {
good[idx(a, b, c, cur)] = 1
cur = cur + 1
}
}
c = c + 1
}
b = b + 1
}
a = a + 1
}
let v: ptr<i64> = calloc(N, 8)
let v2: ptr<i64> = calloc(N, 8)
v[idx(1, 0, 0, 0)] = 1
mat_vec(v2, lead, v)
memcpy(v, v2, N * 8)
if d > 1 {
let p: ptr<i64> = calloc(N * N, 8)
mat_pow(p, step, d - 1)
mat_vec(v2, p, v)
memcpy(v, v2, N * 8)
free(p)
}
let mut ans: i64 = 0
let mut i: i32 = 0
while i < 81 {
if good[i] != 0 {
ans = ans + v[i]
}
i = i + 1
}
printf("%lld\n", ans % MOD)
free(lead)
free(step)
free(good)
free(v)
free(v2)
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; }
int32_t idx_i32_i32_i32_i32(int32_t a, int32_t b, int32_t c, int32_t cur);
void mat_mul_ptr_i64_ptr_i64_ptr_i64(int64_t* c, int64_t* a, int64_t* b);
void mat_pow_ptr_i64_ptr_i64_i64(int64_t* r, int64_t* a0, int64_t e);
void mat_vec_ptr_i64_ptr_i64_ptr_i64(int64_t* out, int64_t* m, int64_t* v);
void build_ptr_i64_i64_i64_i64(int64_t* m, int64_t w0, int64_t w1, int64_t w2);
int32_t main(void);
static const int64_t N = 81;
static const int64_t MOD = 1000000007;
int32_t idx_i32_i32_i32_i32(int32_t a, int32_t b, int32_t c, int32_t cur) {
return ((((((a * 3) + b) * 3) + c) * 3) + cur);
}
void mat_mul_ptr_i64_ptr_i64_ptr_i64(int64_t* c, int64_t* a, int64_t* b) {
int64_t* t = (int64_t*)(calloc((N * N), 8));
memset(t, 0, ((N * N) * 8));
int32_t i = 0;
while (i < 81) {
int32_t k = 0;
while (k < 81) {
if (a[((((int64_t)(i)) * N) + ((int64_t)(k)))] != 0) {
int32_t j = 0;
while (j < 81) {
if (b[((((int64_t)(k)) * N) + ((int64_t)(j)))] != 0) {
int64_t val = (t[((((int64_t)(i)) * N) + ((int64_t)(j)))] + (a[((((int64_t)(i)) * N) + ((int64_t)(k)))] * b[((((int64_t)(k)) * N) + ((int64_t)(j)))]));
t[((((int64_t)(i)) * N) + ((int64_t)(j)))] = FLOW_CHECKED_MOD((val), (MOD));
}
j = (j + 1);
}
}
k = (k + 1);
}
i = (i + 1);
}
memcpy(c, t, ((N * N) * 8));
free(t);
}
void mat_pow_ptr_i64_ptr_i64_i64(int64_t* r, int64_t* a0, int64_t e) {
int64_t* a = (int64_t*)(calloc((N * N), 8));
memcpy(a, a0, ((N * N) * 8));
memset(r, 0, ((N * N) * 8));
int32_t i = 0;
while (i < 81) {
r[((((int64_t)(i)) * N) + ((int64_t)(i)))] = 1;
i = (i + 1);
}
int64_t e2 = e;
while (e2 > 0) {
if ((e2 & 1) == 1) {
mat_mul_ptr_i64_ptr_i64_ptr_i64(r, a, r);
}
e2 = FLOW_CHECKED_SHR((e2), (1));
if (e2 > 0) {
mat_mul_ptr_i64_ptr_i64_ptr_i64(a, a, a);
}
}
free(a);
}
void mat_vec_ptr_i64_ptr_i64_ptr_i64(int64_t* out, int64_t* m, int64_t* v) {
int32_t i = 0;
while (i < 81) {
int64_t s = 0;
int32_t j = 0;
while (j < 81) {
if (m[((((int64_t)(i)) * N) + ((int64_t)(j)))] != 0) {
s = FLOW_CHECKED_MOD(((s + (m[((((int64_t)(i)) * N) + ((int64_t)(j)))] * v[j]))), (MOD));
}
j = (j + 1);
}
out[i] = s;
i = (i + 1);
}
}
void build_ptr_i64_i64_i64_i64(int64_t* m, int64_t w0, int64_t w1, int64_t w2) {
memset(m, 0, ((N * N) * 8));
int64_t w[3] = { w0, w1, w2 };
int32_t a = 0;
while (a < 3) {
int32_t b = 0;
while (b < 3) {
int32_t c = 0;
while (c < 3) {
int32_t cur = 0;
while (cur < 3) {
int32_t frm = idx_i32_i32_i32_i32(a, b, c, cur);
int32_t r = 0;
while (r < 3) {
int32_t nxt = FLOW_CHECKED_MOD(((cur + r)), (3));
int32_t a2 = a;
int32_t b2 = b;
int32_t c2 = c;
if (nxt == 0) {
a2 = FLOW_CHECKED_MOD(((a2 + 1)), (3));
} else {
if (nxt == 1) {
b2 = FLOW_CHECKED_MOD(((b2 + 1)), (3));
} else {
c2 = FLOW_CHECKED_MOD(((c2 + 1)), (3));
}
}
int32_t to_idx = idx_i32_i32_i32_i32(a2, b2, c2, nxt);
m[((((int64_t)(to_idx)) * N) + ((int64_t)(frm)))] = FLOW_CHECKED_MOD(((m[((((int64_t)(to_idx)) * N) + ((int64_t)(frm)))] + (((unsigned)(r) < 3) ? w[r] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(r), 3), flow_fault_handler("array index out of bounds"), w[0])))), (MOD));
r = (r + 1);
}
cur = (cur + 1);
}
c = (c + 1);
}
b = (b + 1);
}
a = (a + 1);
}
}
int32_t main(void) {
int64_t d = 100000;
int64_t* lead = (int64_t*)(calloc((N * N), 8));
int64_t* step = (int64_t*)(calloc((N * N), 8));
build_ptr_i64_i64_i64_i64(lead, 3, 3, 3);
build_ptr_i64_i64_i64_i64(step, 4, 3, 3);
int8_t* good = (int8_t*)(calloc(N, 1));
int32_t a = 0;
while (a < 3) {
int32_t b = 0;
while (b < 3) {
int32_t c = 0;
while (c < 3) {
int32_t k = 0;
if (a == 2) {
k = (k + 1);
}
if (b == 2) {
k = (k + 1);
}
if (c == 2) {
k = (k + 1);
}
if (FLOW_CHECKED_MOD((k), (3)) == 0) {
int32_t cur = 0;
while (cur < 3) {
good[idx_i32_i32_i32_i32(a, b, c, cur)] = 1;
cur = (cur + 1);
}
}
c = (c + 1);
}
b = (b + 1);
}
a = (a + 1);
}
int64_t* v = (int64_t*)(calloc(N, 8));
int64_t* v2 = (int64_t*)(calloc(N, 8));
v[idx_i32_i32_i32_i32(1, 0, 0, 0)] = 1;
mat_vec_ptr_i64_ptr_i64_ptr_i64(v2, lead, v);
memcpy(v, v2, (N * 8));
if (d > 1) {
int64_t* p = (int64_t*)(calloc((N * N), 8));
mat_pow_ptr_i64_ptr_i64_i64(p, step, (d - 1));
mat_vec_ptr_i64_ptr_i64_ptr_i64(v2, p, v);
memcpy(v, v2, (N * 8));
free(p);
}
int64_t ans = 0;
int32_t i = 0;
while (i < 81) {
if (good[i] != 0) {
ans = (ans + v[i]);
}
i = (i + 1);
}
printf("%lld\n", FLOW_CHECKED_MOD((ans), (MOD)));
free(lead);
free(step);
free(good);
free(v);
free(v2);
return 0;
}