E - Loong Tracking

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は座標平面上で龍を操作するゲームを作成しました。

龍は 1 から N までの番号がついた N 個のパーツからなり、パーツ 1 と呼びます。

最初パーツ i は座標 (i,0) にあります。以下のクエリを Q 個処理してください。

  • 1 C: 頭を方向 C1 移動させる。ここで、CR, L, U, D のいずれかであり、それぞれ x 軸正方向、x 軸負方向、y 軸正方向、y 軸負方向を意味する。頭以外の全てのパーツは前のパーツに追従するように動く。すなわち、パーツ i\ \ (2\leq i \leq N) は移動前にパーツ i-1 があった座標に移動する。
  • 2 p: パーツ p のある座標を求める。

制約

  • 2 \leq N \leq 10^6
  • 1 \leq Q \leq 2\times 10^5
  • 1 種類目のクエリにおいて、CR, L, U, D のいずれか
  • 2 種類目のクエリにおいて、1\leq p \leq N
  • 入力に含まれる数値は全て整数

入力

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

N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

各クエリは以下の 2 種類のいずれかの形式である。

1 C
2 p

出力

2 種類目のクエリの回数を q として q 行出力せよ。
i 行目には、i 番目のそのようなクエリに対する答えの座標を (x,y) としたとき、x,y を空白区切りで出力せよ。


入力例 1

5 9
2 3
1 U
2 3
1 R
1 D
2 3
1 L
2 1
2 5

出力例 1

3 0
2 0
1 1
1 0
1 0

2 種類目のクエリを処理する各タイミングにおいて、パーツの位置は次のようになっています。

図

複数のパーツが同じ座標に存在しうることに注意してください。

Score : 300 points

Problem Statement

Takahashi has created a game where the player controls a dragon on a coordinate plane.

The dragon consists of N parts numbered 1 to N, with part 1 being called the head.

Initially, part i is located at the coordinates (i,0). Process Q queries as follows.

  • 1 C: Move the head by 1 in direction C. Here, C is one of R, L, U, and D, which represent the positive x-direction, negative x-direction, positive y-direction, and negative y-direction, respectively. Each part other than the head moves to follow the part in front of it. That is, part i (2\leq i \leq N) moves to the coordinates where part i-1 was before the move.
  • 2 p: Find the coordinates of part p.

Constraints

  • 2 \leq N \leq 10^6
  • 1 \leq Q \leq 2\times 10^5
  • For the first type of query, C is one of R, L, U, and D.
  • For the second type of query, 1\leq p \leq N.
  • All numerical input values are integers.

Input

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

N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

Each query is in one of the following two formats:

1 C
2 p

Output

Print q lines, where q is the number of queries of the second type.
The i-th line should contain x and y separated by a space, where (x,y) are the answer to the i-th such query.


Sample Input 1

5 9
2 3
1 U
2 3
1 R
1 D
2 3
1 L
2 1
2 5

Sample Output 1

3 0
2 0
1 1
1 0
1 0

At each time when processing the second type of query, the parts are at the following positions:

Figure

Note that multiple parts may exist at the same coordinates.

F - Triangle?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

xy 平面上に 1 から N までの番号が付いた N 個の点があります。
i は座標 (X_i,Y_i) にあり、相異なる 2 点は異なる位置に存在します。
この N 点から 3 点を選ぶとき、選ばれた 3 点を線分で結んだ図形が正の面積を持つ三角形になるような点の選び方の総数を求めてください。

制約

  • 入力は全て整数である
  • 3 \le N \le 300
  • -10^9 \le X_i,Y_i \le 10^9
  • i \neq j ならば (X_i,Y_i) \neq (X_j,Y_j)

入力

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

N
X_1 Y_1
X_2 Y_2
\dots
X_N Y_N

出力

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


入力例 1

4
0 1
1 3
1 1
-1 -1

出力例 1

3

点を図示すると、以下のようになります。

三角形をなすような点の選び方は、 \{1,2,3\},\{1,3,4\},\{2,3,4\}3 つです。


入力例 2

20
224 433
987654321 987654321
2 0
6 4
314159265 358979323
0 0
-123456789 123456789
-1000000000 1000000000
124 233
9 -6
-4 0
9 5
-7 3
333333333 -333333333
-9 -1
7 -10
-1 5
324 633
1000000000 -1000000000
20 0

出力例 2

1124

Score : 300 points

Problem Statement

In the xy-plane, we have N points numbered 1 through N.
Point i is at the coordinates (X_i,Y_i). Any two different points are at different positions.
Find the number of ways to choose three of these N points so that connecting the chosen points with segments results in a triangle with a positive area.

Constraints

  • All values in input are integers.
  • 3 \le N \le 300
  • -10^9 \le X_i,Y_i \le 10^9
  • (X_i,Y_i) \neq (X_j,Y_j) if i \neq j.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
\dots
X_N Y_N

Output

Print the answer as an integer.


Sample Input 1

4
0 1
1 3
1 1
-1 -1

Sample Output 1

3

The figure below illustrates the points.

There are three ways to choose points that form a triangle: \{1,2,3\},\{1,3,4\},\{2,3,4\}.


Sample Input 2

20
224 433
987654321 987654321
2 0
6 4
314159265 358979323
0 0
-123456789 123456789
-1000000000 1000000000
124 233
9 -6
-4 0
9 5
-7 3
333333333 -333333333
-9 -1
7 -10
-1 5
324 633
1000000000 -1000000000
20 0

Sample Output 2

1124
G - I Hate Non-integer Number

Time Limit: 2.5 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

項数が N の正整数列 A=(a_1,\ldots,a_N) が与えられます。
A の項を 1 個以上選ぶ方法は 2^N-1 通りありますが、そのうち選んだ項の平均値が整数であるものが何通りかを 998244353 で割った余りを求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq a_i \leq 10^9
  • 入力はすべて整数

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

3
2 6 2

出力例 1

6

A の項を選ぶ方法それぞれに対する平均値は以下のようになります。

  • a_1 のみを選んだ場合、平均値は \frac{a_1}{1}=\frac{2}{1} = 2 であり、整数である。

  • a_2 のみを選んだ場合、平均値は \frac{a_2}{1}=\frac{6}{1} = 6 であり、整数である。

  • a_3 のみを選んだ場合、平均値は \frac{a_3}{1}=\frac{2}{1} = 2 であり、整数である。

  • a_1a_2 を選んだ場合、平均値は \frac{a_1+a_2}{2}=\frac{2+6}{2} = 4 であり、整数である。

  • a_1a_3 を選んだ場合、平均値は \frac{a_1+a_3}{2}=\frac{2+2}{2} = 2 であり、整数である。

  • a_2a_3 を選んだ場合、平均値は \frac{a_2+a_3}{2}=\frac{6+2}{2} = 4 であり、整数である。

  • a_1a_2a_3 を選んだ場合、平均値は \frac{a_1+a_2+a_3}{3}=\frac{2+6+2}{3} = \frac{10}{3} であり、整数ではない。

以上より、6 通りの選び方が条件を満たします。


入力例 2

5
5 5 5 5 5

出力例 2

31

どのように A の項を 1 個以上選んでも平均値が 5 になります。

Score : 400 points

Problem Statement

You are given a sequence of positive integers A=(a_1,\ldots,a_N) of length N.
There are (2^N-1) ways to choose one or more terms of A. How many of them have an integer-valued average? Find the count modulo 998244353.

Constraints

  • 1 \leq N \leq 100
  • 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
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

3
2 6 2

Sample Output 1

6

For each way to choose terms of A, the average is obtained as follows:

  • If just a_1 is chosen, the average is \frac{a_1}{1}=\frac{2}{1} = 2, which is an integer.

  • If just a_2 is chosen, the average is \frac{a_2}{1}=\frac{6}{1} = 6, which is an integer.

  • If just a_3 is chosen, the average is \frac{a_3}{1}=\frac{2}{1} = 2, which is an integer.

  • If a_1 and a_2 are chosen, the average is \frac{a_1+a_2}{2}=\frac{2+6}{2} = 4, which is an integer.

  • If a_1 and a_3 are chosen, the average is \frac{a_1+a_3}{2}=\frac{2+2}{2} = 2, which is an integer.

  • If a_2 and a_3 are chosen, the average is \frac{a_2+a_3}{2}=\frac{6+2}{2} = 4, which is an integer.

  • If a_1, a_2, and a_3 are chosen, the average is \frac{a_1+a_2+a_3}{3}=\frac{2+6+2}{3} = \frac{10}{3}, which is not an integer.

Therefore, 6 ways satisfy the condition.


Sample Input 2

5
5 5 5 5 5

Sample Output 2

31

Regardless of the choice of one or more terms of A, the average equals 5.

H - Distinct Adjacent

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

1 から N の番号がついた N 人の人が輪になってならんでいます。人 1 の右隣には人 2 が、人 2 の右隣には人 3 が、……、人 N の右隣には人 1 がいます。

N 人の人にそれぞれ 0 以上 M 未満の整数を 1 つずつ渡します。
M^N 通りの渡し方のうち、どの隣り合う 2 人が渡された数も異なるものの数を、998244353 で割ったあまりを求めてください。

制約

  • 2 \leq N,M \leq 10^6
  • N,M は整数である

入力

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

N M

出力

答えを出力せよ。


入力例 1

3 3

出力例 1

6

1,2,3 に渡す整数がそれぞれ (0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,0,1),(2,1,0) のときの 6 通りです。


入力例 2

4 2

出力例 2

2

1,2,3,4 に渡す整数がそれぞれ (0,1,0,1),(1,0,1,0) のときの 2 通りです。


入力例 3

987654 456789

出力例 3

778634319

998244353 で割ったあまりを求めてください。

Score : 475 points

Problem Statement

There are N people numbered from 1 to N standing in a circle. Person 1 is to the right of person 2, person 2 is to the right of person 3, ..., and person N is to the right of person 1.

We will give each of the N people an integer between 0 and M-1, inclusive.
Among the M^N ways to distribute integers, find the number, modulo 998244353, of such ways that no two adjacent people have the same integer.

Constraints

  • 2 \leq N,M \leq 10^6
  • N and M are integers.

Input

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

N M

Output

Print the answer.


Sample Input 1

3 3

Sample Output 1

6

There are six desired ways, where the integers given to persons 1,2,3 are (0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,0,1),(2,1,0).


Sample Input 2

4 2

Sample Output 2

2

There are two desired ways, where the integers given to persons 1,2,3,4 are (0,1,0,1),(1,0,1,0).


Sample Input 3

987654 456789

Sample Output 3

778634319

Be sure to find the number modulo 998244353.

I - InterSections

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

1 から N までの番号のついた N 個の区間が与えられます。 区間 i[L_i,R_i] です。

区間 [l_a,r_a] と区間 [l_b,r_b](l_a < l_b < r_a < r_b) または (l_b < l_a < r_b < r_a) を満たすとき、交差するといいます。

f(l,r)1 \leq i \leq N を満たし、区間 [l,r] と区間 i が交差する i の個数と定義します。

0 \leq l < r \leq 10^{9} を満たす整数の組 (l,r) において、 f(l,r) の最大値を達成する (l,r) の組のうち l が最小のものを答えてください。そのような組が複数存在する場合はさらにそのうちで r が最小のものを答えてください (0 \leq l < r より、 答えるべき (l,r) の組は一意に定まります)。

制約

  • 1 \leq N \leq 10^{5}
  • 0 \leq L_i < R_i \leq 10^{9} (1 \leq i \leq N)
  • 入力は全て整数である

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

出力

答えとなる組 (l,r) を次の形式で出力せよ。

l r

入力例 1

5
1 7
3 9
7 18
10 14
15 20

出力例 1

4 11

f(l,r) の最大値は 4 であり、f(l,r)=4 となる (l,r) のうち l の最小値は 4 です。 f(l,r)=4 かつ l=4 を満たす (l,r) は以下の 5 通りです。

  • (l,r)=(4,11)
  • (l,r)=(4,12)
  • (l,r)=(4,13)
  • (l,r)=(4,16)
  • (l,r)=(4,17)

このうち、r の最小値は 11 であるため、411 を出力します。


入力例 2

11
856977192 996441446
298251737 935869360
396653206 658841528
710569907 929136831
325371222 425309117
379628374 697340458
835681913 939343451
140179224 887672320
375607390 611397526
93530028 581033295
249611310 775998537

出力例 2

396653207 887672321

Score : 550 points

Problem Statement

You are given N intervals numbered 1 to N. Interval i is [L_i, R_i].

Two intervals [l_a, r_a] and [l_b, r_b] are said to intersect if and only if they satisfy either (l_a < l_b < r_a < r_b) or (l_b < l_a < r_b < r_a).

Define f(l, r) as the number of intervals i (1 \leq i \leq N) that intersect with the interval [l, r].

Among all pairs of integers (l, r) satisfying 0 \leq l < r \leq 10^{9}, find the pair (l, r) that maximizes f(l, r). If there are multiple such pairs, choose the one with the smallest l. If there are still multiple pairs, choose the one with the smallest r among them. (Since 0 \leq l < r, the pair (l, r) to be answered is uniquely determined.)

Constraints

  • 1 \leq N \leq 10^{5}
  • 0 \leq L_i < R_i \leq 10^{9} (1 \leq i \leq N)
  • All input values are integers.

Input

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the sought pair (l, r) in the following format:

l r

Sample Input 1

5
1 7
3 9
7 18
10 14
15 20

Sample Output 1

4 11

The maximum value of f(l, r) is 4, and among the pairs (l, r) that achieve f(l, r) = 4, the smallest l is 4. The pairs (l, r) that satisfy f(l, r) = 4 and l = 4 are the following five:

  • (l, r) = (4, 11)
  • (l, r) = (4, 12)
  • (l, r) = (4, 13)
  • (l, r) = (4, 16)
  • (l, r) = (4, 17)

Among these, the smallest r is 11, so print 4 and 11.


Sample Input 2

11
856977192 996441446
298251737 935869360
396653206 658841528
710569907 929136831
325371222 425309117
379628374 697340458
835681913 939343451
140179224 887672320
375607390 611397526
93530028 581033295
249611310 775998537

Sample Output 2

396653207 887672321