実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
とある回転寿司に、1 から N までの番号が付けられた N 人の人が訪れています。 人 i の 美食度 は A_i です。
今からベルトコンベア上を M 個の寿司が流れます。 j 番目に流れる寿司の 美味しさ は B_j です。 それぞれの寿司は、人 1,2,\dots,N の前をこの順に流れていきます。 それぞれの人は、美味しさが自分の美食度以上である寿司が自分の前に流れてきたときはその寿司を取って食べ、それ以外のときは何もしません。 人 i が取って食べた寿司は、人 j\ (j > i) の前にはもう流れてきません。
M 個の寿司それぞれについて、その寿司を誰が食べるか、あるいは誰も食べないかどうかを求めてください。
制約
- 1\leq N,M \leq 2\times 10^5
- 1\leq A_i,B_i \leq 2\times 10^5
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
出力
M 行出力せよ。
j\ (1\leq j \leq M) 行目には、j 番目に流れる寿司を食べる人がいるならばその人の番号を、そうでないならば -1 を出力せよ。
入力例 1
3 3 3 8 2 5 2 1
出力例 1
1 3 -1
- 1 番目の寿司について、
- まず人 1 の前を流れます。B_1 \geq A_1 なので、人 1 はこれを取って食べます。
- 人 2,3 の前にはこの寿司は流れてきません。
- 2 番目の寿司について、
- まず人 1 の前を流れます。B_2 < A_1 なので、人 1 は何もしません。
- 次に人 2 の前を流れます。B_2 < A_2 なので、人 2 は何もしません。
- 最後に人 3 の前を流れます。B_2 \geq A_3 なので、人 3 はこれを取って食べます。
- 3 番目の寿司について、
- まず人 1 の前を流れます。B_3 < A_1 なので、人 1 は何もしません。
- 次に人 2 の前を流れます。B_3 < A_2 なので、人 2 は何もしません。
- 最後に人 3 の前を流れます。B_3 < A_3 なので、人 3 は何もしません。
- よって、誰もこの寿司を食べません。
入力例 2
3 3 1 1 1 1 1 1
出力例 2
1 1 1
入力例 3
10 5 60 83 76 45 70 91 37 58 94 22 70 39 52 33 18
出力例 3
1 7 4 10 -1
Score: 350 points
Problem Statement
There are N people numbered from 1 to N visiting a conveyor belt sushi restaurant. The gourmet level of person i is A_i.
Now, M pieces of sushi will be placed on the conveyor belt. The deliciousness of the j-th sushi is B_j. Each piece of sushi passes in front of people 1, 2, \dots, N in this order. Each person, when a sushi whose deliciousness is not less than their gourmet level passes in front of them, will take and eat that sushi; otherwise, they do nothing. A sushi that person i takes and eats will no longer pass in front of person j\ (j > i).
For each of the M pieces of sushi, determine who eats that sushi, or if nobody eats it.
Constraints
- 1 \leq N, M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 2 \times 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
Output
Print M lines.
The j-th line (1 \leq j \leq M) should contain the number representing the person who eats the j-th sushi, or -1 if nobody eats it.
Sample Input 1
3 3 3 8 2 5 2 1
Sample Output 1
1 3 -1
- For the 1st sushi:
- It first passes in front of person 1. Since B_1 \geq A_1, person 1 takes and eats it.
- It will not pass in front of person 2 and 3.
- For the 2nd sushi:
- It first passes in front of person 1. Since B_2 < A_1, person 1 does nothing.
- Next, it passes in front of person 2. Since B_2 < A_2, person 2 does nothing.
- Finally, it passes in front of person 3. Since B_2 \geq A_3, person 3 takes and eats it.
- For the 3rd sushi:
- It first passes in front of person 1. Since B_3 < A_1, person 1 does nothing.
- Next, it passes in front of person 2. Since B_3 < A_2, person 2 does nothing.
- Finally, it passes in front of person 3. Since B_3 < A_3, person 3 does nothing.
- Therefore, nobody eats this sushi.
Sample Input 2
3 3 1 1 1 1 1 1
Sample Output 2
1 1 1
Sample Input 3
10 5 60 83 76 45 70 91 37 58 94 22 70 39 52 33 18
Sample Output 3
1 7 4 10 -1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N かつ全ての要素が 1 以上 M 以下である整数列のうち、狭義単調増加であるものを全て辞書順に出力してください。
注記
ある 2 個の異なる長さの等しい整数列 A_1,A_2,\dots,A_N と B_1,B_2,\dots,B_N が以下を満たすとき、またその時に限り辞書順で A が B より早いと定義されます。
- ある整数 i(1 \le i \le N) が存在し、1 \le j < i である全ての整数 j に対し A_j=B_j が成り立ち、かつ A_i < B_i が成り立つ。
ある整数列 A_1,A_2,\dots,A_N は以下を満たすとき、またその時に限り狭義単調増加です。
- 全ての整数 i(1 \le i \le N-1) に対し A_i < A_{i+1} が成り立つ。
制約
- 1 \le N \le M \le 10
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
条件を満たす整数列を一行に一つずつ、辞書順に出力せよ(出力例を参考にせよ)。
入力例 1
2 3
出力例 1
1 2 1 3 2 3
条件を満たす数列は (1,2),(1,3),(2,3) の 3 個です。これらを辞書順で早い方から出力します。
入力例 2
3 5
出力例 2
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
Score : 300 points
Problem Statement
Print all strictly increasing integer sequences of length N where all elements are between 1 and M (inclusive), in lexicographically ascending order.
Notes
For two integer sequences of the same length A_1,A_2,\dots,A_N and B_1,B_2,\dots,B_N, A is said to be lexicographically earlier than B if and only if:
- there is an integer i (1 \le i \le N) such that A_j=B_j for all integers j satisfying 1 \le j < i, and A_i < B_i.
An integer sequence A_1,A_2,\dots,A_N is said to be strictly increasing if and only if:
- A_i < A_{i+1} for all integers i (1 \le i \le N-1).
Constraints
- 1 \le N \le M \le 10
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the sought sequences in lexicographically ascending order, each in its own line (see Sample Outputs).
Sample Input 1
2 3
Sample Output 1
1 2 1 3 2 3
The sought sequences are (1,2),(1,3),(2,3), which should be printed in lexicographically ascending order.
Sample Input 2
3 5
Sample Output 2
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 人のスポーツ選手がいます。
N 人の選手たちには互いに相性の悪い選手のペアが M 組あり、相性の悪い組のうち i\ (1\leq i\leq M) 組目は A _ i 番目の選手と B _ i 番目の選手です。
あなたは、選手を T チームに分けます。 どの選手もちょうど一つのチームに属さなければならず、どのチームにも少なくとも一人の選手が属さなければなりません。 さらに、どの i=1,2,\ldots,M についても、 A _ i 番目の選手と B _ i 番目の選手が同じチームに属していてはいけません。
この条件を満たすチーム分けの方法は何通りあるか求めてください。 ただし、チーム分けの方法が異なるとは、ある二人が存在して、彼らが一方のチーム分けでは同じチームに所属し、もう一方では異なるチームに所属することをいいます。
制約
- 1\leq T\leq N\leq10
- 0\leq M\leq\dfrac{N(N-1)}2
- 1\leq A _ i\lt B _ i\leq N\ (1\leq i\leq M)
- (A _ i,B _ i)\neq (A _ j,B _ j)\ (1\leq i\lt j\leq M)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N T M A _ 1 B _ 1 A _ 2 B _ 2 \vdots A _ M B _ M
出力
答えを 1 行で出力せよ。
入力例 1
5 2 2 1 3 3 4
出力例 1
4
次の 4 通りのチーム分けが条件を満たします。

他に条件を満たすチーム分けは存在しないので、4 を出力してください。
入力例 2
5 1 2 1 3 3 4
出力例 2
0
条件を満たすチーム分けがひとつも存在しないこともあります。
入力例 3
6 4 0
出力例 3
65
相性の悪いペアがひとつも存在しないこともあります。
入力例 4
10 6 8 5 9 1 4 3 8 1 6 4 10 5 7 5 6 3 7
出力例 4
8001
Score : 400 points
Problem Statement
There are N sports players.
Among them, there are M incompatible pairs. The i-th incompatible pair (1\leq i\leq M) is the A_i-th and B_i-th players.
You will divide the players into T teams. Every player must belong to exactly one team, and every team must have one or more players. Additionally, for each i=1,2,\ldots,M, the A_i-th and B_i-th players must not belong to the same team.
Find the number of ways to satisfy these conditions. Here, two divisions are considered different when there are two players who belong to the same team in one division and different teams in the other.
Constraints
- 1\leq T\leq N\leq10
- 0\leq M\leq\dfrac{N(N-1)}2
- 1\leq A _ i\lt B _ i\leq N\ (1\leq i\leq M)
- (A _ i,B _ i)\neq (A _ j,B _ j)\ (1\leq i\lt j\leq M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N T M A _ 1 B _ 1 A _ 2 B _ 2 \vdots A _ M B _ M
Output
Print the answer in a single line.
Sample Input 1
5 2 2 1 3 3 4
Sample Output 1
4
The following four divisions satisfy the conditions.

No other division satisfies them, so print 4.
Sample Input 2
5 1 2 1 3 3 4
Sample Output 2
0
There may be no division that satisfies the conditions.
Sample Input 3
6 4 0
Sample Output 3
65
There may be no incompatible pair.
Sample Input 4
10 6 8 5 9 1 4 3 8 1 6 4 10 5 7 5 6 3 7
Sample Output 4
8001
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
各要素の値が 0 または 1 である H 行 W 列の行列 A が与えられます。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i,j) について、A の i 行目 j 列目の要素を A_{i,j} で表します。
行列 A に対し、以下の操作を 0 回以上の好きな回数行うことができます。
- 1 \leq i \leq H を満たす整数 i を選び、1 \leq j \leq W を満たす全ての整数 j に対して A_{i,j} の値を 1-A_{i,j} で置き換える。
また、A_{i,j} は行列において上下左右に同じ値が存在しない、すなわち 4 つの整数組 (x,y) = (i-1,j),(i+1,j),(i,j-1),(i,j+1) のいずれかであって、 1 \leq x \leq H, 1 \leq y \leq W かつ A_{i,j} = A_{x,y} を満たすものが存在しないとき、またそのときに限り孤立した要素であると定義されます。
操作を繰り返し行列 A の任意の要素が孤立した要素でない状態にすることが可能か判定し、可能な場合は行う操作回数の最小値を求めてください。
制約
- 2 \leq H,W \leq 1000
- A_{i,j} = 0 または A_{i,j} = 1
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}
出力
操作を繰り返すことにより孤立した要素が存在しないようにできる場合は操作回数の最小値を、できない場合は -1 を出力せよ。
入力例 1
3 3 1 1 0 1 0 1 1 0 0
出力例 1
1
i = 1 を選択し操作を行うと、A = ((0,0,1),(1,0,1),(1,0,0)) となり、孤立した要素は存在しなくなります。
入力例 2
4 4 1 0 0 0 0 1 1 1 0 0 1 0 1 1 0 1
出力例 2
2
入力例 3
2 3 0 1 0 0 1 1
出力例 3
-1
Score : 500 points
Problem Statement
You are given a matrix A with H rows and W columns. The value of each of its elements is 0 or 1. For an integer pair (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, we denote by A_{i,j} the element at the i-th row and j-th column.
You can perform the following operation on the matrix A any number of times (possibly zero):
- Choose an integer i such that 1 \leq i \leq H. For every integer j such that 1 \leq j \leq W, replace the value of A_{i,j} with 1-A_{i,j}.
A_{i,j} is said to be isolated if and only if there is no adjacent element with the same value; in other words, if and only if none of the four integer pairs (x,y) = (i-1,j),(i+1,j),(i,j-1),(i,j+1) satisfies 1 \leq x \leq H, 1 \leq y \leq W, and A_{i,j} = A_{x,y}.
Determine if you can make the matrix A in such a state that no element is isolated by repeating the operation. If it is possible, find the minimum number of operations required to do so.
Constraints
- 2 \leq H,W \leq 1000
- A_{i,j} = 0 or A_{i,j} = 1
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}
Output
If you can make it in such a state that no element is isolated by repeating the operation, print the minimum number of operations required to do so; otherwise, print -1.
Sample Input 1
3 3 1 1 0 1 0 1 1 0 0
Sample Output 1
1
An operation with i = 1 makes A = ((0,0,1),(1,0,1),(1,0,0)), where there is no longer an isolated element.
Sample Input 2
4 4 1 0 0 0 0 1 1 1 0 0 1 0 1 1 0 1
Sample Output 2
2
Sample Input 3
2 3 0 1 0 0 1 1
Sample Output 3
-1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
長さ N の正整数列 A=(A_1,A_2,\dots,A_N) と正整数 M が与えられます。A の空でない連続とは限らない部分列であって、列に含まれる要素の最小公倍数が M になるようなものの個数を 998244353 で割った余りを求めてください。ただし、2 つの部分列が列として同じでも、取り出す位置が異なるならば区別するものとします。また、列の要素の個数が 1 のときはその要素自身を最小公倍数とします。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq M\leq 10^{16}
- 1\leq A_i\leq 10^{16}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 6 2 3 4 6
出力例 1
5
A の部分列であって,列に含まれる要素の最小公倍数が 6 となるものは (2,3),(2,3,6),(2,6),(3,6),(6) の 5 つです。
入力例 2
5 349 1 1 1 1 349
出力例 2
16
部分列が列として同じでも、取り出す位置が異なるならば区別することに注意してください。
入力例 3
16 720720 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
出力例 3
2688
Score: 525 points
Problem Statement
You are given a sequence of positive integers A=(A_1,A_2,\dots,A_N) of length N and a positive integer M. Find the number, modulo 998244353, of non-empty and not necessarily contiguous subsequences of A such that the least common multiple (LCM) of the elements in the subsequence is M. Two subsequences are distinguished if they are taken from different positions in the sequence, even if they coincide as sequences. Also, the LCM of a sequence with a single element is that element itself.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10^{16}
- 1 \leq A_i \leq 10^{16}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
4 6 2 3 4 6
Sample Output 1
5
The subsequences of A whose elements have an LCM of 6 are (2,3),(2,3,6),(2,6),(3,6),(6); there are five of them.
Sample Input 2
5 349 1 1 1 1 349
Sample Output 2
16
Note that even if some subsequences coincide as sequences, they are distinguished if they are taken from different positions.
Sample Input 3
16 720720 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Sample Output 3
2688