E - Even Digits

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

非負整数 n が次の条件を満たすとき、n良い整数 と呼びます。

  • n10 進法で表したときに、偶数の数字 (0, 2, 4, 6, 8) のみが登場する。

例えば 068 および 2024 は良い整数です。

整数 N が与えられます。良い整数のうち小さい方から N 番目の整数を求めてください。

制約

  • 1 \leq N \leq 10^{12}
  • N は整数

入力

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

N

出力

小さい方から N 番目の良い整数を出力せよ。


入力例 1

8

出力例 1

24

良い整数を小さい方から順に並べると 0, 2, 4, 6, 8, 20, 22, 24, 26, 28, \dots となります。
小さい方から 8 番目の良い整数は 24 なので、これを出力します。


入力例 2

133

出力例 2

2024

入力例 3

31415926535

出力例 3

2006628868244228

Score: 300 points

Problem Statement

A non-negative integer n is called a good integer when it satisfies the following condition:

  • All digits in the decimal notation of n are even numbers (0, 2, 4, 6, and 8).

For example, 0, 68, and 2024 are good integers.

You are given an integer N. Find the N-th smallest good integer.

Constraints

  • 1 \leq N \leq 10^{12}
  • N is an integer.

Input

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

N

Output

Print the N-th smallest good integer.


Sample Input 1

8

Sample Output 1

24

The good integers in ascending order are 0, 2, 4, 6, 8, 20, 22, 24, 26, 28, \dots.
The eighth smallest is 24, which should be printed.


Sample Input 2

133

Sample Output 2

2024

Sample Input 3

31415926535

Sample Output 3

2006628868244228
F - Four Variables

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

正整数 N が与えられます。
正整数の組 (A,B,C,D) であって、AB + CD = N を満たすものの個数を求めてください。

なお、本問の制約の下、答えが 9 \times 10^{18} 以下であることが証明できます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N は整数

入力

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

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

8

(A,B,C,D) として以下の 8 個が考えられます。

  • (A,B,C,D)=(1,1,1,3)
  • (A,B,C,D)=(1,1,3,1)
  • (A,B,C,D)=(1,2,1,2)
  • (A,B,C,D)=(1,2,2,1)
  • (A,B,C,D)=(1,3,1,1)
  • (A,B,C,D)=(2,1,1,2)
  • (A,B,C,D)=(2,1,2,1)
  • (A,B,C,D)=(3,1,1,1)

入力例 2

292

出力例 2

10886

入力例 3

19876

出力例 3

2219958

Score : 300 points

Problem Statement

You are given a positive integer N.
Find the number of quadruples of positive integers (A,B,C,D) such that AB + CD = N.

Under the constraints of this problem, it can be proved that the answer is at most 9 \times 10^{18}.

Constraints

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

Input

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

N

Output

Print the answer.


Sample Input 1

4

Sample Output 1

8

Here are the eight desired quadruples.

  • (A,B,C,D)=(1,1,1,3)
  • (A,B,C,D)=(1,1,3,1)
  • (A,B,C,D)=(1,2,1,2)
  • (A,B,C,D)=(1,2,2,1)
  • (A,B,C,D)=(1,3,1,1)
  • (A,B,C,D)=(2,1,1,2)
  • (A,B,C,D)=(2,1,2,1)
  • (A,B,C,D)=(3,1,1,1)

Sample Input 2

292

Sample Output 2

10886

Sample Input 3

19876

Sample Output 3

2219958
G - 1D Country

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

数直線上に N 個の村があります。i 番目の村は座標 X_i にあり、P_i 人の村人がいます。

Q 個のクエリに答えてください。i 番目のクエリは以下の形式です。

  • 整数 L_i,R_i が与えられる。座標が L_i 以上 R_i 以下の村に住んでいる村人の人数の総数を求めよ。

制約

  • 1\leq N,Q\leq 2\times 10^5
  • -10^9\leq X_1 < X_2 < \ldots < X_N \leq 10^9
  • 1\leq P_i\leq 10^9
  • -10^9\leq L_i \leq R_i \leq 10^9
  • 入力される数値は全て整数

入力

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

N
X_1 \ldots X_N
P_1 \ldots P_N
Q
L_1 R_1
\vdots
L_Q R_Q 

出力

Q 行出力せよ。

i\ (1\leq i \leq Q) 行目には、i 番目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

1
5
10
0

1 番目のクエリについて考えます。座標が 1 以上 1 以下の村は、座標 1 にある村で、村人は 1 人います。よって答えは 1 です。

2 番目のクエリについて考えます。座標が 2 以上 6 以下の村は、座標 3 にある村と座標 5 にある村で、村人はそれぞれ 2 人と 3 人います。よって答えは 2+3=5 です。


入力例 2

7
-10 -5 -3 -1 0 1 4
2 5 6 5 2 1 7
8
-7 7
-1 5
-10 -4
-8 10
-5 0
-10 5
-8 7
-8 -3

出力例 2

26
15
7
26
18
28
26
11

Score : 350 points

Problem Statement

There are N villages on a number line. The i-th village is located at coordinate X_i, and has P_i villagers.

Answer Q queries. The i-th query is in the following format:

  • Given integers L_i and R_i, find the total number of villagers living in villages located between coordinates L_i and R_i, inclusive.

Constraints

  • 1\leq N,Q\leq 2\times 10^5
  • -10^9\leq X_1 < X_2 < \ldots < X_N \leq 10^9
  • 1\leq P_i\leq 10^9
  • -10^9\leq L_i \leq R_i \leq 10^9
  • All input values are integers.

Input

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

N
X_1 \ldots X_N
P_1 \ldots P_N
Q
L_1 R_1
\vdots
L_Q R_Q

Output

Print Q lines.

The i-th line(1\leq i \leq Q) should contain the answer to the i-th query.


Sample Input 1

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

Sample Output 1

1
5
10
0

Consider the first query. The villages between coordinates 1 and 1 are the village at coordinate 1, with 1 villager. Hence, the answer is 1.

Consider the second query. The villages between coordinates 2 and 6 are the villages at coordinates 3 and 5, with 2 and 3 villagers, respectively. Hence, the answer is 2+3=5.


Sample Input 2

7
-10 -5 -3 -1 0 1 4
2 5 6 5 2 1 7
8
-7 7
-1 5
-10 -4
-8 10
-5 0
-10 5
-8 7
-8 -3

Sample Output 2

26
15
7
26
18
28
26
11
H - Tangency of Cuboids

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

3 次元空間内に N 個の直方体があります。

直方体同士は重なっていません。厳密には、相異なるどの 2 つの直方体の共通部分の体積も 0 です。

i 番目の直方体は、2(X_{i,1},Y_{i,1},Z_{i,1}), (X_{i,2},Y_{i,2},Z_{i,2}) を結ぶ線分を対角線とし、辺は全ていずれかの座標軸に平行です。

各直方体について、他のいくつの直方体と面で接しているか求めてください。
厳密には、各 i に対し、1\leq j \leq N かつ j\neq i である j のうち、i 番目の直方体の表面と j 番目の直方体の表面の共通部分の面積が正であるものの個数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq X_{i,1} < X_{i,2} \leq 100
  • 0 \leq Y_{i,1} < Y_{i,2} \leq 100
  • 0 \leq Z_{i,1} < Z_{i,2} \leq 100
  • 直方体同士は体積が正の共通部分を持たない
  • 入力は全て整数である

入力

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

N
X_{1,1} Y_{1,1} Z_{1,1} X_{1,2} Y_{1,2} Z_{1,2}
\vdots
X_{N,1} Y_{N,1} Z_{N,1} X_{N,2} Y_{N,2} Z_{N,2}

出力

答えを出力せよ。


入力例 1

4
0 0 0 1 1 1
0 0 1 1 1 2
1 1 1 2 2 2
3 3 3 4 4 4

出力例 1

1
1
0
0

1 番目の直方体と 2 番目の直方体は、2(0,0,1),(1,1,1) を結ぶ線分を対角線とする長方形を共有しています。
1 番目の直方体と 3 番目の直方体は、点 (1,1,1) を共有していますが、面で接してはいません。


入力例 2

3
0 0 10 10 10 20
3 4 1 15 6 10
0 9 6 1 20 10

出力例 2

2
1
1

入力例 3

8
0 0 0 1 1 1
0 0 1 1 1 2
0 1 0 1 2 1
0 1 1 1 2 2
1 0 0 2 1 1
1 0 1 2 1 2
1 1 0 2 2 1
1 1 1 2 2 2

出力例 3

3
3
3
3
3
3
3
3

Score : 500 points

Problem Statement

There are N rectangular cuboids in a three-dimensional space.

These cuboids do not overlap. Formally, for any two different cuboids among them, their intersection has a volume of 0.

The diagonal of the i-th cuboid is a segment that connects two points (X_{i,1},Y_{i,1},Z_{i,1}) and (X_{i,2},Y_{i,2},Z_{i,2}), and its edges are all parallel to one of the coordinate axes.

For each cuboid, find the number of other cuboids that share a face with it.
Formally, for each i, find the number of j with 1\leq j \leq N and j\neq i such that the intersection of the surfaces of the i-th and j-th cuboids has a positive area.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq X_{i,1} < X_{i,2} \leq 100
  • 0 \leq Y_{i,1} < Y_{i,2} \leq 100
  • 0 \leq Z_{i,1} < Z_{i,2} \leq 100
  • Cuboids do not have an intersection with a positive volume.
  • All input values are integers.

Input

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

N
X_{1,1} Y_{1,1} Z_{1,1} X_{1,2} Y_{1,2} Z_{1,2}
\vdots
X_{N,1} Y_{N,1} Z_{N,1} X_{N,2} Y_{N,2} Z_{N,2}

Output

Print the answer.


Sample Input 1

4
0 0 0 1 1 1
0 0 1 1 1 2
1 1 1 2 2 2
3 3 3 4 4 4

Sample Output 1

1
1
0
0

The 1-st and 2-nd cuboids share a rectangle whose diagonal is the segment connecting two points (0,0,1) and (1,1,1).
The 1-st and 3-rd cuboids share a point (1,1,1), but do not share a surface.


Sample Input 2

3
0 0 10 10 10 20
3 4 1 15 6 10
0 9 6 1 20 10

Sample Output 2

2
1
1

Sample Input 3

8
0 0 0 1 1 1
0 0 1 1 1 2
0 1 0 1 2 1
0 1 1 1 2 2
1 0 0 2 1 1
1 0 1 2 1 2
1 1 0 2 2 1
1 1 1 2 2 2

Sample Output 3

3
3
3
3
3
3
3
3
I - Pay or Receive

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

1,\ldots,N の番号がついた N 個の街と、1,\ldots,M の番号がついた M 本の道路があります。

道路 i は街 A_iB_i を結んでいます。道路を通行すると、所持している ポイント が次の通り増減します。

  • 道路 i を使って、街 A_i から街 B_i に移動するときにはポイントが C_i 増加し、街 B_i から街 A_i に移動するときにはポイントが C_i 減少する。

所持しているポイントは負にもなりえます。

次の Q 個の質問に答えてください。

  • 所持しているポイントが 0 である状態で街 X_i から移動を始めたとき、街 Y_i にいる状態で所持しているポイントの最大値を出力せよ。
    ただし、街 X_i から街 Y_i に到達できないときは nan、街 Y_i にいる状態で所持しているポイントをいくらでも増やせるときは inf を代わりに出力せよ。

制約

  • 2\leq N \leq 10^5
  • 0\leq M \leq 10^5
  • 1\leq Q \leq 10^5
  • 1\leq A_i,B_i,X_i,Y_i \leq N
  • 0\leq C_i \leq 10^9
  • 入力は全て整数である

入力

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

N M Q
A_1 B_1 C_1
\vdots
A_M B_M C_M
X_1 Y_1
\vdots
X_Q Y_Q

出力

問題文の指示通りに Q 行出力せよ。
i 行目には i 番目の質問に対する答えを出力せよ。


入力例 1

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

出力例 1

-2
inf
nan

1 番目の質問では、道路 5 を使って街 5 から街 3 に移動すると、ポイントを -2 所持している状態で街 3 にいることができます。
これ以上ポイントを大きくすることはできないので答えは -2 になります。

2 番目の質問では、「道路 2 を使って街 1 から街 2 に移動し、道路 1 を使って街 2 から街 1 に移動する」 という行動を好きなだけ繰り返したあと、道路 2 を使って街 1 から街 2 に移動することで、 街 2 にいる状態で所持しているポイントをいくらでも増やすことができます。

3 番目の質問では、街 3 から移動を始めて街 1 へ到達することはできません。


入力例 2

2 1 1
1 1 1
1 1

出力例 2

inf

始点と終点が同じ街である道路や、始点と終点が同じ街である質問が含まれることもあります。


入力例 3

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

出力例 3

inf
nan
nan
inf
-9

Score : 500 points

Problem Statement

There are N towns numbered 1,\ldots,N and M roads numbered 1,\ldots,M.

Road i connects towns A_i and B_i. When you use a road, your score changes as follows:

  • when you move from town A_i to town B_i using road i, your score increases by C_i; when you move from town B_i to town A_i using road i, your score decreases by C_i.

Your score may become negative.

Answer the following Q questions.

  • If you start traveling from town X_i with initial score 0, find the maximum possible score when you are at town Y_i.
    Here, if you cannot get from town X_i to town Y_i, print nan instead; if you can have as large a score as you want when you are at town Y_i, print inf instead.

Constraints

  • 2\leq N \leq 10^5
  • 0\leq M \leq 10^5
  • 1\leq Q \leq 10^5
  • 1\leq A_i,B_i,X_i,Y_i \leq N
  • 0\leq C_i \leq 10^9
  • All values in the input are integers.

Input

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

N M Q
A_1 B_1 C_1
\vdots
A_M B_M C_M
X_1 Y_1
\vdots
X_Q Y_Q

Output

Print Q lines as specified in the Problem Statement.
The i-th line should contain the answer to the i-th question.


Sample Input 1

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

Sample Output 1

-2
inf
nan

For the first question, if you use road 5 to move from town 5 to town 3, you can have a score -2 when you are at town 3.
Since you cannot make the score larger, the answer is -2.

For the second question, you can have as large a score as you want when you are at town 2 if you travel as follows: repeatedly "use road 2 to move from town 1 to town 2 and then use road 1 to move from town 2 to town 1" as many times as you want, and finally use road 2 to move from town 1 to town 2.

For the third question, you cannot get from town 3 to town 1.


Sample Input 2

2 1 1
1 1 1
1 1

Sample Output 2

inf

The endpoints of a road may be the same, and so may the endpoints given in a question.


Sample Input 3

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

Sample Output 3

inf
nan
nan
inf
-9