A - TLD

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

英小文字と . のみからなる文字列 S が与えられます。
S. で分割したときの末尾の文字列を出力してください。
すなわち、. を含まない S の接尾辞のうち最長のものを出力してください。

制約

  • S は英小文字と . からなる、長さ 2 以上 100 以下の文字列
  • S には .1 つ以上含まれる
  • S の末尾は . ではない

入力

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

S

出力

答えを出力せよ。


入力例 1

atcoder.jp

出力例 1

jp

atcoder.jp の、 . を含まない最長の接尾辞は jp です。


入力例 2

translate.google.com

出力例 2

com

S. が複数含まれることもあります。


入力例 3

.z

出力例 3

z

S. から始まることもあります。


入力例 4

..........txt

出力例 4

txt

S 中で . が連続することもあります。

Score: 100 points

Problem Statement

You are given a string S consisting of lowercase English letters and the character ..
Print the last substring when S is split by .s.
In other words, print the longest suffix of S that does not contain ..

Constraints

  • S is a string of length between 2 and 100, inclusive, consisting of lowercase English letters and ..
  • S contains at least one ..
  • S does not end with ..

Input

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

S

Output

Print the answer.


Sample Input 1

atcoder.jp

Sample Output 1

jp

The longest suffix of atcoder.jp that does not contain . is jp.


Sample Input 2

translate.google.com

Sample Output 2

com

S may contain multiple .s.


Sample Input 3

.z

Sample Output 3

z

S may start with ..


Sample Input 4

..........txt

Sample Output 4

txt

S may contain consecutive .s.

B - Langton's Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 250

問題文

HW 列のグリッドがあり、はじめすべてのマスが白で塗られています。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

このグリッドはトーラス状であるとみなします。すなわち、各 1 \leq i \leq H に対して (i, W) の右に (i, 1) があり、各 1 \leq j \leq W に対して (H, j) の下に (1, j) があるとします。

高橋君が (1, 1) にいて上を向いています。高橋君が以下の操作を N 回繰り返した後のグリッドの各マスがどの色で塗られているか出力してください。

  • 現在いるマスが白で塗られている場合は、現在いるマスを黒に塗り替え、時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。そうでない場合は、現在いるマスを白に塗り替え、反時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。

制約

  • 1 \leq H, W \leq 100
  • 1 \leq N \leq 1000
  • 入力される数値はすべて整数

入力

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

H W N

出力

H 行出力せよ。i 行目には長さ W の文字列であって、(i, j) が白で塗られている場合は j 文字目が .、黒で塗られている場合は j 文字目が # であるものを出力せよ。


入力例 1

3 4 5

出力例 1

.#..
##..
....

グリッドの各マスは操作によって以下のように変化します。

....   #...   ##..   ##..   ##..   .#..
.... → .... → .... → .#.. → ##.. → ##..
....   ....   ....   ....   ....   ....

入力例 2

2 2 1000

出力例 2

..
..

入力例 3

10 10 10

出力例 3

##........
##........
..........
..........
..........
..........
..........
..........
..........
#........#

Score: 250 points

Problem Statement

There is a grid with H rows and W columns; initially, all cells are painted white. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

This grid is considered to be toroidal. That is, (i, 1) is to the right of (i, W) for each 1 \leq i \leq H, and (1, j) is below (H, j) for each 1 \leq j \leq W.

Takahashi is at (1, 1) and facing upwards. Print the color of each cell in the grid after Takahashi repeats the following operation N times.

  • If the current cell is painted white, repaint it black, rotate 90^\circ clockwise, and move forward one cell in the direction he is facing. Otherwise, repaint the current cell white, rotate 90^\circ counterclockwise, and move forward one cell in the direction he is facing.

Constraints

  • 1 \leq H, W \leq 100
  • 1 \leq N \leq 1000
  • All input values are integers.

Input

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

H W N

Output

Print H lines. The i-th line should contain a string of length W where the j-th character is . if the cell (i, j) is painted white, and # if it is painted black.


Sample Input 1

3 4 5

Sample Output 1

.#..
##..
....

The cells of the grid change as follows due to the operations:

....   #...   ##..   ##..   ##..   .#..
.... → .... → .... → .#.. → ##.. → ##..
....   ....   ....   ....   ....   ....

Sample Input 2

2 2 1000

Sample Output 2

..
..

Sample Input 3

10 10 10

Sample Output 3

##........
##........
..........
..........
..........
..........
..........
..........
..........
#........#
C - Perfect Bus

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 250

問題文

一台のバスが走っています。バスの乗客の数は常に非負整数です。

このバスにはある時点で 0 人以上の乗客が乗っており、その時点から現在までに N 回停車しました。このうち i 回目の停車では乗客が差し引き A_i 人増えました。A_i は負の値であることもあり、その場合は乗客が差し引き -A_i 人減ったことを意味しています。また、停車時以外には乗客の乗り降りはありませんでした。

与えられた情報に矛盾しない現在のバスの乗客の数として考えられる最小値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • -10^9 \leq A_i \leq 10^9
  • 入力される数値はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4
3 -5 7 -4

出力例 1

3

はじめに乗っている乗客の人数が 2 人であるとき、現在の乗客の人数は 2 + 3 + (-5) + 7 + (-4) = 3 人であり、さらにバスの乗客の人数は常に非負整数となります。


入力例 2

5
0 0 0 0 0

出力例 2

0

入力例 3

4
-1 1000000000 1000000000 1000000000

出力例 3

3000000000

Score: 250 points

Problem Statement

A bus is in operation. The number of passengers on the bus is always a non-negative integer.

At some point in time, the bus had zero or more passengers, and it has stopped N times since then. At the i-th stop, the number of passengers increased by A_i. Here, A_i can be negative, meaning the number of passengers decreased by -A_i. Also, no passengers got on or off the bus other than at the stops.

Find the minimum possible current number of passengers on the bus that is consistent with the given information.

Constraints

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

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

4
3 -5 7 -4

Sample Output 1

3

If the initial number of passengers was 2, the current number of passengers would be 2 + 3 + (-5) + 7 + (-4) = 3, and the number of passengers on the bus would have always been a non-negative integer.


Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

Sample Input 3

4
-1 1000000000 1000000000 1000000000

Sample Output 3

3000000000
D - Synchronized Players

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 400

問題文

NN 列のグリッドがあり、各マスは空きマスか障害物のあるマスのいずれかです。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

また、2 人のプレイヤーがグリッド上の相異なる空きマス上におり、各マスの情報は N 個の長さ N の文字列 S_1, S_2, \ldots, S_N として以下の形式で与えられます。

  • S_ij 文字目が P であるとき、(i, j) は空きマスであり、プレイヤーがいる

  • S_ij 文字目が . であるとき、(i, j) は空きマスであり、プレイヤーがいない

  • S_ij 文字目が # であるとき、(i, j) は障害物のあるマスである

以下の操作を繰り返し 2 人のプレイヤーを同じマスに集めるために必要な操作回数の最小値を求めてください。ただし、操作の繰り返しにより 2 人のプレイヤーを同じマスに集めることができない場合には -1 を出力してください。

  • 上下左右のいずれかの方向を決める。そして各プレイヤーはともにその方向に隣接するマスへの移動を試みる。各プレイヤーは移動先のマスが存在し、かつ空きマスであるならば移動し、そうでないならば移動しない。

制約

  • N2 以上 60 以下の整数
  • S_i は長さ NP, ., # からなる文字列
  • S_ij 文字目が P であるような組 (i, j) の個数はちょうど 2

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

5
....#
#..#.
.P...
..P..
....#

出力例 1

3

はじめに (3, 2) にいるプレイヤーをプレイヤー 1(4, 3) にいるプレイヤーをプレイヤー 2 とします。

例えば以下のようにすることで、3 回の操作で 2 人のプレイヤーが同じマスに集まります。

  • 左を選択する。プレイヤー 1(3, 1) に移動し、プレイヤー 2(4, 2) に移動する。

  • 上を選択する。プレイヤー 1 は移動せず、プレイヤー 2(3, 2) に移動する。

  • 左を選択する。プレイヤー 1 は移動せず、プレイヤー 2(3, 1) に移動する。


入力例 2

2
P#
#P

出力例 2

-1

入力例 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

出力例 3

10

Score: 400 points

Problem Statement

There is an N \times N grid, where each cell is either empty or contains an obstacle. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

There are also two players on distinct empty cells of the grid. The information about each cell is given as N strings S_1, S_2, \ldots, S_N of length N, in the following format:

  • If the j-th character of S_i is P, then (i, j) is an empty cell with a player on it.

  • If the j-th character of S_i is ., then (i, j) is an empty cell without a player.

  • If the j-th character of S_i is #, then (i, j) contains an obstacle.

Find the minimum number of moves required to bring the two players to the same cell by repeating the following operation. If it is impossible to bring the two players to the same cell by repeating the operation, print -1.

  • Choose one of the four directions: up, down, left, or right. Then, each player attempts to move to the adjacent cell in that direction. Each player moves if the destination cell exists and is empty, and does not move otherwise.

Constraints

  • N is an integer between 2 and 60, inclusive.
  • S_i is a string of length N consisting of P, ., and #.
  • There are exactly two pairs (i, j) where the j-th character of S_i is P.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

5
....#
#..#.
.P...
..P..
....#

Sample Output 1

3

Let us call the player starting at (3, 2) Player 1 and the player starting at (4, 3) Player 2.

For example, doing the following brings the two players to the same cell in three moves:

  • Choose left. Player 1 moves to (3, 1), and Player 2 moves to (4, 2).

  • Choose up. Player 1 does not move, and Player 2 moves to (3, 2).

  • Choose left. Player 1 does not move, and Player 2 moves to (3, 1).


Sample Input 2

2
P#
#P

Sample Output 2

-1

Sample Input 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

Sample Output 3

10
E - Smooth Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 525

問題文

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

A の部分列であって、隣接する 2 項の差の絶対値が D 以下であるようなものの長さの最大値を求めてください。

ただし、数列 A の部分列とは、A の要素を 0 個以上選んで削除し、残った要素を元の順序を保って並べた数列のことを指します。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 0 \leq D \leq 5 \times 10^5
  • 1 \leq A_i \leq 5 \times 10^5
  • 入力される数値はすべて整数

入力

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

N D
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4 2
3 5 1 2

出力例 1

3

A の部分列 (3, 1, 2) は隣接する 2 項の差の絶対値が 2 以下です。


入力例 2

5 10
10 20 100 110 120

出力例 2

3

入力例 3

11 7
21 10 3 19 28 12 11 3 3 15 16

出力例 3

6

Score: 525 points

Problem Statement

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

Find the maximum length of a subsequence of A such that the absolute difference between any two adjacent terms is at most D.

A subsequence of a sequence A is a sequence that can be obtained by deleting zero or more elements from A and arranging the remaining elements in their original order.

Constraints

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

Input

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

N D
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

4 2
3 5 1 2

Sample Output 1

3

The subsequence (3, 1, 2) of A has absolute differences of at most 2 between adjacent terms.


Sample Input 2

5 10
10 20 100 110 120

Sample Output 2

3

Sample Input 3

11 7
21 10 3 19 28 12 11 3 3 15 16

Sample Output 3

6
F - Product Equality

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 550

問題文

N 個の整数 A_1,A_2,\dots,A_N が与えられます。
以下の条件を満たす整数の組 (i,j,k) の個数を求めてください。

  • 1 \le i,j,k \le N
  • A_i \times A_j = A_k

制約

  • 1 \le N \le 1000
  • \color{red}{1 \le A_i < 10^{1000}}

入力

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

N
A_1
A_2
\vdots
A_N

出力

答えを整数として出力せよ。


入力例 1

5
2
3
6
12
24

出力例 1

6

問題文中の条件を満たす (i,j,k) の組は以下の 6 通りです。

  • (1,2,3)
  • (1,3,4)
  • (1,4,5)
  • (2,1,3)
  • (3,1,4)
  • (4,1,5)

入力例 2

11
1
2
3
4
5
6
123456789123456789
123456789123456789
987654321987654321
987654321987654321
121932631356500531347203169112635269

出力例 2

40

各整数 A_i の値が非常に大きくなりうることに注意してください。


入力例 3

9
4
4
4
2
2
2
1
1
1

出力例 3

162

A_i の値に重複がありうることに注意してください。

Score: 550 points

Problem Statement

You are given N integers A_1, A_2, \dots, A_N.
Find the number of triples of integers (i, j, k) that satisfy the following conditions:

  • 1 \le i, j, k \le N
  • A_i \times A_j = A_k

Constraints

  • 1 \le N \le 1000
  • \color{red}{1 \le A_i < 10^{1000}}

Input

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

N
A_1
A_2
\vdots
A_N

Output

Print the answer as an integer.


Sample Input 1

5
2
3
6
12
24

Sample Output 1

6

The following six triples (i, j, k) satisfy the conditions in the problem statement:

  • (1, 2, 3)
  • (1, 3, 4)
  • (1, 4, 5)
  • (2, 1, 3)
  • (3, 1, 4)
  • (4, 1, 5)

Sample Input 2

11
1
2
3
4
5
6
123456789123456789
123456789123456789
987654321987654321
987654321987654321
121932631356500531347203169112635269

Sample Output 2

40

Note that the values of each integer A_i can be huge.


Sample Input 3

9
4
4
4
2
2
2
1
1
1

Sample Output 3

162

Note that there may be duplicates among the values of A_i.

G - Smaller Sum

Time Limit: 3.5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

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

以下の Q 個のクエリに答えてください。このうち i 個目のクエリは以下の通りです。

  • A_{L_i},A_{L_i+1},\dots,A_{R_i} のうち X_i 以下であるものの総和を求めよ。

但し、あなたはこのクエリにオンラインで答える必要があります。
「オンラインでクエリに答える」とは、あるクエリへの回答を行った後で次のクエリが判明することを指します。

このため、 i 個目のクエリの代わりに、このクエリを暗号化した入力 \alpha_i, \beta_i, \gamma_i が与えられます。 以下の手順で本来の i 個目のクエリを復元して回答してください。

  • B_0=0B_i = ( i 個目のクエリの答え ) とする。
  • このとき、クエリの復号は以下のようにして行うことができる。
    • L_i = \alpha_i \oplus B_{i-1}
    • R_i = \beta_i \oplus B_{i-1}
    • X_i = \gamma_i \oplus B_{i-1}

但し、 x \oplus yxy とのビット単位 XOR を表します。

ビット単位 XOR とは 非負整数 A, B のビット単位 XOR 、A \oplus B は、以下のように定義されます。
  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。

制約

  • 入力は全て整数
  • 1 \le N \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • 1 \le Q \le 2 \times 10^5
  • 暗号化されたクエリに対して、以下が成立する。
    • 0 \le \alpha_i, \beta_i, \gamma_i \le 10^{18}
  • 復号した後のクエリに対して、以下が成立する。
    • 1 \le L_i \le R_i \le N
    • 0 \le X_i \le 10^9

入力

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

N
A_1 A_2 \dots A_N
Q
\alpha_1 \beta_1 \gamma_1
\alpha_2 \beta_2 \gamma_2
\vdots
\alpha_Q \beta_Q \gamma_Q

出力

全体で Q 行出力せよ。
このうち i 行目には、 i 個目のクエリの答えを出力せよ。


入力例 1

8
2 0 2 4 0 2 0 3
5
1 8 3
10 12 11
3 3 2
3 6 5
12 0 11

出力例 1

9
2
0
8
5

数列は A=(2,0,2,4,0,2,0,3) です。
この入力には 5 個のクエリが含まれます。

  • 最初、 B_0=0 です。
  • 最初のクエリは \alpha = 1, \beta = 8, \gamma = 3 です。
    • 復号すると L_i = \alpha \oplus B_0 = 1, R_i = \beta \oplus B_0 = 8, X_i = \gamma \oplus B_0 = 3 となります。
    • このクエリに対する答えは 9 です。これを B_1 とします。
  • 次のクエリは \alpha = 10, \beta = 12, \gamma = 11 です。
    • 復号すると L_i = \alpha \oplus B_1 = 3, R_i = \beta \oplus B_1 = 5, X_i = \gamma \oplus B_1 = 2 となります。
    • このクエリに対する答えは 2 です。これを B_2 とします。
  • 次のクエリは \alpha = 3, \beta = 3, \gamma = 2 です。
    • 復号すると L_i = \alpha \oplus B_2 = 1, R_i = \beta \oplus B_2 = 1, X_i = \gamma \oplus B_2 = 0 となります。
    • このクエリに対する答えは 0 です。これを B_3 とします。
  • 次のクエリは \alpha = 3, \beta = 6, \gamma = 5 です。
    • 復号すると L_i = \alpha \oplus B_3 = 3, R_i = \beta \oplus B_3 = 6, X_i = \gamma \oplus B_3 = 5 となります。
    • このクエリに対する答えは 8 です。これを B_4 とします。
  • 次のクエリは \alpha = 12, \beta = 0, \gamma = 11 です。
    • 復号すると L_i = \alpha \oplus B_4 = 4, R_i = \beta \oplus B_4 = 8, X_i = \gamma \oplus B_4 = 3 となります。
    • このクエリに対する答えは 5 です。これを B_5 とします。

Score: 600 points

Problem Statement

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

Answer the following Q queries. The i-th query is as follows:

  • Find the sum of the elements among A_{L_i},A_{L_i+1},\dots,A_{R_i} that are not greater than X_i.

Here, you need to answer these queries online.
That is, only after you answer the current query is the next query revealed.

For this reason, instead of the i-th query itself, you are given encrypted inputs \alpha_i, \beta_i, \gamma_i for the query. Restore the original i-th query using the following steps and then answer it.

  • Let B_0=0 and B_i = (the answer to the i-th query).
  • Then, the query can be decrypted as follows:
    • L_i = \alpha_i \oplus B_{i-1}
    • R_i = \beta_i \oplus B_{i-1}
    • X_i = \gamma_i \oplus B_{i-1}

Here, x \oplus y denotes the bitwise XOR of x and y.

What is bitwise XOR? The bitwise XOR of non-negative integers A and B, A \oplus B, is defined as follows:
  • The digit in the 2^k place (k \geq 0) of A \oplus B in binary is 1 if exactly one of the corresponding digits of A and B in binary is 1, and 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110).

Constraints

  • All input values are integers.
  • 1 \le N \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • 1 \le Q \le 2 \times 10^5
  • For the encrypted inputs, the following holds:
    • 0 \le \alpha_i, \beta_i, \gamma_i \le 10^{18}
  • For the decrypted queries, the following holds:
    • 1 \le L_i \le R_i \le N
    • 0 \le X_i \le 10^9

Input

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

N
A_1 A_2 \dots A_N
Q
\alpha_1 \beta_1 \gamma_1
\alpha_2 \beta_2 \gamma_2
\vdots
\alpha_Q \beta_Q \gamma_Q

Output

Print Q lines.
The i-th line should contain the answer to the i-th query.


Sample Input 1

8
2 0 2 4 0 2 0 3
5
1 8 3
10 12 11
3 3 2
3 6 5
12 0 11

Sample Output 1

9
2
0
8
5

The given sequence is A=(2,0,2,4,0,2,0,3).
This input contains five queries.

  • Initially, B_0=0.
  • The first query is \alpha = 1, \beta = 8, \gamma = 3.
    • After decryption, we get L_i = \alpha \oplus B_0 = 1, R_i = \beta \oplus B_0 = 8, X_i = \gamma \oplus B_0 = 3.
    • The answer to this query is 9. This becomes B_1.
  • The next query is \alpha = 10, \beta = 12, \gamma = 11.
    • After decryption, we get L_i = \alpha \oplus B_1 = 3, R_i = \beta \oplus B_1 = 5, X_i = \gamma \oplus B_1 = 2.
    • The answer to this query is 2. This becomes B_2.
  • The next query is \alpha = 3, \beta = 3, \gamma = 2.
    • After decryption, we get L_i = \alpha \oplus B_2 = 1, R_i = \beta \oplus B_2 = 1, X_i = \gamma \oplus B_2 = 0.
    • The answer to this query is 0. This becomes B_3.
  • The next query is \alpha = 3, \beta = 6, \gamma = 5.
    • After decryption, we get L_i = \alpha \oplus B_3 = 3, R_i = \beta \oplus B_3 = 6, X_i = \gamma \oplus B_3 = 5.
    • The answer to this query is 8. This becomes B_4.
  • The next query is \alpha = 12, \beta = 0, \gamma = 11.
    • After decryption, we get L_i = \alpha \oplus B_4 = 4, R_i = \beta \oplus B_4 = 8, X_i = \gamma \oplus B_4 = 3.
    • The answer to this query is 5. This becomes B_5.