Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 900 点
問題文
各成分が 0,1 である N 次正方行列 A,B について、以下の条件を満たしているとき A と B は 似ている と言います。
- 各行の総和が等しい。つまり、どの i=1,\dots,N についても A_{i,1} + \dots + A_{i,N} = B_{i,1} + \dots + B_{i,N}
- 各列の総和が等しい。つまり、どの j=1,\dots,N についても A_{1,j} + \dots + A_{N,j} = B_{1,j} + \dots + B_{N,j}
また、各成分が 0,1 である N 次正方行列 A と整数 i,j (1 \leq i,j \leq N) について、A と似ているどの行列 B についても A_{i,j} = B_{i,j} が成り立つとき、A の i 行 j 列成分は 固定されている と言います。
以下の Q 個のクエリに答えてください。
- i 番目のクエリ:各成分が 0,1 である N 次正方行列であって、固定されている成分がちょうど K_i 個であるようなものが存在するなら
Yes
、そうでないならNo
を出力せよ。
制約
- 2 \le N \le 30
- 1 \le Q \le N^2+1
- 0 \le K_i \le N^2
- K_i \ne K_j (1 \le i < j \le Q)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q K_1 K_2 \vdots K_Q
出力
Q 行に出力せよ。 i (1 \le i \le Q) 行目には i 番目のクエリの答えを出力せよ。
入力例 1
3 3 0 9 7
出力例 1
Yes Yes No
1 番目のクエリ: 例えば、以下のような行列 X は、固定されている成分が 0 個です。
1 0 0 0 1 0 0 0 1
なぜなら、次のように列を循環シフトさせていったものはすべて X と似ており、どの成分も 0 にも 1 にもなりうるためです。
0 0 1 1 0 0 0 1 0
0 1 0 0 0 1 1 0 0
2 番目のクエリ: 例えば、以下のような行列 X は、固定されている成分が 9 個です。
0 0 1 0 1 1 1 1 1
なぜなら、似ている行列は X 以外に存在せず、すべての成分が固定されているためです。
3 番目のクエリ: 固定されている成分がちょうど 7 個であるような行列は存在しません。
入力例 2
29 6 186 681 18 108 123 321
出力例 2
No Yes No Yes No Yes
Score : 900 points
Problem Statement
For two N \times N matrices A and B whose elements are 0 or 1, we say that A and B are similar if they satisfy the following conditions:
- The sums of corresponding rows are equal. That is, A_{i,1} + \dots + A_{i,N} = B_{i,1} + \dots + B_{i,N} for any i=1,\dots,N.
- The sums of corresponding columns are equal. That is, A_{1,j} + \dots + A_{N,j} = B_{1,j} + \dots + B_{N,j} for any j=1,\dots,N.
Furthermore, for an N \times N matrix A whose elements are 0 or 1, and integers i,j (1 \leq i,j \leq N), we say that the element at row i column j is fixed if A_{i,j} = B_{i,j} holds for any matrix B that is similar to A.
Answer the following Q queries:
- The i-th query: If there exists an N \times N matrix whose elements are 0 or 1 such that exactly K_i elements are fixed, output
Yes
; otherwise, outputNo
.
Constraints
- 2 \le N \le 30
- 1 \le Q \le N^2+1
- 0 \le K_i \le N^2
- K_i \ne K_j (1 \le i < j \le Q)
- All inputs are integers
Input
The input is given from Standard Input in the following format:
N Q K_1 K_2 \vdots K_Q
Output
Output Q lines. For the i-th line (1 \le i \le Q), output the answer for the i-th query.
Sample Input 1
3 3 0 9 7
Sample Output 1
Yes Yes No
Query 1: For example, the following matrix X has exactly 0 fixed elements.
1 0 0 0 1 0 0 0 1
This is because all the following matrices, obtained by cyclically shifting the columns, are similar to X, and each element can be either 0 or 1.
0 0 1 1 0 0 0 1 0
0 1 0 0 0 1 1 0 0
Query 2: For example, the following matrix X has exactly 9 fixed elements.
0 0 1 0 1 1 1 1 1
This is because no other matrix similar to X exists, and all elements are fixed.
Query 3: No matrix exists with exactly 7 fixed elements.
Sample Input 2
29 6 186 681 18 108 123 321
Sample Output 2
No Yes No Yes No Yes
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 900 点
問題文
長さ N の整数列 (A_1,\dots,A_N) が与えられます。この整数列は、各 i=1,\dots,N について、0\le A_i < i を満たします。 次の条件を満たす (1,\dots,N) の順列 (P_1,\dots,P_N) の数を 998244353 で割ったあまりを求めてください。
- i=1,\dots,N について、
- A_i < j < i であるすべての整数 j について、P_j > P_i
- A_i > 0 ならば P_{A_i} < P_i
ただし、この問題の入力で与えられる (A_1,\dots,A_N) について、条件を満たす順列が存在することが保証されます。
制約
- 1\le N\le 3\times 10^5
- 0\le A_i \lt i
- A_1,\dots,A_N について、問題文中の条件を満たすような順列が存在する
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
条件を満たす順列の数を 998244353 で割ったあまりを出力せよ。
入力例 1
4 0 1 0 3
出力例 1
3
(2, 3, 1, 4), (2, 4, 1, 3), (3, 4, 1, 2) の 3 つです。
入力例 2
22 0 1 2 2 2 2 2 2 1 9 9 9 9 0 14 15 15 15 14 19 19 19
出力例 2
353820794
2350309500 を 998244353 で割ったあまりである、 353820794 が答えです。
Score : 900 points
Problem Statement
You are given a sequence of integers (A_1,\dots,A_N) of length N. This sequence satisfies 0\le A_i < i for each i=1,\dots,N. Find the number of permutations (P_1,\dots,P_N) of (1,\dots,N) that satisfy the following conditions, modulo 998244353.
- For each i=1,\dots,N:
- P_j > P_i for any integer j with A_i < j < i
- P_{A_i} < P_i if A_i > 0
For the sequence (A_1,\dots,A_N) given in the input, it is guaranteed that there exists a permutation satisfying the conditions.
Constraints
- 1\le N\le 3\times 10^5
- 0\le A_i \lt i
- For A_1,\dots,A_N, there exists a permutation satisfying the conditions in the problem statement.
- 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 number of permutations satisfying the conditions, modulo 998244353.
Sample Input 1
4 0 1 0 3
Sample Output 1
3
There are three such permutations: (2, 3, 1, 4), (2, 4, 1, 3), and (3, 4, 1, 2).
Sample Input 2
22 0 1 2 2 2 2 2 2 1 9 9 9 9 0 14 15 15 15 14 19 19 19
Sample Output 2
353820794
The answer is 353820794, which is 2350309500 modulo 998244353.
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 900 点
問題文
球橋さんと箱木さんはボールと箱を使ったゲームをします。
最初、球橋さんは M 種類のボールをそれぞれ 10^{100} 個ずつ持っていて、 箱木さんは 10^{100} 円持っています。 また、N 個の箱があり、i 番目の箱の容量は V_i で、値段は P_i 円です。ゲーム中、箱木さんはいつでも好きな箱を買うことができます。
このゲームでは、ゲームが終わるまで以下の操作を繰り返します。
- 球橋さんはボールを 1 つ選び、箱木さんに渡す。
- 箱木さんは渡されたボールを受け取るか、受け取らずにゲームを終えるかを選ぶ。
- ボールを受け取った場合、箱木さんは購入済みの箱を 1 つ選び、受け取ったボールを入れる。
- ボールを入れた箱が以下の条件を満たしている場合、箱木さんは 1 円をもらう。そうでない場合、ゲームを終える。
- 箱の中のボールの個数は、その箱の容量以下である。
- 箱の中のボールの種類は、すべて同じである。
球橋さんは、ゲーム終了時の箱木さんの所持金がなるべく少なくなるための最適な行動をし、反対に、箱木さんはなるべく多くなるための最適な行動をします。 ゲームを通して、箱木さんの所持金はいくら増えますか?
ただし、両者ともにすべての情報が公開されているとします。特に、球橋さんは、それぞれの箱について、容量と値段、どの種類のボールがいくつ入っているかを見ることができます。 また、箱木さんの初期の所持金は十分多く、お金が足りなくて箱が買えなくなることはないことに注意してください。
1 つの入力ファイルにつき、T 個のテストケースを解いてください。
制約
- 1\le T,N,M\le 3\times 10^5
- 1\le V_i,P_i \le 10^9
- T 個のテストケースに対する N の総和は 3\times 10^5 以下
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_i は i 番目のテストケースを意味する。
T \mathrm{case}_1 \mathrm{case}_2 \vdots \mathrm{case}_T
各テストケースは以下の形式で与えられる。
N M V_1 P_1 V_2 P_2 \vdots V_N P_N
出力
両者が最適に行動したときの、箱木さんの最終的な所持金と最初の所持金の差を出力せよ。
入力例 1
3 3 2 1 1000000000 3 1 3 1 1 300000 1000000000 1 10 4 22 5 26 45 72 21 47 39 97 2 75 35 82 24 17 46 32 22 28 67
出力例 1
2 0 28
最初のテストケースでは 2 種類のボールと 3 つの箱を使います。 2 種類のボールをそれぞれ白のボールと黒のボールと呼び、i 種類目の箱を箱 i と呼ぶことにします。 このテストケースについて、所持金が 2 円増えるゲームの進み方の例を示します。
- 球橋さんが白のボールを選び、渡す。
- 箱木さんはボールを受け取り、箱 2 を 1 円で買って白のボールを入れる。
- 箱 2 には白のボールが 1 個入っている。これは条件を満たしているため、箱木さんは 1 円をもらう。
- 球橋さんが白のボールを選び、渡す。
- 箱木さんはボールを受け取り、箱 2 に白のボールを入れる。
- 箱 2 には白のボールが 2 個入っている。これは条件を満たしているため、箱木さんは 1 円をもらう。
- 球橋さんが黒のボールを選び、渡す。
- 箱木さんはボールを受け取り、箱 3 を 1 円で買って黒のボールを入れる。
- 箱 3 には黒のボールが 1 個入っている。これは条件を満たしているため、箱木さんは 1 円をもらう。
- 球橋さんが白のボールを選び、渡す。
- 箱木さんはボールを受け取り、箱 2 に白のボールを入れる。
- 箱 2 には白のボールが 3 個入っている。これは条件を満たしているため、箱木さんは 1 円をもらう。
- 球橋さんが白のボールを選び、渡す。
- 箱木さんは受け取らずにゲームを終えることを選ぶ。
最終的に、箱 2 には白のボールが 3 個、箱 3 には黒のボールが 1 個入っています。 合計で 2 円使って 4 円もらったので、所持金は 2 円増えました。
2 つめのテストケースでは、球橋さんは、箱木さんにお金を稼がせないような行動ができます。
Score : 900 points
Problem Statement
Mr. Ball and Mr. Box will play a game with balls and boxes.
Initially, Mr. Ball has 10^{100} balls of each of M different types, and Mr. Box has 10^{100} yen. There are N boxes, where the i-th box has capacity V_i and costs P_i yen. During the game, Mr. Box can buy any box at any time.
In this game, the following operations are repeated until the game ends:
- Mr. Ball chooses one ball and gives it to Mr. Box.
- Mr. Box either accepts the ball or ends the game without accepting it.
- If Mr. Box accepts the ball, he chooses one of his purchased boxes and puts the ball in it.
- If the box with the ball satisfies the following conditions, Mr. Box receives 1 yen. Otherwise, the game ends.
- The number of balls in the box does not exceed its capacity.
- All balls in the box are of the same type.
Mr. Ball will play optimally to minimize Mr. Box's final money, while Mr. Box will play optimally to maximize it. How much will Mr. Box's money increase throughout the game?
Here, both players have access to all information. In particular, Mr. Ball can see the capacity, price, and contents (type and number of balls) of each box. Also, note that Mr. Box's initial money is large enough that he will never run out of money to buy boxes.
Solve T test cases for each input file.
Constraints
- 1\le T,N,M\le 3\times 10^5
- 1\le V_i,P_i \le 10^9
- The sum of N over the T test cases is at most 3\times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{case}_i represents the i-th test case:
T \mathrm{case}_1 \mathrm{case}_2 \vdots \mathrm{case}_T
Each test case is given in the following format:
N M V_1 P_1 V_2 P_2 \vdots V_N P_N
Output
Print the difference between Mr. Box's final and initial money when both players play optimally.
Sample Input 1
3 3 2 1 1000000000 3 1 3 1 1 300000 1000000000 1 10 4 22 5 26 45 72 21 47 39 97 2 75 35 82 24 17 46 32 22 28 67
Sample Output 1
2 0 28
In the first test case, there are two types of balls and three boxes. Let us call the two types of balls white and black balls, and call the i-th box box i. Here is an example of how the game could proceed where the money increases by 2 yen.
- Mr. Ball chooses and gives a white ball.
- Mr. Box accepts the ball, buys box 2 for 1 yen, and puts the white ball in it.
- Box 2 contains 1 white ball. This satisfies the conditions, so Mr. Box receives 1 yen.
- Mr. Ball chooses and gives a white ball.
- Mr. Box accepts the ball and puts it in box 2.
- Box 2 contains 2 white balls. This satisfies the conditions, so Mr. Box receives 1 yen.
- Mr. Ball chooses and gives a black ball.
- Mr. Box accepts the ball, buys box 3 for 1 yen, and puts the black ball in it.
- Box 3 contains 1 black ball. This satisfies the conditions, so Mr. Box receives 1 yen.
- Mr. Ball chooses and gives a white ball.
- Mr. Box accepts the ball and puts it in box 2.
- Box 2 contains 3 white balls. This satisfies the conditions, so Mr. Box receives 1 yen.
- Mr. Ball chooses and gives a white ball.
- Mr. Box chooses to end the game without accepting it.
Finally, box 2 contains 3 white balls and box 3 contains 1 black ball. Mr. Box spent 2 yen and received 4 yen, so his money increased by 2 yen.
In the second test case, Mr. Ball can play in a way that prevents Mr. Box from earning any money.
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 900 点
問題文
空でない非負整数列 (V_1, V_2, \dots, V_M) が Polish であることを、次のように再帰的に定義します。
- V_1 個の Polish 数列 W_1, W_2, \dots, W_{V_1} が存在して、数列 (V_1), W_1, W_2, \dots, W_{V_1} をこの順に連結したものが数列 (V_1, V_2, \dots, V_M) と一致する
特に、数列 (0) は Polish です。
長さ N の非負整数列 (A_1, A_2, \dots, A_N) が与えられます。辞書順で (A_1, A_2, \dots, A_N) 以下である、長さ N の Polish 数列の数を 998244353 で割ったあまりを求めてください。
数列の辞書順とは?
数列 S = (S_1,S_2,\ldots,S_{|S|}) が数列 T = (T_1,T_2,\ldots,T_{|T|}) より辞書順で小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、|S|, |T| はそれぞれ S, T の長さを表します。
- |S| \lt |T| かつ (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|})。
- ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して、下記の 2 つがともに成り立つ。
- (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
- S_i が T_i より(数として)小さい。
制約
- 1\leq N \leq 3\times 10^5
- 0\leq A_i \lt N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
条件を満たす数列の数を 998244353 で割ったあまりを出力せよ。
入力例 1
6 1 1 1 2 0 0
出力例 1
2
(1, 1, 1, 1, 1, 0) と (1, 1, 1, 2, 0, 0) が条件を満たします。
(1, 1, 1, 2, 0, 0) が Polish であることは、次のように確認できます。
- 問題文中にあるとおり、(0) は Polish である
- (2, 0, 0) は、 (2) と 2 つの Polilsh 数列 (0) と (0) をこの順に連結したものと一致するため、Polish である
- (1, 2, 0, 0) は、 (1) と 1 つの Polilsh 数列 (2, 0, 0) をこの順に連結したものと一致するため、Polish である
- (1, 1, 2, 0, 0) は、 (1) と 1 つの Polilsh 数列 (1, 2, 0, 0) をこの順に連結したものと一致するため、Polish である
- (1, 1, 1, 2, 0, 0) は、 (1) と 1 つの Polilsh 数列 (1, 1, 2, 0, 0) をこの順に連結したものと一致するため、Polish である
入力例 2
11 3 3 4 4 5 5 6 6 7 7 8
出力例 2
13002
入力例 3
19 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18
出力例 3
477638700
入力例 4
4 1 1 0 0
出力例 4
0
Score : 900 points
Problem Statement
Whether a non-empty sequence of non-negative integers (V_1, V_2, \dots, V_M) is Polish or not is recursively defined as follows:
- We say (V_1, V_2, \dots, V_M) is Polish if there exist V_1 Polish sequences W_1, W_2, \dots, W_{V_1} such that the concatenation of sequences (V_1), W_1, W_2, \dots, W_{V_1} in this order equals (V_1, V_2, \dots, V_M).
In particular, the sequence (0) is Polish.
Given a sequence of non-negative integers (A_1, A_2, \dots, A_N) of length N, find the number of Polish sequences of length N that are lexicographically not greater than (A_1, A_2, \dots, A_N), modulo 998244353.
What is lexicographical order on sequences?
We say that sequence S = (S_1,S_2,\ldots,S_{|S|}) is lexicographically less than sequence T = (T_1,T_2,\ldots,T_{|T|}) if either condition 1. or 2. below holds. Here, |S|, |T| represent the lengths of S, T respectively.
- |S| \lt |T| and (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}).
- There exists an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace such that both of the following hold:
- (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
- S_i is (numerically) less than T_i.
Constraints
- 1\leq N \leq 3\times 10^5
- 0\leq A_i \lt N
- 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 number of sequences satisfying the conditions, modulo 998244353.
Sample Input 1
6 1 1 1 2 0 0
Sample Output 1
2
(1, 1, 1, 1, 1, 0) and (1, 1, 1, 2, 0, 0) satisfy the conditions.
We can verify that (1, 1, 1, 2, 0, 0) is Polish as follows.
- As stated in the problem statement, (0) is Polish.
- (2, 0, 0) is Polish because it equals the concatenation of (2) and two Polish sequences (0) and (0) in this order.
- (1, 2, 0, 0) is Polish because it equals the concatenation of (1) and one Polish sequence (2, 0, 0) in this order.
- (1, 1, 2, 0, 0) is Polish because it equals the concatenation of (1) and one Polish sequence (1, 2, 0, 0) in this order.
- (1, 1, 1, 2, 0, 0) is Polish because it equals the concatenation of (1) and one Polish sequence (1, 1, 2, 0, 0) in this order.
Sample Input 2
11 3 3 4 4 5 5 6 6 7 7 8
Sample Output 2
13002
Sample Input 3
19 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18
Sample Output 3
477638700
Sample Input 4
4 1 1 0 0
Sample Output 4
0
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 1000 点
問題文
1,\dots,K からなる長さ M の整数列 (X_1,\dots,X_M) が与えられます。
1,\dots,K からなる長さ N の整数列 (A_1,\dots,A_N) のうち、以下の条件を満たすものの数を 998244353 で割ったあまりを求めてください。
- 1,\dots,K からなる長さ M の整数列のうち、(A_1,\dots,A_N) の(連続とは限らない)部分列として取れないものは (X_1,\dots,X_M) のみ
制約
- 2\le M,K \le N \le 400
- 1\le X_i \le K
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M K X_1 X_2 \dots X_M
出力
条件を満たす数列の数を 998244353 で割ったあまりを出力せよ。
入力例 1
5 2 3 1 1
出力例 1
4
以下の 4 通りが条件を満たします。
- (2, 3, 1, 2, 3)
- (2, 3, 1, 3, 2)
- (3, 2, 1, 2, 3)
- (3, 2, 1, 3, 2)
入力例 2
400 3 9 1 8 6
出力例 2
417833302
入力例 3
29 3 10 3 3 3
出力例 3
495293602
入力例 4
29 3 10 3 3 4
出力例 4
0
Score : 1000 points
Problem Statement
You are given a sequence of integers (X_1,\dots,X_M) of length M consisting of 1,\dots,K.
Find the number of sequences (A_1,\dots,A_N) of length N consisting of 1,\dots,K that satisfy the following condition, modulo 998244353:
- Among all sequences of length M consisting of 1,\dots,K, the only sequence that cannot be obtained as a (not necessarily contiguous) subsequence of (A_1,\dots,A_N) is (X_1,\dots,X_M).
Constraints
- 2\le M,K \le N \le 400
- 1\le X_i \le K
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M K X_1 X_2 \dots X_M
Output
Print the number of sequences satisfying the condition, modulo 998244353.
Sample Input 1
5 2 3 1 1
Sample Output 1
4
The following four sequences satisfy the condition:
- (2, 3, 1, 2, 3)
- (2, 3, 1, 3, 2)
- (3, 2, 1, 2, 3)
- (3, 2, 1, 3, 2)
Sample Input 2
400 3 9 1 8 6
Sample Output 2
417833302
Sample Input 3
29 3 10 3 3 3
Sample Output 3
495293602
Sample Input 4
29 3 10 3 3 4
Sample Output 4
0