DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Colorful Domino Tiling

Back to problem

Editorial

Small inputs: enumerate placements and colorings

If NMNMNM is odd, the answer is zero. Otherwise, repeatedly take the first uncovered cell and pair it with the cell to its right or below. This enumerates every domino tiling once. For each tiling, build the graph whose vertices are dominoes and whose edges represent adjacency.

Color its vertices in order. If ccc colors have been used, try each existing color that does not conflict with an earlier neighbor, and also try one representative new color with multiplicity K−cK-cK−c. This canonicalizes color names by order of first appearance. Subtask 1 has at most eight dominoes. With DDD dominoes and LLL tilings, a conservative bound is O(LD2BD)O(LD^2B_D)O(LD2BD​) time and O(NM+D2)O(NM+D^2)O(NM+D2) space, where BDB_DBD​ is a Bell number.

Full solution: frontier DP

Rotate the grid if necessary so its width is W=min⁡(N,M)W=\min(N,M)W=min(N,M) and height is H=max⁡(N,M)H=\max(N,M)H=max(N,M). Process cells in row-major order. Retain the most recently processed cell in each column. Before processing a cell, the frontier entry in that column is its upper neighbor; the preceding entry, when present, is its left neighbor in the current row.

For each frontier entry, store its canonical color label, with zero for an absent cell, and a bit recording whether it has already been paired with a processed cell. An unpaired cell already has a color but must eventually be paired to its right or below. Forgotten cells have no future neighbors, so their colors need not be retained.

Canonicalize by scanning the frontier from left to right and assigning labels 1,2,3,…1,2,3,\ldots1,2,3,… in order of first appearance. For example, actual colors (7,2,7,5)(7,2,7,5)(7,2,7,5) become (1,2,1,3)(1,2,1,3)(1,. A DP value counts actual colored placements represented by that state.

Transitions

Let UUU and LLL be the upper and left neighbors of the new cell; ignore nonexistent neighbors.

  • If UUU is unpaired, it must be paired with the new cell now. Copy its color and mark the new cell paired. Its color must differ from LLL if LLL exists. Forget UUU.
  • If UUU is absent or already paired, an unpaired LLL may be paired with the new cell. Copy the color of , require it to differ from when present, and mark both and the new cell paired.

For the last choice, if the current frontier contains ccc distinct colors, try every allowed existing color. All colors absent from the frontier lead to the same new label, so combine them in one transition with multiplier K−cK-cK−c. These choices include colors used in the past but no longer present on the frontier. Count active colors, not all colors ever used.

Replace UUU by the new cell and canonicalize again. Initially the frontier is empty and its value is one. After processing all cells, sum only states in which every frontier cell is paired.

Correctness

Each domino is completed exactly when its second cell in row-major order is processed. An unpaired upper cell cannot find a later partner, so pairing it now is forced. Otherwise the remaining choices partition all possibilities into pairing with the left cell or waiting for a future cell. The only earlier neighbors of a new cell are its upper and left neighbors, so the transitions enforce every adjacency condition exactly. Renaming colors preserves feasibility, and the K−cK-cK−c multiplier restores the number of actual choices for a new active color. Thus every valid colored tiling is counted exactly once.

State bound and implementation

Color equivalence patterns are set partitions. Their number is bounded by the Bell number BWB_WBW​, with B8=4140B_8=4140B8​=4140. Including the paired bits gives at most BW2WB_W2^W states. Color equivalence need not be noncrossing: four vertical dominoes may have colors . Consequently, a Catalan-number bound or discarding crossing color patterns is incorrect for this DP.

Each state has O(W)O(W)O(W) color choices and each canonicalization costs O(W)O(W)O(W). With expected constant-time hash operations, the bound is O(HW⋅BW2W⋅W2)O(HW\cdot B_W2^W\cdot W^2)O(HW⋅B time and auxiliary space. Store only reachable states; this implementation keeps about states at a step on the largest input.

Reduce counts modulo P=998 244 353P=998\,244\,353P=998244353 after every operation. Products are at most 100(P−1)100(P-1)100(P−1) and additions at most 2(P−1)2(P-1)2(P−. Packed states use at most 40 bits, so signed 64-bit arithmetic suffices. The independent verification program instead expands different-color constraints by inclusion-exclusion and tracks connected components.

Solution written by GPT6

2
,
1
,
3
)
LL
L
UUU
LLL
  • If UUU is absent or already paired, the new cell may remain unpaired. Its color must differ from both neighbor colors. Omit this choice at the last cell, where no future partner exists.
  • BW​2W
    A,B,A,BA,B,A,BA,B,A,B
    W
    ​
    2W
    ⋅
    W2)
    O(BW2W)O(B_W2^W)O(BW​2W)
    10510^5105
    1
    )