実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 233 点
問題文
高橋君は N 個の花壇を管理しています。
i 番目の花壇には、現在 A_i リットルの水が入っています。高橋君は、すべての花壇の水の量を等しくしたいと考えています。
高橋君は以下の操作を好きな回数( 0 回でもよい)行うことができます。
- 花壇を 1 つ選び、その花壇に水を 1 リットル追加する。この操作には 1 円かかる。同じ花壇を何度選んでもよい。
花壇から水を抜くことはできないため、操作後の各花壇の水の量は操作前以上になります。
高橋君は、すべての花壇の水の量がある整数値で等しくなることを目標とします。目標とする値は自由に決めることができます。
この目標を達成するために必要な最小の費用(円)を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
N A_1 A_2 \ldots A_N
- 1 行目には、花壇の数を表す整数 N が与えられる。
- 2 行目には、各花壇に入っている水の量を表す N 個の整数 A_1, A_2, \ldots, A_N が空白区切りで与えられる。
出力
すべての花壇の水の量を等しくするために必要な最小の費用(円)を 1 行で出力せよ。
入力例 1
3 2 5 3
出力例 1
5
入力例 2
5 10 10 10 10 10
出力例 2
0
入力例 3
6 1 100 50 25 75 200
出力例 3
749
Score : 233 pts
Problem Statement
Takahashi manages N flower beds.
The i-th flower bed currently contains A_i liters of water. Takahashi wants to make the amount of water in all flower beds equal.
Takahashi can perform the following operation any number of times (possibly 0 times):
- Choose one flower bed and add 1 liter of water to it. This operation costs 1 yen. The same flower bed may be chosen multiple times.
Since water cannot be removed from a flower bed, the amount of water in each flower bed after the operations will be at least the amount before the operations.
Takahashi's goal is to make the amount of water in all flower beds equal to some integer value. He is free to choose the target value.
Find the minimum cost (in yen) required to achieve this goal.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All inputs are integers
Input
N A_1 A_2 \ldots A_N
- The first line contains an integer N, representing the number of flower beds.
- The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the amount of water in each flower bed.
Output
Print in one line the minimum cost (in yen) required to make the amount of water in all flower beds equal.
Sample Input 1
3 2 5 3
Sample Output 1
5
Sample Input 2
5 10 10 10 10 10
Sample Output 2
0
Sample Input 3
6 1 100 50 25 75 200
Sample Output 3
749
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 333 点
問題文
高橋君は倉庫の整理を任されている。倉庫には N 個の荷物が一列に並んでおり、左から i 番目の荷物の重さは A_i である。
高橋君はトラックを使って荷物を搬出する。高橋君は以下の搬出操作を好きな回数(0 回でもよい)繰り返し行うことができる。ただし、一度搬出した荷物が列に戻ることはない。
搬出操作:
現在残っている荷物の列から、その列で連続する 1 個以上の荷物からなる区間を 1 つ選ぶ。ただし、選んだ区間に含まれる荷物の重さの合計は K 以下でなければならない。選んだ区間に含まれる荷物をすべて列から取り除いて搬出する。荷物が取り除かれた後、残った荷物は元の相対的な順序を保ったまま隙間を詰めて再び一列に並ぶ(残った荷物がない場合、列は空になる)。
なお、1 回の搬出操作で選べる区間は 1 つだけであり、また、各回の搬出操作における重さの合計の上限は毎回 K である(過去の操作で搬出した量によらない)。
例えば、荷物が [A_1, A_2, A_3, A_4, A_5] と並んでいるとき、A_2 + A_3 \leq K であれば、最初の搬出操作で A_2 と A_3 をまとめて搬出できる。搬出後、列は [A_1, A_4, A_5] となる。このとき、元の列では隣接していなかった A_1 と A_4 が新たに隣接するため、次の搬出操作で A_1 と A_4 をまとめて 1 つの区間として選ぶことができる(ただし A_1 + A_4 \leq K である場合に限る)。
高橋君ができるだけ多くの荷物を搬出するとき、搬出できる荷物の最大個数を求めよ。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{18}
- 1 \leq A_i \leq 10^{18}
- 入力はすべて整数である
入力
N K A_1 A_2 \ldots A_N
- 1 行目には、荷物の個数を表す整数 N と、1 回の搬出操作で搬出できる荷物の重さの合計の上限を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各荷物の重さを表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
搬出できる荷物の最大個数を 1 行で出力せよ。
入力例 1
5 4 3 1 3 1 3
出力例 1
5
入力例 2
3 2 3 3 3
出力例 2
0
入力例 3
10 10 5 6 3 8 2 7 1 4 9 5
出力例 3
10
入力例 4
20 15 7 3 12 5 8 1 6 14 2 9 4 11 3 7 10 2 13 5 8 6
出力例 4
20
入力例 5
1 1000000000000000000 1000000000000000000
出力例 5
1
Score : 333 pts
Problem Statement
Takahashi is in charge of organizing a warehouse. There are N items of luggage lined up in a row in the warehouse, and the weight of the i-th item from the left is A_i.
Takahashi uses a truck to carry out the luggage. Takahashi can repeat the following carrying-out operation any number of times (including 0 times). Once an item has been carried out, it does not return to the row.
Carrying-out operation:
From the current remaining row of items, choose one contiguous interval consisting of 1 or more consecutive items in that row. However, the total weight of the items in the chosen interval must be at most K. Remove all items in the chosen interval from the row and carry them out. After the items are removed, the remaining items close the gaps while maintaining their original relative order and form a row again (if no items remain, the row becomes empty).
Note that only one interval can be chosen per carrying-out operation, and the upper limit on the total weight for each carrying-out operation is K every time (regardless of the amount carried out in previous operations).
For example, if the items are arranged as [A_1, A_2, A_3, A_4, A_5], and A_2 + A_3 \leq K, then in the first carrying-out operation, A_2 and A_3 can be carried out together. After this, the row becomes [A_1, A_4, A_5]. At this point, A_1 and A_4, which were not adjacent in the original row, are now adjacent, so in the next carrying-out operation, A_1 and A_4 can be selected together as one interval (provided that A_1 + A_4 \leq K).
Find the maximum number of items Takahashi can carry out when he tries to carry out as many items as possible.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{18}
- 1 \leq A_i \leq 10^{18}
- All input values are integers
Input
N K A_1 A_2 \ldots A_N
- The first line contains an integer N representing the number of items and an integer K representing the upper limit on the total weight of items that can be carried out in one operation, separated by a space.
- The second line contains integers A_1, A_2, \ldots, A_N representing the weight of each item, separated by spaces.
Output
Output the maximum number of items that can be carried out, in one line.
Sample Input 1
5 4 3 1 3 1 3
Sample Output 1
5
Sample Input 2
3 2 3 3 3
Sample Output 2
0
Sample Input 3
10 10 5 6 3 8 2 7 1 4 9 5
Sample Output 3
10
Sample Input 4
20 15 7 3 12 5 8 1 6 14 2 9 4 11 3 7 10 2 13 5 8 6
Sample Output 4
20
Sample Input 5
1 1000000000000000000 1000000000000000000
Sample Output 5
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君はこれから N 回の小テストを受けます。i 回目の小テストにおける高橋君の点数は H_i 点です。点数は負の値になることもあります。
高橋君は 0 回以上 K 回以下の好きな回数だけ「補習授業」を受けることができます。補習授業を 1 回受けるごとに、N 回のテストの中から 1 つを選び、そのテストの点数に P を加算します。同じテストを複数回選ぶこともでき、どのテストを選んでも構いません。
すべての補習授業の割り当てが終わった後、各テストの最終的な点数を確認します。最終的な点数が 0 以下であるテストは「赤点」となります。0 点ちょうどのテストも赤点であることに注意してください。
高橋君は赤点となるテストの数をできるだけ少なくしたいと考えています。補習授業を最適に割り当てたとき、赤点となるテストの数の最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq 10^9
- 1 \leq P \leq 10^9
- -10^9 \leq H_i \leq 10^9 (1 \leq i \leq N)
- 入力はすべて整数である
入力
N K P H_1 H_2 \ldots H_N
- 1 行目には、小テストの回数を表す整数 N、補習授業を受けられる最大回数を表す整数 K、補習授業 1 回あたりの加算点を表す正の整数 P が、スペース区切りで与えられる。
- 2 行目には、各テストの点数を表す整数 H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。
出力
補習授業を最適に割り当てたとき、赤点となるテストの数の最小値を 1 行で出力せよ。
入力例 1
5 3 2 -3 1 -1 0 5
出力例 1
1
入力例 2
4 2 3 -5 -2 3 -8
出力例 2
2
入力例 3
8 10 5 -9 -4 2 -1 0 -14 7 -6
出力例 3
0
入力例 4
10 5 4 -7 -3 1 -15 0 -1 8 -10 -4 2
出力例 4
3
入力例 5
1 0 1 0
出力例 5
1
Score : 366 pts
Problem Statement
Takahashi will take N quizzes. His score on the i-th quiz is H_i points. Scores can be negative.
Takahashi can attend "supplementary lessons" any number of times from 0 to K times, inclusive. Each time he attends a supplementary lesson, he chooses one of the N quizzes and adds P to that quiz's score. He may choose the same quiz multiple times, and he may choose any quiz.
After all supplementary lessons have been assigned, the final score of each quiz is checked. A quiz whose final score is 0 or less is considered a "failing grade." Note that a quiz with a score of exactly 0 is also a failing grade.
Takahashi wants to minimize the number of quizzes with failing grades. Find the minimum number of quizzes with failing grades when the supplementary lessons are assigned optimally.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq 10^9
- 1 \leq P \leq 10^9
- -10^9 \leq H_i \leq 10^9 (1 \leq i \leq N)
- All inputs are integers.
Input
N K P H_1 H_2 \ldots H_N
- The first line contains an integer N representing the number of quizzes, an integer K representing the maximum number of supplementary lessons, and a positive integer P representing the points added per supplementary lesson, separated by spaces.
- The second line contains integers H_1, H_2, \ldots, H_N representing the scores of each quiz, separated by spaces.
Output
Print in one line the minimum number of quizzes with failing grades when the supplementary lessons are assigned optimally.
Sample Input 1
5 3 2 -3 1 -1 0 5
Sample Output 1
1
Sample Input 2
4 2 3 -5 -2 3 -8
Sample Output 2
2
Sample Input 3
8 10 5 -9 -4 2 -1 0 -14 7 -6
Sample Output 3
0
Sample Input 4
10 5 4 -7 -3 1 -15 0 -1 8 -10 -4 2
Sample Output 4
3
Sample Input 5
1 0 1 0
Sample Output 5
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は、直線の道路上を移動する N 台の山車(だし)のパレードを撮影しようとしています。
撮影日は 0 日目から D 日目までの整数日の中から 1 日選びます。高橋君は、すべての山車が収まるようにカメラのフレーム幅をできるだけ小さくしたいと考えています。
d 日目における山車 i の中心座標は X_i + V_i \cdot d です。ここで X_i は 0 日目の中心座標、V_i は 1 日あたりの移動速度(負なら座標が減る方向に移動)です。また、山車 i は中心から左右にそれぞれ L_i の半幅を持つため、道路上で区間
[X_i + V_i \cdot d - L_i, \ X_i + V_i \cdot d + L_i]
を占有します。
d 日目にすべての山車を収めるために必要なフレーム幅は
F(d) = \max_{1 \le i \le N}(X_i + V_i \cdot d + L_i) - \min_{1 \le i \le N}(X_i + V_i \cdot d - L_i)
です。
ところが、青木君からパレードの運行計画について Q 回の修正指示が届きました。j 回目の修正では、山車 P_j の初期座標・移動速度・半幅がそれぞれ A_j, B_j, C_j に変更されます。
バージョン 0 を初期データ、バージョン j(1 \le j \le Q)をバージョン j-1 に j 回目の修正を反映したデータとします。バージョン t における山車 i の初期座標、移動速度、半幅をそれぞれ X_i^{(t)}, V_i^{(t)}, L_i^{(t)} と表すとき、バージョン t における日 d のフレーム幅は
F_t(d) = \max_{1 \le i \le N}(X_i^{(t)} + V_i^{(t)} \cdot d + L_i^{(t)}) - \min_{1 \le i \le N}(X_i^{(t)} + V_i^{(t)} \cdot d - L_i^{(t)})
です。
各バージョン t(0 \le t \le Q)について、撮影日を最適に選んだときのフレーム幅の最小値
G_t = \min_{\substack{d \in \mathbb{Z} \\ 0 \le d \le D}} F_t(d)
を求めてください。各バージョンで撮影日は独立に最適なものを選べます。
制約
- 2 \leq N \leq 10^5
- 1 \leq D \leq 10^9
- 0 \leq Q \leq 10^5
- N(Q+1) \leq 5 \times 10^5
- |X_i| \leq 10^6 (1 \leq i \leq N)
- |V_i| \leq 10^6 (1 \leq i \leq N)
- 1 \leq L_i \leq 10^6 (1 \leq i \leq N)
- 1 \leq P_j \leq N (1 \leq j \leq Q)
- |A_j| \leq 10^6 (1 \leq j \leq Q)
- |B_j| \leq 10^6 (1 \leq j \leq Q)
- 1 \leq C_j \leq 10^6 (1 \leq j \leq Q)
- 入力はすべて整数である。
- この制約下で、出力すべき値はすべて signed 64-bit integer に収まる。
入力
N D Q X_1 V_1 L_1 X_2 V_2 L_2 : X_N V_N L_N P_1 A_1 B_1 C_1 P_2 A_2 B_2 C_2 : P_Q A_Q B_Q C_Q
- 1 行目には、山車の台数 N、撮影日として選べる最終日 D、修正回数 Q がスペース区切りで与えられる。
- 続く N 行では、初期データが与えられる。
- 1 + i 行目には、山車 i の 0 日目の中心座標 X_i、1 日あたりの移動速度 V_i、半幅 L_i がスペース区切りで与えられる。
- 続く Q 行では、修正指示が時系列順に与えられる。
- 1 + N + j 行目には、j 回目の修正を表す P_j, A_j, B_j, C_j がスペース区切りで与えられる。
- これは、山車 P_j の初期座標を A_j、移動速度を B_j、半幅を C_j に置き換えることを意味する。この変更はバージョン j 以降すべてに反映される。
出力
G_0 G_1 : G_Q
Q + 1 行出力せよ。1 + t 行目には、バージョン t における G_t を整数として出力せよ。
入力例 1
2 5 2 0 1 1 10 -1 2 1 2 0 1 2 5 1 1
出力例 1
4 6 5
入力例 2
3 4 0 0 0 1 5 -1 2 -3 2 1
出力例 2
5
入力例 3
6 30 5 -10 3 2 15 -2 3 0 0 1 25 -1 4 -20 4 2 8 1 5 3 5 -3 2 6 -5 2 1 1 12 -1 3 4 0 0 6 2 -15 5 2
出力例 3
24 34 34 34 20 20
入力例 4
12 1000000000 10 1000000 -1000000 1000000 -1000000 1000000 999999 0 500000 12345 500000 0 777777 -500000 -250000 333333 123456 654321 111111 -654321 -123456 222222 999999 -1 1 -999999 1 2 314159 -271828 31415 -271828 314159 92653 42 -42 1000000 1 -1000000 1000000 1 12 1000000 -1000000 1000000 6 0 0 500000 9 500000 500000 10 4 -999999 999999 999999 7 12345 -54321 123456 2 222222 -333333 444444 11 -1 1 1 5 1000000 0 1000000 10 -1000000 -1000000 999999
出力例 4
2361110 2361110 2361110 2361110 2361110 2083343 2083343 2083343 2083343 3000000 3999999
入力例 5
2 1 0 -1000000 -1000000 1000000 1000000 1000000 1000000
出力例 5
4000000
Score : 400 pts
Problem Statement
Takahashi is trying to photograph a parade of N floats moving along a straight road.
He will choose one shooting day from the integer days between day 0 and day D (inclusive). Takahashi wants to make the camera's frame width as small as possible while fitting all the floats in the frame.
The center coordinate of float i on day d is X_i + V_i \cdot d. Here, X_i is the center coordinate on day 0, and V_i is the movement speed per day (if negative, the float moves in the direction of decreasing coordinates). Float i has a half-width of L_i extending in both directions from its center, so it occupies the interval
[X_i + V_i \cdot d - L_i, \ X_i + V_i \cdot d + L_i]
on the road.
The frame width required to capture all floats on day d is
F(d) = \max_{1 \le i \le N}(X_i + V_i \cdot d + L_i) - \min_{1 \le i \le N}(X_i + V_i \cdot d - L_i)
However, Aoki has sent Q modification instructions regarding the parade operation plan. In the j-th modification, the initial coordinate, movement speed, and half-width of float P_j are changed to A_j, B_j, C_j respectively.
Let version 0 be the initial data, and version j (1 \le j \le Q) be the data obtained by applying the j-th modification to version j-1. Denoting the initial coordinate, movement speed, and half-width of float i in version t as X_i^{(t)}, V_i^{(t)}, L_i^{(t)} respectively, the frame width on day d in version t is
F_t(d) = \max_{1 \le i \le N}(X_i^{(t)} + V_i^{(t)} \cdot d + L_i^{(t)}) - \min_{1 \le i \le N}(X_i^{(t)} + V_i^{(t)} \cdot d - L_i^{(t)})
For each version t (0 \le t \le Q), find the minimum frame width when the shooting day is chosen optimally:
G_t = \min_{\substack{d \in \mathbb{Z} \\ 0 \le d \le D}} F_t(d)
The shooting day can be chosen independently and optimally for each version.
Constraints
- 2 \leq N \leq 10^5
- 1 \leq D \leq 10^9
- 0 \leq Q \leq 10^5
- N(Q+1) \leq 5 \times 10^5
- |X_i| \leq 10^6 (1 \leq i \leq N)
- |V_i| \leq 10^6 (1 \leq i \leq N)
- 1 \leq L_i \leq 10^6 (1 \leq i \leq N)
- 1 \leq P_j \leq N (1 \leq j \leq Q)
- |A_j| \leq 10^6 (1 \leq j \leq Q)
- |B_j| \leq 10^6 (1 \leq j \leq Q)
- 1 \leq C_j \leq 10^6 (1 \leq j \leq Q)
- All input values are integers.
- Under these constraints, all values to be output fit in a signed 64-bit integer.
Input
N D Q X_1 V_1 L_1 X_2 V_2 L_2 : X_N V_N L_N P_1 A_1 B_1 C_1 P_2 A_2 B_2 C_2 : P_Q A_Q B_Q C_Q
- The first line contains the number of floats N, the last selectable shooting day D, and the number of modifications Q, separated by spaces.
- The following N lines give the initial data.
- The (1 + i)-th line contains the center coordinate X_i of float i on day 0, the movement speed per day V_i, and the half-width L_i, separated by spaces.
- The following Q lines give the modification instructions in chronological order.
- The (1 + N + j)-th line contains P_j, A_j, B_j, C_j representing the j-th modification, separated by spaces.
- This means replacing the initial coordinate of float P_j with A_j, the movement speed with B_j, and the half-width with C_j. This change is reflected in version j and all subsequent versions.
Output
G_0 G_1 : G_Q
Output Q + 1 lines. The (1 + t)-th line should contain G_t for version t as an integer.
Sample Input 1
2 5 2 0 1 1 10 -1 2 1 2 0 1 2 5 1 1
Sample Output 1
4 6 5
Sample Input 2
3 4 0 0 0 1 5 -1 2 -3 2 1
Sample Output 2
5
Sample Input 3
6 30 5 -10 3 2 15 -2 3 0 0 1 25 -1 4 -20 4 2 8 1 5 3 5 -3 2 6 -5 2 1 1 12 -1 3 4 0 0 6 2 -15 5 2
Sample Output 3
24 34 34 34 20 20
Sample Input 4
12 1000000000 10 1000000 -1000000 1000000 -1000000 1000000 999999 0 500000 12345 500000 0 777777 -500000 -250000 333333 123456 654321 111111 -654321 -123456 222222 999999 -1 1 -999999 1 2 314159 -271828 31415 -271828 314159 92653 42 -42 1000000 1 -1000000 1000000 1 12 1000000 -1000000 1000000 6 0 0 500000 9 500000 500000 10 4 -999999 999999 999999 7 12345 -54321 123456 2 222222 -333333 444444 11 -1 1 1 5 1000000 0 1000000 10 -1000000 -1000000 999999
Sample Output 4
2361110 2361110 2361110 2361110 2361110 2083343 2083343 2083343 2083343 3000000 3999999
Sample Input 5
2 1 0 -1000000 -1000000 1000000 1000000 1000000 1000000
Sample Output 5
4000000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 433 点
問題文
高橋君は、文字列の検索アルゴリズムの研究をしています。今回のテーマは、円環状の文字列に対するパターンマッチングです。
英小文字からなる文字列 S が与えられます。S の長さを N とします。この文字列は円環状に並んでいるものとみなします。すなわち、S の末尾の次には再び先頭の文字が続くものとします。
また、英小文字からなるパターン文字列 T が与えられます。T の長さを M とします。
S の各文字の位置を 0, 1, \ldots, N-1(0-indexed)で表します。各位置 i(0 \leq i < N)に対して、位置 i を開始位置として円環上で連続する M 文字を順に読んだ文字列 R_i を次のように定義します。各 j(0 \leq j < M)に対して、R_i の j 文字目(0-indexed)は S の位置 (i + j) \bmod N の文字です。
なお、この定義は M > N の場合にもそのまま適用されます。その場合は円環を複数周回することになり、同じ位置の文字が繰り返し参照されます。
N 個の開始位置 i = 0, 1, \ldots, N-1 のそれぞれについて R_i = T であるかを判定し、R_i = T となる開始位置の個数を求めてください。
制約
- 1 \leq N \leq 5 \times 10^5
- 1 \leq M \leq 5 \times 10^5
- S は英小文字のみからなる長さ N の文字列
- T は英小文字のみからなる長さ M の文字列
入力
S T
- 1 行目には、文字列 S が与えられる。
- 2 行目には、パターン文字列 T が与えられる。
出力
円環状の文字列 S においてパターン文字列 T と一致する開始位置の個数を 1 行で出力せよ。
入力例 1
ababc abc
出力例 1
1
入力例 2
abc bcab
出力例 2
1
入力例 3
abracadabracadabra cadabra
出力例 3
2
入力例 4
abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde deabcdeabcdeabcdeabcdeabc
出力例 4
20
入力例 5
a a
出力例 5
1
Score : 433 pts
Problem Statement
Takahashi is researching string search algorithms. His current topic is pattern matching on circular strings.
You are given a string S consisting of lowercase English letters. Let N be the length of S. This string is considered to be arranged in a circular manner. That is, the first character follows immediately after the last character of S.
You are also given a pattern string T consisting of lowercase English letters. Let M be the length of T.
The positions of each character in S are represented as 0, 1, \ldots, N-1 (0-indexed). For each position i (0 \leq i < N), define the string R_i as the string obtained by reading M consecutive characters on the circle starting from position i. Specifically, for each j (0 \leq j < M), the j-th character (0-indexed) of R_i is the character at position (i + j) \bmod N in S.
Note that this definition applies even when M > N. In that case, the circle is traversed multiple times, and characters at the same position are referenced repeatedly.
For each of the N starting positions i = 0, 1, \ldots, N-1, determine whether R_i = T, and find the number of starting positions where R_i = T.
Constraints
- 1 \leq N \leq 5 \times 10^5
- 1 \leq M \leq 5 \times 10^5
- S is a string of length N consisting of lowercase English letters only
- T is a string of length M consisting of lowercase English letters only
Input
S T
- The first line contains the string S.
- The second line contains the pattern string T.
Output
Output in one line the number of starting positions in the circular string S that match the pattern string T.
Sample Input 1
ababc abc
Sample Output 1
1
Sample Input 2
abc bcab
Sample Output 2
1
Sample Input 3
abracadabracadabra cadabra
Sample Output 3
2
Sample Input 4
abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde deabcdeabcdeabcdeabcdeabc
Sample Output 4
20
Sample Input 5
a a
Sample Output 5
1