#P16449. [XJTUPC 2026] 怪商一克拉七鲜鱼丸

    ID: 18480 远端评测题 8000ms 256MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>动态规划 DP排列组合2026高校校赛

[XJTUPC 2026] 怪商一克拉七鲜鱼丸

Problem Description

Once upon a time, there were two good friends named Xiao Yi and Xiao Huan. They were both famous sorting masters! For a permutation p1,p2,,pnp_1, p_2,\cdots, p_n of length nn, Xiao Yi can, in one operation, choose an interval and cyclically shift it to the right by one position, while Xiao Huan can, in one operation, swap the positions of two different elements.

Here, a permutation p1,p2,,pnp_1, p_2,\cdots, p_n means a sequence satisfying {p1,p2,,pn}={1,2,,n}\{p_1, p_2,\cdots, p_n\}=\{1,2,\cdots,n\}.

Formally,

  • One operation of Xiao Yi: choose an interval [l,r][l,r] (1lrn1\le l\le r\le n). If the elements in this interval are pl,pl+1,pl+2,,prp_l,p_{l+1},p_{l+2},\dots,p_r, then after the operation they become pr,pl,pl+1,pl+2,pr1p_r,p_l,p_{l+1},p_{l+2}\dots,p_{r-1}.
  • One operation of Xiao Huan: choose two different positions i,ji,j (1i<jn1\le i<j\le n), and swap pip_i and pjp_j.

Today, you planned to host a game of Shadow Kill, but Xiao Yi and Xiao Huan, who usually always join, did not come. You felt puzzled, so you decided to investigate. It turned out that a villain was trying to sow discord! First, he said to Xiao Yi: “Look at the permutation [9,2,3,4,5,6,7,8,1][9,2,3,4,5,6,7,8,1]. You actually need a whole 88 operations to sort it! If Xiao Huan were here, he could finish it in one move. You are really not good at all—retire now!” Xiao Yi angrily drove him away, but that night this permutation kept haunting him, and he barely slept at all... Then the villain said to Xiao Huan: “Look at the permutation [2,3,4,5,6,7,8,9,1][2,3,4,5,6,7,8,9,1]. You actually need a whole 88 operations to sort it! If Xiao Yi did it, he could finish it in one move. You are unbelievably bad—retire now!” Xiao Huan angrily chased him away, but that night this permutation kept spinning in his mind, and he had nightmares all night...

This is simply too absurd. Outsiders see more clearly than those involved, so you decide to clear up the misunderstanding in the most direct way: for a given integer nn, for every 0i,jn10\le i , j \le n-1, you want to compute the number of permutations of length nn, denoted Pi,jP_{i ,j}, that satisfy: “when using a strategy that minimizes the number of operations, Xiao Yi needs exactly ii operations and Xiao Huan needs exactly jj operations to finish sorting.” Let the testdata speak—then they can fully understand each other’s ability, resolve the knot in their hearts, and become even better friends. Can you complete this task? Of course, since the number of permutations may be very large, given modmod, please output the answers modulo modmod.

Note that for two permutations of length nn, p1,p2,,pnp_1,p_2,\cdots,p_n and p1,p2,,pnp'_1,p'_2,\cdots,p'_n, they are considered different permutations and should be counted twice if and only if there exists an ii (1in1\le i\le n) such that pipip_i\ne p'_i.

Also note that “sorting” in the text above means sorting the permutation in increasing order, i.e. transforming a permutation p1,p2,,pnp_1,p_2,\cdots,p_n into 1,2,,n1,2,\cdots,n through operations.

Input Format

The input contains one line with two integers nn and modmod (1n1501\le n\le 150, 2mod109+72\le mod \le 10^9+7), separated by a single space.

Output Format

Output nn lines, each containing nn integers separated by a single space. The jj-th integer in the ii-th line represents Pi1,j1modmodP_{i-1,j-1} \bmod mod.

3 21
1 0 0
0 2 1
0 1 1
10 10
1 0 0 0 0 0 0 0 0 0
0 9 8 7 6 5 4 3 2 1
0 8 7 9 1 0 3 7 9 6
0 7 9 3 1 0 2 4 8 6
0 6 1 1 4 7 8 4 6 6
0 5 0 0 7 6 7 3 8 9
0 4 3 2 8 7 4 3 5 4
0 3 7 4 4 3 3 6 6 4
0 2 9 8 6 8 5 6 8 4
0 1 6 6 6 9 4 4 4 0
15 2
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 1 1 0 1 1 1 0 1 1 1 0 1 1 1
0 0 0 1 0 1 1 0 1 0 0 1 0 1 1
0 1 1 0 0 1 1 0 1 1 1 0 0 1 1
0 0 1 1 1 0 0 1 0 1 0 0 0 1 1
0 1 1 1 1 0 0 1 0 0 0 0 0 1 1
0 0 0 0 0 1 1 0 1 0 0 0 0 1 1
0 1 1 1 1 0 0 1 1 0 0 0 0 1 1
0 0 1 0 1 1 0 0 0 0 1 0 1 1 0
0 1 1 0 1 0 0 0 0 1 1 0 1 0 0
0 0 0 1 0 0 0 0 0 0 0 1 0 0 0
0 1 1 0 0 0 0 0 0 1 1 0 0 0 0
0 0 1 1 1 1 1 1 1 1 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0 0 0 0 0 0

Hint

Translated by ChatGPT 5