Mahjong: w(10^8, 10^8, 30) mod 1e9+7. Split by suit: a hand is t triples + one pair, and the pair's suit is forced (only that suit holds a tile count = 2 mod 3), so w(n, s, t) = s * [x^t] B(x) * A(x)^(s-1), where a_t(n) / b_t(n) count one-suit multisets forming exactly t triples without / with the pair. Counting multisets once despite ambiguous decompositions (3 chows = 3 pungs, and pair-shift trades like 22 345 345 = 234 234 55) is done with a subset-construction DP: track the set of reachable (pending chows, pair used, triples) states per multiset, so decomposability is judged deterministically. data/p696_tables.txt holds a_t(n), b_t(n) mod 1e9+7 for n = 401..444 from that DP (t = 0..30; 62 rows). Both sequences are polynomial in n of degree <= t+1 there (checked by extrapolating to n = 600 and n = 1000 against the direct DP), so Lagrange interpolation gives n = 10^8. Verified: w(4,1,1)=20, w(9,1,4)=13259, w(9,3,4)=5237550, w(1000,1000,5) = 107662178 mod 1e9+7.
# Project Euler 696
# Mahjong: w(10^8, 10^8, 30) mod 1e9+7.
#
# Split by suit: a hand is t triples + one pair, and the pair's suit is
# forced (only that suit holds a tile count = 2 mod 3), so
# w(n, s, t) = s * [x^t] B(x) * A(x)^(s-1),
# where a_t(n) / b_t(n) count one-suit multisets forming exactly t triples
# without / with the pair. Counting multisets once despite ambiguous
# decompositions (3 chows = 3 pungs, and pair-shift trades like
# 22 345 345 = 234 234 55) is done with a subset-construction DP: track the
# set of reachable (pending chows, pair used, triples) states per multiset,
# so decomposability is judged deterministically.
#
# data/p696_tables.txt holds a_t(n), b_t(n) mod 1e9+7 for n = 401..444 from
# that DP (t = 0..30; 62 rows). Both sequences are polynomial in n of
# degree <= t+1 there (checked by extrapolating to n = 600 and n = 1000
# against the direct DP), so Lagrange interpolation gives n = 10^8.
# Verified: w(4,1,1)=20, w(9,1,4)=13259, w(9,3,4)=5237550,
# w(1000,1000,5) = 107662178 mod 1e9+7.
extern {
function fopen(path: string, mode: string) -> ptr<void>
function fgetc(f: ptr<void>) -> i32
function fclose(f: ptr<void>) -> i32
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 1000000007
const NPTS: i64 = 44 # samples at n = 401..444
const TMAX: i64 = 30
const BIGN: i64 = 100000000
const SUITS: i64 = 100000000
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
}
# Lagrange interpolation at X from points (401+i, ys[i]), i = 0..NPTS-1
function lagrange(ys: ptr<i64>, X: i64) -> i64 {
let mut res: i64 = 0
let mut i: i64 = 0
while i < NPTS {
let mut num: i64 = 1
let mut den: i64 = 1
let mut j: i64 = 0
while j < NPTS {
if j != i {
num = num * ((X - (401 + j)) % MOD) % MOD
let mut d: i64 = (i - j) % MOD
if d < 0 {
d = d + MOD
}
den = den * d % MOD
}
j = j + 1
}
res = (res + ys[i] * num % MOD * powmod(den, MOD - 2)) % MOD
i = i + 1
}
return res
}
# multiply degree-<=TMAX polynomials mod x^(TMAX+1)
function pmul(u: ptr<i64>, v: ptr<i64>, out: ptr<i64>) -> void {
let mut i: i64 = 0
while i <= TMAX {
out[i] = 0
i = i + 1
}
i = 0
while i <= TMAX {
if u[i] != 0 {
let mut j: i64 = 0
while i + j <= TMAX {
out[i + j] = (out[i + j] + u[i] * v[j]) % MOD
j = j + 1
}
}
i = i + 1
}
}
function main() -> i32 {
let f: ptr<void> = fopen("data/p696_tables.txt", "r")
if f == 0 {
printf("failed to read data/p696_tables.txt\n")
return 1
}
let tab: ptr<i64> = calloc(62 * NPTS, 8)
let mut idx: i64 = 0
let mut cur: i64 = 0
let mut have: i64 = 0
let mut c: i32 = fgetc(f)
while c >= 0 {
if c >= 48 && c <= 57 {
cur = cur * 10 + ((c - 48) as i64)
have = 1
} else {
if have == 1 {
tab[idx] = cur
idx = idx + 1
cur = 0
have = 0
}
}
c = fgetc(f)
}
if have == 1 {
tab[idx] = cur
idx = idx + 1
}
fclose(f)
# interpolate a_t(BIGN), b_t(BIGN)
let A: ptr<i64> = calloc(TMAX + 1, 8)
let B: ptr<i64> = calloc(TMAX + 1, 8)
let mut t: i64 = 0
while t <= TMAX {
A[t] = lagrange(tab + t * NPTS, BIGN)
B[t] = lagrange(tab + (31 + t) * NPTS, BIGN)
t = t + 1
}
# res = A(x)^(SUITS-1) mod x^31, then * B(x); answer = SUITS * res[TMAX]
let res: ptr<i64> = calloc(TMAX + 1, 8)
let base: ptr<i64> = calloc(TMAX + 1, 8)
let tmp: ptr<i64> = calloc(TMAX + 1, 8)
res[0] = 1
let mut i: i64 = 0
while i <= TMAX {
base[i] = A[i]
i = i + 1
}
let mut e: i64 = SUITS - 1
while e > 0 {
if (e & 1) == 1 {
pmul(res, base, tmp)
i = 0
while i <= TMAX {
res[i] = tmp[i]
i = i + 1
}
}
e = e >> 1
if e > 0 {
pmul(base, base, tmp)
i = 0
while i <= TMAX {
base[i] = tmp[i]
i = i + 1
}
}
}
pmul(res, B, tmp)
let ans: i64 = SUITS % MOD * tmp[TMAX] % MOD
printf("%lld\n", ans)
free(tab)
free(A)
free(B)
free(res)
free(base)
free(tmp)
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);
int64_t lagrange_ptr_i64_i64(int64_t* ys, int64_t X);
void pmul_ptr_i64_ptr_i64_ptr_i64(int64_t* u, int64_t* v, int64_t* out);
int32_t main(void);
static const int64_t MOD = 1000000007;
static const int64_t NPTS = 44;
static const int64_t TMAX = 30;
static const int64_t BIGN = 100000000;
static const int64_t SUITS = 100000000;
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;
}
int64_t lagrange_ptr_i64_i64(int64_t* ys, int64_t X) {
int64_t res = 0;
int64_t i = 0;
while (i < NPTS) {
int64_t num = 1;
int64_t den = 1;
int64_t j = 0;
while (j < NPTS) {
if (j != i) {
num = FLOW_CHECKED_MOD(((num * FLOW_CHECKED_MOD(((X - (401 + j))), (MOD)))), (MOD));
int64_t d = FLOW_CHECKED_MOD(((i - j)), (MOD));
if (d < 0) {
d = (d + MOD);
}
den = FLOW_CHECKED_MOD(((den * d)), (MOD));
}
j = (j + 1);
}
res = FLOW_CHECKED_MOD(((res + (FLOW_CHECKED_MOD(((ys[i] * num)), (MOD)) * powmod_i64_i64(den, (MOD - 2))))), (MOD));
i = (i + 1);
}
return res;
}
void pmul_ptr_i64_ptr_i64_ptr_i64(int64_t* u, int64_t* v, int64_t* out) {
int64_t i = 0;
while (i <= TMAX) {
out[i] = 0;
i = (i + 1);
}
i = 0;
while (i <= TMAX) {
if (u[i] != 0) {
int64_t j = 0;
while ((i + j) <= TMAX) {
out[(i + j)] = FLOW_CHECKED_MOD(((out[(i + j)] + (u[i] * v[j]))), (MOD));
j = (j + 1);
}
}
i = (i + 1);
}
}
int32_t main(void) {
void* f = (void*)(fopen("data/p696_tables.txt", "r"));
if (f == 0) {
printf("failed to read data/p696_tables.txt\n");
return 1;
}
int64_t* tab = (int64_t*)(calloc((62 * NPTS), 8));
int64_t idx = 0;
int64_t cur = 0;
int64_t have = 0;
int32_t c = fgetc(f);
while (c >= 0) {
if ((c >= 48 && c <= 57)) {
cur = ((cur * 10) + ((int64_t)((c - 48))));
have = 1;
} else {
if (have == 1) {
tab[idx] = cur;
idx = (idx + 1);
cur = 0;
have = 0;
}
}
c = fgetc(f);
}
if (have == 1) {
tab[idx] = cur;
idx = (idx + 1);
}
fclose(f);
int64_t* A = (int64_t*)(calloc((TMAX + 1), 8));
int64_t* B = (int64_t*)(calloc((TMAX + 1), 8));
int64_t t = 0;
while (t <= TMAX) {
A[t] = lagrange_ptr_i64_i64((tab + (t * NPTS)), BIGN);
B[t] = lagrange_ptr_i64_i64((tab + ((31 + t) * NPTS)), BIGN);
t = (t + 1);
}
int64_t* res = (int64_t*)(calloc((TMAX + 1), 8));
int64_t* base = (int64_t*)(calloc((TMAX + 1), 8));
int64_t* tmp = (int64_t*)(calloc((TMAX + 1), 8));
res[0] = 1;
int64_t i = 0;
while (i <= TMAX) {
base[i] = A[i];
i = (i + 1);
}
int64_t e = (SUITS - 1);
while (e > 0) {
if ((e & 1) == 1) {
pmul_ptr_i64_ptr_i64_ptr_i64(res, base, tmp);
i = 0;
while (i <= TMAX) {
res[i] = tmp[i];
i = (i + 1);
}
}
e = FLOW_CHECKED_SHR((e), (1));
if (e > 0) {
pmul_ptr_i64_ptr_i64_ptr_i64(base, base, tmp);
i = 0;
while (i <= TMAX) {
base[i] = tmp[i];
i = (i + 1);
}
}
}
pmul_ptr_i64_ptr_i64_ptr_i64(res, B, tmp);
int64_t ans = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((SUITS), (MOD)) * tmp[TMAX])), (MOD));
printf("%lld\n", ans);
free(tab);
free(A);
free(B);
free(res);
free(base);
free(tmp);
return 0;
}