Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
非負整数列が書かれている黒板があります。はじめ、黒板には長さ N の非負整数列 A=(A_1,A_2,\dots,A_N) のみが書かれています。
黒板に書かれている非負整数列の長さがすべて 1 になるまで以下の操作を行い続けます。
- 黒板に書かれている非負整数列のうち、長さ 2 以上の非負整数列を 1 つ選び、黒板から消去する。選んだ非負整数列を B=(B_1,B_2,\dots,B_n) とする。続けて 1 \leq k < n を満たす整数 k を 1 つ選び、 X = B_1 \oplus B_2 \oplus \dots \oplus B_k,\ Y=B_{k+1} \oplus B_{k+2} \oplus \dots \oplus B_n とする。最後に黒板に 2 つの非負整数列 (B_1 \oplus Y, B_2 \oplus Y,\dots,B_k \oplus Y),\ (B_{k+1} \oplus X,\ B_{k+2} \oplus X,\dots,B_n \oplus X) を書き込む
ただしここで、 \oplus はビット単位 \mathrm{XOR} 演算を表します。
一連の操作を行った後、黒板に書かれている N 個の長さ 1 の非負整数列を (C_1),(C_2),\dots,(C_N) としたとき、 \sum_{i=1}^{N}C_i として考えられる値の最小値を求めてください。
ビット単位 \mathrm{XOR} 演算とは
非負整数 A, B のビット単位 \mathrm{XOR} 、A \oplus B は、以下のように定義されます。
- A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR} は (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。
制約
- 1 \leq N \leq 500
- 0 \leq A_i < 2^{40}
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
4 1 3 4 5
出力例 1
6
はじめ、黒板には (1,3,4,5) のみが書かれています。ここから、以下の手順で操作を行います。
- (1,3,4,5) を黒板から消去し、 k=2 とする。 X=1 \oplus 3 = 2, Y= 4 \oplus 5 = 1 となり、黒板には新たに 2 つの非負整数列 (1 \oplus 1, 3 \oplus 1)=(0,2), (4 \oplus 2, 5 \oplus 2)=(6,7) が書き込まれる。
- (0,2) を黒板から消去し、 k=1 とする。黒板には新たに 2 つの非負整数列 (2),(2) が書き込まれる。
- (6,7) を黒板から消去し、 k=1 とする。黒板には新たに 2 つの非負整数列 (1),(1) が書き込まれる。
以上の手順により、黒板に書かれている非負整数列はいずれも長さ 1 の非負整数列 (2),(2),(1),(1) の 4 つになり、値の合計は 2+2+1+1=6 となります。
入力例 2
7 1 5 14 23 20 18 20
出力例 2
39
入力例 3
20 246260893965 450834729933 1091503137264 661761201979 238822689279 375606126051 183045127603 1004516515418 976478741401 957665143474 451659136716 828528157302 204109014940 184065081345 122138832666 130646707415 144391522538 87966805947 381909891703 343575641318
出力例 3
3597854777632
Score : 700 points
Problem Statement
(In this problem statement, all sequences consist of non-negative integers.)
There is a blackboard on which sequences are written. Initially, the blackboard only has one length-N sequence A=(A_1,A_2,\dots,A_N).
We repeat the following operation until all sequences on the blackboard are of length 1:
- Among the sequences on the blackboard, choose one that has length at least 2, and remove it from the blackboard. Let the chosen sequence be B=(B_1,B_2,\dots,B_n). Then, choose an integer k satisfying 1 \leq k < n, and define X = B_1 \oplus B_2 \oplus \dots \oplus B_k and Y=B_{k+1} \oplus B_{k+2} \oplus \dots \oplus B_n. Finally, write two sequences (B_1 \oplus Y, B_2 \oplus Y,\dots,B_k \oplus Y) and (B_{k+1} \oplus X,\ B_{k+2} \oplus X,\dots,B_n \oplus X) on the blackboard.
Here, \oplus denotes the bitwise \mathrm{XOR} operation.
After this series of operations, let (C_1),(C_2),\dots,(C_N) be the N sequences of length 1 on the blackboard. Find the minimum possible value of \sum_{i=1}^{N} C_i.
Notes on the bitwise \mathrm{XOR} operation
For two non-negative integers A, B, the bitwise \mathrm{XOR} operation A \oplus B is defined as follows.
- When A \oplus B is written in binary, for each 2^k place (k \geq 0), the bit is 1 if and only if exactly one of the bits in the 2^k place of A and B (in binary) is 1, and 0 otherwise.
In general, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), and it can be proved that this value does not depend on the order of p_1, p_2, \dots, p_k.
Constraints
- 1 \leq N \leq 500
- 0 \leq A_i < 2^{40}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
4 1 3 4 5
Sample Output 1
6
Initially, the blackboard has (1,3,4,5) written on it. From here, we perform operations in the following steps.
- Remove (1,3,4,5) from the blackboard and set k=2. Then, X=1 \oplus 3=2 and Y=4 \oplus 5=1, and two new sequences (1 \oplus 1, 3 \oplus 1)=(0,2) and (4 \oplus 2, 5 \oplus 2)=(6,7) are written on the blackboard.
- Remove (0,2) from the blackboard and set k=1. Two new sequences (2),(2) are written on the blackboard.
- Remove (6,7) from the blackboard and set k=1. Two new sequences (1),(1) are written on the blackboard.
After these steps, the four sequences on the blackboard are (2),(2),(1),(1), each of length 1. Their sum is 2+2+1+1=6.
Sample Input 2
7 1 5 14 23 20 18 20
Sample Output 2
39
Sample Input 3
20 246260893965 450834729933 1091503137264 661761201979 238822689279 375606126051 183045127603 1004516515418 976478741401 957665143474 451659136716 828528157302 204109014940 184065081345 122138832666 130646707415 144391522538 87966805947 381909891703 343575641318
Sample Output 3
3597854777632
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 900 点
問題文
整数 N,K および (, ) からなる長さ K の正しい括弧列 S が与えられます。
(, ) からなる長さ N の文字列 T のうち、以下の条件をすべて満たすものの数を 998244353 で割った余りを求めてください。
- T の部分列であって、長さ K の正しい括弧列であるものが存在する
- T の部分列であって、長さ K の正しい括弧列であるもののうち、辞書順で最大のものは S である
なお、 (, ) からなる文字列の辞書順について、 ( は ) より小さい文字であるものとします。
正しい括弧列とは
正しい括弧列とは、() である部分文字列を削除することを 0 回以上繰り返して空文字列にできる文字列を指します。
部分列とは
文字列 S の部分列とは、 S の文字を 0 文字以上選んで削除し、残った文字を元の順序を保って結合することで得られる文字列のことを指します。辞書順とは?
文字列 S = S_1S_2\ldots S_{|S|} が文字列 T = T_1T_2\ldots T_{|T|} より辞書順で小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、|S|, |T| はそれぞれ S, T の長さを表します。
- |S| \lt |T| かつ S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}。
- ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して、下記の 2 つがともに成り立つ。
- S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}
- S_i が T_i より小さい文字である。
制約
- K \leq N \leq 10^7
- 2 \leq K \leq 5 \times 10^5
- N,K は整数
- S は
(,)からなる長さ K の正しい括弧列
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
答えを出力せよ。
入力例 1
7 4 (())
出力例 1
20
たとえば T=((()))) の場合、 T の部分列であって、長さ 4 の正しい括弧列であるものは (()) のみであり、これは S に等しいため条件を満たします。
T=((())() の場合、 T の部分列であって、長さ 4 の正しい括弧列であるものは ()() と (()) の 2 種類存在し、辞書順で最大のものは ()() であるため、条件を満たしません。
入力例 2
20 4 ()()
出力例 2
1047225
入力例 3
71 10 ()(()())()
出力例 3
190654464
入力例 4
10000000 28 (()()(()))(()((()))())()(())
出力例 4
769035381
Score : 900 points
Problem Statement
You are given integers N, K, and a correct bracket sequence S of length K consisting of ( and ).
Find the number, modulo 998244353, of length-N strings T consisting of ( and ) that satisfy all of the following conditions:
- There exists a (not necessarily contiguous) subsequence of T that is a correct bracket sequence of length K.
- Among all (not necessarily contiguous) subsequences of T that are correct bracket sequences of length K, the lexicographically largest one is S.
Here, concerning the lexicographical order of strings consisting of ( and ), we consider ( smaller than ).
Notes on correct bracket sequences
A correct bracket sequence is a string that can be transformed into the empty string by repeatedly deleting a substring that is() zero or more times.
Notes on lexicographical order
A string S = S_1S_2\ldots S_{|S|} is said to be lexicographically smaller than a string T = T_1T_2\ldots T_{|T|} if either of the following holds.
- |S| < |T| and S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}.
- There exists an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace such that:
- S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1},
- S_i is a character smaller than T_i.
Constraints
- K \leq N \leq 10^7
- 2 \leq K \leq 5 \times 10^5
- N and K are integers.
- S is a correct bracket sequence of length K.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the answer.
Sample Input 1
7 4 (())
Sample Output 1
20
For example, if T=((()))), the only (not necessarily contiguous) subsequence of T that is a correct bracket sequence of length 4 is (()), which equals S, so it satisfies the conditions.
If T=((())(), there are two (not necessarily contiguous) subsequences of T that are correct bracket sequences of length 4: ()() and (()). The lexicographically largest one is ()(), so it does not satisfy the conditions.
Sample Input 2
20 4 ()()
Sample Output 2
1047225
Sample Input 3
71 10 ()(()())()
Sample Output 3
190654464
Sample Input 4
10000000 28 (()()(()))(()((()))())()(())
Sample Output 4
769035381
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 1100 点
問題文
頂点に 1,2,\dots,N の番号が付いた N 頂点 M 辺の単純連結無向グラフ G があります。 i 番目の辺は 2 つの頂点 u_i,v_i を結んでいます。
以下の条件をすべて満たす長さ N の非負整数列 X=(X_1,X_2,\dots,X_N) が存在するか判定してください。
- 各 v=1,2,\dots,N に対し、 X_v は G における頂点 v の次数を超えない
- G の M 本の辺を向き付けることで得られる有向グラフであって、各 v=1,2,\dots,N に対し、頂点 v の入次数・出次数のいずれかが X_v に等しいようなものが存在しない
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T \leq 10^5
- 2 \leq N \leq 2 \times 10^5
- N-1 \leq M \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- 入力される値はすべて整数
- 入力で与えられるグラフ G は単純連結無向グラフ
- 1 つの入力に含まれるテストケースについて、 N の総和は 2 \times 10^5 以下
- 1 つの入力に含まれるテストケースについて、 M の総和は 2 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\vdots
\mathrm{case}_T
各ケースは以下の形式で与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
T 行出力せよ。i 行目には i 番目のテストケースについて、条件をすべて満たす X が存在する場合は Yes を、存在しない場合は No を出力せよ。
入力例 1
3 4 4 1 2 2 3 3 4 4 1 7 6 3 4 3 6 2 5 1 2 1 3 2 7 15 20 11 13 11 7 12 6 5 2 4 1 15 3 9 8 13 5 8 6 7 5 11 8 12 9 14 1 1 11 6 7 1 3 2 3 3 7 5 12 10 6
出力例 1
Yes No Yes
1 番目のテストケースについて、たとえば X=(2,2,2,1) が条件を満たします。
2 番目のテストケースについて、条件を満たす X は存在しません。
Score : 1100 points
Problem Statement
There is a simple connected undirected graph G with N vertices and M edges, where the vertices are labeled 1,2,\dots,N. The i-th edge connects two vertices u_i and v_i.
Determine whether there exists a length-N sequence of non-negative integers X=(X_1,X_2,\dots,X_N) that satisfies all of the following conditions:
- X_v does not exceed the degree of vertex v in G for each v=1,2,\dots,N.
- There is no directed graph obtained by assigning directions to the M edges of G in which, for each v=1,2,\dots,N, either the in-degree or the out-degree of v equals X_v.
You have T test cases; solve each of them.
Constraints
- 1 \leq T \leq 10^5
- 2 \leq N \leq 2 \times 10^5
- N-1 \leq M \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- All input values are integers.
- The graph G given in the input is a simple connected undirected graph.
- The sum of N over all test cases in each input is at most 2 \times 10^5.
- The sum of M over all test cases in each input is at most 2 \times 10^5.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\vdots
\mathrm{case}_T
Each case is given in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Print T lines. The i-th line should contain the answer for the i-th test case.
If there exists an X that satisfies the conditions, print Yes; otherwise, print No.
Sample Input 1
3 4 4 1 2 2 3 3 4 4 1 7 6 3 4 3 6 2 5 1 2 1 3 2 7 15 20 11 13 11 7 12 6 5 2 4 1 15 3 9 8 13 5 8 6 7 5 11 8 12 9 14 1 1 11 6 7 1 3 2 3 3 7 5 12 10 6
Sample Output 1
Yes No Yes
In the first test case, for example, X=(2,2,2,1) satisfies the conditions.
In the second test case, no sequence X satisfies the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 1300 点
問題文
整数 N,K および長さ N の非負整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
A は単調非減少列です。すなわち、 i=1,2,\dots,N-1 に対して A_i \leq A_{i+1} が成り立ちます。
長さ N の単調非減少列である非負整数列 X=(X_1,X_2,\dots,X_N) に対して以下の操作を 0 回以上行うことで X の全ての項の値を 0 にできるとき、 X は「良い数列」であるといいます。
- 1 \leq i \leq N-K+1 を満たす整数 i を選ぶ。 S=X_i+X_{i+1}+\dots +X_{i+K-1} とする。 X の i,i+1,\dots,i+K-1 項目の値を全て \left\lfloor \frac{S}{K}\right\rfloor で置き換える。
以下の条件をすべて満たす非負整数列 B=(B_1,B_2,\dots,B_N) に対する \sum_{i=1}^N B_i の最大値を求めてください。
- i=1,2,\dots,N に対し、 B_i \leq A_i が成り立つ。
- 非負整数列 B は単調非減少列であり、「良い数列」である。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T \leq 10^5
- 2 \leq K \leq N \leq 2 \times 10^5
- 0\leq A_i \leq 10^{9}
- i=1,2,\dots,N-1 に対して A_i \leq A_{i+1} が成り立つ
- 入力される値はすべて整数
- 1 つの入力に含まれるテストケースについて、 N の総和は 2 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\vdots
\mathrm{case}_T
各ケースは以下の形式で与えられる。
N K A_1 A_2 \dots A_N
出力
T 行出力せよ。i 行目には i 番目のテストケースについて、答えを出力せよ。
入力例 1
4 4 2 0 1 2 3 4 3 0 1 2 3 20 5 1 2 3 3 4 5 6 7 7 8 9 9 11 13 14 16 16 17 18 19 20 10 36410986 135774405 160873759 200077345 386257217 408140905 426675713 448759426 470780343 572969565 653981029 693304642 741916215 764524315 768298046 811893935 840607643 847634785 856942510 883746061
出力例 1
6 5 185 739
1 番目のテストケースについて、 A は「良い数列」になっています。これについて、例えば操作を以下の手順で行うことで A の全ての項の値を 0 にできます。
- i=3 とする。 S=A_3+A_4=5 となり、 A の 3,4 項目の値は \left\lfloor \frac{5}{2}\right \rfloor = 2 に置き換えられ、 A=(0,1,2,2) となる。
- i=2 とする。 S=A_2+A_3=3 となり、 A の 2,3 項目の値は \left\lfloor \frac{3}{2}\right \rfloor = 1 に置き換えられ、 A=(0,1,1,2) となる。
- i=3 とする。 S=A_3+A_4=3 となり、 A の 3,4 項目の値は \left\lfloor \frac{3}{2}\right \rfloor = 1 に置き換えられ、 A=(0,1,1,1) となる。
- i=1 とする。 S=A_1+A_2=1 となり、 A の 1,2 項目の値は \left\lfloor \frac{1}{2}\right \rfloor = 0 に置き換えられ、 A=(0,0,1,1) となる。
- i=2 とする。 S=A_2+A_3=1 となり、 A の 2,3 項目の値は \left\lfloor \frac{1}{2}\right \rfloor = 0 に置き換えられ、 A=(0,0,0,1) となる。
- i=3 とする。 S=A_3+A_4=1 となり、 A の 3,4 項目の値は \left\lfloor \frac{1}{2}\right \rfloor = 0 に置き換えられ、 A=(0,0,0,0) となる。
よって B=A とすることで最大値 0+1+2+3=6 を達成します。
2 番目のテストケースについて、 A は「良い数列」ではありません。 B=(0,0,2,3) とすると B は「良い数列」であり、条件を満たします。
Score : 1300 points
Problem Statement
You are given integers N, K, and a length-N sequence of non-negative integers A = (A_1, A_2, \dots, A_N).
A is a monotonically non-decreasing sequence. That is, A_i \leq A_{i+1} holds for all i = 1, 2, \dots, N - 1.
We call a length-N sequence of non-negative integers X = (X_1, X_2, \dots, X_N) a "good sequence" if X is monotonically non-decreasing and one can make all elements of X equal to 0 by applying the following operation zero or more times:
- Choose an integer i such that 1 \leq i \leq N - K + 1. Let S = X_i + X_{i+1} + \dots + X_{i+K-1}. Replace each of the i-th through (i+K-1)-th elements of X with \left\lfloor \frac{S}{K} \right\rfloor.
Find the maximum possible value of \sum_{i=1}^N B_i for a sequence of non-negative integers B = (B_1, B_2, \dots, B_N) that satisfy all of the following conditions:
- B_i \leq A_i holds for all i = 1, 2, \dots, N.
- B is a monotonically non-decreasing sequence and is a "good sequence."
You have T test cases; solve each of them.
Constraints
- 1 \leq T \leq 10^5
- 2 \leq K \leq N \leq 2 \times 10^5
- 0 \leq A_i \leq 10^{9}
- A_i \leq A_{i+1} holds for all i = 1, 2, \dots, N - 1.
- All input values are integers.
- The sum of N over all test cases in one input is at most 2 \times 10^5.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\vdots
\mathrm{case}_T
Each case is given in the following format:
N K A_1 A_2 \dots A_N
Output
Print T lines. The i-th line should contain the answer for the i-th test case.
Sample Input 1
4 4 2 0 1 2 3 4 3 0 1 2 3 20 5 1 2 3 3 4 5 6 7 7 8 9 9 11 13 14 16 16 17 18 19 20 10 36410986 135774405 160873759 200077345 386257217 408140905 426675713 448759426 470780343 572969565 653981029 693304642 741916215 764524315 768298046 811893935 840607643 847634785 856942510 883746061
Sample Output 1
6 5 185 739
In the first test case, A itself is a "good sequence." For example, we can make all elements of A equal to 0 with the following steps:
- Let i=3. Then, S=A_3+A_4=5. Replace A_3 and A_4 with \left\lfloor \frac{5}{2} \right\rfloor = 2, resulting in A=(0,1,2,2).
- Let i=2. Then, S=A_2+A_3=3. Replace A_2 and A_3 with \left\lfloor \frac{3}{2} \right\rfloor = 1, resulting in A=(0,1,1,2).
- Let i=3. Then, S=A_3+A_4=3. Replace A_3 and A_4 with \left\lfloor \frac{3}{2} \right\rfloor = 1, resulting in A=(0,1,1,1).
- Let i=1. Then, S=A_1+A_2=1. Replace A_1 and A_2 with \left\lfloor \frac{1}{2} \right\rfloor = 0, resulting in A=(0,0,1,1).
- Let i=2. Then, S=A_2+A_3=1. Replace A_2 and A_3 with \left\lfloor \frac{1}{2} \right\rfloor = 0, resulting in A=(0,0,0,1).
- Let i=3. Then, S=A_3+A_4=1. Replace A_3 and A_4 with \left\lfloor \frac{1}{2} \right\rfloor = 0, resulting in A=(0,0,0,0).
Thus, taking B = A achieves the maximum sum 0+1+2+3=6.
In the second test case, A is not a "good sequence." However, if we take B = (0,0,2,3), then B is a "good sequence" and satisfies the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 1700 点
問題文
(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) に対し、長さ N の整数列 f(P) を以下のように定めます。
-
整数列 X を X=(P_1,P_2,\dots,P_N) で初期化し、 X が単調増加数列になるまで以下の操作を行い続ける。なお、任意の順列 P に対し、 X は有限回の操作によって単調増加数列になることが示せる。
- X_i > X_{i+1} を満たす最小の i\ (1 \leq i < N) を k とする。 X の k 項目を末尾に移動する。より正確には、 X を (X_1,X_2,\dots,X_{k-1},X_{k+1},X_{k+2},\dots,X_N,X_{k}) で置き換える。
一連の操作で整数 x が末尾に移動させられた回数を C_x とし、 f(P)=(C_1,C_2,\dots,C_N) と定める。
f(P) としてありうるものの種類数を素数 M で割った余りを求めてください。
制約
- 1 \leq N \leq 500
- 10^8 \leq M \leq 10^9
- M は素数
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
答えを出力せよ。
入力例 1
4 998244353
出力例 1
8
例えば P=(1,4,3,2) の場合、 X は X=(1,4,3,2) で初期化され、操作は以下のように行われます。
- X_1 \leq X_2 かつ X_2 > X_3 であるため、 X_2=4 が末尾に移動し、 X は (1,3,2,4) に変化する
- X_2=3 が末尾に移動し、 X は (1,2,4,3) に変化する
- X_3=4 が末尾に移動し、 X は (1,2,3,4) に変化する
1,2,3,4 が末尾に移動させられた回数はそれぞれ 0,0,1,2 回であるため、 f(P)=(0,0,1,2) となります。
入力例 2
71 998244353
出力例 2
473972314
入力例 3
300 923223991
出力例 3
333532467
Score : 1700 points
Problem Statement
For a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N), define a length-N integer sequence f(P) as follows.
-
Initialize an integer sequence X with X=(P_1,P_2,\dots,P_N). Repeat the following operation until X is a strictly increasing sequence. (It can be shown that X will be a strictly increasing sequence in a finite number of operations for any permutation P.)
- Let k be the smallest index i (1 \leq i < N) such that X_i > X_{i+1}. Move the k-th element of X to the end. More precisely, replace X with (X_1,X_2,\dots,X_{k-1},X_{k+1},X_{k+2},\dots,X_N,X_{k}).
Let C_x be the number of times an integer x is moved to the end during this series of operations. Then, define f(P)=(C_1,C_2,\dots,C_N).
Find the number, modulo a prime number M, of distinct sequences that can occur as f(P).
Constraints
- 1 \leq N \leq 500
- 10^8 \leq M \leq 10^9
- M is prime.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
Output
Print the answer.
Sample Input 1
4 998244353
Sample Output 1
8
For example, when P=(1,4,3,2), we initialize X=(1,4,3,2) and perform operations as follows:
- Since X_1 \leq X_2 but X_2 > X_3, the element X_2=4 is moved to the end, and X becomes (1,3,2,4).
- X_2=3 is moved to the end, and X becomes (1,2,4,3).
- X_3=4 is moved to the end, and X becomes (1,2,3,4).
The number of times 1,2,3,4 are moved to the end is 0,0,1,2, respectively, so f(P)=(0,0,1,2).
Sample Input 2
71 998244353
Sample Output 2
473972314
Sample Input 3
300 923223991
Sample Output 3
333532467