E - Martial artist

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

高橋君は武術家です。 武術家の覚えられる技は N 個あり、技 1, 2, \ldots, N と名前がついています。 1 \leq i \leq N について、技 i を習得するには時間 T_i の修練が必要で、 さらに、修練の開始時点で技 A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} をすでに習得している必要があります。 ここで、1 \leq j \leq K_i について、A_{i,j} < i であることが保証されます。

高橋君は時刻 0 の時点で技を 1 つも覚えていません。 高橋君は同時に 1 つの技に対する修練しかできず、また、一度始めた修練を途中でやめることもできません。 高橋君が技 N を習得するのに必要な時間の最小値を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq T_i \leq 10^9
  • 0 \leq K_i < i
  • 1 \leq A_{i,j} < i
  • \sum_{i=1}^N K_i \leq 2\times 10^5
  • A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} はすべて異なる。
  • 入力は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

N
T_1 K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
T_2 K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
T_N K_N A_{N,1} A_{N,2} \ldots A_{N,K_N}

出力

N を習得するのに必要な時間の最小値を出力せよ。


入力例 1

3
3 0
5 1 1
7 1 1

出力例 1

10

例えば高橋君は次のように行動することができます。

  • 高橋君は時刻 0 に技 1 の修練を開始し、時刻 3 に技 1 を習得します。
  • その後、時刻 3 に技 3 の修練を開始し、時刻 10 に技 3 を習得します。

このときが最短で、高橋君が技 3 を習得するのに必要な時間は 3+7=10 となります。 技 3 の習得のためには、技 2 を習得する必要はありません。


入力例 2

5
1000000000 0
1000000000 0
1000000000 0
1000000000 0
1000000000 4 1 2 3 4

出力例 2

5000000000

答えが 32 bit 整数に収まらないことがある事に注意してください。

Score : 300 points

Problem Statement

Takahashi is a martial artist. There are N moves that a martial artist can learn, called Move 1, 2, \ldots, N. For each 1 \leq i \leq N, it takes T_i minutes of practice to learn Move i. Additionally, at the beginning of that practice, all of the Moves A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} must be already learned. Here, it is guaranteed that A_{i,j} < i for each 1 \leq j \leq K_i.

Takahashi has not learned any move at time 0. He cannot practice for more than one move simultaneously, nor can he stop a practice he has already started. Find the minimum number of minutes needed for Takahashi to learn Move N.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq T_i \leq 10^9
  • 0 \leq K_i < i
  • 1 \leq A_{i,j} < i
  • \sum_{i=1}^N K_i \leq 2\times 10^5
  • A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} are all distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
T_1 K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
T_2 K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
T_N K_N A_{N,1} A_{N,2} \ldots A_{N,K_N}

Output

Print the minimum number of minutes needed for Takahashi to learn Move N.


Sample Input 1

3
3 0
5 1 1
7 1 1

Sample Output 1

10

Here is one possible plan for Takahashi.

  • At time 0, start practicing for Move 1 to learn Move 1 at time 3.
  • Then, at time 3, start practicing for Move 3 to learn Move 3 at time 10.

Here, Takahashi spends 3+7=10 minutes to learn Move 3, which is the fastest possible. Note that he does not need to learn Move 2 to learn Move 3.


Sample Input 2

5
1000000000 0
1000000000 0
1000000000 0
1000000000 0
1000000000 4 1 2 3 4

Sample Output 2

5000000000

Note that the answer may not fit into a 32-bit integer.

F - Sum of Numbers Greater Than Me

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

長さ N の数列 A=(A_1,\ldots,A_N) が与えられます。

i=1,\ldots,N のそれぞれについて次の問題を解いてください。

問題:A の要素のうち A_i より大きな要素全ての和を求めよ。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^6
  • 入力は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 \ldots A_N

出力

1\leq k\leq N について、i=k に対する問題の答えを B_k とする。B_1,\ldots,B_N をこの順に空白区切りで出力せよ。


入力例 1

5
1 4 1 4 2

出力例 1

10 0 10 0 8
  • i=1 のとき A_1=1 より大きな要素の和は 4+4+2=10
  • i=2 のとき A_2=4 より大きな要素の和は 0
  • i=3 のとき A_3=1 より大きな要素の和は 4+4+2=10
  • i=4 のとき A_4=4 より大きな要素の和は 0
  • i=5 のとき A_5=2 より大きな要素の和は 4+4=8

入力例 2

10
31 42 59 26 53 58 97 93 23 54

出力例 2

456 414 190 487 361 249 0 97 513 307

入力例 3

50
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

出力例 3

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Score : 300 points

Problem Statement

You are given a sequence A=(A_1,\ldots,A_N) of length N.

For each i=1,\ldots,N, solve the following problem.

Problem: Find the sum of all elements in A that are greater than A_i.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^6
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

For each 1\leq k\leq N, let B_k be the answer to the problem when i=k. Print B_1,\ldots,B_N in this order, separated by spaces.


Sample Input 1

5
1 4 1 4 2

Sample Output 1

10 0 10 0 8
  • For i=1, the sum of elements greater than A_1=1 is 4+4+2=10.
  • For i=2, the sum of elements greater than A_2=4 is 0.
  • For i=3, the sum of elements greater than A_3=1 is 4+4+2=10.
  • For i=4, the sum of elements greater than A_4=4 is 0.
  • For i=5, the sum of elements greater than A_5=2 is 4+4=8.

Sample Input 2

10
31 42 59 26 53 58 97 93 23 54

Sample Output 2

456 414 190 487 361 249 0 97 513 307

Sample Input 3

50
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 3

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
G - Happy New Year 2023

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

正整数 N が与えられます。N は、2 つの相異なる素数 p,q を用いて N=p^2q と表せることがわかっています。

p,q を求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 入力は全て整数
  • 1\leq T\leq 10
  • 1\leq N \leq 9\times 10^{18}
  • N は、2 つの相異なる素数 p,q を用いて N=p^2q と表せる

入力

入力は以下の形式で標準入力から与えられる。ここで \text{test}_ii 番目のテストケースを意味する。

T
\text{test}_1
\text{test}_2
\vdots
\text{test}_T

各テストケースは以下の形式で与えられる。

N

出力

T 行出力せよ。

i\ (1\leq i \leq T) 行目には、i 番目のテストケースにおける p,q を空白区切りで出力せよ。 なお、この問題の制約下では、N=p^2q を満たす素数 p,q の組は 1 通りしか存在しないことが証明できる。


入力例 1

3
2023
63
1059872604593911

出力例 1

17 7
3 7
104149 97711

1 番目のテストケースについて、N=2023=17^2\times 7 です。よって、p=17,q=7 です。

Score : 400 points

Problem Statement

You are given a positive integer N. It is known that N can be represented as N=p^2q using two different prime numbers p and q.

Find p and q.

You have T test cases to solve.

Constraints

  • All values in the input are integers.
  • 1\leq T\leq 10
  • 1\leq N \leq 9\times 10^{18}
  • N can be represented as N=p^2q using two different prime numbers p and q.

Input

The input is given from Standard Input in the following format, where \text{test}_i represents the i-th test case:

T
\text{test}_1
\text{test}_2
\vdots
\text{test}_T

Each test case is in the following format:

N

Output

Print T lines.

The i-th (1\leq i \leq T) line should contain p and q for the i-th test case, separated by a space. Under the constraints of this problem, it can be proved that the pair of prime numbers p and q such that N=p^2q is unique.


Sample Input 1

3
2023
63
1059872604593911

Sample Output 1

17 7
3 7
104149 97711

For the first test case, we have N=2023=17^2\times 7. Thus, p=17 and q=7.

H - Grid Filling

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

H 行、横 W 列のグリッドがあります。 上から i 行目、左から j 列目のマスを (i,j) で表します。 (i,j)\ (1\leq i\leq H,1\leq j\leq W) には 1 以上 N 以下の整数 A _ {i,j} が書かれています。

整数 h,w が与えられます。0\leq k\leq H-h,0\leq l\leq W-w を満たすすべての (k,l) の組について、次の問題を解いてください。

  • k\lt i\leq k+h,l\lt j\leq l+w を満たす (i,j) を塗りつぶしたとき、塗りつぶされていないマスに書かれている数が何種類あるか求めよ。

ただし、問題を解く際に実際にマスを塗りつぶすことはない(各問題が独立である)ことに注意してください。

制約

  • 1 \leq H,W,N \leq 300
  • 1 \leq h \leq H
  • 1 \leq w \leq W
  • (h,w)\neq(H,W)
  • 1 \leq A _ {i,j} \leq N\ (1\leq i\leq H,1\leq j\leq W)
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

H W N h w
A _ {1,1} A _ {1,2} \dots A _ {1,W}
A _ {2,1} A _ {2,2} \dots A _ {2,W}
\vdots
A _ {H,1} A _ {H,2} \dots A _ {H,W}

出力

(k,l) に対する答えを \operatorname{ans}_{k,l} として、以下の形式で出力せよ。

\operatorname{ans} _ {0,0} \operatorname{ans} _ {0,1} \dots \operatorname{ans} _ {0,W-w}
\operatorname{ans} _ {1,0} \operatorname{ans} _ {1,1} \dots \operatorname{ans} _ {1,W-w}
\vdots
\operatorname{ans} _ {H-h,0} \operatorname{ans} _ {H-h,1} \dots \operatorname{ans} _ {H-h,W-w}

入力例 1

3 4 5 2 2
2 2 1 1
3 2 5 3
3 4 4 3

出力例 1

4 4 3
5 3 4

与えられた盤面は下の図のようになります。

例えば、(k,l)=(0,0) のときは塗りつぶされていないマスに書かれている数は 1,3,4,54 種類なので、4 が答えになります。


入力例 2

5 6 9 3 4
7 1 5 3 9 5
4 5 4 5 1 2
6 1 6 2 9 7
4 7 1 5 8 8
3 4 3 3 5 3

出力例 2

8 8 7
8 9 7
8 9 8

入力例 3

9 12 30 4 7
2 2 2 2 2 2 2 2 2 2 2 2
2 2 20 20 2 2 5 9 10 9 9 23
2 29 29 29 29 29 28 28 26 26 26 15
2 29 29 29 29 29 25 25 26 26 26 15
2 29 29 29 29 29 25 25 8 25 15 15
2 18 18 18 18 1 27 27 25 25 16 16
2 19 22 1 1 1 7 3 7 7 7 7
2 19 22 22 6 6 21 21 21 7 7 7
2 19 22 22 22 22 21 21 21 24 24 24

出力例 3

21 20 19 20 18 17
20 19 18 19 17 15
21 19 20 19 18 16
21 19 19 18 19 18
20 18 18 18 19 18
18 16 17 18 19 17

Score : 500 points

Problem Statement

You have a grid with H rows from top to bottom and W columns from left to right. We denote by (i, j) the square at the i-th row from the top and j-th column from the left. (i,j)\ (1\leq i\leq H,1\leq j\leq W) has an integer A _ {i,j} between 1 and N written on it.

You are given integers h and w. For all pairs (k,l) such that 0\leq k\leq H-h and 0\leq l\leq W-w, solve the following problem:

  • If you black out the squares (i,j) such that k\lt i\leq k+h and l\lt j\leq l+w, how many distinct integers are written on the squares that are not blacked out?

Note, however, that you do not actually black out the squares (that is, the problems are independent).

Constraints

  • 1 \leq H,W,N \leq 300
  • 1 \leq h \leq H
  • 1 \leq w \leq W
  • (h,w)\neq(H,W)
  • 1 \leq A _ {i,j} \leq N\ (1\leq i\leq H,1\leq j\leq W)
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

H W N h w
A _ {1,1} A _ {1,2} \dots A _ {1,W}
A _ {2,1} A _ {2,2} \dots A _ {2,W}
\vdots
A _ {H,1} A _ {H,2} \dots A _ {H,W}

Output

Print the answers in the following format, where \operatorname{ans}_{k,l} denotes the answer to (k, l):

\operatorname{ans} _ {0,0} \operatorname{ans} _ {0,1} \dots \operatorname{ans} _ {0,W-w}
\operatorname{ans} _ {1,0} \operatorname{ans} _ {1,1} \dots \operatorname{ans} _ {1,W-w}
\vdots
\operatorname{ans} _ {H-h,0} \operatorname{ans} _ {H-h,1} \dots \operatorname{ans} _ {H-h,W-w}

Sample Input 1

3 4 5 2 2
2 2 1 1
3 2 5 3
3 4 4 3

Sample Output 1

4 4 3
5 3 4

The given grid is as follows:

For example, when (k,l)=(0,0), four distinct integers 1,3,4, and 5 are written on the squares that are not blacked out, so 4 is the answer.


Sample Input 2

5 6 9 3 4
7 1 5 3 9 5
4 5 4 5 1 2
6 1 6 2 9 7
4 7 1 5 8 8
3 4 3 3 5 3

Sample Output 2

8 8 7
8 9 7
8 9 8

Sample Input 3

9 12 30 4 7
2 2 2 2 2 2 2 2 2 2 2 2
2 2 20 20 2 2 5 9 10 9 9 23
2 29 29 29 29 29 28 28 26 26 26 15
2 29 29 29 29 29 25 25 26 26 26 15
2 29 29 29 29 29 25 25 8 25 15 15
2 18 18 18 18 1 27 27 25 25 16 16
2 19 22 1 1 1 7 3 7 7 7 7
2 19 22 22 6 6 21 21 21 7 7 7
2 19 22 22 22 22 21 21 21 24 24 24

Sample Output 3

21 20 19 20 18 17
20 19 18 19 17 15
21 19 20 19 18 16
21 19 19 18 19 18
20 18 18 18 19 18
18 16 17 18 19 17
I - Common Prefixes

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

2 つの文字列 X,Y に対して、その類似度 f(X,Y) を、XY を先頭から見て一致している文字数とします。
例えば abcaxbc の類似度は 1aaaaaaa の類似度は 3 です。

長さ N の文字列 S が与えられます。Si 文字目以降からなる文字列を S_i とします。k=1,\ldots,N のそれぞれについて、f(S_k,S_1)+f(S_k,S_2)+\ldots+f(S_k,S_N) を求めてください。

制約

  • 1 \leq N \leq 10^6
  • S は英小文字のみからなる長さ N の文字列

入力

入力は以下の形式で標準入力から与えられる。

N
S

出力

N 行出力せよ。

i 行目には k=i の問題の答えを出力せよ。


入力例 1

3
abb

出力例 1

3
3
2

S_1,S_2,S_3 はそれぞれ abb, bb, b です。

  • k=1 のとき f(S_1,S_1)+f(S_1,S_2)+f(S_1,S_3)=3+0+0=3
  • k=2 のとき f(S_2,S_1)+f(S_2,S_2)+f(S_2,S_3)=0+2+1=3
  • k=3 のとき f(S_3,S_1)+f(S_3,S_2)+f(S_3,S_3)=0+1+1=2

入力例 2

11
mississippi

出力例 2

11
16
14
12
13
11
9
7
4
3
4

Score : 500 points

Problem Statement

Let the similarity f(X,Y) between two strings X and Y be the length of their longest common prefix.
For example, the similarity between abc and axbc is 1, and the similarity between aaa and aaaa is 3.

You are given a string S of length N. Let S_i be the suffix of S beginning with the i-th character of S. For each k=1,\ldots,N, find f(S_k,S_1)+f(S_k,S_2)+\ldots+f(S_k,S_N).

Constraints

  • 1 \leq N \leq 10^6
  • S is a string of length N consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print N lines.

The i-th line should contain the answer for k=i.


Sample Input 1

3
abb

Sample Output 1

3
3
2

S_1 is abb, S_2 is bb, and S_3 is b.

  • For k=1: f(S_1,S_1)+f(S_1,S_2)+f(S_1,S_3)=3+0+0=3.
  • For k=2: f(S_2,S_1)+f(S_2,S_2)+f(S_2,S_3)=0+2+1=3.
  • For k=3: f(S_3,S_1)+f(S_3,S_2)+f(S_3,S_3)=0+1+1=2.

Sample Input 2

11
mississippi

Sample Output 2

11
16
14
12
13
11
9
7
4
3
4