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

DT-Tiling

Back to problem

Editorial

Frontier states

Suppose every cell strictly to the left of the current column has been filled. Since a horizontal straight tromino has length 333, tiles already placed can occupy cells only in the current and next columns. Encode these four cells as a bitmask: bits 0,10,10,1 represent the top and bottom cells of the current column, and bits 2,32,32,3 represent those of the next column.

Place a tile covering the first empty cell of the current column. Once that column is full, shift the mask right by two bits. Always choosing the first empty cell avoids counting the same tiling in different placement orders. Start at mask 000 and accept only mask 000 after processing NNN columns.

The reachable masks are 0,1,2,3,5,7,10,11,150,1,2,3,5,7,10,11,150,1,2,3,5,7,10,11,15. Vertical reflection gives equal counts for symmetric masks. After nnn columns, define six quantities:

  • fnf_nfn​: the count for mask 000.
  • unu_nun​: the count for mask 11, equal to that for mask .

Each quantity stores the count of one mask, not the sum of a symmetric pair. Enumerating placements covering the first empty cell gives the following transitions.

fn+1=fn+vn,un+1=fn+un+xn,vn+1=fn+2un+2wn+yn,wn+1=un,xn+1=fn+wn, (f0,u0,v0,w0,x0,y0)=(1,0,0,0,0,0).(f_0,u_0,v_0,w_0,x_0,y_0)=(1,0,0,0,0,0).(f0​,u0​,v

Linear DP and matrix exponentiation

Evaluating the transitions NNN times takes O(N)O(N)O(N) time and O(1)O(1)O(1) space, sufficient for subtask 2. For the full constraints, write the six quantities as a column vector with sn+1=Msns_{n+1}=Ms_ns, where

M=(101000110010120201010000100

Binary exponentiation computes MNM^NMN in O(63log⁡N)O(6^3\log N)O(63logN) time and O(62)O(6^2)O( space. Perform all arithmetic modulo .

Recurrence used in the supplied code

The transition matrix satisfies

M6=2M5+3M3+2M2+M−I.M^6=2M^5+3M^3+2M^2+M-I.M6=2M5+3M3+

Hence, for n≥6n\geq6n≥6,

fn=2fn−1+3fn−3+2fn−4+fn−5−fn−6,f_n=2f_{n-1}+3f_{n-3}+2f_{n-4}+f_{n-5}-f_{n-6},fn​=2fn−1​

with

(f0,f1,f2,f3,f4,f5)=(1,1,2,6,17,43).(f_0,f_1,f_2,f_3,f_4,f_5)=(1,1,2,6,17,43).(f0​,f1​,f

The supplied linear solution evaluates this recurrence directly. The main solution exponentiates its 6×66\times66×6 companion matrix on the state (fn,fn−1,⋯ ,fn−5)(f_n,f_{n-1},\cdots,f_{n-5})(fn​,f. Represent the coefficient by the modulus minus one, and store the exponent in a 64-bit integer.

Small-case search

For subtask 1, represent all 2N2N2N board cells by a bitmask and try every tile covering the first empty cell. Memoize each occupied-cell mask. Filling cells from left to right leaves only a constant number of frontier states per column, since protrusions span at most two future columns. Scanning for the first empty cell gives O(N2)O(N^2)O(N2) time and O(N)O(N)O(N) stored states. The independent naive implementation uses at most 40 bits for the whole board.

Both straight and L-shaped trominoes are required. A vertical straight tromino does not fit in a height-two board. The empty-board value f0=1f_0=1f0​=1 is an internal base case; input never has N=0N=0N=0.

Reduce after each multiply-add. Each intermediate is at most (109+6)2+(109+6)<1.01⋅1018(10^9+6)^2+(10^9+6)<1.01\cdot10^{18}(109+6)2+(109+, so signed 64-bit arithmetic suffices.

Solution written by GPT

1
222
  • vnv_nvn​: the count for mask 333.
  • wnw_nwn​: the count for mask 555, equal to that for mask 101010.
  • xnx_nxn​: the count for mask 777, equal to that for mask 111111.
  • yny_nyn​: the count for mask 151515.
  • yn+1=fn.\begin{aligned} f_{n+1}&=f_n+v_n,\\ u_{n+1}&=f_n+u_n+x_n,\\ v_{n+1}&=f_n+2u_n+2w_n+y_n,\\ w_{n+1}&=u_n,\\ x_{n+1}&=f_n+w_n,\\ y_{n+1}&=f_n. \end{aligned}
    fn+1​un+1​vn+1​wn+1​xn+1​yn+1​​=fn​+vn​,=fn​+u
    0​
    ,
    w0​
    ,
    x0​
    ,
    y0​
    )
    =
    (1,0,0,0,0,0).
    n+1
    ​
    =
    Msn​
    100100000).M=\begin{pmatrix} 1&0&1&0&0&0\\ 1&1&0&0&1&0\\ 1&2&0&2&0&1\\ 0&1&0&0&0&0\\ 1&0&0&1&0&0\\ 1&0&0&0&0&0 \end{pmatrix}.
    M=​111011​012100​100000​002010​010000​001000​​.
    62
    )
    1 000 000 0071\,000\,000\,0071000000007
    2
    M2
    +
    M−
    I.
    +
    3fn−3​+
    2fn−4​+
    fn−5​−
    fn−6​,
    2​
    ,
    f3​
    ,
    f4​
    ,
    f5​
    )
    =
    (1,1,2,6,17,43).
    n
    −
    1
    ​
    ,
    ⋯
    ,
    fn−5​
    )
    −1-1−1
    6)<
    1.01⋅
    1018
    n​
    +
    xn​
    ,
    =fn​+2un​+2wn​+yn​,
    =un​,
    =fn​+wn​,
    =fn​.
    ​