A - Last Two Digits

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

100 以上の整数 N が与えられます。N の下 2 桁を出力してください。

ただし、N の下 2 桁とは十の位と一の位をこの順に並べたものを言います。

制約

  • 100 \le N \le 999
  • N は整数である。

入力

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

N

出力

答えを出力せよ。


入力例 1

254

出力例 1

54

254 の下 2 桁は 54 であるため、54 を出力します。


入力例 2

101

出力例 2

01

101 の下 2 桁は 01 であるため、01 を出力します。

Score : 100 points

Problem Statement

You are given an integer N at least 100. Print the last two digits of N.

Strictly speaking, print the tens and ones digits of N in this order.

Constraints

  • 100 \le N \le 999
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

254

Sample Output 1

54

The last two digits of 254 are 54, which should be printed.


Sample Input 2

101

Sample Output 2

01

The last two digits of 101 are 01, which should be printed.

B - Practical Computing

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

以下のような N 個の整数列 A_0,\ldots,A_{N-1} を求めてください。

  • i (0\leq i \leq N-1) について、A_i の長さは i+1 である。
  • i,j (0\leq i \leq N-1, 0 \leq j \leq i) について、A_ij+1 番目の値 a_{i,j} は次のように定められる。

    • j=0 または j=i の時、a_{i,j}=1
    • それ以外の時、a_{i,j} = a_{i-1,j-1} + a_{i-1,j}

制約

  • 1 \leq N \leq 30
  • N は整数

入力

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

N

出力

N 行出力せよ。 i 行目には A_{i-1} の値を順に空白区切りで出力せよ。


入力例 1

3

出力例 1

1
1 1
1 2 1

入力例 2

10

出力例 2

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Score : 200 points

Problem Statement

Find the N integer sequences A_0,\ldots,A_{N-1} defined as follows.

  • For each i (0\leq i \leq N-1), the length of A_i is i+1.
  • For each i and j (0\leq i \leq N-1, 0 \leq j \leq i), the (j+1)-th term of A_i, denoted by a_{i,j}, is defined as follows.
    • a_{i,j}=1, if j=0 or j=i.
    • a_{i,j} = a_{i-1,j-1} + a_{i-1,j}, otherwise.

Constraints

  • 1 \leq N \leq 30
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the terms of A_{i-1} separated by spaces.


Sample Input 1

3

Sample Output 1

1
1 1
1 2 1

Sample Input 2

10

Sample Output 2

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
C - K Swap

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さ N の数列 A=(a_1,\ldots,a_N) があります。また、整数 K が与えられます。

あなたは次の操作を 0 回以上何度でも行えます。

  • 1 \leq i \leq N-K を満たす整数 i を選び、a_ia_{i+K} の値を入れ替える。

A を昇順に並べ替えることが出来るかどうかを判定してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N-1
  • 1 \leq a_i \leq 10^9
  • 入力はすべて整数

入力

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

N K
a_1 \ldots a_N

出力

A を昇順に並び替えることが出来るならば Yes と、出来ないならば No と出力せよ。


入力例 1

5 2
3 4 1 3 4

出力例 1

Yes

次のように操作をすることで A を昇順に並び替えることが出来ます。

  • i=1 とし、a_1a_3 の値を入れ替える。数列は (1,4,3,3,4) となる。
  • i=2 とし、a_2a_4 の値を入れ替える。数列は (1,3,3,4,4) となる。

入力例 2

5 3
3 4 1 3 4

出力例 2

No

入力例 3

7 5
1 2 3 4 5 5 10

出力例 3

Yes

操作を行う必要が無い場合もあります。

Score : 300 points

Problem Statement

We have a sequence of length N: A=(a_1,\ldots,a_N). Additionally, you are given an integer K.

You can perform the following operation zero or more times.

  • Choose an integer i such that 1 \leq i \leq N-K, then swap the values of a_i and a_{i+K}.

Determine whether it is possible to sort A in ascending order.

Constraints

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

Input

Input is given from Standard Input in the following format:

N K
a_1 \ldots a_N

Output

If it is possible to sort A in ascending order, print Yes; otherwise, print No.


Sample Input 1

5 2
3 4 1 3 4

Sample Output 1

Yes

The following sequence of operations sorts A in ascending order.

  • Choose i=1 to swap the values of a_1 and a_3. A is now (1,4,3,3,4).
  • Choose i=2 to swap the values of a_2 and a_4. A is now (1,3,3,4,4).

Sample Input 2

5 3
3 4 1 3 4

Sample Output 2

No

Sample Input 3

7 5
1 2 3 4 5 5 10

Sample Output 3

Yes

No operations may be needed.

D - Together Square

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

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

  • i \times j は平方数である。

制約

  • 1 \le N \le 2 \times 10^5
  • N は整数である。

入力

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

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

6

(1,1),(1,4),(2,2),(3,3),(4,1),(4,4)6 個が条件を満たします。

(2,3)2 \times 3 =6 が平方数でないため条件を満たしません。


入力例 2

254

出力例 2

896

Score : 400 points

Problem Statement

You are given an integer N. Find the number of pairs (i,j) of positive integers at most N that satisfy the following condition:

  • i \times j is a square number.

Constraints

  • 1 \le N \le 2 \times 10^5
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

4

Sample Output 1

6

The six pairs (1,1),(1,4),(2,2),(3,3),(4,1),(4,4) satisfy the condition.

On the other hand, (2,3) does not, since 2 \times 3 =6 is not a square number.


Sample Input 2

254

Sample Output 2

896
E - Small d and k

Time Limit: 3.5 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点 M 辺の単純無向グラフがあり、各頂点には 1,\ldots,N と番号が付けられています。 i=1,\ldots,M に対し、 i 番目の辺は頂点 a_i と頂点 b_i を結びます。また、各頂点の次数は 3 以下です。

i=1,\ldots,Q に対し、次のクエリに答えてください。

  • 頂点 x_i との距離が k_i 以下であるような頂点の番号の総和を求めよ。

制約

  • 1 \leq N \leq 1.5 \times 10^5
  • 0 \leq M \leq \min (\frac{N(N-1)}{2},\frac{3N}{2})
  • 1 \leq a_i \lt b_i \leq N
  • i\neq j ならば (a_i,b_i) \neq (a_j,b_j)
  • 与えられるグラフの各頂点の次数は 3 以下
  • 1 \leq Q \leq 1.5 \times 10^5
  • 1 \leq x_i \leq N
  • 0 \leq k_i \leq 3
  • 入力はすべて整数

入力

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

N M
a_1 b_1
\vdots
a_M b_M
Q
x_1 k_1
\vdots
x_Q k_Q

出力

Q 行出力せよ。 i 行目には i 番目のクエリへの答えを出力せよ。


入力例 1

6 5
2 3
3 4
3 5
5 6
2 6
7
1 1
2 2
2 0
2 3
4 1
6 0
4 3

出力例 1

1
20
2
20
7
6
20

1 番目のクエリでは、頂点 1 との距離が 1 以下であるような頂点は頂点 1 のみなので 1 が答えです。
2 番目のクエリでは、頂点 2 との距離が 2 以下であるような頂点は頂点 2,3,4,5,6 なのでこれらの総和の 20 が答えになります。
3 番目以降のクエリも同様にして答えを求められます。

Score : 500 points

Problem Statement

We have a simple undirected graph with N vertices and M edges. The vertices are numbered 1,\ldots,N. For each i=1,\ldots,M, the i-th edge connects Vertex a_i and Vertex b_i. Additionally, the degree of each vertex is at most 3.

For each i=1,\ldots,Q, answer the following query.

  • Find the sum of indices of vertices whose distances from Vertex x_i are at most k_i.

Constraints

  • 1 \leq N \leq 1.5 \times 10^5
  • 0 \leq M \leq \min (\frac{N(N-1)}{2},\frac{3N}{2})
  • 1 \leq a_i \lt b_i \leq N
  • (a_i,b_i) \neq (a_j,b_j), if i\neq j.
  • The degree of each vertex in the graph is at most 3.
  • 1 \leq Q \leq 1.5 \times 10^5
  • 1 \leq x_i \leq N
  • 0 \leq k_i \leq 3
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
\vdots
a_M b_M
Q
x_1 k_1
\vdots
x_Q k_Q

Output

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


Sample Input 1

6 5
2 3
3 4
3 5
5 6
2 6
7
1 1
2 2
2 0
2 3
4 1
6 0
4 3

Sample Output 1

1
20
2
20
7
6
20

For the 1-st query, the only vertex whose distance from Vertex 1 is at most 1 is Vertex 1, so the answer is 1.
For the 2-nd query, the vertices whose distances from Vertex 2 are at most 2 are Vertex 2, 3, 4, 5, and 6, so the answer is their sum, 20.
The 3-rd and subsequent queries can be answered similarly.

F - Rectangle GCD

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

正整数 N と長さ N の正整数列 A=(A_1,A_2,\dots,A_N)B=(B_1,B_2,\dots,B_N) が与えられます。

N \times N のマス目があります。上から i 行目、左から j 列目のマスをマス (i,j) と呼びます。1 \le i,j \le N を満たす整数の組 (i,j) に対し、マス (i,j)A_i + B_j が書かれています。以下のクエリを Q 個処理してください。

  • 1 \le h_1 \le h_2 \le N,1 \le w_1 \le w_2 \le N を満たす整数の組 h_1,h_2,w_1,w_2 が与えられる。左上隅が (h_1,w_1)、右下隅が (h_2,w_2) である矩形領域に含まれる整数の最大公約数を求めよ。

制約

  • 1 \le N,Q \le 2 \times 10^5
  • 1 \le A_i,B_i \le 10^9
  • 1 \le h_1 \le h_2 \le N
  • 1 \le w_1 \le w_2 \le N
  • 入力はすべて整数である。

入力

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

N Q
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

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

h_1 h_2 w_1 w_2

出力

Q 行出力せよ。i 行目には \mathrm{query}_i の答えを出力せよ。


入力例 1

3 5
3 5 2
8 1 3
1 2 2 3
1 3 1 3
1 1 1 1
2 2 2 2
3 3 1 1

出力例 1

2
1
11
6
10

マス (i,j) に書かれている整数を C_{i,j} とします。

1 個目のクエリについて、C_{1,2}=4,C_{1,3}=6,C_{2,2}=6,C_{2,3}=8 なのでこれらの最大公約数の 2 が答えとなります。


入力例 2

1 1
9
100
1 1 1 1

出力例 2

109

Score : 500 points

Problem Statement

You are given a positive integer N and sequences of N positive integers each: A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_N).

We have an N \times N grid. The square at the i-th row from the top and the j-th column from the left is called the square (i,j). For each pair of integers (i,j) such that 1 \le i,j \le N, the square (i,j) has the integer A_i + B_j written on it. Process Q queries of the following form.

  • You are given a quadruple of integers h_1,h_2,w_1,w_2 such that 1 \le h_1 \le h_2 \le N,1 \le w_1 \le w_2 \le N. Find the greatest common divisor of the integers contained in the rectangle region whose top-left and bottom-right corners are (h_1,w_1) and (h_2,w_2), respectively.

Constraints

  • 1 \le N,Q \le 2 \times 10^5
  • 1 \le A_i,B_i \le 10^9
  • 1 \le h_1 \le h_2 \le N
  • 1 \le w_1 \le w_2 \le N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is in the following format:

h_1 h_2 w_1 w_2

Output

Print Q lines. The i-th line should contain the answer to \mathrm{query}_i.


Sample Input 1

3 5
3 5 2
8 1 3
1 2 2 3
1 3 1 3
1 1 1 1
2 2 2 2
3 3 1 1

Sample Output 1

2
1
11
6
10

Let C_{i,j} denote the integer on the square (i,j).

For the 1-st query, we have C_{1,2}=4,C_{1,3}=6,C_{2,2}=6,C_{2,3}=8, so the answer is their greatest common divisor, which is 2.


Sample Input 2

1 1
9
100
1 1 1 1

Sample Output 2

109
G - Elevators

Time Limit: 6 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 棟の 10^9 階建てのビルからなる建物があります。ビルには 1 から N の番号がついています。

任意の異なるビルの同じ階は連絡通路で結ばれているため 1 分で移動可能です。

また、M 基のエレベーターがあります。i 個目のエレベーターはビル A_iB_i 階から C_i 階を結ぶものです。このエレベーターを使うと、B_i \le x,y \le C_i を満たす全ての整数の組 x,y に対し、ビル A_ix 階から y 階に |x-y| 分で移動することができます。

以下の Q 個のクエリに答えてください。

  • ビル X_iY_i 階からビル Z_iW_i 階に移動することが可能か判定し、可能な場合は移動時間の最小値を求めてください。

制約

  • 1 \le N,M,Q \le 2 \times 10^5
  • 1 \le A_i \le N
  • 1 \le B_i < C_i \le 10^9
  • 1 \le X_i,Z_i \le N
  • 1 \le Y_i,W_i \le 10^9
  • 入力はすべて整数である。

入力

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

N M Q
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

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

X_i Y_i Z_i W_i

出力

Q 行出力せよ。i 行目には \mathrm{query}_i について、移動することが不可能であれば -1 を、そうでないならば移動時間の最小値を出力せよ。


入力例 1

3 4 3
1 2 10
2 3 7
3 9 14
3 1 3
1 3 3 14
3 1 2 7
1 100 1 101

出力例 1

12
7
-1

1 番目のクエリについては、以下のようにすることで 12 分で移動が可能です。

  • エレベーター 1 を使い、ビル 13 階から 9 階へ移動する。この移動には 6 分かかる。
  • 9 階の連絡通路を使い、ビル 1 からビル 3 へ移動する。この移動には 1 分かかる。
  • エレベーター 3 を使い、ビル 39 階から 14 階で移動する。この移動には 5 分かかる。

また、3 番目のクエリについては、移動することが不可能であるため -1 を出力します。


入力例 2

1 1 1
1 1 2
1 1 1 2

出力例 2

1

Score : 600 points

Problem Statement

There is a complex composed of N 10^9-story skyscrapers. The skyscrapers are numbered 1 to N, and the floors are numbered 1 to 10^9.

From any floor of any skyscraper, one can use a skybridge to get to the same floor of any other skyscraper in one minute.

Additionally, there are M elevators. The i-th elevator runs between Floor B_i and Floor C_i of Skyscraper A_i. With this elevator, one can get from Floor x to Floor y of Skyscraper A_i in |x-y| minutes, for every pair of integers x,y such that B_i \le x,y \le C_i.

Answer the following Q queries.

  • Determine whether it is possible to get from Floor Y_i of Skyscraper X_i to Floor W_i of Skyscraper Z_i, and find the shortest time needed to get there if it is possible.

Constraints

  • 1 \le N,M,Q \le 2 \times 10^5
  • 1 \le A_i \le N
  • 1 \le B_i < C_i \le 10^9
  • 1 \le X_i,Z_i \le N
  • 1 \le Y_i,W_i \le 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is in the following format:

X_i Y_i Z_i W_i

Output

Print Q lines. The i-th line should contain -1 if, for \mathrm{query}_i, the destination is unreachable; otherwise, it should contain the minimum number of minutes needed to get there.


Sample Input 1

3 4 3
1 2 10
2 3 7
3 9 14
3 1 3
1 3 3 14
3 1 2 7
1 100 1 101

Sample Output 1

12
7
-1

For the 1-st query, you can get to the destination in 12 minutes as follows.

  • Use Elevator 1 to get from Floor 3 to Floor 9 of Skyscraper 1, in 6 minutes.
  • Use the skybridge on Floor 9 to get from Skyscraper 1 to Skyscraper 3, in 1 minute.
  • Use Elevator 3 to get from Floor 9 to Floor 14 of Skyscraper 3, in 5 minutes.

For the 3-rd query, the destination is unreachable, so -1 should be printed.


Sample Input 2

1 1 1
1 1 2
1 1 1 2

Sample Output 2

1
Ex - Multiply or Divide by 2

Time Limit: 2.5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 個の非負整数からなる多重集合 A=\{ a_1,\ldots,a_N \}, B=\{ b_1,\ldots,b_N \} が与えられます。
あなたは以下の操作を好きな順番で何度でも行えます。

  • A に含まれている非負整数を 1 つ選び、x とする。 A から x1 つ削除し、代わりに 2x1 つ追加する。
  • A に含まれている非負整数を 1 つ選び、x とする。 A から x1 つ削除し、代わりに \left\lfloor \frac{x}{2} \right\rfloor1 つ追加する。(\lfloor x \rfloorx を超えない最大の整数)

あなたの目的は AB を(多重集合として)一致させることです。
目的を達成することが出来るかどうかを判定し、出来る場合は必要な操作回数の最小値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq a_1 \leq \ldots \leq a_N \leq 10^9
  • 0 \leq b_1 \leq \ldots \leq b_N \leq 10^9
  • 入力はすべて整数

入力

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

N
a_1 \ldots a_N
b_1 \ldots b_N

出力

目的を達成出来る場合は必要な操作回数の最小値を出力せよ。出来ない場合は -1 を出力せよ。


入力例 1

3
3 4 5
2 4 6

出力例 1

2

次のようにして 2 回の操作で目的を達成できます。

  • x=3 とし、A から x\, (=3)1 つ削除し代わりに 2x\, (=6)1 つ追加する。これによって A=\{4,5,6\} となる。
  • x=5 とし、A から x\, (=5)1 つ削除し代わりに \left\lfloor \frac{x}{2} \right\rfloor \, (=2)1 つ追加する。これによって A=\{2,4,6\} となる。

入力例 2

1
0
1

出力例 2

-1

\{ 0 \}\{ 1 \} にすることは出来ません。

Score : 600 points

Problem Statement

You are given multisets with N non-negative integers each: A=\{ a_1,\ldots,a_N \} and B=\{ b_1,\ldots,b_N \}.
You can perform the operations below any number of times in any order.

  • Choose a non-negative integer x in A. Delete one instance of x from A and add one instance of 2x instead.
  • Choose a non-negative integer x in A. Delete one instance of x from A and add one instance of \left\lfloor \frac{x}{2} \right\rfloor instead. (\lfloor x \rfloor is the greatest integer not exceeding x.)

Your objective is to make A and B equal (as multisets).
Determine whether it is achievable, and find the minimum number of operations needed to achieve it if it is achievable.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq a_1 \leq \ldots \leq a_N \leq 10^9
  • 0 \leq b_1 \leq \ldots \leq b_N \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 \ldots a_N
b_1 \ldots b_N

Output

If the objective is achievable, print the minimum number of operations needed to achieve it; otherwise, print -1.


Sample Input 1

3
3 4 5
2 4 6

Sample Output 1

2

You can achieve the objective in two operations as follows.

  • Choose x=3 to delete one instance of x\, (=3) from A and add one instance of 2x\, (=6) instead. Now we have A=\{4,5,6\}.
  • Choose x=5 to delete one instance of x\, (=5) from A and add one instance of \left\lfloor \frac{x}{2} \right\rfloor \, (=2) instead. Now we have A=\{2,4,6\}.

Sample Input 2

1
0
1

Sample Output 2

-1

You cannot turn \{ 0 \} into \{ 1 \} .