実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
整数列 A=(A_1,A_2,A_3) が与えられます。
A の要素を自由に並べ替えた列を B=(B_1,B_2,B_3) とします。
このとき、 B_1 \times B_2 = B_3 を満たすようにできるか判定してください。
制約
- 入力は全て整数
- 1 \le A_1,A_2,A_3 \le 100
入力
入力は以下の形式で標準入力から与えられる。
A_1 A_2 A_3
出力
B_1 \times B_2 = B_3 を満たすようにできるなら Yes 、そうでないなら No と出力せよ。
入力例 1
3 15 5
出力例 1
Yes
A=(3,15,5) です。
B=(3,5,15) と並べ替えることで、 B_1 \times B_2 = B_3 を満たすようにできます。
入力例 2
5 3 2
出力例 2
No
B をどのように定めても、 B_1 \times B_2 = B_3 を満たすようにはできません。
入力例 3
3 3 9
出力例 3
Yes
Score : 100 points
Problem Statement
You are given a sequence of integers A = (A_1, A_2, A_3).
Let B = (B_1, B_2, B_3) be any permutation of A.
Determine whether it is possible that B_1 \times B_2 = B_3.
Constraints
- All input values are integers.
- 1 \le A_1, A_2, A_3 \le 100
Input
The input is given from Standard Input in the following format:
A_1 A_2 A_3
Output
If it is possible that B_1 \times B_2 = B_3, print Yes; otherwise, print No.
Sample Input 1
3 15 5
Sample Output 1
Yes
Here, A=(3,15,5). By rearranging it as B=(3,5,15), we can satisfy B_1 \times B_2 = B_3.
Sample Input 2
5 3 2
Sample Output 2
No
No permutation of B satisfies B_1 \times B_2 = B_3.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
長さ M の整数列 A=(A_1,A_2,\dots,A_M) が与えられます。
A の各要素は 1 以上 N 以下で、全ての要素は相異なります。
A の要素として含まれない 1 以上 N 以下の整数を、昇順に全て列挙してください。
制約
- 入力は全て整数
- 1 \le M \le N \le 1000
- 1 \le A_i \le N
- A の要素は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_M
出力
A の要素として含まれない 1 以上 N 以下の整数を昇順に全て挙げた列が (X_1,X_2,\dots,X_C) であるとき、以下の形式で出力せよ。
C X_1 X_2 \dots X_C
入力例 1
10 3 3 9 2
出力例 1
7 1 4 5 6 7 8 10
A=(3,9,2) です。
A の要素として含まれない 1 以上 10 以下の整数を昇順に全て挙げると、 1,4,5,6,7,8,10 となります。
入力例 2
6 6 1 3 5 2 4 6
出力例 2
0
A の要素として含まれない 1 以上 6 以下の整数がひとつもありません。
この場合、 1 行目に 0 と出力し、 2 行目は空行としてください。
入力例 3
9 1 9
出力例 3
8 1 2 3 4 5 6 7 8
Score : 200 points
Problem Statement
You are given a sequence of M integers A = (A_1, A_2, \dots, A_M).
Each element of A is an integer between 1 and N, inclusive, and all elements are distinct.
List all integers between 1 and N that do not appear in A in ascending order.
Constraints
- All input values are integers.
- 1 \le M \le N \le 1000
- 1 \le A_i \le N
- The elements of A are distinct.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_M
Output
Let (X_1, X_2, \dots, X_C) be the sequence of all integers between 1 and N, inclusive, that do not appear in A, listed in ascending order. The output should be in the following format:
C X_1 X_2 \dots X_C
Sample Input 1
10 3 3 9 2
Sample Output 1
7 1 4 5 6 7 8 10
Here, A=(3,9,2).
The integers between 1 and 10 that do not appear in A, listed in ascending order, are 1,4,5,6,7,8,10.
Sample Input 2
6 6 1 3 5 2 4 6
Sample Output 2
0
No integer between 1 and 6 is missing from A.
In this case, print 0 on the first line and leave the second line empty.
Sample Input 3
9 1 9
Sample Output 3
8 1 2 3 4 5 6 7 8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
1 から N の番号がついた N 人の人がいます。
人 i は数 Q_i が書かれたゼッケンを着けており、人 P_i を見つめています。
i が書かれたゼッケンを着けている人が見つめている人の着けているゼッケンにかかれている数を、i=1,2,\ldots,N のそれぞれについて求めてください。
制約
- 2 \leq N \leq 3\times 10^5
- 1 \leq P_i \leq N
- P_i は相異なる
- 1 \leq Q_i \leq N
- Q_i は相異なる
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \dots P_N Q_1 Q_2 \dots Q_N
出力
i が書かれたゼッケンを着けている人が見つめている人の着けているゼッケンにかかれている数を S_i とする。
S_1,S_2,\ldots,S_N をこの順に空白区切りで出力せよ。
入力例 1
4 4 3 2 1 2 3 1 4
出力例 1
3 4 1 2
人 3 は 1 が書かれたゼッケンを着けており、人 3 が見つめている人 2 は 3 が書かれたゼッケンを着けています。 よって i=1 に対する答えは 3 になります。

入力例 2
10 2 6 4 3 7 8 9 10 1 5 1 4 8 2 10 5 7 3 9 6
出力例 2
4 8 6 5 3 10 9 2 1 7
Score : 300 points
Problem Statement
There are N people numbered from 1 to N.
Person i is wearing a bib with the number Q_i and is staring at person P_i.
For each i = 1,2,\ldots,N, find the number written on the bib of the person that the person wearing the bib with number i is staring at.
Constraints
- 2 \leq N \leq 3\times 10^5
- 1 \leq P_i \leq N
- The values of P_i are distinct.
- 1 \leq Q_i \leq N
- The values of Q_i are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P_1 P_2 \dots P_N Q_1 Q_2 \dots Q_N
Output
Let S_i be the number written on the bib of the person that the person wearing the bib with number i is staring at.
Print S_1, S_2, \ldots, S_N in this order, separated by a single space.
Sample Input 1
4 4 3 2 1 2 3 1 4
Sample Output 1
3 4 1 2
Person 3 is wearing the bib with the number 1, and the person that person 3 is staring at, person 2, is wearing the bib with the number 3. Thus, the answer for i = 1 is 3.

Sample Input 2
10 2 6 4 3 7 8 9 10 1 5 1 4 8 2 10 5 7 3 9 6
Sample Output 2
4 8 6 5 3 10 9 2 1 7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 個のサイコロがあります。 i 番目のサイコロは K_i 個の面をもち、各面にはそれぞれ A_{i,1},A_{i,2},\ldots,A_{i,K_i} が書かれています。このサイコロを振ると、各面がそれぞれ \frac{1}{K_i} の確率で出ます。
あなたは N 個のサイコロから 2 個を選んで振ります。サイコロを適切に選んだときの、2 つのサイコロの出目が等しくなる確率の最大値を求めてください。
制約
- 2 \leq N \leq 100
- 1 \leq K_i
- K_1+K_2+\dots+K_N \leq 10^5
- 1 \leq A_{i,j} \leq 10^5
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N
K_1 A_{1,1} A_{1,2} \dots A_{1,K_1}
\vdots
K_N A_{N,1} A_{N,2} \dots A_{N,K_N}
出力
答えを出力せよ。真の解との相対誤差または絶対誤差が 10^{-8} 以下のとき正解とみなされる。
入力例 1
3 3 1 2 3 4 1 2 2 1 6 1 2 3 4 5 6
出力例 1
0.333333333333333
- 1 番目のサイコロと 2 番目のサイコロを選んで振ったとき、出目が等しくなる確率は \frac{1}{3} です。
- 1 番目のサイコロと 3 番目のサイコロを選んで振ったとき、出目が等しくなる確率は \frac{1}{6} です。
- 2 番目のサイコロと 3 番目のサイコロを選んで振ったとき、出目が等しくなる確率は \frac{1}{6} です。
よって最大値は \frac{1}{3}=0.3333333333\ldots となります。
入力例 2
3 5 1 1 1 1 1 4 2 2 2 2 3 1 1 2
出力例 2
0.666666666666667
Score : 400 points
Problem Statement
There are N dice. The i-th die has K_i faces, with the numbers A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} written on them. When you roll this die, each face appears with probability \frac{1}{K_i}.
You choose two dice from the N dice and roll them. Determine the maximum probability that the two dice show the same number, when the dice are chosen optimally.
Constraints
- 2 \leq N \leq 100
- 1 \leq K_i
- K_1 + K_2 + \dots + K_N \leq 10^5
- 1 \leq A_{i,j} \leq 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
K_1 A_{1,1} A_{1,2} \dots A_{1,K_1}
\vdots
K_N A_{N,1} A_{N,2} \dots A_{N,K_N}
Output
Print the answer. Your answer is considered correct if the absolute or relative error from the true solution does not exceed 10^{-8}.
Sample Input 1
3 3 1 2 3 4 1 2 2 1 6 1 2 3 4 5 6
Sample Output 1
0.333333333333333
- When choosing the 1st and 2nd dice, the probability that the outcomes are the same is \frac{1}{3}.
- When choosing the 1st and 3rd dice, the probability is \frac{1}{6}.
- When choosing the 2nd and 3rd dice, the probability is \frac{1}{6}.
Therefore, the maximum probability is \frac{1}{3} = 0.3333333333\ldots.
Sample Input 2
3 5 1 1 1 1 1 4 2 2 2 2 3 1 1 2
Sample Output 2
0.666666666666667
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
1 から N の番号がついた N 台のサーバーと 1 から M の番号がついた M 本のケーブルがあります。
ケーブル i はサーバー A_i とサーバー B_i を双方向に結んでいます。
以下の操作を何回か( 0 回でもよい)行うことで、全てのサーバー同士がケーブルを介して繋がっているようにしてください。
- 操作:ケーブルを 1 つ選び、その片方の端を別のサーバーに繋ぎ変える
操作の最小回数と、最小回数となる操作列を求めてください。
制約
- 2 \leq N \leq 2\times 10^5
- N-1 \leq M \leq 2\times 10^5
- 1 \leq A_i,B_i \leq N
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
出力
操作回数の最小値を K とする。K+1 行出力せよ。
1 行目には K を出力せよ。
i+1 行目には i 回目の操作で選ぶケーブルの番号、繋ぎ変える前に繋がっていたサーバーの番号、繋ぎ変えたあと繋がっているサーバーの番号をこの順に空白区切りで出力せよ。
条件を満たす解が複数存在する場合、どれを出力しても正解とみなされる。
入力例 1
4 5 1 1 1 2 2 1 3 4 4 4
出力例 1
1 1 1 3
ケーブル 1 のサーバー 1 に繋がっている側の端をサーバー 3 に繋ぎ変えることで、サーバー同士がケーブルを介して繋がっている状態にすることができます。

このほか、「ケーブル 5 のサーバー 4 に繋がっている側の端をサーバー 1 に繋ぎ変える」という操作や、「ケーブル 2 のサーバー 2 に繋がっている側の端をサーバー 3 に繋ぎ変える」という操作でもサーバー同士がケーブルを介して繋がっている状態にすることができるため、正解とみなされます。
入力例 2
4 3 3 4 4 1 1 2
出力例 2
0
操作をする必要がないこともあります。
入力例 3
5 4 3 3 3 3 3 3 3 3
出力例 3
4 1 3 5 2 3 4 3 3 2 4 3 1
Score : 450 points
Problem Statement
There are N servers numbered from 1 to N and M cables numbered from 1 to M.
Cable i connects servers A_i and B_i bidirectionally.
By performing the following operation some number of times (possibly zero), make all servers connected via cables.
- Operation: Choose one cable and reconnect one of its ends to a different server.
Find the minimum number of operations required and output an operation sequence achieving this minimum.
Constraints
- 2 \leq N \leq 2\times 10^5
- N-1 \leq M \leq 2\times 10^5
- 1 \leq A_i, B_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
Output
Let the minimum number of operations be K. Print K+1 lines.
- The first line should contain K.
- The (i+1)-th line should contain three space-separated integers: the number of the cable chosen in the i-th operation, the server number that was originally connected at that end, and the server number to which it is connected after the operation, in this order.
If there are multiple valid solutions, any one of them will be accepted.
Sample Input 1
4 5 1 1 1 2 2 1 3 4 4 4
Sample Output 1
1 1 1 3
By reconnecting the end of cable 1 that is connected to server 1 to server 3, the servers can be connected via cables.

Operations such as reconnecting the end of cable 5 that is connected to server 4 to server 1, or reconnecting the end of cable 2 that is connected to server 2 to server 3, will also result in all servers being connected and are considered correct.
Sample Input 2
4 3 3 4 4 1 1 2
Sample Output 2
0
No operation may be necessary.
Sample Input 3
5 4 3 3 3 3 3 3 3 3
Sample Output 3
4 1 3 5 2 3 4 3 3 2 4 3 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
空の配列 A があります。 i=1,2,\ldots,N の順に以下の操作を行います。
- 数 i を、A の前から P_i 番目の位置になるように挿入する。
- より正確には、「A の P_i-1 項目まで」「 i 」「A の P_i 項目以降」をこの順に連結したもので A を置き換える。
全ての操作を終えた後の A を出力してください。
制約
- 1 \leq N \leq 5\times 10^5
- 1 \leq P_i \leq i
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \ldots P_N
出力
全ての操作を終えた後の A を (A_1,\ldots,A_N) とするとき、A_1,\ldots,A_N をこの順に空白区切りで出力せよ。
入力例 1
4 1 1 2 1
出力例 1
4 2 3 1
操作は以下のように行われます。
- 数 1 を、A の前から 1 番目の位置になるように挿入する。 A=(1) となる。
- 数 2 を、A の前から 1 番目の位置になるように挿入する。 A=(2,1) となる。
- 数 3 を、A の前から 2 番目の位置になるように挿入する。 A=(2,3,1) となる。
- 数 4 を、A の前から 1 番目の位置になるように挿入する。 A=(4,2,3,1) となる。
入力例 2
5 1 2 3 4 5
出力例 2
1 2 3 4 5
Score : 500 points
Problem Statement
There is an empty array A. For i = 1,2,\ldots,N, perform the following operation in order:
- Insert the number i into A so that it becomes the P_i-th element from the beginning.
- More precisely, replace A with the concatenation of the first P_i-1 elements of A, then i, then the remaining elements of A starting from the P_i-th element, in this order.
Output the final array A after all operations have been completed.
Constraints
- 1 \leq N \leq 5\times 10^5
- 1 \leq P_i \leq i
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P_1 P_2 \ldots P_N
Output
Let the final array be A = (A_1, A_2, \ldots, A_N). Print A_1, A_2, \ldots, A_N in this order, separated by spaces.
Sample Input 1
4 1 1 2 1
Sample Output 1
4 2 3 1
The operations are performed as follows:
- Insert the number 1 so that it becomes the 1st element of A. Now, A = (1).
- Insert the number 2 so that it becomes the 1st element of A. Now, A = (2, 1).
- Insert the number 3 so that it becomes the 2nd element of A. Now, A = (2, 3, 1).
- Insert the number 4 so that it becomes the 1st element of A. Now, A = (4, 2, 3, 1).
Sample Input 2
5 1 2 3 4 5
Sample Output 2
1 2 3 4 5
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 600 点
問題文
整数 A,B,C ( A<B<C ) が B-A = C-B を満たす時、 (A,B,C) を 良い三つ組 と呼びます。
N 要素からなる正整数の集合 S=\{ S_1,S_2,\dots,S_N \} が与えられるので、 A,B,C \in S なる良い三つ組の個数を求めてください。
制約
- 入力は全て整数
- 1 \le N \le 10^6
- 1 \le S_i \le 10^6
- S の要素は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \dots S_N
出力
答えを整数として出力せよ。
入力例 1
5 8 3 1 5 2
出力例 1
3
S=\{8,3,1,5,2\} です。数えるべき良い三つ組は以下の 3 つです。
- (1,2,3)
- (1,3,5)
- (2,5,8)
入力例 2
7 300000 100000 499998 499999 200000 400000 500000
出力例 2
5
入力例 3
10 13 1 16 15 12 4 7 10 2 19
出力例 3
10
Score : 600 points
Problem Statement
For integers A, B, C ( A < B < C ), if they satisfy B-A = C-B, then (A, B, C) is called a fine triplet.
You are given a set of N distinct positive integers S = \{ S_1, S_2, \dots, S_N \}. Find the number of fine triplets (A, B, C) with A, B, C \in S.
Constraints
- All input values are integers.
- 1 \le N \le 10^6
- 1 \le S_i \le 10^6
- The elements of S are distinct.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \dots S_N
Output
Print the number of fine triplets as an integer.
Sample Input 1
5 8 3 1 5 2
Sample Output 1
3
Here, S = \{8,3,1,5,2\}. The fine triplets to be counted are the following three:
- (1,2,3)
- (1,3,5)
- (2,5,8)
Sample Input 2
7 300000 100000 499998 499999 200000 400000 500000
Sample Output 2
5
Sample Input 3
10 13 1 16 15 12 4 7 10 2 19
Sample Output 3
10