E - 343

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

正整数 N が与えられます。

N 以下の正整数であって回文立方数であるものの最大値を求めてください。

ただし、正整数 K は以下の 2 つの条件を満たすとき、またそのときに限り回文立方数であると定義します。

  • ある正整数 x が存在し、x^3 = K を満たす。
  • K を先頭に 0 をつけずに 10 進表記した文字列が回文となる。より厳密には、0 以上 9 以下の整数 A_0, A_1, \ldots, A_{L-2} および 1 以上 9 以下の整数 A_{L-1} を用いて K = \sum_{i = 0}^{L-1} A_i10^i と表記したときに i = 0, 1, \ldots, L-1 に対して A_i = A_{L-1-i} を満たす。

制約

  • N10^{18} 以下の正整数

入力

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

N

出力

答えを出力せよ。


入力例 1

345

出力例 1

343

343 は回文立方数であり、344, 345 は回文立方数ではありません。したがって、343 が答えとなります。


入力例 2

6

出力例 2

1

入力例 3

123456789012345

出力例 3

1334996994331

Score: 250 points

Problem Statement

You are given a positive integer N.

Find the maximum value of a palindromic cube number not greater than N.

Here, a positive integer K is defined to be a palindromic cube number if and only if it satisfies the following two conditions:

  • There is a positive integer x such that x^3 = K.
  • The decimal representation of K without leading zeros is a palindrome. More precisely, if K is represented as K = \sum_{i = 0}^{L-1} A_i10^i using integers A_0, A_1, \ldots, A_{L-2} between 0 and 9, inclusive, and an integer A_{L-1} between 1 and 9, inclusive, then A_i = A_{L-1-i} for all i = 0, 1, \ldots, L-1.

Constraints

  • N is a positive integer not greater than 10^{18}.

Input

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

N

Output

Print the answer.


Sample Input 1

345

Sample Output 1

343

343 is a palindromic cube number, while 344 and 345 are not. Thus, the answer is 343.


Sample Input 2

6

Sample Output 2

1

Sample Input 3

123456789012345

Sample Output 3

1334996994331
F - Approximate Equalization 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

整数列 A=(A_1,A_2,\dots,A_N) があります。 あなたは次の操作を好きな回数(0 回でもよい)行うことができます。

  • 1\leq i,j \leq N を満たす整数 i,j を選ぶ。A_i1 減らし、A_j1 増やす。

A の最小値と最大値の差を 1 以下にするために必要な最小の操作回数を求めてください。

制約

  • 1\leq N \leq 2\times 10^5
  • 1\leq A_i \leq 10^9
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

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


入力例 1

4
4 7 3 7

出力例 1

3

以下のように 3 回の操作を行うことで、A の最小値と最大値の差を 1 以下にすることができます。

  • i=2,j=3 として操作を行う。A=(4,6,4,7) になる。
  • i=4,j=1 として操作を行う。A=(5,6,4,6) になる。
  • i=4,j=3 として操作を行う。A=(5,6,5,5) になる。

3 回未満の操作で A の最小値と最大値の差を 1 以下にすることはできません。よって答えは 3 です。


入力例 2

1
313

出力例 2

0

入力例 3

10
999999997 999999999 4 3 2 4 999999990 8 999999991 999999993

出力例 3

2499999974

Score : 400 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N). You can perform the following operation any number of times (possibly zero).

  • Choose integers i and j with 1\leq i,j \leq N. Decrease A_i by one and increase A_j by one.

Find the minimum number of operations required to make the difference between the minimum and maximum values of A at most one.

Constraints

  • 1\leq N \leq 2\times 10^5
  • 1\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 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

4
4 7 3 7

Sample Output 1

3

By the following three operations, the difference between the minimum and maximum values of A becomes at most one.

  • Choose i=2 and j=3 to make A=(4,6,4,7).
  • Choose i=4 and j=1 to make A=(5,6,4,6).
  • Choose i=4 and j=3 to make A=(5,6,5,5).

You cannot make the difference between maximum and minimum values of A at most one by less than three operations, so the answer is 3.


Sample Input 2

1
313

Sample Output 2

0

Sample Input 3

10
999999997 999999999 4 3 2 4 999999990 8 999999991 999999993

Sample Output 3

2499999974
G - Tile Pattern

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

縦横 10^9 マスのグリッドがあります。上から i + 1 行目、左から j + 1 列目 (0 \leq i, j \lt 10^9) にあるマスを (i, j) と呼びます。(通常と異なる index の割り当て方に注意してください。)
各マスは黒マスか白マスのいずれかです。マス (i, j) の色は文字 P[i \bmod N][j \bmod N] によって表されて、B ならばマス (i, j) は黒マス、W ならば白マスです。ここで a \bmod bab で割った余りを意味します。

Q 個のクエリが与えられるので順に処理してください。
各クエリでは 4 つの整数 A, B, C, D が与えられるので、(A, B) を左上隅、(C, D) を右下隅とする長方形領域に含まれる黒マスの個数を求めてください。

制約

  • 1 \leq N \leq 1000
  • P[i][j]W または B
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A \leq C \lt 10^9
  • 0 \leq B \leq D \lt 10^9
  • N, Q, A, B, C, D は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \text{query}_ii 番目に処理するクエリである。

N Q
P[0][0]P[0][1]\dots P[0][N-1]
P[1][0]P[1][1]\dots P[1][N-1]
\vdots
P[N-1][0]P[N-1][1]\dots P[N-1][N-1]
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリは以下の形式で与えられる。

A B C D

出力

問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。


入力例 1

3 2
WWB
BBW
WBW
1 2 3 4
0 3 4 5

出力例 1

4
7

グリッドの左上部分を図示すると次の図のようになります。

image

1 番目のクエリについて、(1, 2) を左上隅、(3, 4) を右下隅とする長方形領域は図の赤い枠線に囲まれた部分で、領域に含まれる黒マスの個数は 4 個です。
2 番目のクエリについて、(0, 3) を左上隅、(4, 5) を右下隅とする長方形領域は図の青い枠線に囲まれた部分で、領域に含まれる黒マスの個数は 7 個です。


入力例 2

10 5
BBBWWWBBBW
WWWWWBBBWB
BBBWBBWBBB
BBBWWBWWWW
WWWWBWBWBW
WBBWBWBBBB
WWBBBWWBWB
WBWBWWBBBB
WBWBWBBWWW
WWWBWWBWWB
5 21 21 93
35 35 70 43
55 72 61 84
36 33 46 95
0 0 999999999 999999999

出力例 2

621
167
44
344
500000000000000000

Score : 450 points

Problem Statement

There is a grid with 10^9 by 10^9 squares. Let (i, j) denote the square at the (i + 1)-th row from the top and the (j + 1)-th column from the left (0 \leq i, j \lt 10^9). (Note the unusual index assignment.)
Each square is black or white. The color of the square (i, j) is represented by a character P[i \bmod N][j \bmod N], where B means black, and W means white. Here, a \bmod b denotes the remainder when a is divided by b.

Answer Q queries.
Each query gives you four integers A, B, C, D and asks you to find the number of black squares contained in the rectangular area with (A, B) as the top-left corner and (C, D) as the bottom-right corner.

Constraints

  • 1 \leq N \leq 1000
  • P[i][j] is W or B.
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A \leq C \lt 10^9
  • 0 \leq B \leq D \lt 10^9
  • N, Q, A, B, C, D are all integers.

Input

The input is given from Standard Input in the following format. Here, \text{query}_i is the i-th query to be processed.

N Q
P[0][0]P[0][1]\dots P[0][N-1]
P[1][0]P[1][1]\dots P[1][N-1]
\vdots
P[N-1][0]P[N-1][1]\dots P[N-1][N-1]
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in the following format:

A B C D

Output

Follow the instructions in the problem statement and print the answers to the queries, separated by newlines.


Sample Input 1

3 2
WWB
BBW
WBW
1 2 3 4
0 3 4 5

Sample Output 1

4
7

The figure below illustrates the upper left part of the grid.

image

For the first query, the rectangular area with (1, 2) as the top-left corner and (3, 4) as the bottom-right corner, surrounded by the red frame in the figure, contains four black squares.
For the second query, the rectangular area with (0, 3) as the top-left corner and (4, 5) as the bottom-right corner, surrounded by the blue frame in the figure, contains seven black squares.


Sample Input 2

10 5
BBBWWWBBBW
WWWWWBBBWB
BBBWBBWBBB
BBBWWBWWWW
WWWWBWBWBW
WBBWBWBBBB
WWBBBWWBWB
WBWBWWBBBB
WBWBWBBWWW
WWWBWWBWWB
5 21 21 93
35 35 70 43
55 72 61 84
36 33 46 95
0 0 999999999 999999999

Sample Output 2

621
167
44
344
500000000000000000
H - Many Operations

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

変数 X と、X の値を変更する N 種類の操作があります。操作 i は整数の組 (T_i,A_i) で表され、意味は次の通りです。

  • T_i=1 のとき、X の値を X\ {\rm and}\ A_i に置き換える。
  • T_i=2 のとき、X の値を X\ {\rm or}\ A_i に置き換える。
  • T_i=3 のとき、X の値を X\ {\rm xor}\ A_i に置き換える。

変数 X を値 C で初期化した状態から、以下の処理を順に実行してください。

  • 操作 1 を行い、操作後の X の値を出力する。
  • 続けて、操作 1,2 を順に行い、操作後の X の値を出力する。
  • 続けて、操作 1,2,3 を順に行い、操作後の X の値を出力する。
  • \vdots
  • 続けて、操作 1,2,\ldots,N を順に行い、操作後の X の値を出力する。
{\rm and}, {\rm or}, {\rm xor} とは 非負整数 A, B{\rm and}, {\rm or}, {\rm xor} は、以下のように定義されます。
  • A\ {\rm and}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち両方が 1 であれば 1、そうでなければ 0 である。
  • A\ {\rm or}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも一方が 1 であれば 1、そうでなければ 0 である。
  • A\ {\rm xor}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうちちょうど一方が 1 であれば 1、そうでなければ 0 である。
例えば、3\ {\rm and}\ 5 = 13\ {\rm or}\ 5 = 73\ {\rm xor}\ 5 = 6 となります。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1\leq T_i \leq 3
  • 0\leq A_i \lt 2^{30}
  • 0\leq C \lt 2^{30}
  • 入力に含まれる値は全て整数である

入力

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

N C
T_1 A_1
T_2 A_2
\vdots
T_N A_N

出力

問題文中の指示に従って N 行出力せよ。


入力例 1

3 10
3 3
2 5
1 12

出力例 1

9
15
12

最初、X の値は 10 です。

  • 操作 1 を行うと X の値は 9 になります。
  • 続けて操作 1 を行うと X の値は 10 になり、さらに操作 2 を行うと 15 になります。
  • 続けて操作 1 を行うと X の値は 12 になり、さらに操作 2 を行うと 13 に、さらに続けて操作 3 を行うと 12 になります。

入力例 2

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

出力例 2

0
2
1
0
5
3
3
11
2

Score : 500 points

Problem Statement

We have a variable X and N kinds of operations that change the value of X. Operation i is represented as a pair of integers (T_i,A_i), and is the following operation:

  • if T_i=1, it replaces the value of X with X\ {\rm and}\ A_i;
  • if T_i=2, it replaces the value of X with X\ {\rm or}\ A_i;
  • if T_i=3, it replaces the value of X with X\ {\rm xor}\ A_i.

Initialize X with the value of C and execute the following procedures in order:

  • Perform Operation 1, and then print the resulting value of X.
  • Next, perform Operation 1, 2 in this order, and then print the value of X.
  • Next, perform Operation 1, 2, 3 in this order, and then print the value of X.
  • \vdots
  • Next, perform Operation 1, 2, \ldots, N in this order, and then print the value of X.
What are {\rm and}, {\rm or}, {\rm xor}?

The {\rm and}, {\rm or}, {\rm xor} of non-negative integers A and B are defined as follows:

  • When A\ {\rm and}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if both of the digits in that place of A and B are 1, and 0 otherwise.
  • When A\ {\rm or}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if at least one of the digits in that place of A and B is 1, and 0 otherwise.
  • When A\ {\rm xor}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of the digits in that place of A and B is 1, and 0 otherwise.
For example, 3\ {\rm and}\ 5 = 1, 3\ {\rm or}\ 5 = 7, and 3\ {\rm xor}\ 5 = 6.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1\leq T_i \leq 3
  • 0\leq A_i \lt 2^{30}
  • 0\leq C \lt 2^{30}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N C
T_1 A_1
T_2 A_2
\vdots
T_N A_N

Output

Print N lines, as specified in the Problem Statement.


Sample Input 1

3 10
3 3
2 5
1 12

Sample Output 1

9
15
12

The initial value of X is 10.

  • Operation 1 changes X to 9.
  • Next, Operation 1 changes X to 10, and then Operation 2 changes it to 15.
  • Next, Operation 1 changes X to 12, and then Operation 2 changes it to 13, and then Operation 3 changes it to 12.

Sample Input 2

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

Sample Output 2

0
2
1
0
5
3
3
11
2
I - Hammer 2

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

数直線の原点に高橋君がいます。高橋君は座標 X にあるゴールに移動しようとしています。

また、数直線上に N 枚の壁と N 本のハンマーがあります。

  • 座標 Y_1,Y_2,\dots,Y_N にはそれぞれタイプ 1,2,\dots,N の壁があります。
    • 最初、高橋君は壁を超えて移動することができません。
  • 座標 Z_1,Z_2,\dots,Z_N にはそれぞれタイプ 1,2,\dots,N のハンマーがあります。
    • 高橋君はハンマーのある座標に着くとそこにあるハンマーを手に入れます。
    • タイプ i のハンマーはタイプ i の壁を破壊するための専用のもので、タイプ i のハンマーを手に入れた後でなら、タイプ i の壁を破壊して通過できるようになります。

高橋君がゴールに到達することが可能か判定し、可能であれば移動距離の最小値を求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 1500
  • 1 \le |X|,|Y_i|,|Z_i| \le 10^9
  • 合計 2 \times N + 1 個の座標 X,Y_i,Z_i は相異なる

入力

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

N X
Y_1 Y_2 \dots Y_N
Z_1 Z_2 \dots Z_N

出力

高橋君がゴールに到達することが可能であれば移動距離の最小値を整数として出力せよ。
不可能であれば -1 と出力せよ。


入力例 1

3 10
-2 8 -5
5 -10 3

出力例 1

40

以下の手順により、移動距離 40 で高橋くんがゴールに到達でき、これが移動距離の最小です。

  • 座標 0 から高橋君が行動を開始する。
  • 座標 3 に行く。タイプ 3 のハンマーを手に入れる。
  • 座標 5 に行く。タイプ 1 のハンマーを手に入れる。
  • 座標 -2 に行く。タイプ 1 の壁を破壊する。
  • 座標 -5 に行く。タイプ 3 の壁を破壊する。
  • 座標 -10 に行く。タイプ 2 のハンマーを手に入れる。
  • 座標 8 に行く。タイプ 2 の壁を破壊する。
  • 座標 10 に行く。ここがゴールである。

入力例 2

5 -1
10 -20 30 -40 50
-10 20 -30 40 -50

出力例 2

1

ゴールに移動するために、ハンマーを手に入れる必要も壁を破壊する必要もない場合もあります。


入力例 3

1 100
30
60

出力例 3

-1

高橋君がタイプ 1 のハンマーを手に入れることは不可能であり、ゴールに辿り着くこともできません。


入力例 4

4 865942261
703164879 -531670946 -874856231 -700164975
-941120316 599462305 -649785130 665402307

出力例 4

4078987507

Score : 500 points

Problem Statement

Takahashi is at the origin of a number line. Takahashi wants to reach the goal at coordinate X.

Also, there are N walls and N hammers on the number line.

  • At coordinates Y_1,Y_2,\dots,Y_N are walls of types 1,2,\dots,N, respectively.
    • Initially, Takahashi cannot get over the walls.
  • At coordinates Z_1,Z_2,\dots,Z_N are hammers of types 1,2,\dots,N, respectively.
    • When he arrives at a coordinate with a hammer, he obtains the hammer.
    • The hammer of type i is dedicated to destroying the wall of type i. After he obtains the hammer of type i, he can destroy the wall of type i and get over it.

Determine if he can reach the goal. If he can, find the minimum distance he travels.

Constraints

  • All values in the input are integers.
  • 1 \le N \le 1500
  • 1 \le |X|,|Y_i|,|Z_i| \le 10^9
  • The (2 \times N + 1) coordinates X,Y_i and Z_i are distinct.

Input

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

N X
Y_1 Y_2 \dots Y_N
Z_1 Z_2 \dots Z_N

Output

If Takahashi can reach the goal, print the minimum possible distance he travels as an integer.
Otherwise, print -1.


Sample Input 1

3 10
-2 8 -5
5 -10 3

Sample Output 1

40

Takahashi can reach the goal by traveling a distance of 40 as follows, which is the minimum possible:

  • He starts at coordinate 0.
  • He moves to coordinate 3 to obtain the hammer of type 3.
  • He moves to coordinate 5 to obtain the hammer of type 1.
  • He moves to coordinate -2 to destroy the wall of type 1.
  • He moves to coordinate -5 to destroy the wall of type 3.
  • He moves to coordinate -10 to obtain the hammer of type 2.
  • He moves to coordinate 8 to destroy the wall of type 2.
  • He moves to coordinate 10, which is the goal.

Sample Input 2

5 -1
10 -20 30 -40 50
-10 20 -30 40 -50

Sample Output 2

1

It may not be required that he obtains a hammer or destroys a wall to reach the goal.


Sample Input 3

1 100
30
60

Sample Output 3

-1

Takahashi cannot obtain the hammer of type 1, and neither can he reach the goal.


Sample Input 4

4 865942261
703164879 -531670946 -874856231 -700164975
-941120316 599462305 -649785130 665402307

Sample Output 4

4078987507