Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
文字列 S,T が与えられます。S は英小文字からなり、T は S に英小文字を 1 つ挿入して作られたことがわかっています。
挿入された文字は T の先頭から何番目の文字であるか求めてください。
複数の候補が考えられる場合はいずれか 1 つを求めてください。
制約
- 1 \leq |S| \leq 5\times 10^5
- S は英小文字からなる
- T は S に英小文字を 1 つ挿入して作られた文字列である
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
答えを出力せよ。なお、答えが複数考えられる場合はどれを出力しても正解となる。
入力例 1
atcoder atcorder
出力例 1
5
T の先頭から 5 番目の文字 r が挿入された文字です。
入力例 2
million milllion
出力例 2
5
T の先頭から 3,4,5 番目の文字のいずれかが挿入された文字です。
よって、3,4,5 のいずれかを出力すると正解となります。
入力例 3
vvwvw vvvwvw
出力例 3
3
Score : 300 points
Problem Statement
You are given strings S and T. S consists of lowercase English letters, and T is obtained by inserting a lowercase English letter into S.
Find the position of the inserted character in T.
If there are multiple candidates, find any of them.
Constraints
- 1 \leq |S| \leq 5\times 10^5
- S consists of lowercase English letters.
- T is obtained by inserting a lowercase English letter into S.
Input
The input is given from Standard Input in the following format:
S T
Output
Print an integer i, representing that the inserted character is the i-th character from the beginning of T. If there are multiple possible answers, printing any of them is accepted.
Sample Input 1
atcoder atcorder
Sample Output 1
5
The 5-th character from the beginning of T, r, is inserted.
Sample Input 2
million milllion
Sample Output 2
5
One of the 3-rd, 4-th, and 5-th characters from the beginning of T is inserted. Thus, printing any one of 3, 4, and 5 is accepted.
Sample Input 3
vvwvw vvvwvw
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
列 S_n を次のように定義します。
- S_1 は 1 つの 1 からなる長さ 1 の列である。
- S_n (n は 2 以上の整数) は S_{n-1}, n, S_{n-1} をこの順につなげた列である。
たとえば S_2,S_3 は次のような列です。
- S_2 は S_1, 2, S_1 をこの順につなげた列なので 1,2,1 である。
- S_3 は S_2, 3, S_2 をこの順につなげた列なので 1,2,1,3,1,2,1 である。
N が与えられるので、列 S_N をすべて出力してください。
制約
- N は整数
- 1 \leq N \leq 16
入力
入力は以下の形式で標準入力から与えられる。
N
出力
S_N を空白区切りで出力せよ。
入力例 1
2
出力例 1
1 2 1
問題文の説明にある通り、S_2 は 1,2,1 となります。
入力例 2
1
出力例 2
1
入力例 3
4
出力例 3
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
S_4 は S_3,4,S_3 をこの順につなげた列です。
Score : 300 points
Problem Statement
We define sequences S_n as follows.
- S_1 is a sequence of length 1 containing a single 1.
- S_n (n is an integer greater than or equal to 2) is a sequence obtained by concatenating S_{n-1}, n, S_{n-1} in this order.
For example, S_2 and S_3 is defined as follows.
- S_2 is a concatenation of S_1, 2, and S_1, in this order, so it is 1,2,1.
- S_3 is a concatenation of S_2, 3, and S_2, in this order, so it is 1,2,1,3,1,2,1.
Given N, print the entire sequence S_N.
Constraints
- N is an integer.
- 1 \leq N \leq 16
Input
Input is given from Standard Input in the following format:
N
Output
Print S_N, with spaces in between.
Sample Input 1
2
Sample Output 1
1 2 1
As described in the Problem Statement, S_2 is 1,2,1.
Sample Input 2
1
Sample Output 2
1
Sample Input 3
4
Sample Output 3
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
- S_4 is a concatenation of S_3, 4, and S_3, in this order.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
N \times N のマス目があります。上から i 行目、左から j 列目のマスを (i,j) と表します。
始め、(1,1) に駒が 1 個置かれています。あなたは以下の操作を何度でも行うことができます。
- 今駒が置かれているマスと距離がちょうど \sqrt{M} であるマスに駒を移動させる。
ここで、マス (i,j) とマス (k,l) の距離は \sqrt{(i-k)^2+(j-l)^2} とします。
全てのマス (i,j) に対して、以下の問題を解いてください。
- 駒を (1,1) から (i,j) に移動させることができるかを判定し、できる場合は操作回数の最小値を求めてください。
制約
- 1 \le N \le 400
- 1 \le M \le 10^6
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
N 行出力せよ。i 行目には N 個の整数を出力せよ。i 行目の j 個目の出力は、マス (i,j) に駒を移動させることができるのであれば操作回数の最小値を、そうでないのであれば -1 を出力せよ。
入力例 1
3 1
出力例 1
0 1 2 1 2 3 2 3 4
駒は上下左右の 4 個のマスに移動することができます。
例えば (2,2) に移動するには、以下のように 2 回の操作を行えばよいです。
- 今駒は (1,1) に置かれている。(1,1) と (1,2) の距離はちょうど \sqrt{1} であるため、駒を (1,2) に移動させる。
- 今駒は (1,2) に置かれている。(1,2) と (2,2) の距離はちょうど \sqrt{1} であるため、駒を (2,2) に移動させる。
入力例 2
10 5
出力例 2
0 3 2 3 2 3 4 5 4 5 3 4 1 2 3 4 3 4 5 6 2 1 4 3 2 3 4 5 4 5 3 2 3 2 3 4 3 4 5 6 2 3 2 3 4 3 4 5 4 5 3 4 3 4 3 4 5 4 5 6 4 3 4 3 4 5 4 5 6 5 5 4 5 4 5 4 5 6 5 6 4 5 4 5 4 5 6 5 6 7 5 6 5 6 5 6 5 6 7 6
Score : 400 points
Problem Statement
There is a grid with N \times N squares. We denote by (i, j) the square at the i-th row from the top and j-th column from the left.
Initially, a piece is placed on (1, 1). You may repeat the following operation any number of times:
- Let (i, j) be the square the piece is currently on. Move the piece to the square whose distance from (i, j) is exactly \sqrt{M}.
Here, we define the distance between square (i, j) and square (k, l) as \sqrt{(i-k)^2+(j-l)^2}.
For all squares (i, j), determine if the piece can reach (i, j). If it can, find the minimum number of operations required to do so.
Constraints
- 1 \le N \le 400
- 1 \le M \le 10^6
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M
Output
Print N lines. The i-th line should contain N integers. If the piece can reach (i, j), the j-th integer in the i-th line should be the minimum number of operations required to do so; otherwise, it should be -1.
Sample Input 1
3 1
Sample Output 1
0 1 2 1 2 3 2 3 4
You can move the piece to four adjacent squares.
For example, we can move the piece to (2,2) with two operations as follows.
- The piece is now on (1,1). The distance between (1,1) and (1,2) is exactly \sqrt{1}, so move the piece to (1,2).
- The piece is now on (1,2). The distance between (1,2) and (2,2) is exactly \sqrt{1}, so move the piece to (2,2).
Sample Input 2
10 5
Sample Output 2
0 3 2 3 2 3 4 5 4 5 3 4 1 2 3 4 3 4 5 6 2 1 4 3 2 3 4 5 4 5 3 2 3 2 3 4 3 4 5 6 2 3 2 3 4 3 4 5 4 5 3 4 3 4 3 4 5 4 5 6 4 3 4 3 4 5 4 5 6 5 5 4 5 4 5 4 5 6 5 6 4 5 4 5 4 5 6 5 6 7 5 6 5 6 5 6 5 6 7 6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
以下の条件を満たす k+1 頂点 k 辺のグラフをレベル k\ (k\geq 2) の星と呼びます。
- ある 1 つの頂点が、他の k 個の頂点と 1 本ずつ辺で結ばれている。それ以外の辺は存在しない。
高橋君は、はじめ何個かの星からなるグラフを持っていました。そして、以下の手続きを全てのグラフの頂点が連結になるまでくり返し行いました。
- 持っているグラフの頂点から二つの頂点を選ぶ。このとき、選んだ二つの頂点の次数は共に 1 であり、かつ選んだ二つの頂点は非連結である必要がある。選んだ二つの頂点を結ぶ辺を張る。
その後、高橋君は手続きが終了した後のグラフの頂点に、適当に 1 から N の番号を付けました。このグラフは木となっており、これを T と呼びます。T には N-1 本の辺があり、 i 番目の辺は u_i と v_i を結んでいました。
その後高橋君は、はじめ持っていた星の個数とレベルを忘れてしまいました。T の情報からはじめ持っていた星の個数とレベルを求めてください。
制約
- 3\leq N\leq 2\times 10^5
- 1\leq u_i, v_i\leq N
- 与えられるグラフは、問題文中の手続きによって得られる N 頂点の木
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
u_1 v_1
\vdots
u_{N-1} v_{N-1}
出力
高橋君が持っていた星が M 個であり、星のレベルがそれぞれ L=(L_1,L_2,\ldots,L_M) であったとする。 このとき、L を昇順に並び替え空白区切りで出力せよ。
なお、この問題では常に解が一意に定まることが証明できる。
入力例 1
6 1 2 2 3 3 4 4 5 5 6
出力例 1
2 2
以下の図のように、2 つのレベル 2 の星から T は得られます。

入力例 2
9 3 9 7 8 8 6 4 6 4 1 5 9 7 3 5 2
出力例 2
2 2 2
入力例 3
20 8 3 8 18 2 19 8 20 9 17 19 7 8 7 14 12 2 15 14 10 2 13 2 16 2 1 9 5 10 15 14 6 2 4 2 11 5 12
出力例 3
2 3 4 7
Score : 475 points
Problem Statement
A graph with (k+1) vertices and k edges is called a level-k\ (k\geq 2) star if and only if:
- it has a vertex that is connected to each of the other k vertices with an edge, and there are no other edges.
At first, Takahashi had a graph consisting of stars. He repeated the following operation until every pair of vertices in the graph was connected:
- choose two vertices in the graph. Here, the vertices must be disconnected, and their degrees must be both 1. Add an edge that connects the chosen two vertices.
He then arbitrarily assigned an integer from 1 through N to each of the vertices in the graph after the procedure. The resulting graph is a tree; we call it T. T has (N-1) edges, the i-th of which connects u_i and v_i.
Takahashi has now forgotten the number and levels of the stars that he initially had. Find them, given T.
Constraints
- 3\leq N\leq 2\times 10^5
- 1\leq u_i, v_i\leq N
- The given graph is an N-vertex tree obtained by the procedure in the problem statement.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N
u_1 v_1
\vdots
u_{N-1} v_{N-1}
Output
Suppose that Takahashi initially had M stars, whose levels were L=(L_1,L_2,\ldots,L_M). Sort L in ascending order, and print them with spaces in between.
We can prove that the solution is unique in this problem.
Sample Input 1
6 1 2 2 3 3 4 4 5 5 6
Sample Output 1
2 2
Two level-2 stars yield T, as the following figure shows:

Sample Input 2
9 3 9 7 8 8 6 4 6 4 1 5 9 7 3 5 2
Sample Output 2
2 2 2
Sample Input 3
20 8 3 8 18 2 19 8 20 9 17 19 7 8 7 14 12 2 15 14 10 2 13 2 16 2 1 9 5 10 15 14 6 2 4 2 11 5 12
Sample Output 3
2 3 4 7
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
円周上に 2N 個の点が等間隔に並んでおり、ある点から始めて時計回りに 1,2,\dots,2N の番号が付けられています。
これらの点を結ぶ M 本の線分 1,2,\dots,M が存在し、線分 i は点 A_i と点 B_i を結んでいます。 ここで、A_i と B_i は相異なる 偶数 です。 また、これらの線分のうちのどの 2 つの線分も共有点を持たないことが保証されます。
Q 個のクエリを処理してください。j 番目のクエリは以下の通りです。
- 相異なる 2 つの 奇数 C_j, D_j が与えられる。上で与えられた M 本の線分 1,2,\dots,M のうち、点 C_j と点 D_j を結ぶ線分と共有点を持つものの本数を求めよ。
制約
- 2\leq N \leq 10^6
- 1\leq M \leq \min\left(\lfloor\frac{N}{2}\rfloor, 2\times 10^5\right)
- 1\leq Q \leq 2\times 10^5
- 1\leq A_i< B_i\leq 2N
- 1\leq C_j< D_j\leq 2N
- A_i, B_i は偶数
- C_j, D_j は奇数
- どの i_1, i_2\ (i_1 \neq i_2) についても、線分 i_1 と線分 i_2 は共有点を持たない
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_M B_M Q C_1 D_1 C_2 D_2 \vdots C_Q D_Q
出力
Q 行出力せよ。 j\ (1\leq j \leq Q) 行目には、j 番目のクエリに対する答えを出力せよ。
入力例 1
4 2 2 4 6 8 3 1 3 3 7 1 5
出力例 1
1 2 0

上図は入力例 1 を図示したものであり、黒丸は 2N\ (=8) 個の点を、青い実線は最初に与えられる M\ (=2) 本の線分を、赤い点線はクエリで与えられる Q\ (=3) 本の線分をそれぞれ表しています。
- 1 番目のクエリについて、最初に与えられた M 本の線分のうち、点 1 と点 3 を結ぶ線分と共有点を持つものは線分 1 の 1 本です。
- 2 番目のクエリについて、最初に与えられた M 本の線分のうち、点 3 と点 7 を結ぶ線分と共有点を持つものは線分 1,2 の 2 本です。
- 3 番目のクエリについて、最初に与えられた M 本の線分のうち、点 1 と点 5 を結ぶ線分と共有点を持つものは 0 本です。
入力例 2
20 7 24 34 26 28 18 38 2 14 8 12 30 32 20 22 10 7 29 31 39 9 21 19 29 15 21 11 39 17 21 15 31 5 25 25 31
出力例 2
3 3 4 1 2 2 2 3 3 1
Score : 525 points
Problem Statement
On a circle, 2N points are placed at equal intervals and numbered 1,2,\dots,2N in clockwise order starting from an arbitrary point.
There are M line segments numbered 1,2,\dots,M connecting these points. Segment i connects points A_i and B_i. Here, A_i and B_i are distinct even numbers. It is guaranteed that no two of these segments share a point.
Process Q queries. The j‑th query is as follows:
- You are given two distinct odd numbers C_j and D_j. Among the M segments 1,2,\dots,M, find how many share a point with the segment connecting points C_j and D_j.
Constraints
- 2 \le N \le 10^6
- 1\leq M \leq \min\left(\lfloor\frac{N}{2}\rfloor, 2\times 10^5\right)
- 1 \le Q \le 2 \times 10^5
- 1 \le A_i < B_i \le 2N
- 1 \le C_j < D_j \le 2N
- A_i and B_i are even.
- C_j and D_j are odd.
- For any i_1 and i_2 (i_1 \neq i_2), segments i_1 and i_2 do not share a point.
- 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 Q C_1 D_1 C_2 D_2 \vdots C_Q D_Q
Output
Output Q lines. The j-th line (1\leq j \leq Q) should contain the answer to the j‑th query.
Sample Input 1
4 2 2 4 6 8 3 1 3 3 7 1 5
Sample Output 1
1 2 0

The figure above illustrates Sample Input 1. Black dots are the 2N\ (=8) points, blue solid lines are the initial M\ (=2) segments, and red dashed lines are the Q\ (=3) query segments.
- For the first query, the segment connecting points 1 and 3 intersects one initial segment: segment 1.
- For the second query, the segment connecting points 3 and 7 intersects two initial segments: segments 1 and 2.
- For the third query, the segment connecting points 1 and 5 intersects zero initial segments.
Sample Input 2
20 7 24 34 26 28 18 38 2 14 8 12 30 32 20 22 10 7 29 31 39 9 21 19 29 15 21 11 39 17 21 15 31 5 25 25 31
Sample Output 2
3 3 4 1 2 2 2 3 3 1