Project Euler Lab - Problem 696

#696 - Mahjong

● ResearchOfficial difficulty: 97%Two-player gamesTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

The game of Mahjong is played with tiles belonging to \(s\) suits. Each tile also has a number in the range \(1\ldots n\), and for each suit/number combination there are exactly four indistinguishable tiles with that suit and number. (The real Mahjong game also contains other bonus tiles, but those will not feature in this problem.)

A winning hand is a collection of \(3t+2\) Tiles (where \(t\) is a fixed integer) that can be arranged as \(t\) Triples and one Pair, where:

  • A Triple is either a Chow or a Pung
  • A Chow is three tiles of the same suit and consecutive numbers
  • A Pung is three identical tiles (same suit and same number)
  • A Pair is two identical tiles (same suit and same number)

For example, here is a winning hand with \(n=9\), \(s=3\), \(t=4\), consisting in this case of two Chows, two Pungs, and one Pair:

A winning Mahjong hand

Note that sometimes the same collection of tiles can be represented as \(t\) Triples and one Pair in more than one way. This only counts as one winning hand. For example, this is considered to be the same winning hand as above, because it consists of the same tiles:

Alternative arrangement of the same hand

Let \(w(n, s, t)\) be the number of distinct winning hands formed of \(t\) Triples and one Pair, where there are \(s\) suits available and tiles are numbered up to \(n\).

For example, with a single suit and tiles numbered up to \(4\), we have \(w(4, 1, 1) = 20\): there are \(12\) winning hands consisting of a Pung and a Pair, and another \(8\) containing a Chow and a Pair. You are also given that \(w(9, 1, 4) = 13259\), \(w(9, 3, 4) = 5237550\), and \(w(1000, 1000, 5) \equiv 107662178 \pmod{1\,000\,000\,007}\).

Find \(w(10^8, 10^8, 30)\). Give your answer modulo \(1\,000\,000\,007\).

This problem is taken from Project Euler, Problem 696.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=696. Published Sunday, 5th January 2020, 01:00 am. Solved by 213 members at time of mirroring.

Why this is useful

Mathematical Foundation. Exact counting underlies discrete pricing lattices, scenario enumeration, and combinatorial probability (Phase 7).

We classify relevance honestly - not every Euler problem is a trading application.

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier C - reduced scale in browser; full scale in notebook
Browser runs a reduced, clearly-labelled educational scale; the original scale is provided in a local notebook.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.