Toriangulations: count tilings of a x b tori by unit equilateral triangles, up to continuous motion of the triangles. G(N) = sum F(n), n <= N = 10^9. Every tiling is a strip tiling: rows of 2*lambda triangles between parallel full lines, s rows, n = 2*lambda*s. On a fixed ordered a x b torus each valid strip direction gives one connected family, and families merge exactly at edge-to-edge tilings, i.e. at copies of the triangular lattice T that contain the torus lattice. With m = n/2 this gives G(N) = 2*D(M) + 4*T(M) - (4/3)*R(M), M = N/2, where D(M) = sum_{m<=M} d(m) (axis-parallel directions), T(M) = #{(lambda,s,p,q): gcd(p,q)=1, p,q>=1, sqrt(3)*p*q*s < lambda, lambda*s <= M} (tilted directions; counted via 2^omega(k) over k = p*q, segmented sieve), R(M) = # rectangular sublattices of T with index <= M = (1/2) sum over primitive Eisenstein points u, 3 !| norm(u), of D(floor(M/2 / norm(u))) (axes t*u and r*u*sqrt(-3)), computed with Moebius over content, the ellipse point count Circ(z) = 6*sum chi_3(d)*floor(z/d), and floor-value blocks. Verified: G(6)=14, G(100)=8090, G(10^5) = 645124048 (mod 1e9+7).
# Project Euler 780
# Toriangulations: count tilings of a x b tori by unit equilateral triangles,
# up to continuous motion of the triangles. G(N) = sum F(n), n <= N = 10^9.
#
# Every tiling is a strip tiling: rows of 2*lambda triangles between parallel
# full lines, s rows, n = 2*lambda*s. On a fixed ordered a x b torus each
# valid strip direction gives one connected family, and families merge exactly
# at edge-to-edge tilings, i.e. at copies of the triangular lattice T that
# contain the torus lattice. With m = n/2 this gives
# G(N) = 2*D(M) + 4*T(M) - (4/3)*R(M), M = N/2, where
# D(M) = sum_{m<=M} d(m) (axis-parallel directions),
# T(M) = #{(lambda,s,p,q): gcd(p,q)=1, p,q>=1,
# sqrt(3)*p*q*s < lambda, lambda*s <= M} (tilted directions;
# counted via 2^omega(k) over k = p*q, segmented sieve),
# R(M) = # rectangular sublattices of T with index <= M
# = (1/2) sum over primitive Eisenstein points u, 3 !| norm(u),
# of D(floor(M/2 / norm(u))) (axes t*u and r*u*sqrt(-3)),
# computed with Moebius over content, the ellipse point count
# Circ(z) = 6*sum chi_3(d)*floor(z/d), and floor-value blocks.
# Verified: G(6)=14, G(100)=8090, G(10^5) = 645124048 (mod 1e9+7).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const N: i64 = 1000000000
const MOD: i64 = 1000000007
const INV3: i64 = 333333336
const PLIM: i64 = 17000
const BS: i64 = 1048576
function isqrt64(n: i64) -> i64 {
if n <= 0 {
return 0
}
let mut x: i64 = n
let mut y: i64 = (x + 1) / 2
while y < x {
x = y
y = (x + n / x) / 2
}
return x
}
# D(v) = sum_{t<=v} d(t) via the hyperbola method (exact, fits i64)
function dbig(v: i64) -> i64 {
if v <= 0 {
return 0
}
let q: i64 = isqrt64(v)
let mut s: i64 = 0
let mut k: i64 = 1
while k <= q {
s = s + v / k
k = k + 1
}
return 2 * s - q * q
}
# Nonzero Eisenstein points with norm <= z: Circ(z) = 6*sum_d chi(d)*floor(z/d),
# chi the character mod 3; partial sum of chi over 1..t is (t % 3 == 1).
function circ(z: i64) -> i64 {
if z <= 0 {
return 0
}
let mut tot: i64 = 0
let mut d: i64 = 1
while d <= z {
let v: i64 = z / d
let d2: i64 = z / v
let mut x2: i64 = 0
if d2 % 3 == 1 {
x2 = 1
}
let mut x1: i64 = 0
if (d - 1) % 3 == 1 {
x1 = 1
}
tot = tot + v * (x2 - x1)
d = d2 + 1
}
return 6 * tot
}
# Primitive nonzero points with norm <= y (Moebius over content)
function primall(y: i64, mu: ptr<i64>) -> i64 {
if y <= 0 {
return 0
}
let q: i64 = isqrt64(y)
let mut tot: i64 = 0
let mut g: i64 = 1
while g <= q {
if mu[g] != 0 {
tot = tot + mu[g] * circ(y / (g * g))
}
g = g + 1
}
return tot
}
# Primitive points with norm <= x and norm not divisible by 3:
# PF(x) = sum_i (-1)^i * primall(x / 3^i)
function pfree(x: i64, mu: ptr<i64>) -> i64 {
let mut tot: i64 = 0
let mut sign: i64 = 1
let mut y: i64 = x
while y >= 1 {
tot = tot + sign * primall(y, mu)
sign = 0 - sign
y = y / 3
}
return tot
}
function main() -> i32 {
let M: i64 = N / 2
# ---- part A: 2*D(M) ----
let dm: i64 = dbig(M) % MOD
# ---- part B: T(M), tilted (torus, direction) nodes / 4 ----
let m2m1: i64 = M * M - 1
let kmax: i64 = isqrt64(m2m1 / 3)
let smax: i64 = isqrt64(kmax)
let hh: ptr<i64> = calloc(smax + 2, 8)
let mut s: i64 = 1
while s <= smax {
hh[s] = hh[s - 1] + M / s
s = s + 1
}
# primes up to PLIM for the segmented 2^omega sieve
let comp: ptr<i64> = calloc(PLIM + 1, 8)
let prlist: ptr<i64> = calloc(2100, 8)
let mut npr: i64 = 0
let mut i: i64 = 2
while i <= PLIM {
if comp[i] == 0 {
prlist[npr] = i
npr = npr + 1
let mut j: i64 = i * i
while j <= PLIM {
comp[j] = 1
j = j + i
}
}
i = i + 1
}
let pw2: ptr<i64> = calloc(16, 8)
pw2[0] = 1
i = 1
while i < 16 {
pw2[i] = pw2[i - 1] * 2
i = i + 1
}
let cnt: ptr<i64> = calloc(BS, 8)
let rem: ptr<i64> = calloc(BS, 8)
let mut tmod: i64 = 0
let mut base: i64 = 0 # floor(sqrt(3)*k), maintained incrementally
let mut scur: i64 = smax # S_k, nonincreasing in k
let mut lo: i64 = 1
while lo <= kmax {
let mut hi: i64 = lo + BS
if hi > kmax + 1 {
hi = kmax + 1
}
let blen: i64 = hi - lo
i = 0
while i < blen {
cnt[i] = 0
rem[i] = lo + i
i = i + 1
}
let mut pi: i64 = 0
while pi < npr {
let pr: i64 = prlist[pi]
let mut st: i64 = ((lo + pr - 1) / pr) * pr
let mut j: i64 = st - lo
while j < blen {
cnt[j] = cnt[j] + 1
rem[j] = rem[j] / pr
j = j + pr
}
let mut pe: i64 = pr * pr
while pe < hi {
st = ((lo + pe - 1) / pe) * pe
j = st - lo
while j < blen {
rem[j] = rem[j] / pr
j = j + pe
}
pe = pe * pr
}
pi = pi + 1
}
i = 0
while i < blen {
let k: i64 = lo + i
# advance base = floor(sqrt(3*k*k))
base = base + 1
while (base + 1) * (base + 1) <= 3 * k * k {
base = base + 1
}
# shrink S_k: largest s with 3*k^2*s^4 <= M^2-1
let qk: i64 = m2m1 / (3 * k * k)
while scur > 0 && scur * scur * scur * scur > qk {
scur = scur - 1
}
if scur > 0 {
# sub = sum_{s<=S_k} floor(sqrt(3)*k*s), incremental
let mut sub: i64 = 0
let mut v: i64 = 0
let mut t: i64 = 0
let mut ss: i64 = 1
while ss <= scur {
t = t + k
v = v + base
let tgt: i64 = 3 * t * t
while (v + 1) * (v + 1) <= tgt {
v = v + 1
}
sub = sub + v
ss = ss + 1
}
let mut wexp: i64 = cnt[i]
if rem[i] > 1 {
wexp = wexp + 1
}
tmod = (tmod + pw2[wexp] * ((hh[scur] - sub) % MOD)) % MOD
}
i = i + 1
}
lo = hi
}
# ---- part C: R(M) = (1/2) sum_{k<=J, 3!|k} primpts(k) * D(J/k) ----
let jj: i64 = M / 2
let vj: i64 = isqrt64(jj)
# Moebius sieve up to vj
let mu: ptr<i64> = calloc(vj + 2, 8)
let isc: ptr<i64> = calloc(vj + 2, 8)
i = 0
while i <= vj {
mu[i] = 1
i = i + 1
}
i = 2
while i <= vj {
if isc[i] == 0 {
let mut j: i64 = i
while j <= vj {
if j > i {
isc[j] = 1
}
mu[j] = 0 - mu[j]
j = j + i
}
let pp: i64 = i * i
j = pp
while j <= vj {
mu[j] = 0
j = j + pp
}
}
i = i + 1
}
# small divisor-summatory prefix D(v) for v <= vj
let dcnt: ptr<i64> = calloc(vj + 2, 8)
i = 1
while i <= vj {
let mut j: i64 = i
while j <= vj {
dcnt[j] = dcnt[j] + 1
j = j + i
}
i = i + 1
}
let dpre: ptr<i64> = calloc(vj + 2, 8)
i = 1
while i <= vj {
dpre[i] = dpre[i - 1] + dcnt[i]
i = i + 1
}
let mut r2mod: i64 = 0 # 2*R(M) mod p
let mut k2: i64 = 0
let mut pfprev: i64 = 0
let mut k: i64 = 1
while k <= jj {
let v: i64 = jj / k
k2 = jj / v
let mut dv: i64 = 0
if v <= vj {
dv = dpre[v]
} else {
dv = dbig(v)
}
let pf2: i64 = pfree(k2, mu)
let diff: i64 = pf2 - pfprev
r2mod = (r2mod + (diff % MOD) * (dv % MOD)) % MOD
pfprev = pf2
k = k2 + 1
}
# ---- assemble: G = 2*D(M) + 4*T - (4/3)*R = 2*D + 4*T - (2/3)*(2R) ----
let mut g: i64 = (2 * dm + 4 * tmod) % MOD
let rterm: i64 = ((r2mod * 2) % MOD) * INV3 % MOD
g = (g + MOD - rterm) % MOD
printf("%lld\n", g)
free(hh)
free(comp)
free(prlist)
free(pw2)
free(cnt)
free(rem)
free(mu)
free(isc)
free(dcnt)
free(dpre)
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 isqrt64_i64(int64_t n);
int64_t dbig_i64(int64_t v);
int64_t circ_i64(int64_t z);
int64_t primall_i64_ptr_i64(int64_t y, int64_t* mu);
int64_t pfree_i64_ptr_i64(int64_t x, int64_t* mu);
int32_t main(void);
static const int64_t N = 1000000000;
static const int64_t MOD = 1000000007;
static const int64_t INV3 = 333333336;
static const int64_t PLIM = 17000;
static const int64_t BS = 1048576;
int64_t isqrt64_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t x = n;
int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
while (y < x) {
x = y;
y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
}
return x;
}
int64_t dbig_i64(int64_t v) {
if (v <= 0) {
return 0;
}
int64_t q = isqrt64_i64(v);
int64_t s = 0;
int64_t k = 1;
while (k <= q) {
s = (s + FLOW_CHECKED_DIV((v), (k)));
k = (k + 1);
}
return ((2 * s) - (q * q));
}
int64_t circ_i64(int64_t z) {
if (z <= 0) {
return 0;
}
int64_t tot = 0;
int64_t d = 1;
while (d <= z) {
int64_t v = FLOW_CHECKED_DIV((z), (d));
int64_t d2 = FLOW_CHECKED_DIV((z), (v));
int64_t x2 = 0;
if (FLOW_CHECKED_MOD((d2), (3)) == 1) {
x2 = 1;
}
int64_t x1 = 0;
if (FLOW_CHECKED_MOD(((d - 1)), (3)) == 1) {
x1 = 1;
}
tot = (tot + (v * (x2 - x1)));
d = (d2 + 1);
}
return (6 * tot);
}
int64_t primall_i64_ptr_i64(int64_t y, int64_t* mu) {
if (y <= 0) {
return 0;
}
int64_t q = isqrt64_i64(y);
int64_t tot = 0;
int64_t g = 1;
while (g <= q) {
if (mu[g] != 0) {
tot = (tot + (mu[g] * circ_i64(FLOW_CHECKED_DIV((y), ((g * g))))));
}
g = (g + 1);
}
return tot;
}
int64_t pfree_i64_ptr_i64(int64_t x, int64_t* mu) {
int64_t tot = 0;
int64_t sign = 1;
int64_t y = x;
while (y >= 1) {
tot = (tot + (sign * primall_i64_ptr_i64(y, mu)));
sign = (0 - sign);
y = FLOW_CHECKED_DIV((y), (3));
}
return tot;
}
int32_t main(void) {
int64_t M = FLOW_CHECKED_DIV((N), (2));
int64_t dm = FLOW_CHECKED_MOD((dbig_i64(M)), (MOD));
int64_t m2m1 = ((M * M) - 1);
int64_t kmax = isqrt64_i64(FLOW_CHECKED_DIV((m2m1), (3)));
int64_t smax = isqrt64_i64(kmax);
int64_t* hh = (int64_t*)(calloc((smax + 2), 8));
int64_t s = 1;
while (s <= smax) {
hh[s] = (hh[(s - 1)] + FLOW_CHECKED_DIV((M), (s)));
s = (s + 1);
}
int64_t* comp = (int64_t*)(calloc((PLIM + 1), 8));
int64_t* prlist = (int64_t*)(calloc(2100, 8));
int64_t npr = 0;
int64_t i = 2;
while (i <= PLIM) {
if (comp[i] == 0) {
prlist[npr] = i;
npr = (npr + 1);
int64_t j = (i * i);
while (j <= PLIM) {
comp[j] = 1;
j = (j + i);
}
}
i = (i + 1);
}
int64_t* pw2 = (int64_t*)(calloc(16, 8));
pw2[0] = 1;
i = 1;
while (i < 16) {
pw2[i] = (pw2[(i - 1)] * 2);
i = (i + 1);
}
int64_t* cnt = (int64_t*)(calloc(BS, 8));
int64_t* rem = (int64_t*)(calloc(BS, 8));
int64_t tmod = 0;
int64_t base = 0;
int64_t scur = smax;
int64_t lo = 1;
while (lo <= kmax) {
int64_t hi = (lo + BS);
if (hi > (kmax + 1)) {
hi = (kmax + 1);
}
int64_t blen = (hi - lo);
i = 0;
while (i < blen) {
cnt[i] = 0;
rem[i] = (lo + i);
i = (i + 1);
}
int64_t pi = 0;
while (pi < npr) {
int64_t pr = prlist[pi];
int64_t st = (FLOW_CHECKED_DIV((((lo + pr) - 1)), (pr)) * pr);
int64_t j = (st - lo);
while (j < blen) {
cnt[j] = (cnt[j] + 1);
rem[j] = FLOW_CHECKED_DIV((rem[j]), (pr));
j = (j + pr);
}
int64_t pe = (pr * pr);
while (pe < hi) {
st = (FLOW_CHECKED_DIV((((lo + pe) - 1)), (pe)) * pe);
j = (st - lo);
while (j < blen) {
rem[j] = FLOW_CHECKED_DIV((rem[j]), (pr));
j = (j + pe);
}
pe = (pe * pr);
}
pi = (pi + 1);
}
i = 0;
while (i < blen) {
int64_t k = (lo + i);
base = (base + 1);
while (((base + 1) * (base + 1)) <= ((3 * k) * k)) {
base = (base + 1);
}
int64_t qk = FLOW_CHECKED_DIV((m2m1), (((3 * k) * k)));
while ((scur > 0 && (((scur * scur) * scur) * scur) > qk)) {
scur = (scur - 1);
}
if (scur > 0) {
int64_t sub = 0;
int64_t v = 0;
int64_t t = 0;
int64_t ss = 1;
while (ss <= scur) {
t = (t + k);
v = (v + base);
int64_t tgt = ((3 * t) * t);
while (((v + 1) * (v + 1)) <= tgt) {
v = (v + 1);
}
sub = (sub + v);
ss = (ss + 1);
}
int64_t wexp = cnt[i];
if (rem[i] > 1) {
wexp = (wexp + 1);
}
tmod = FLOW_CHECKED_MOD(((tmod + (pw2[wexp] * FLOW_CHECKED_MOD(((hh[scur] - sub)), (MOD))))), (MOD));
}
i = (i + 1);
}
lo = hi;
}
int64_t jj = FLOW_CHECKED_DIV((M), (2));
int64_t vj = isqrt64_i64(jj);
int64_t* mu = (int64_t*)(calloc((vj + 2), 8));
int64_t* isc = (int64_t*)(calloc((vj + 2), 8));
i = 0;
while (i <= vj) {
mu[i] = 1;
i = (i + 1);
}
i = 2;
while (i <= vj) {
if (isc[i] == 0) {
int64_t j = i;
while (j <= vj) {
if (j > i) {
isc[j] = 1;
}
mu[j] = (0 - mu[j]);
j = (j + i);
}
int64_t pp = (i * i);
j = pp;
while (j <= vj) {
mu[j] = 0;
j = (j + pp);
}
}
i = (i + 1);
}
int64_t* dcnt = (int64_t*)(calloc((vj + 2), 8));
i = 1;
while (i <= vj) {
int64_t j = i;
while (j <= vj) {
dcnt[j] = (dcnt[j] + 1);
j = (j + i);
}
i = (i + 1);
}
int64_t* dpre = (int64_t*)(calloc((vj + 2), 8));
i = 1;
while (i <= vj) {
dpre[i] = (dpre[(i - 1)] + dcnt[i]);
i = (i + 1);
}
int64_t r2mod = 0;
int64_t k2 = 0;
int64_t pfprev = 0;
int64_t k = 1;
while (k <= jj) {
int64_t v = FLOW_CHECKED_DIV((jj), (k));
k2 = FLOW_CHECKED_DIV((jj), (v));
int64_t dv = 0;
if (v <= vj) {
dv = dpre[v];
} else {
dv = dbig_i64(v);
}
int64_t pf2 = pfree_i64_ptr_i64(k2, mu);
int64_t diff = (pf2 - pfprev);
r2mod = FLOW_CHECKED_MOD(((r2mod + (FLOW_CHECKED_MOD((diff), (MOD)) * FLOW_CHECKED_MOD((dv), (MOD))))), (MOD));
pfprev = pf2;
k = (k2 + 1);
}
int64_t g = FLOW_CHECKED_MOD((((2 * dm) + (4 * tmod))), (MOD));
int64_t rterm = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((r2mod * 2)), (MOD)) * INV3)), (MOD));
g = FLOW_CHECKED_MOD((((g + MOD) - rterm)), (MOD));
printf("%lld\n", g);
free(hh);
free(comp);
free(prlist);
free(pw2);
free(cnt);
free(rem);
free(mu);
free(isc);
free(dcnt);
free(dpre);
return 0;
}