#P11216. 【MX-J8-T4】2048

    ID: 12251 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DPO2优化区间 DP动态规划优化前缀和梦熊比赛

【MX-J8-T4】2048

Background

Original link: https://oier.team/problems/J8D.


2048 is a very fun mini game that is popular worldwide.

Problem Description

Now, Little Y has made a small modification to 2048, obtaining the following one-dimensional variant (some rules may differ from what you remember about 2048; please follow the rules below):

  • The game is played on a grid consisting of nn cells in a single row. Each cell is either empty or contains a tile with a positive integer value.
  • At the beginning of the game, a tile with value 22 is generated in an arbitrary cell, and all other cells are empty.
  • The player operates by sliding left (or right; similarly below). In each operation:
    1. All tiles are stacked completely to the left (or right), placed tightly with no empty cells in between.
    2. If after stacking there exist two adjacent tiles with equal values, suppose both values are kk, then remove these two tiles and generate a new tile with value 2k2k at the position of one of the original tiles (this is called a merge) (it can be proven that during this game there will never be 3\bm 3 consecutive adjacent tiles with the same value, so you do not need to consider the merge order). Then all tiles continue to stack to the left (or right) until no further merges are possible.
    3. Finally, generate a new tile with value 22 at the far right (or far left), i.e., the direction opposite to the sliding direction.

The figure below shows an example of a left slide operation.

  • Define the appearance time of a tile as follows:
    • Suppose when it is generated, the game is in round ii (i.e., the number of slide operations the player has performed), including the current operation.
    • If this tile is generated by merging, its appearance time is defined as 2i2 i.
    • Otherwise, if this tile is newly generated, its appearance time is defined as 2i+12 i + 1.
    • If this tile is the initial tile with value 22 generated at the very beginning of the game, its appearance time is defined as 11.
    • It can be proven that with the appearance time defined above, at any moment during the game, any two different tiles have different appearance times.
  • The goal of the game is to generate 2x2^x. Therefore, at any time during the game, once 2x2^x appears, the game ends immediately and the player wins.
  • If after step 2 of a slide operation, all nn cells contain tiles (in fact, the slide cannot move anything, but it is still considered an operation), then in step 3 a new tile cannot be generated normally; step 3 will not be performed, and the game fails.

Little Y is studying the number of all failure states in this new 2048 game. Specifically, when the game fails, two failure states A and B are considered essentially the same if and only if the following conditions are both satisfied:

  • For every 1in1 \leq i \leq n, the values of tile ii in A and tile ii in B are the same.
  • For every pair 1i<jn1 \le i < j \le n, the order relation between the appearance times of tile ii and tile jj in A is the same as that in B.

Little Y wants to know how many essentially different failure states there are in total. The answer should be taken modulo the given modulus pp (pp is not necessarily prime).

Input Format

This problem contains multiple test cases.

The first line contains two positive integers T,pT, p, representing the number of test cases and the modulus. For each test case:

  • One line containing two integers n,xn, x.

Output Format

For each test case:

  • Output one positive integer per line, the number of essentially different failure states, taken modulo pp.
5 71
3 4
4 3
4 4
4 5
5 6

8
0
12
34
20

Hint

[Sample Explanation #1]

For the first test case, n=3n = 3, x=4x = 4:

  • Considering only the grid states, there are 66 possible failures: $[8, 4, 2], [2, 4, 8], [2, 8, 4], [4, 8, 2], [2, 8, 2], [2, 4, 2]$.
    • But consider [2,8,2][2, 8, 2]. It can correspond to two essentially different failure states:
      • The middle 88 is generated first, then the left 22 is generated, then the right 22 is generated.
      • The middle 88 is generated first, then the right 22 is generated, then the left 22 is generated.
    • The same applies to [2,4,2][2, 4, 2].
  • For the other possibilities, it can be proven that each corresponds to only one essentially different failure state.
  • Therefore, the answer is 1+1+1+1+2+2=81 + 1 + 1 + 1 + 2 + 2 = 8, which is 88 modulo 7171.

For the second test case, n=4n = 4, x=3x = 3:

  • It can be proven that no matter what, the game will always be won, so there are no failure states and the answer is 00.

For the third test case, n=4n = 4, x=4x = 4:

  • Considering only the grid states, there are 44 possible failures: $[2, 8, 4, 2], [2, 4, 8, 2], [4, 8, 4, 2], [2, 4, 8, 4]$.
  • Among them, [2,8,4,2][2, 8, 4, 2] and [2,4,8,2][2, 4, 8, 2] each correspond to 44 essentially different failures, while [4,8,4,2][4, 8, 4, 2] and [2,4,8,4][2, 4, 8, 4] each correspond to 22 essentially different failures.
    • Take [2,8,4,2][2, 8, 4, 2] as an example. The 44 essentially different failures corresponding to this position are listed below (the operation sequence is not unique; the small numbers above the tiles denote appearance times):$$\begin{aligned} & [\overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}1\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}] & & [\overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}1\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}] & & [\overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}1\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}] & & [\overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}1\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}] \\ \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}1\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}3\hspace{3.84625mu}}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}1\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}3\hspace{3.84625mu}}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}1\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}3\hspace{3.84625mu}}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}1\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}3\hspace{3.84625mu}}{2}] \\ \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}4\hspace{3.84625mu}}{4}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}4\hspace{3.84625mu}}{4}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}4\hspace{3.84625mu}}{4}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}] & \stackrel{\text{R}}\to& [\overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}4\hspace{3.84625mu}}{4}] \\ \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}4\hspace{3.84625mu}}{4}, \overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}7\hspace{3.84625mu}}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}4\hspace{3.84625mu}}{4}, \overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}7\hspace{3.84625mu}}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}4\hspace{3.84625mu}}{4}, \overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}7\hspace{3.84625mu}}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}, \overset{\hspace{3.84625mu}4\hspace{3.84625mu}}{4}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}7\hspace{3.84625mu}}{2}] \\ \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}9\hspace{3.84625mu}}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}9\hspace{3.84625mu}}{2}] & \stackrel{\text{R}}\to& [\overset{\hspace{3.84625mu}9\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}, \overset{\hspace{3.84625mu}4\hspace{3.84625mu}}{4}, \overset{\hspace{3.84625mu}7\hspace{3.84625mu}}{2}, \overset{\hspace{3.84625mu}9\hspace{3.84625mu}}{2}] \\ \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}, \overset{\hspace{3.84625mu}9\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{11}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}, \overset{\hspace{3.84625mu}9\hspace{3.84625mu}}{2}, \overset{\hspace{15.385mu}}{}, \overset{11}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}9\hspace{3.84625mu}}{2}, \overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}, \overset{\hspace{15.385mu}}{}, \overset{11}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}, \overset{10}{8}, \overset{\hspace{15.385mu}}{}, \overset{11}{2}] \\ \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}, \overset{12}{4}, \overset{\hspace{15.385mu}}{}, \overset{13}{2}] & \stackrel{\text{R}}\to& [\overset{13}{2}, \overset{\hspace{15.385mu}}{}, \overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}, \overset{12}{4}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}9\hspace{3.84625mu}}{2}, \overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}, \overset{11}{2}, \overset{13}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}, \overset{10}{8}, \overset{11}{2}, \overset{13}{2}] \\ \stackrel{\text{R}}\to& [\overset{15}{2}, \overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}, \overset{12}{4}, \overset{13}{2}] & \stackrel{\text{L}}\to& [\overset{13}{2}, \overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}, \overset{12}{4}, \overset{15}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}9\hspace{3.84625mu}}{2}, \overset{\hspace{3.84625mu}8\hspace{3.84625mu}}{8}, \overset{14}{4}, \overset{15}{2}] & \stackrel{\text{L}}\to& [\overset{\hspace{3.84625mu}5\hspace{3.84625mu}}{2}, \overset{10}{8}, \overset{14}{4}, \overset{15}{2}] \end{aligned}$$For these 44 cases, the order relations of appearance times (after discretization) are [4,1,2,3][4, 1, 2, 3], [3,1,2,4][3, 1, 2, 4], [2,1,3,4][2, 1, 3, 4], and [1,2,3,4][1, 2, 3, 4], respectively.
  • Therefore, the answer is 4+4+2+2=124 + 4 + 2 + 2 = 12, which is 1212 modulo 7171.

For the fourth test case, n=4n = 4, x=5x = 5:

  • It can be proven that the answer is 3434, which is 3434 modulo 7171.

For the fifth test case, n=5n = 5, x=6x = 6:

  • It can be proven that the answer is 162162, which is 2020 modulo 7171.

[Sample #2]

See game/game2.in and game/game2.ans in the attachment.

This sample set satisfies the constraints of test points 353 \sim 5.

[Sample #3]

See game/game3.in and game/game3.ans in the attachment.

This sample set satisfies the constraints of test points 6106 \sim 10.

[Sample #4]

See game/game4.in and game/game4.ans in the attachment.

This sample set satisfies the constraints of test points 141714 \sim 17.

[Sample #5]

See game/game5.in and game/game5.ans in the attachment.

This sample set satisfies the constraints of test points 222522 \sim 25.

[Constraints]

This problem has 2525 test points, 44 points each.

Test point ID TT \le n,xn, x \le Special property
121\sim2 1010 44 None
353\sim5 1010
6106\sim10 2222
111311\sim13 11 8080
141714\sim17 10001000
182018\sim20 11 300300
2121 10510^5 p=2p = 2
222522\sim25 None

For all data, it is guaranteed that 1T1051 \le T \le 10^5, 1n,x3001 \le n, x \le 300, and 2p1092 \le p \le 10^9.

Translated by ChatGPT 5