E - Convex Quadrilateral

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

配点 : 300

問題文

2 次元座標平面があります。x 軸正方向を右向き、y 軸正方向を上向きとします。

この平面上に自己交差のない四角形があります。
4 つの頂点の座標は反時計回りに (A_x,A_y),(B_x,B_y),(C_x,C_y),(D_x,D_y) です。

この四角形が凸であるか判定してください。

なお、四角形の 4 つの内角が全て 180 度未満であるとき、かつ、その時に限り、その四角形は凸であるといいます。

制約

  • -100 \leq A_x,A_y,B_x,B_y,C_x,C_y,D_x,D_y \leq 100
  • 入力に含まれる値は全て整数である
  • 与えられる 4 点は四角形の 4 頂点を反時計回りに並べたものである
  • 与えられる 4 点のなす四角形は自己交差がなく退化していない。すなわち
    • どの 2 頂点も同じ座標にない
    • どの 3 頂点も同一直線上にない
    • 隣接しない 2 辺は共有点を持たない

入力

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

A_x A_y
B_x B_y
C_x C_y
D_x D_y

出力

与えられる四角形が凸なら Yes、凸でないなら No を出力せよ。


入力例 1

0 0
1 0
1 1
0 1

出力例 1

Yes

与えられた四角形は正方形であり、4 つの内角は全て 90 度です。したがって、この四角形は凸です。

図


入力例 2

0 0
1 1
-1 0
1 -1

出力例 2

No

A270 度です。したがって、この四角形は凸ではありません。

図

Score : 300 points

Problem Statement

Consider a two-dimensional coordinate plane, where the x-axis is oriented to the right, and the y-axis is oriented upward.

In this plane, there is a quadrilateral without self-intersection.
The coordinates of the four vertices are (A_x,A_y), (B_x,B_y), (C_x,C_y), and (D_x,D_y), in counter-clockwise order.

Determine whether this quadrilateral is convex.

Here, a quadrilateral is convex if and only if all four interior angles are less than 180 degrees.

Constraints

  • -100 \leq A_x,A_y,B_x,B_y,C_x,C_y,D_x,D_y \leq 100
  • All values in input are integers.
  • The given four points are the four vertices of a quadrilateral in counter-clockwise order.
  • The quadrilateral formed by the given four points has no self-intersection and is non-degenerate. That is,
    • no two vertices are at the same coordinates;
    • no three vertices are colinear; and
    • no two edges that are not adjacent have a common point.

Input

Input is given from Standard Input in the following format:

A_x A_y
B_x B_y
C_x C_y
D_x D_y

Output

If the given quadrilateral is convex, print Yes; otherwise, print No.


Sample Input 1

0 0
1 0
1 1
0 1

Sample Output 1

Yes

The given quadrilateral is a square, whose four interior angles are all 90 degrees. Thus, this quadrilateral is convex.

Figure


Sample Input 2

0 0
1 1
-1 0
1 -1

Sample Output 2

No

The angle A is 270 degrees. Thus, this quadrilateral is not convex.

Figure

F - Reorder Cards

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

配点 : 300

問題文

HW 列の格子状に HW 枚のカードが並べられています。
i=1,\ldots,N について、上から A_i 行目、左から B_i 列目にあるカードには数 i が書かれており、それ以外の HW-N 枚のカードには何も書かれていません。

これらのカードに対し、以下の 2 種類の操作を可能な限り繰り返します。

  • 数の書かれたカードを含まない行が存在するとき、その行のカードを全て取り除き、残りのカードを上へ詰める
  • 数の書かれたカードを含まない列が存在するとき、その列のカードを全て取り除き、残りのカードを左へ詰める

操作が終了したとき、数が書かれたカードがそれぞれどこにあるか求めてください。なお、答えは操作の仕方に依らず一意に定まることが証明されます。

制約

  • 1 \leq H,W \leq 10^9
  • 1 \leq N \leq \min(10^5,HW)
  • 1 \leq A_i \leq H
  • 1 \leq B_i \leq W
  • (A_i,B_i) は相異なる
  • 入力に含まれる値は全て整数である

入力

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

H W N
A_1 B_1
\vdots
A_N B_N

出力

N 行出力せよ。

操作終了後に数 i が書かれたカードが上から C_i 行目、左から D_i 列目に存在するとき、i 行目には C_i,D_i をこの順に空白区切りで出力せよ。


入力例 1

4 5 2
3 2
2 5

出力例 1

2 1
1 2

何も書かれていないカードを * で表すことにします。最初、カードの配置は以下の通りです。

*****
****2
*1***
*****

操作終了後、カードの配置は以下の通りになります。

*2
1*

1 が書かれたカードは上から 2 行目、左から 1 列目にあり、2 が書かれたカードは上から 1 行目、左から 2 列目にあります。


入力例 2

1000000000 1000000000 10
1 1
10 10
100 100
1000 1000
10000 10000
100000 100000
1000000 1000000
10000000 10000000
100000000 100000000
1000000000 1000000000

出力例 2

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

Score : 300 points

Problem Statement

We have HW cards arranged in a matrix with H rows and W columns.
For each i=1, \ldots, N, the card at the A_i-th row from the top and B_i-th column from the left has a number i written on it. The other HW-N cards have nothing written on them.

We will repeat the following two kinds of operations on these cards as long as we can.

  • If there is a row without a numbered card, remove all the cards in that row and fill the gap by shifting the remaining cards upward.
  • If there is a column without a numbered card, remove all the cards in that column and fill the gap by shifting the remaining cards to the left.

Find the position of each numbered card after the end of the process above. It can be proved that these positions are uniquely determined without depending on the order in which the operations are done.

Constraints

  • 1 \leq H,W \leq 10^9
  • 1 \leq N \leq \min(10^5,HW)
  • 1 \leq A_i \leq H
  • 1 \leq B_i \leq W
  • All pairs (A_i,B_i) are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
A_1 B_1
\vdots
A_N B_N

Output

Print N lines.

If, after the end of the process, the card with the number i is at the C_i-th row from the top and D_i-th column from the left, the i-th line should contain C_i and D_i in this order with a space between them.


Sample Input 1

4 5 2
3 2
2 5

Sample Output 1

2 1
1 2

Let * represent a card with nothing written on it. The initial arrangement of cards is:

*****
****2
*1***
*****

After the end of the process, they will be arranged as follows:

*2
1*

Here, the card with 1 is at the 2-nd row from the top and 1-st column from the left, and the card with 2 is at the 1-st row from the top and 2-nd column from the left.


Sample Input 2

1000000000 1000000000 10
1 1
10 10
100 100
1000 1000
10000 10000
100000 100000
1000000 1000000
10000000 10000000
100000000 100000000
1000000000 1000000000

Sample Output 2

1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
G - Yet Another Recursive Function

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

配点 : 400

問題文

非負整数 x に対し定義される関数 f(x) は以下の条件を満たします。

  • f(0) = 1
  • 任意の正整数 k に対し f(k) = f(\lfloor \frac{k}{2}\rfloor) + f(\lfloor \frac{k}{3}\rfloor)

ここで、\lfloor A \rfloorA の小数点以下を切り捨てた値を指します。

このとき、 f(N) を求めてください。

制約

  • N0 \le N \le 10^{18} を満たす整数

入力

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

N

出力

答えを出力せよ。


入力例 1

2

出力例 1

3

f(2) = f(\lfloor \frac{2}{2}\rfloor) + f(\lfloor \frac{2}{3}\rfloor) = f(1) + f(0) =(f(\lfloor \frac{1}{2}\rfloor) + f(\lfloor \frac{1}{3}\rfloor)) + f(0) =(f(0)+f(0)) + f(0)= 3 です。


入力例 2

0

出力例 2

1

入力例 3

100

出力例 3

55

Score : 400 points

Problem Statement

A function f(x) defined for non-negative integers x satisfies the following conditions.

  • f(0) = 1.
  • f(k) = f(\lfloor \frac{k}{2}\rfloor) + f(\lfloor \frac{k}{3}\rfloor) for any positive integer k.

Here, \lfloor A \rfloor denotes the value of A rounded down to an integer.

Find f(N).

Constraints

  • N is an integer satisfying 0 \le N \le 10^{18}.

Input

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

N

Output

Print the answer.


Sample Input 1

2

Sample Output 1

3

We have f(2) = f(\lfloor \frac{2}{2}\rfloor) + f(\lfloor \frac{2}{3}\rfloor) = f(1) + f(0) =(f(\lfloor \frac{1}{2}\rfloor) + f(\lfloor \frac{1}{3}\rfloor)) + f(0) =(f(0)+f(0)) + f(0)= 3.


Sample Input 2

0

Sample Output 2

1

Sample Input 3

100

Sample Output 3

55
H - Geometric Progression

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

配点 : 500

問題文

整数 A, X, M が与えられます。\displaystyle \sum_{i = 0}^{X-1} A^iM で割った余りを求めてください。

制約

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • 入力はすべて整数

入力

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

A X M

出力

答えを出力せよ。


入力例 1

3 4 7

出力例 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40 です。407 で割った余りは 5 であるため、5 を出力します。


入力例 2

8 10 9

出力例 2

0

入力例 3

1000000000 1000000000000 998244353

出力例 3

919667211

Score : 500 points

Problem Statement

Given integers A, X, and M, find \displaystyle \sum_{i = 0}^{X-1} A^i, modulo M.

Constraints

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • All values in the input are integers.

Input

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

A X M

Output

Print the answer.


Sample Input 1

3 4 7

Sample Output 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40, which equals 5 modulo 7, so 5 should be printed.


Sample Input 2

8 10 9

Sample Output 2

0

Sample Input 3

1000000000 1000000000000 998244353

Sample Output 3

919667211
I - Rectangle GCD

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

配点 : 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