Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は N 頂点の木を持っています。頂点には 1 から N の番号が付けられています。木には N - 1 本の辺があり、辺にも 1 から N-1 の番号が付けられています。i 番目 (1 \leq i \leq N-1) の辺は頂点 u_i と頂点 v_i を結んでおり、正の整数の 重み w_i が定められています。
高橋君は、各辺 i に正の整数(1 以上の整数)の 乗数 r_i を設定しようとしています。辺 i に乗数 r_i を設定したとき、その辺の コスト は w_i \times r_i です。
乗数の設定は、以下の条件を満たさなければなりません:
- 木の任意の相異なる 2 頂点の組 \{a, b\} について、頂点 a から頂点 b への(木上で一意に定まる)パスに含まれる辺の本数を k とし、そのパスが順に辺番号 e_1, e_2, \ldots, e_k を通るとする。このとき、
r_{e_1} + r_{e_2} + \cdots + r_{e_k} \leq k^2
が成り立たなければならない。
条件を満たす乗数の設定は必ず存在します。例えば、すべての辺の乗数を 1 に設定すると、任意のパスについて乗数の合計は k であり、k \geq 1 のとき k \leq k^2 が成り立つため、条件を満たします。
条件を満たすすべての設定の中で、全辺のコストの合計
\sum_{i=1}^{N-1} w_i \times r_i
の最小値を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- 1 \leq w_i \leq 10^9
- 入力で与えられるグラフは木である
- 入力はすべて整数である
- 答えは 2^{63} - 1 以下である
入力
N
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_{N-1} v_{N-1} w_{N-1}
- 1 行目には、頂点の数を表す整数 N が与えられる。
- 続く N - 1 行のうち i 行目 (1 \leq i \leq N-1) では、i 番目の辺が結ぶ 2 つの頂点の番号 u_i, v_i と重み w_i が空白区切りで与えられる。
出力
条件を満たすように各辺の乗数(正の整数)を設定したとき、全辺のコストの合計の最小値を 1 行で出力せよ。
入力例 1
4 1 2 3 2 3 5 2 4 2
出力例 1
10
入力例 2
5 1 2 10 1 3 1 1 4 7 1 5 4
出力例 2
22
入力例 3
12 1 2 8 1 3 6 2 4 15 2 5 3 3 6 20 3 7 5 4 8 11 5 9 9 6 10 2 7 11 14 7 12 1
出力例 3
94
入力例 4
30 1 2 100 1 3 200 2 4 50 2 5 75 3 6 125 3 7 60 4 8 90 4 9 30 5 10 45 5 11 85 6 12 110 6 13 95 7 14 40 7 15 70 8 16 150 9 17 20 10 18 130 11 19 55 12 20 65 13 21 35 14 22 25 15 23 115 16 24 105 17 25 10 18 26 140 19 27 80 20 28 120 21 29 5 22 30 160
出力例 4
2390
入力例 5
2 1 2 1000000000
出力例 5
1000000000
Score : 200 pts
Problem Statement
Takahashi has a tree with N vertices. The vertices are numbered 1 to N. The tree has N - 1 edges, also numbered 1 to N-1. The i-th edge (1 \leq i \leq N-1) connects vertex u_i and vertex v_i, and has a positive integer weight w_i.
Takahashi wants to assign a positive integer (an integer \geq 1) multiplier r_i to each edge i. When a multiplier r_i is assigned to edge i, the cost of this edge is w_i \times r_i.
The assignment of multipliers must satisfy the following condition:
- For any pair of distinct vertices \{a, b\} in the tree, let k be the number of edges on the (unique) path between vertex a and vertex b, and let the path traverse edges e_1, e_2, \ldots, e_k in order. Then,
r_{e_1} + r_{e_2} + \cdots + r_{e_k} \leq k^2
must hold.
An assignment satisfying the condition always exists. For example, if we set the multipliers of all edges to 1, the sum of multipliers for any path of length k is k, and since k \leq k^2 holds for k \geq 1, this assignment satisfies the condition.
Find the minimum possible total cost of all edges,
\sum_{i=1}^{N-1} w_i \times r_i
among all assignments that satisfy the condition.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- 1 \leq w_i \leq 10^9
- The given graph is a tree.
- All input values are integers.
- The answer is at most 2^{63} - 1.
Input
N
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_{N-1} v_{N-1} w_{N-1}
- The first line contains an integer N, representing the number of vertices.
- In the following N - 1 lines, the i-th line (1 \leq i \leq N-1) contains three space-separated integers u_i, v_i, and w_i, representing the vertices u_i and v_i connected by the i-th edge, and its weight w_i.
Output
Print the minimum possible total cost of all edges when the multipliers (positive integers) are assigned to satisfy the condition, in a single line.
Sample Input 1
4 1 2 3 2 3 5 2 4 2
Sample Output 1
10
Sample Input 2
5 1 2 10 1 3 1 1 4 7 1 5 4
Sample Output 2
22
Sample Input 3
12 1 2 8 1 3 6 2 4 15 2 5 3 3 6 20 3 7 5 4 8 11 5 9 9 6 10 2 7 11 14 7 12 1
Sample Output 3
94
Sample Input 4
30 1 2 100 1 3 200 2 4 50 2 5 75 3 6 125 3 7 60 4 8 90 4 9 30 5 10 45 5 11 85 6 12 110 6 13 95 7 14 40 7 15 70 8 16 150 9 17 20 10 18 130 11 19 55 12 20 65 13 21 35 14 22 25 15 23 115 16 24 105 17 25 10 18 26 140 19 27 80 20 28 120 21 29 5 22 30 160
Sample Output 4
2390
Sample Input 5
2 1 2 1000000000
Sample Output 5
1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
ある学校のクラスには N 人の生徒がいます。生徒には 1 から N までの出席番号が付けられています。
今日の体育の授業では、出席番号の昇順に 1 列に並んでいます。すなわち、出席番号 1 の生徒が列の先頭(最も前)にいて、出席番号 N の生徒が列の最後尾にいます。先生は、身長が T cm 以上の生徒を「高身長グループ」に指定しました。
並んでいる生徒のうち、自分より前(自分より出席番号が小さい位置)に、高身長グループに属し、かつ自分より厳密に身長が高い生徒が 1 人でもいる場合、その生徒は「見上げる生徒」と呼ばれます。見上げる生徒であるかの判定において、その生徒自身が高身長グループに属している必要はありません。また、列の先頭の生徒(出席番号 1)は、自分より前に誰もいないため、見上げる生徒にはなりません。
より形式的に述べると、生徒 i の身長を S_i cm としたとき、生徒 i(1 \leq i \leq N)が見上げる生徒であるための条件は以下の通りです:
- 1 \leq j < i かつ S_j \geq T かつ S_j > S_i を満たす生徒 j が少なくとも 1 人存在する。
見上げる生徒の人数を求めてください。
制約
- 1 \leq N \leq 10^6
- 1 \leq T \leq 300
- 1 \leq S_i \leq 300(1 \leq i \leq N)
- 入力はすべて整数である。
入力
N T S_1 S_2 \ldots S_N
- 1 行目には、生徒の人数を表す整数 N と、高身長グループの閾値を表す整数 T(cm)が、スペース区切りで与えられる。
- 2 行目には、出席番号 1 から N の各生徒の身長を表す整数 S_1, S_2, \ldots, S_N(cm)が、スペース区切りで与えられる。
出力
見上げる生徒の人数を 1 行で出力せよ。
入力例 1
5 170 180 160 175 150 165
出力例 1
4
入力例 2
4 180 160 165 170 175
出力例 2
0
入力例 3
10 165 150 170 160 180 155 165 190 140 200 195
出力例 3
5
入力例 4
20 160 155 162 158 170 165 150 168 172 160 145 180 175 153 167 159 190 140 185 163 171
出力例 4
14
入力例 5
1 100 150
出力例 5
0
Score : 300 pts
Problem Statement
A class in a certain school has N students. The students are assigned attendance numbers from 1 to N.
In today's physical education class, they are lined up in a single row in ascending order of attendance numbers. That is, the student with attendance number 1 is at the front of the line (the foremost position), and the student with attendance number N is at the end of the line. The teacher designated students whose height is at least T cm as the "tall group."
Among the students in the line, if there exists at least one student ahead of them (at a position with a smaller attendance number) who belongs to the tall group and is strictly taller than them, that student is called a "looking-up student." To determine whether a student is a looking-up student, the student themselves does not need to belong to the tall group. Also, the student at the front of the line (attendance number 1) cannot be a looking-up student, since there is no one ahead of them.
More formally, letting S_i cm be the height of student i, the condition for student i (1 \leq i \leq N) to be a looking-up student is as follows:
- There exists at least one student j satisfying 1 \leq j < i, S_j \geq T, and S_j > S_i.
Find the number of looking-up students.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq T \leq 300
- 1 \leq S_i \leq 300 (1 \leq i \leq N)
- All input values are integers.
Input
N T S_1 S_2 \ldots S_N
- The first line contains the integer N representing the number of students and the integer T (cm) representing the threshold for the tall group, separated by a space.
- The second line contains the integers S_1, S_2, \ldots, S_N (cm) representing the heights of the students with attendance numbers 1 through N, separated by spaces.
Output
Output the number of looking-up students in a single line.
Sample Input 1
5 170 180 160 175 150 165
Sample Output 1
4
Sample Input 2
4 180 160 165 170 175
Sample Output 2
0
Sample Input 3
10 165 150 170 160 180 155 165 190 140 200 195
Sample Output 3
5
Sample Input 4
20 160 155 162 158 170 165 150 168 172 160 145 180 175 153 167 159 190 140 185 163 171
Sample Output 4
14
Sample Input 5
1 100 150
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は、配送センターのマネージャーを務めています。このセンターには N 人の配達員がおり、それぞれ 1 から N の番号が付けられています。各配達員 i には、運搬できる荷物の最大重量 S_i が決まっています。
今日、M 件の配達依頼が届きました。各依頼 j の荷物の重量は D_j です。
高橋君は、配達員たちにできるだけ多くの依頼を割り当てたいと考えています。割り当ては以下のルールに従います。
- 各依頼は高々 1 人の配達員に割り当てられる。どの配達員にも割り当てられない依頼があってもよい。
- 各配達員は高々 1 件の依頼しか担当できない。どの依頼も担当しない配達員がいてもよい。
- 依頼 j を配達員 i に割り当てるには、配達員の最大重量が荷物の重量以上、すなわち S_i \geq D_j でなければならない。
これらのルールに従うすべての割り当て方の中で、割り当てられる依頼の件数の最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq D_j \leq 10^9 (1 \leq j \leq M)
- 入力はすべて整数である。
入力
N M S_1 S_2 \ldots S_N D_1 D_2 \ldots D_M
- 1 行目には、配達員の人数 N と依頼の件数 M が、スペース区切りで与えられる。
- 2 行目には、各配達員が運搬できる最大重量 S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。
- 3 行目には、各依頼の荷物の重量 D_1, D_2, \ldots, D_M が、スペース区切りで与えられる。
出力
割り当てられる依頼の件数の最大値を 1 行で出力せよ。
入力例 1
3 3 5 3 8 4 6 2
出力例 1
3
入力例 2
3 3 1 2 3 10 20 30
出力例 2
0
入力例 3
10 8 2 5 8 1 10 3 7 6 4 9 3 6 1 8 5 11 7 2
出力例 3
7
入力例 4
15 12 14 3 7 25 11 9 30 5 18 2 22 8 16 12 20 6 10 15 28 4 19 13 1 24 17 21 35
出力例 4
11
入力例 5
1 1 1 1
出力例 5
1
Score : 366 pts
Problem Statement
Takahashi is the manager of a delivery center. The center has N delivery workers, numbered from 1 to N. Each delivery worker i has a maximum weight S_i that they can carry.
Today, M delivery requests have arrived. The weight of the package for each request j is D_j.
Takahashi wants to assign as many requests as possible to the delivery workers. The assignment must follow these rules:
- Each request is assigned to at most 1 delivery worker. Some requests may remain unassigned.
- Each delivery worker can handle at most 1 request. Some delivery workers may not handle any request.
- To assign request j to delivery worker i, the delivery worker's maximum weight must be at least the weight of the package, i.e., S_i \geq D_j.
Among all assignments that follow these rules, find the maximum number of requests that can be assigned.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq D_j \leq 10^9 (1 \leq j \leq M)
- All input values are integers.
Input
N M S_1 S_2 \ldots S_N D_1 D_2 \ldots D_M
- The first line contains the number of delivery workers N and the number of requests M, separated by a space.
- The second line contains the maximum weights S_1, S_2, \ldots, S_N that each delivery worker can carry, separated by spaces.
- The third line contains the package weights D_1, D_2, \ldots, D_M for each request, separated by spaces.
Output
Print the maximum number of requests that can be assigned, on a single line.
Sample Input 1
3 3 5 3 8 4 6 2
Sample Output 1
3
Sample Input 2
3 3 1 2 3 10 20 30
Sample Output 2
0
Sample Input 3
10 8 2 5 8 1 10 3 7 6 4 9 3 6 1 8 5 11 7 2
Sample Output 3
7
Sample Input 4
15 12 14 3 7 25 11 9 30 5 18 2 22 8 16 12 20 6 10 15 28 4 19 13 1 24 17 21 35
Sample Output 4
11
Sample Input 5
1 1 1 1
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は、全長 T km の高速道路の管理を担当しています。安全性を保つため、道路上にいくつかの点検区間を設置する必要があります。
高速道路の起点を地点 0 km、終点を地点 T km とします。
点検区間は、整数 s(0 \leq s かつ s + K \leq T)を選び、開区間 (s, s+K) として設置します。すなわち、地点 s km から地点 (s+K) km までの長さ K km の区間です。点検区間は 0 個以上いくつでも設置でき、異なる点検区間同士が重なっても構いません。
道路上には N 個のイベント会場があり、i 番目のイベント会場は開区間 (L_i, R_i)(地点 L_i km から地点 R_i km まで)を使用しています。ある点検区間 (s, s+K) がイベント会場 (L_i, R_i) と重なるとは、これら二つの開区間の共通部分が空でないことを指します。一つのイベント会場が複数の点検区間と重なっていても、影響を受けるイベント会場としては 1 個と数えます。
安全基準を満たすため、点検区間の配置は以下の間隔制約を満たさなければなりません。設置した点検区間の個数を p(p \geq 0)とし、それらの開始地点を昇順に s_1 \leq s_2 \leq \cdots \leq s_p とします。このとき、以下のすべてが成り立つ必要があります。
p = 0 の場合:
- T \leq M
p \geq 1 の場合:
- s_1 \leq M
- s_{j+1} - (s_j + K) \leq M(1 \leq j \leq p-1)
- T - (s_p + K) \leq M
1 番目の条件は、起点 0 km から最初の点検区間の開始地点までが M km 以下であること、2 番目の条件は、j 番目の点検区間の終了地点 s_j + K から j+1 番目の点検区間の開始地点 s_{j+1} までが M km 以下であること、3 番目の条件は、最後の点検区間の終了地点 s_p + K から終点 T km までが M km 以下であることを意味します。なお、2 番目の条件において点検区間同士が重なる場合は左辺が負になり、条件は自動的に満たされます。
直感的には、道路上のどの地点をとっても、そこから M km 以内にいずれかの点検区間が存在するように配置する必要がある、ということです。制約より M \geq K が保証されるため、例えば道路全体を隙間なく点検区間で覆うことが可能であり、間隔制約を満たす配置は常に存在します。
高橋君は、間隔制約を満たしながら点検区間を配置し、いずれかの点検区間と重なるイベント会場の数を最小化したいと考えています。その最小値を求めてください。
制約
- 1 \leq T \leq 1000
- 0 \leq N \leq 12
- 1 \leq K \leq M \leq T
- 0 \leq L_i < R_i \leq T(1 \leq i \leq N)
- 入力はすべて整数
入力
T N K M L_1 R_1 L_2 R_2 \vdots L_N R_N
- 1 行目には、高速道路の全長を表す T、イベント会場の数を表す N、点検区間の長さを表す K、間隔制約における距離の上限を表す M が、スペース区切りで与えられる。
- 2 行目から N+1 行目では、各イベント会場の区間を表す L_i と R_i が、スペース区切りで与えられる。
- 1+i 行目には、i 番目のイベント会場の開始地点 L_i と終了地点 R_i が与えられる。
出力
間隔制約を満たす点検区間の配置において、いずれかの点検区間と重なるイベント会場の数の最小値を 1 行で出力せよ。
入力例 1
10 3 2 3 1 2 4 6 8 9
出力例 1
0
入力例 2
6 2 2 6 1 3 4 6
出力例 2
0
入力例 3
50 8 5 9 0 4 6 10 12 18 19 23 25 31 33 37 39 45 46 50
出力例 3
4
入力例 4
200 12 17 30 0 12 15 28 31 45 48 63 70 82 85 101 105 119 123 140 142 158 160 176 178 190 191 200
出力例 4
5
入力例 5
1 0 1 1
出力例 5
0
Score : 400 pts
Problem Statement
Takahashi is in charge of managing a highway with a total length of T km. To maintain safety, he needs to set up several inspection intervals along the highway.
Let the starting point of the highway be 0 km and the ending point be T km.
An inspection interval is set up by choosing an integer s (0 \leq s and s + K \leq T) as an open interval (s, s+K). That is, it is an interval of length K km from point s km to point (s+K) km. You can set up any number of inspection intervals (including 0), and different inspection intervals may overlap.
There are N event venues along the highway, where the i-th event venue uses the open interval (L_i, R_i) (from point L_i km to point R_i km). We say that an inspection interval (s, s+K) overlaps with an event venue (L_i, R_i) if the intersection of these two open intervals is non-empty. Even if a single event venue overlaps with multiple inspection intervals, it is counted as 1 affected event venue.
To meet safety standards, the placement of the inspection intervals must satisfy the following interval constraints. Let p (p \geq 0) be the number of inspection intervals set up, and let their starting points in non-decreasing order be s_1 \leq s_2 \leq \cdots \leq s_p. Then, all of the following must hold:
If p = 0:
- T \leq M
If p \geq 1:
- s_1 \leq M
- s_{j+1} - (s_j + K) \leq M (1 \leq j \leq p-1)
- T - (s_p + K) \leq M
The first condition means that the distance from the starting point 0 km to the starting point of the first inspection interval is at most M km. The second condition means that the distance from the ending point of the j-th inspection interval s_j + K to the starting point of the (j+1)-th inspection interval s_{j+1} is at most M km. The third condition means that the distance from the ending point of the last inspection interval s_p + K to the ending point T km is at most M km. Note that if the inspection intervals overlap in the second condition, the left-hand side becomes negative, and the condition is automatically satisfied.
Intuitively, this means that for any point on the highway, there must be some inspection interval within M km of it. Since the constraints guarantee M \geq K, it is always possible to, for example, cover the entire highway with inspection intervals without gaps, so a placement satisfying the interval constraints always exists.
Takahashi wants to place the inspection intervals to satisfy the interval constraints while minimizing the number of event venues that overlap with at least one inspection interval. Find this minimum value.
Constraints
- 1 \leq T \leq 1000
- 0 \leq N \leq 12
- 1 \leq K \leq M \leq T
- 0 \leq L_i < R_i \leq T (1 \leq i \leq N)
- All input values are integers.
Input
T N K M L_1 R_1 L_2 R_2 \vdots L_N R_N
- The first line contains four space-separated integers: T, the total length of the highway; N, the number of event venues; K, the length of each inspection interval; and M, the upper limit of the distance in the interval constraints.
- The next N lines describe the intervals of each event venue.
- The (1+i)-th line contains two space-separated integers, L_i and R_i, representing the starting and ending points of the i-th event venue.
Output
Print the minimum number of event venues that overlap with at least one inspection interval in a placement that satisfies the interval constraints, in a single line.
Sample Input 1
10 3 2 3 1 2 4 6 8 9
Sample Output 1
0
Sample Input 2
6 2 2 6 1 3 4 6
Sample Output 2
0
Sample Input 3
50 8 5 9 0 4 6 10 12 18 19 23 25 31 33 37 39 45 46 50
Sample Output 3
4
Sample Input 4
200 12 17 30 0 12 15 28 31 45 48 63 70 82 85 101 105 119 123 140 142 158 160 176 178 190 191 200
Sample Output 4
5
Sample Input 5
1 0 1 1
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君は文字列の構造を分析するプログラムを作っています。
文字列 S のボーダーとは、以下の条件をすべて満たす文字列 T のことを言います。
- T は空文字列ではない。
- T の長さは S の長さより真に小さい(すなわち |T| < |S|)。
- T は S の接頭辞かつ接尾辞である。すなわち、S の先頭 |T| 文字が T と一致し、かつ S の末尾 |T| 文字も T と一致する。
例えば、文字列 abcab のボーダーは ab のみです。ab は abcab の先頭 2 文字とも末尾 2 文字とも一致し、空でなく、長さも abcab より真に小さいからです。一方、a は先頭 1 文字とは一致しますが末尾 1 文字(b)とは一致しないため、ボーダーではありません。
別の例として、文字列 abababab のボーダーは ab、abab、ababab の 3 つで、それぞれの長さは 2, 4, 6 です。
なお、長さ |S| 未満の正整数 \ell を決めると、S の先頭 \ell 文字からなる文字列は一意に定まり、それが S の末尾 \ell 文字と一致するかも一意に決まります。したがって、同じ長さのボーダーは高々 1 つであり、ボーダーの集合はその長さの集合と一対一に対応します。
高橋君は Q 個の文字列を受け取り、それぞれについて以下の値を求めたいと考えています。
文字列 S のボーダーの個数を k とします。k \geq 1 のとき、すべてのボーダーの長さを昇順に並べた列を d_1 < d_2 < \dots < d_k とします(ボーダーの長さはすべて異なるため、この列は狭義単調増加です)。このとき、次の値を求めてください。
- k = 0 の場合(ボーダーが存在しない):0 を出力する。
- k = 1 の場合(ボーダーがちょうど 1 つ):そのボーダーの長さ d_1 を出力する。
- k \geq 2 の場合(ボーダーが 2 つ以上):隣接する要素の差 d_2 - d_1,\, d_3 - d_2,\, \dots,\, d_k - d_{k-1} はいずれも正の整数である。これら k-1 個の正整数の最大公約数を出力する。
先ほどの例では、abababab のボーダーの長さは d_1 = 2,\, d_2 = 4,\, d_3 = 6 なので、隣接要素の差は d_2 - d_1 = 2,\, d_3 - d_2 = 2 となり、その最大公約数は 2 です。
制約
- 1 \leq Q \leq 10^5
- 各文字列 S_i の長さ |S_i| は 1 \leq |S_i| \leq 10^6 を満たす。
- S_i は英小文字からなる。
- すべての文字列の長さの合計は \displaystyle \sum_{i=1}^{Q} |S_i| \leq 10^6 を満たす。
入力
Q S_1 S_2 \vdots S_Q
- 1 行目には、文字列の個数を表す整数 Q が与えられる。
- 続く Q 行にわたって、各文字列が 1 行に 1 つずつ与えられる。
- 1 + i 行目には、i 番目の文字列 S_i が与えられる。各 S_i は英小文字からなる。
出力
Q 行にわたって出力せよ。i 行目(1 \leq i \leq Q)には、文字列 S_i に対する答えを整数 1 つで出力せよ。
入力例 1
5 abcab abababab aaaa abcde aba
出力例 1
2 2 1 0 1
入力例 2
6 a aa ab abcabc zzz xyxxyx
出力例 2
0 1 0 3 1 2
入力例 3
8 abcababcab aaaaabaaaaab abababa abcabcabcabc abcdabcabcd zzxyzzxyzz mississippi aabaaabaaab
出力例 3
3 6 2 3 4 1 0 4
入力例 4
10 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abababababababababababababababababababababababababababababababab abcabcabcabcabcabcabcabcabcabcabcabcabcabc abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd thequickbrownfoxjumpsoverthelazydogthequickbrownfox zzzzzyzzzzzyzzzzzyzzzzzyzzzzzy aabbaabbaabbaabbaabbaabbaabbaabb abcdeffedcbaabcdeffedcbaabcdeffedcba mnopqrmnopqrmnopqrmnopqrmnopqr
出力例 4
26 1 2 3 4 16 6 4 1 6
入力例 5
1 a
出力例 5
0
Score : 466 pts
Problem Statement
Takahashi is writing a program to analyze the structure of strings.
A border of a string S is a string T that satisfies all of the following conditions:
- T is not the empty string.
- The length of T is strictly less than the length of S (i.e., |T| < |S|).
- T is both a prefix and a suffix of S. That is, the first |T| characters of S match T, and the last |T| characters of S also match T.
For example, the only border of the string abcab is ab. This is because ab matches both the first 2 characters and the last 2 characters of abcab, is non-empty, and has length strictly less than that of abcab. On the other hand, a matches the first 1 character but does not match the last 1 character (b), so it is not a border.
As another example, the string abababab has 3 borders: ab, abab, and ababab, with lengths 2, 4, 6 respectively.
Note that for a positive integer \ell less than |S|, the string consisting of the first \ell characters of S is uniquely determined, and whether it matches the last \ell characters of S is also uniquely determined. Therefore, there is at most one border of each length, and the set of borders is in one-to-one correspondence with the set of their lengths.
Takahashi receives Q strings and wants to compute the following value for each of them.
Let k be the number of borders of string S. When k \geq 1, let d_1 < d_2 < \dots < d_k be the sequence of all border lengths sorted in ascending order (since all border lengths are distinct, this sequence is strictly increasing). Compute the following value:
- If k = 0 (no border exists): output 0.
- If k = 1 (exactly one border): output the length of that border, d_1.
- If k \geq 2 (two or more borders): the differences between adjacent elements d_2 - d_1,\, d_3 - d_2,\, \dots,\, d_k - d_{k-1} are all positive integers. Output the greatest common divisor of these k-1 positive integers.
In the example above, the border lengths of abababab are d_1 = 2,\, d_2 = 4,\, d_3 = 6, so the differences between adjacent elements are d_2 - d_1 = 2,\, d_3 - d_2 = 2, and their greatest common divisor is 2.
Constraints
- 1 \leq Q \leq 10^5
- The length |S_i| of each string S_i satisfies 1 \leq |S_i| \leq 10^6.
- S_i consists of lowercase English letters.
- The total length of all strings satisfies \displaystyle \sum_{i=1}^{Q} |S_i| \leq 10^6.
Input
Q S_1 S_2 \vdots S_Q
- The first line contains an integer Q representing the number of strings.
- The following Q lines each contain one string.
- The (1 + i)-th line contains the i-th string S_i. Each S_i consists of lowercase English letters.
Output
Output Q lines. The i-th line (1 \leq i \leq Q) should contain a single integer representing the answer for string S_i.
Sample Input 1
5 abcab abababab aaaa abcde aba
Sample Output 1
2 2 1 0 1
Sample Input 2
6 a aa ab abcabc zzz xyxxyx
Sample Output 2
0 1 0 3 1 2
Sample Input 3
8 abcababcab aaaaabaaaaab abababa abcabcabcabc abcdabcabcd zzxyzzxyzz mississippi aabaaabaaab
Sample Output 3
3 6 2 3 4 1 0 4
Sample Input 4
10 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abababababababababababababababababababababababababababababababab abcabcabcabcabcabcabcabcabcabcabcabcabcabc abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd thequickbrownfoxjumpsoverthelazydogthequickbrownfox zzzzzyzzzzzyzzzzzyzzzzzyzzzzzy aabbaabbaabbaabbaabbaabbaabbaabb abcdeffedcbaabcdeffedcbaabcdeffedcba mnopqrmnopqrmnopqrmnopqrmnopqr
Sample Output 4
26 1 2 3 4 16 6 4 1 6
Sample Input 5
1 a
Sample Output 5
0