A - Three cards

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 9

問題文

3 枚のカードがあります。 1 枚目のカードには整数 A が、2 枚目のカードには整数 B が、3 枚目のカードには整数 C が書かれています。

3 枚のカードの中から 2 枚のカードを選ぶとき、選んだ 2 枚に書かれた整数の積としてあり得る最小値と最大値を出力して下さい。

制約

  • -100 \leq A, B, C \leq 100
  • A, B, C は整数

入力

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

A B C

出力

選んだ 2 枚に書かれた整数の積としてあり得る最小値 X と最大値 Y を、下記の通りに空白区切りで出力せよ。

X Y

入力例 1

5 -2 4

出力例 1

-10 20

選んだ 2 枚に書かれた整数の積は、

  • 1 枚目と 2 枚目のカードを選んだとき、5 \times (-2) = -10
  • 1 枚目と 3 枚目のカードを選んだとき、5 \times 4 = 20
  • 2 枚目と 3 枚目のカードを選んだとき、(-2) \times 4 = -8

です。よって、選んだ 2 枚に書かれた整数の積としてあり得る最小値は -10 、最大値は 20 です。


入力例 2

0 0 0

出力例 2

0 0

Score : 9 points

Problem Statement

There are 3 cards. The 1-st card has an integer A written on it; the 2-nd has an integer B; the 3-rd has an integer C.

When choosing 2 cards out of these 3 cards, find the minimum and maximum possible products of the integers written on the chosen 2 cards.

Constraints

  • -100 \leq A, B, C \leq 100
  • A, B, and C are integers

Input

Input is given from Standard Input in the following format:

A B C

Output

Find the minimum possible product X and the maximum possible product Y of the integers written on the chosen 2 cards in the following format, with a space in between.

X Y

Sample Input 1

5 -2 4

Sample Output 1

-10 20

The product of the integers written on the chosen 2 cards will be

  • 5 \times (-2) = -10 if the 1-st and the 2-nd cards were chosen;
  • 5 \times 4 = 20 if the 1-st and the 3-rd cards were chosen;
  • (-2) \times 4 = -8 if the 2-nd and the 3-rd cards were chosen.

Therefore, the minimum possible product of the 2 integers written on the chosen 2 cards are -10, and the maximum is 20.


Sample Input 2

0 0 0

Sample Output 2

0 0
B - Flowers

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 8

問題文

高橋君は、A 本の赤い花と B 本の青い花からなる花束をたくさん作りたいです。

赤い花を X 本、青い花を Y 本持っているとき、最大で何個の花束を作れますか?

制約

  • 1 \leq A,B \leq 100
  • 0 \leq X,Y \leq 100
  • 入力は全て整数

入力

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

A B X Y

出力

作れる花束の個数の最大値を出力せよ。


入力例 1

3 2 6 4

出力例 1

2

花束は 2 つ作ることが可能です。


入力例 2

3 3 100 0

出力例 2

0

入力例 3

5 10 30 23

出力例 3

2

Score : 8 points

Problem Statement

Takahashi wants to make many bouquets. Each bouquet should consist of A red flowers and B blue flowers.

Takahashi has X red flowers and Y blue flowers. How many bouquets can he make at most?

Constraints

  • 1 \leq A,B \leq 100
  • 0 \leq X,Y \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B X Y

Output

Print the maximum number of bouquets that Takahashi can make.


Sample Input 1

3 2 6 4

Sample Output 1

2

He can make 2 bouquets.


Sample Input 2

3 3 100 0

Sample Output 2

0

Sample Input 3

5 10 30 23

Sample Output 3

2
C - Go Further

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 8

問題文

数字と英小文字の変数からなる単項式が 良い単項式 であるとは、英小文字 1 文字の変数の左に 1 以上 999 以下の整数が係数として掛けられているような式を指します。ただし、係数が 1 であっても、良い単項式ではそれを省略せずに書くものとします。
例えば、 123a1z は良い単項式ですが、 0a1000b12312aba123a などは良い単項式ではありません。

さらに、良い単項式の変数に

  • a=1000
  • b=1000a
  • \dots
  • z=1000y

で定まる値を代入したときの値をその良い単項式の値とします。
例えば、 123a の値は 12300020b の値は 200000001c の値は 1000000000 となります。

良い単項式の値としてあり得るもののうち、 \alpha 以下で最大のものを良い単項式の形で出力してください。
なお、この問題の制約下でそのようなものは必ず一意に存在することが証明できます。

制約

  • 1000 \le \alpha < 10^{81}
  • \alpha は整数

入力

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

\alpha

出力

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


入力例 1

123456789

出力例 1

123b

123b = 123000000 であり、この値は \alpha = 123456789 以下の良い単項式の値のうち最大です。


入力例 2

77777

出力例 2

77a

入力例 3

9982443539982448531000000007

出力例 3

9i

入力が 64bit 整数に収まらない場合もあります。

Score : 8 points

Problem Statement

A monomial consisting of a number and an English lowercase variable is said to be a good monomial if an English lowercase single-letter variable is multiplied from the left by an integer coefficient between 1 and 999 (inclusive). Here, we assume that the coefficient in a good monomial is never omitted even if the coefficient is 1.
For example, 123a and 1z are good monomials, while 0a, 1000b, 123, 12ab, a123, and a are not.

Next, let us define the value of a good monomial by the value when certain values are assigned to the monomial. Specifically, the value for each variable to be assigned is determined by the following equations:

  • a=1000
  • b=1000a
  • \dots
  • z=1000y

For instance, the value of 123a is 123000, the value of 20b is 20000000, and the value of 1c is 1000000000.

Among the possible values of good monomials, print the maximum one not exceeding \alpha in the form of a good monomial.
We can prove that such monomial always uniquely exists under the Constraints of this problem.

Constraints

  • 1000 \le \alpha < 10^{81}
  • \alpha is an integer.

Input

Input is given from Standard Input in the following format:

\alpha

Output

Print the answer according to the instruction in the Problem Statement.


Sample Input 1

123456789

Sample Output 1

123b

We have 123b = 123000000, which is the maximum possible value of a good monomial not exceeding \alpha = 123456789.


Sample Input 2

77777

Sample Output 2

77a

Sample Input 3

9982443539982448531000000007

Sample Output 3

9i

The input may not fit into 64-bit integer type.

D - High Score

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

問題文

高橋君はある同じ試験を T 回受験しました。各試験は 1 から N の番号のついた N 科目からなります。
高橋君は i 回目の試験では科目 jP_{i,j} 点をとりました。

K=1,2,\ldots,T について次の問題に答えてください。

問題:
K 回目まで( K 回目含む)の試験の結果のうち、科目 j の最高得点を C_j とする。C_1+\ldots+C_N を求めよ。

制約

  • 1 \leq T \leq 10^3
  • 1 \leq N \leq 10
  • 0 \leq P_{i,j} \leq 10^5
  • 入力に含まれる値は全て整数である

入力

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

T N
P_{1,1} P_{1,2} \ldots P_{1,N}
\vdots
P_{T,1} P_{T,2} \ldots P_{T,N}

出力

T 行出力せよ。i 行目には K=i のときの答えを出力せよ。


入力例 1

2 3
50 80 60
60 70 70

出力例 1

190
210

高橋君は 1 回目の試験では各科目で 50,80,60 点を、2 回目は 60,70,70 点を取りました。

  • K=1 のときの答えは 50+80+60=190 です。
  • K=2 のときの答えは 60+80+70=210 です。

入力例 2

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

出力例 2

10
13
15
16

Score : 7 points

Problem Statement

Takahashi took T tests of the same format. Each test has N subjects numbered 1 to N.
In the i-th test, he scored P_{i,j} points in Subject j.

For each K=1,2,\ldots,T, answer the following question.

Question:
Let C_j be the highest score in Subject j in the first K tests. Find C_1+\ldots+C_N.

Constraints

  • 1 \leq T \leq 10^3
  • 1 \leq N \leq 10
  • 0 \leq P_{i,j} \leq 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

T N
P_{1,1} P_{1,2} \ldots P_{1,N}
\vdots
P_{T,1} P_{T,2} \ldots P_{T,N}

Output

Print T lines. The i-th line should contain the answer for K=i.


Sample Input 1

2 3
50 80 60
60 70 70

Sample Output 1

190
210

In the 1-st test, Takahashi scored 50,80,60 in Subject 1,2,3, respecitively. In the 2-nd test, he scored 60,70,70.

  • The answer for K=1 is 50+80+60=190.
  • The answer for K=2 is 60+80+70=210.

Sample Input 2

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

Sample Output 2

10
13
15
16
E - Only 2 kinds

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

問題文

YYYY/MM/DD 形式とは日付の表記方法の 1 つで、0 埋めした 4 桁の西暦年、0 埋めした 2 桁の月、 0 埋めした 2 桁の日をスラッシュで区切って並べたものを言います。たとえば 202211 日は 2022/01/01 と表されます。

日付を YYYY/MM/DD 形式で表したときに表れる数字が 2 種類以下であるとき、良い 日付と呼びます。

日付 S が YYYY/MM/DD 形式で与えられます。S 以降の日で はじめて 現れる良い日付を YYYY/MM/DD 形式で出力してください。
ただし、暦はグレゴリオ暦 (現行の日本をはじめ世界各国で使用されている暦) を使用するものとします。

制約

  • S はグレゴリオ暦に現れる日付である。
  • S は YYYY/MM/DD 形式で与えられる。
  • S200111 日以降 29991231 日以前である。

入力

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

S

出力

答えを YYYY/MM/DD 形式で出力せよ。


入力例 1

2022/01/01

出力例 1

2022/02/02

S = 202211 日以降では 202222 日がはじめて条件を満たす日です。
たとえば 2222222 日なども S 以降という条件は満たしていますが、 202222 日より遅い日付なので「はじめて 現れる」という条件を満たしません。


入力例 2

2002/02/22

出力例 2

2002/02/22

入力例 3

2002/02/23

出力例 3

2020/02/02

入力例 4

2999/12/31

出力例 4

3000/03/03

Score : 7 points

Problem Statement

YYYY/MM/DD format is a notation of date, consisting of the zero-padded 4-digit year, zero-padded 2-digit month, and zero-padded 2-digit day, separated by slashes. For example, January 1, 2022 is represented as 2022/01/01.

A date is called good when it has two or fewer different digits in YYYY/MM/DD format.

You are given a date S in YYYY/MM/DD format. Print the first good day not earlier than S, in YYYY/MM/DD format.
Here, we use the Gregorian calendar.

Constraints

  • S is a valid date in the Gregorian calendar.
  • S is given in YYYY/MM/DD format.
  • S is between January 1, 2001 and December 31, 2999 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer in YYYY/MM/DD format.


Sample Input 1

2022/01/01

Sample Output 1

2022/02/02

The first day that satisfies the condition not earlier than S = January 1, 2022 is February 2, 2022.
There are other good dates such as February 22, 2222 that are not earlier than S, but they are later than February 2, 2022 and not the first good day.


Sample Input 2

2002/02/22

Sample Output 2

2002/02/22

Sample Input 3

2002/02/23

Sample Output 3

2020/02/02

Sample Input 4

2999/12/31

Sample Output 4

3000/03/03
F - Coloring map

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

問題文

高橋王国は 1 から N までの番号がついた N 個の州からなります。
高橋王国の地図は HW 列のマス目の形をしていて、上から i 行目、左から j 列目のマスは州 A_{i,j} です。

この地図の州 i を色 C_i で塗るとき、以下の条件は満たされていますか?

異なる州が上下左右に隣り合っているならば、それらの州は異なる色で塗られている

制約

  • 1 \leq H \leq 200
  • 1 \leq W \leq 200
  • 1\leq N \leq \min(100,H\times W)
  • 1\leq A_{i,j} \leq N
  • 全ての k(1\leq k\leq N) について、A_{i,j}=k を満たす (i,j) が少なくとも 1 つ存在する
  • 1\leq C_i \leq N
  • 入力に含まれる値は全て整数である

入力

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

H W N
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}
C_1 C_2 \ldots C_N

出力

条件が満たされているならば Yes と、満たされていないならば No と出力せよ。


入力例 1

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

出力例 1

No

2 と州 3 は隣り合っていますが同じ色で塗られているので、条件を満たしません。


入力例 2

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

出力例 2

Yes

1 のように穴が空いている場合もあります。
また、州 2 のように非連結である場合もあります。


入力例 3

2 2 3
1 2
3 1
1 2 2

出力例 3

Yes

2 と州 3 は上下左右に隣り合っていないので、同じ色で塗られていても条件を満たします。

Score : 7 points

Problem Statement

The Kingdom of Takahashi consists of N states numbered 1 to N.
The map of the kingdom is a grid with H rows and W columns. The square at the i-th row from the top and j-th column from the left belongs to State A_{i,j}.

Is the condition below satisfied when painting State i in Color C_i in this map?

Different states that are vertically or horizontally adjacent are painted in different colors.

Constraints

  • 1 \leq H \leq 200
  • 1 \leq W \leq 200
  • 1\leq N \leq \min(100,H\times W)
  • 1\leq A_{i,j} \leq N
  • For every k(1\leq k\leq N), there is at least one pair (i,j) such that A_{i,j}=k.
  • 1\leq C_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}
C_1 C_2 \ldots C_N

Output

If the condition is satisfied, print Yes; otherwise, print No.


Sample Input 1

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

Sample Output 1

No

State 2 and State 3 are adjacent but painted in the same color, so the condition is not satisfied. (In the figure below, "色" means color.)


Sample Input 2

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

Sample Output 2

Yes

Some states, such as State 1 here, may have holes. Also, some states, such as State 2 here, may be disconnected.


Sample Input 3

2 2 3
1 2
3 1
1 2 2

Sample Output 3

Yes

Since State 2 and State 3 are not vertically or horizontally adjacent, painting them in the same color does not violate the condition.

G - Equations

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

方程式 a x^5 + bx + c=0 は次の条件を満たすとします。

  • 1\lt x \lt 2 の範囲に解をただ 1 つ持つ

1\lt x \lt 2 を満たす方程式の解を計算してください。

制約

  • 1 \leq a \leq 10^9
  • 1 \leq b \leq 10^9
  • -10^9 \leq c \leq 10^9
  • 入力は全て整数
  • 与えられた方程式は、1\lt x \lt 2 の範囲に解をただ 1 つ持つ

入力

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

a b c

出力

答えを出力せよ。
想定解答との絶対誤差または相対誤差が 10^{-9} 以下であれば正解と判定される。


入力例 1

32 2 -246

出力例 1

1.500000000000000000

x = 1.5 のとき、ax^5+bx+c=32\times(1.5)^5 + 2\times1.5 - 246 = 0 を満たします。


入力例 2

12 3 -45

出力例 2

1.279562760087743278

Score : 6 points

Problem Statement

Assume that the equation a x^5 + bx + c=0 satisfies the condition below.

  • It has exactly one solution in the interval 1\lt x \lt 2.

Find the solution of the equation in the interval 1\lt x \lt 2.

Constraints

  • 1 \leq a \leq 10^9
  • 1 \leq b \leq 10^9
  • -10^9 \leq c \leq 10^9
  • All values in input are integers.
  • The given equation has exactly one solution in the interval 1\lt x \lt 2.

Input

Input is given from Standard Input in the following format:

a b c

Output

Print the answer. Your output will be judged as correct when its absolute or relative error from the judge's answer is at most 10^{-9}.


Sample Input 1

32 2 -246

Sample Output 1

1.500000000000000000

For x = 1.5, we have ax^5+bx+c=32\times(1.5)^5 + 2\times1.5 - 246 = 0.


Sample Input 2

12 3 -45

Sample Output 2

1.279562760087743278
H - Connected Components

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

N 頂点のグラフがあり、頂点には 1, \dots, N の番号が付けられています。はじめ、このグラフに辺は存在しません。

以下の形式のクエリを Q 個処理してください。

  • 1 u v : 頂点 u, v を結ぶ無向辺を追加する。
  • 2 u : クエリが与えられた時点でのグラフにおいて、頂点 u から 0 本以上の辺を通ってたどり着くことのできる頂点の番号を全て昇順に出力する。

ただし、1 つのテストケースにおいて、2 u の形式のクエリで出力する頂点の番号の個数の合計は 2 \times 10^5 個以下であることが保証されます。

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 u v の形式のクエリにおいて 1 \leq u \lt v \leq N
  • 2 u の形式のクエリにおいて 1 \leq u \leq N
  • 2 u の形式のクエリが 1 個以上存在する。
  • 1 つのテストケースにおいて、2 u の形式のクエリで出力する頂点の番号の個数の合計は 2 \times 10^5 個以下である。
  • 入力は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。ただし、\mathrm{Query}_i \, (1 \leq i \leq Q)i 番目に与えられるクエリを表す。

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

出力

2 u の形式のクエリそれぞれに対し、クエリが与えられた時点でのグラフにおいて、頂点 u から 0 本以上の辺を通ってたどり着くことのできる頂点の番号を全て昇順に出力せよ。 それぞれの頂点の番号は空白区切りで出力し、末尾には改行を出力すること。


入力例 1

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

出力例 1

1 2
5
1 2 3 4
1 2 3 4

入力例 2

1 2
2 1
2 1

出力例 2

1
1

入力例 3

3 3
1 1 2
2 1
1 1 2

出力例 3

1 2

同じ頂点の組を結ぶ辺が複数回追加されることもあります。

Score : 6 points

Problem Statement

There is a graph with N vertices, numbered 1, \dots, N. Initially, there is no edge in this graph.

Process Q queries of the format below.

  • 1 u v : Add an undirected edge connecting Vertex u and Vertex v.
  • 2 u: List in ascending order all indices of vertices reachable from Vertex u by traversing zero or more edges at the moment the query is given.

Here, it is guaranteed that the total number of vertices to be listed in queries of the form 2 u within a single test case is at most 2 \times 10^5.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 \leq u \lt v \leq N in a query of the form 1 u v.
  • 1 \leq u \leq N in a query of the form 2 u.
  • There is at least one query of the form 2 u.
  • Within a single test case, the total number of vertices to be listed in queries of the form 2 u is at most 2 \times 10^5.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format, where \mathrm{Query}_i \, (1 \leq i \leq Q) represents the i-th query given:

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

Output

For each query of the format 2 u, print in ascending order all indices of vertices reachable from Vertex u by traversing zero or more edges at the moment the query is given. The indices should be separated by spaces and followed by a newline.


Sample Input 1

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

Sample Output 1

1 2
5
1 2 3 4
1 2 3 4

Sample Input 2

1 2
2 1
2 1

Sample Output 2

1
1

Sample Input 3

3 3
1 1 2
2 1
1 1 2

Sample Output 3

1 2

Multiple edges may be added between the same pair of vertices.

I - Symmetric Transformation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

無限に広がる xy 平面を考えます。
平面上の N 個の点からなる 2 つの集合 S = \lbrace (x_1, y_1), (x_2, y_2), \ldots, (x_N, y_N) \rbrace および T = \lbrace (X_1, Y_1), (X_2, Y_2), \ldots, (X_N, Y_N) \rbrace が与えられます。

下記の 2 つの操作のうち、どちらか一方のみを 0 回または 1 回行うことができます。

  • x 軸に平行な直線を 1 本選び、S の各点を選んだ直線に関して対称な位置に移動させる。
  • y 軸に平行な直線を 1 本選び、S の各点を選んだ直線に関して対称な位置に移動させる。

ST に集合として一致させることが可能かどうかを判定してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • |x_i|, |y_i|, |X_i|, |Y_i| \leq 10^9
  • i \neq j \Rightarrow (x_i, y_i) \neq (x_j, y_j)
  • i \neq j \Rightarrow (X_i, Y_i) \neq (X_j, Y_j)
  • 入力はすべて整数

入力

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

N
x_1 y_1
x_2 y_2
\vdots
x_N y_N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

ST に集合として一致させることが可能な場合は Yes を、不可能な場合は No を出力せよ。


入力例 1

3
2 3
0 3
0 1
-1 3
1 1
1 3

出力例 1

Yes

S = \lbrace (0, 1), (0, 3), (2, 3) \rbrace, T = \lbrace (1, 3), (-1, 3), (1, 1) \rbrace です。
y 軸に平行な直線として直線 x = 0.5 を選ぶと、S の各点は (0, 1) \rightarrow (1, 1), (0, 3) \rightarrow (1, 3), (2, 3) \rightarrow (-1, 3) と移動し、ST に集合として一致させることが可能です。よって、Yes を出力します。


入力例 2

2
1 1
0 0
0 0
-1 -1

出力例 2

No

どのように操作を行っても、 ST に集合として一致させることができないため No を出力します。 問題文中の 2 つの操作のうち、どちらか一方のみしか行えないことに注意してください。


入力例 3

3
3 1
4 1
5 9
5 9
3 1
4 1

出力例 3

Yes

操作を行わなくても ST は集合として一致しています。

Score : 6 points

Problem Statement

Consider an infinite xy-plane.
You are given two sets of N points each: S = \lbrace (x_1, y_1), (x_2, y_2), \ldots, (x_N, y_N) \rbrace and T = \lbrace (X_1, Y_1), (X_2, Y_2), \ldots, (X_N, Y_N) \rbrace.

You may do exactly one of the two operations below zero or one time.

  • Choose a line parallel to the x-axis and reflect each point in S over the chosen line.
  • Choose a line parallel to the y-axis and reflect each point in S over the chosen line.

Determine whether it is possible to make S equal T as a set.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • |x_i|, |y_i|, |X_i|, |Y_i| \leq 10^9
  • i \neq j \Rightarrow (x_i, y_i) \neq (x_j, y_j)
  • i \neq j \Rightarrow (X_i, Y_i) \neq (X_j, Y_j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
\vdots
x_N y_N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

If it is possible to make S equal T as a set, print Yes; otherwise, print No.


Sample Input 1

3
2 3
0 3
0 1
-1 3
1 1
1 3

Sample Output 1

Yes

We have S = \lbrace (0, 1), (0, 3), (2, 3) \rbrace, T = \lbrace (1, 3), (-1, 3), (1, 1) \rbrace.
If you choose the line x = 0.5, which is parallel to the y-axis, each point in S moves as follows: (0, 1) \rightarrow (1, 1), (0, 3) \rightarrow (1, 3), (2, 3) \rightarrow (-1, 3), after which S is equal to T as a set. Thus, Yes should be printed.


Sample Input 2

2
1 1
0 0
0 0
-1 -1

Sample Output 2

No

There is no way to do an operation to make S equal T as a set, so No should be printed. Note that you may do only one of the two operations in the Problem Statement.


Sample Input 3

3
3 1
4 1
5 9
5 9
3 1
4 1

Sample Output 3

Yes

Before doing any operation, S is already equal to T.

J - Expected Range

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

N 個の整数 A_1,A_2,\ldots,A_N から相異なる K 個を選びます。ここで、どの K 個の組も等確率で選ばれます。

選んだ整数の最大値を X とし、最小値を Y とするとき、X-Y の期待値を \bmod\,998244353 で求めてください。

注意

この問題の制約のもとで、求める期待値は互いに素な 2 整数 p,q を用いて p/q で表せることが証明でき、rq\equiv p \pmod{998244353} かつ 0\leq r < 998244353 を満たす整数 r が一意に定まります。この r が求める値です。

制約

  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • A_i は相異なる
  • 入力は全て整数

入力

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

N K
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

3 2
3 7 5

出力例 1

665496238

選ぶ K 個の値としてありうるものは以下の 3 つで、全て等確率で選ばれます。

\{3,7\}:このとき、X-Y=7-3=4

\{3,5\}:このとき、X-Y=5-3=2

\{7,5\}:このとき、X-Y=7-5=2

期待値は \dfrac{4+2+2}{3}=\dfrac{8}{3} で、これを \bmod\,998244353 で表すと 665496238 です。


入力例 2

3 1
3 7 5

出力例 2

0

入力例 3

10 5
384 887 778 916 794 336 387 493 650 422

出力例 3

621922587

Score : 6 points

Problem Statement

From N integers A_1,A_2,\ldots,A_N, we will choose K distinct integers. Here, any combination of K of the integers is chosen with equal probability.

Let X and Y be the maximum and minimum values among the chosen integers, respectively. Find the expected value of X-Y, modulo 998244353.

Notes

Under the Constraints of this problem, it can be proved that the sought expected value can be represented as p/q using two coprime integers p,q, and there uniquely exists an integer r such that rq\equiv p \pmod{998244353} and 0\leq r < 998244353. You should find this r.

Constraints

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

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

3 2
3 7 5

Sample Output 1

665496238

There are three possible choices of K integers, as follows, chosen with equal probability.

\{3,7\}: Here we have X-Y=7-3=4.

\{3,5\}: Here we have X-Y=5-3=2.

\{7,5\}: Here we have X-Y=7-5=2.

The expected value is \dfrac{4+2+2}{3}=\dfrac{8}{3}, which modulo 998244353 is 665496238.


Sample Input 2

3 1
3 7 5

Sample Output 2

0

Sample Input 3

10 5
384 887 778 916 794 336 387 493 650 422

Sample Output 3

621922587
K - Planning

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

N 頂点 M 辺の重み付き有向グラフが与えられます。

頂点には 1 から N までの番号が振られています。
また、i\ (1 \leq i \leq M) 本目の辺は頂点 u_i から頂点 v_i に向けて張られており、その重みは w_i です。

k=1,2,\ldots,N について、以下の問題を解いてください。

  • 頂点 1 から始めて頂点 k を一度以上通り、頂点 N まで行く経路が存在するか判定し、存在するならそのうち最短のものの長さを出力せよ。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq u_i,v_i \leq N
  • u_i \neq v_i
  • 1 \leq w_i \leq 10^9
  • 入力はすべて整数

入力

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

N M
u_1 v_1 w_1
u_2 v_2 w_2
\hspace{0.8cm}\vdots
u_M v_M w_M

出力

N 行にわたって出力せよ。i\ (1 \leq i \leq N) 行目には k=i の場合の答えを以下に従って出力すること。

  • 頂点 1 から始めて頂点 k を一度以上通り、頂点 N まで行く経路が存在するなら、そのうち最短のものの長さを出力。
  • 頂点 1 から始めて頂点 k を一度以上通り、頂点 N まで行く経路が存在しないなら -1 を出力。

入力例 1

3 3
1 2 3
2 3 4
1 3 2

出力例 1

2
7
2
  • k=1 のとき、3 本目の辺を通って直接頂点 3 に移動する経路が最短です。
  • k=2 のとき、1 本目の辺を通って頂点 2 に移動したあと 2 本目の辺を通って頂点 3 に移動する経路が最短です。
  • k=3 のとき、3 本目の辺を通って直接頂点 3 に移動する経路が最短です。

入力例 2

5 10
2 1 1
2 5 5
1 2 6
2 5 4
5 3 2
1 3 1
1 3 4
3 5 4
1 5 3
5 2 3

出力例 2

3
10
5
-1
3

Score : 6 points

Problem Statement

You are given a weighted directed graph with N vertices and M edges.

The vertices are numbered 1 to N.
The i-th edge (1 \leq i \leq M) goes from Vertex u_i to Vertex v_i and has a weight of w_i.

For each k=1,2,\ldots,N, solve the problem below.

  • Determine whether there is a path that starts at Vertex 1, visits Vertex k one or more times, and ends at Vertex N. If it exists, print the shortest possible length of such a path.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq u_i,v_i \leq N
  • u_i \neq v_i
  • 1 \leq w_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1 w_1
u_2 v_2 w_2
\hspace{0.8cm}\vdots
u_M v_M w_M

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the answer for the case k=i, as follows.

  • If there is a path that starts at Vertex 1, visits Vertex k one or more times, and ends at Vertex N, print the shortest possible length of such a path.
  • If there is no path that starts at Vertex 1, visits Vertex k one or more times, and ends at Vertex N, print -1.

Sample Input 1

3 3
1 2 3
2 3 4
1 3 2

Sample Output 1

2
7
2
  • For k=1, the shortest path traverses the 3-rd edge to reach Vertex 3 directly.
  • For k=2, the shortest path first traverses the 1-rd edge to get to Vertex 2 and then traverses the 2-nd edge to reach Vertex 3.
  • For k=3, the shortest path traverses the 3-rd edge to reach Vertex 3 directly.

Sample Input 2

5 10
2 1 1
2 5 5
1 2 6
2 5 4
5 3 2
1 3 1
1 3 4
3 5 4
1 5 3
5 2 3

Sample Output 2

3
10
5
-1
3
L - N mod M

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

整数 NM で割った余りを求めてください。

ただし、N は非常に大きいため、次のような形式で与えられます。

形式:K 個の文字 C_iK 個の正整数 D_i が与えられる。S_i を「文字 C_iD_i 個繋げた文字列」とするとき、S_1,\ldots,S_N をこの順に繋げた文字列を 10 進法で表された整数とみなしたものが N である。

制約

  • 2\leq M \leq 10^9
  • 1 \leq K \leq 10^5
  • C_i0 から 9 の数字
  • C_10 でない
  • 1\leq D_i \leq 10^{12}
  • 入力に含まれる値は全て整数である

入力

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

K M
C_1 D_1
\vdots
C_K D_K

出力

答えを出力せよ。


入力例 1

3 11
1 4
2 2
3 1

出力例 1

3

N=1111223 です。N11 で割った余りは 3 です。


入力例 2

2 10000
1 1
0 1000000000000

出力例 2

0

N1 の後ろに 010^{12} 個つく数です。N10000 で割った余りは 0 です。


入力例 3

8 5054049
1 41421356
1 7320508075
2 2360679
3 141592653589
0 57721566
1 644934066848
2 99792458
9 192631770

出力例 3

3689688

Score : 6 points

Problem Statement

Find the remainder when an integer N is divided by M.

Since N is enormous, it is given to you in the format below.

Format: You are given K characters C_i and K positive integers D_i. Let S_i be the concatenation of D_i copies of the character C_i. N is the concatenation of S_1,\ldots,S_N in this order, seen as a decimal integer.

Constraints

  • 2\leq M \leq 10^9
  • 1 \leq K \leq 10^5
  • C_i is a digit between 0 and 9 (inclusive).
  • C_1 is not 0.
  • 1\leq D_i \leq 10^{12}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

K M
C_1 D_1
\vdots
C_K D_K

Output

Print the answer.


Sample Input 1

3 11
1 4
2 2
3 1

Sample Output 1

3

We have N=1111223. The remainder when N is divided by 11 is 3.


Sample Input 2

2 10000
1 1
0 1000000000000

Sample Output 2

0

N is written as 1 followed by 10^{12} zeros. The remainder when N is divided by 10000 is 0.


Sample Input 3

8 5054049
1 41421356
1 7320508075
2 2360679
3 141592653589
0 57721566
1 644934066848
2 99792458
9 192631770

Sample Output 3

3689688
M - Ranking

Time Limit: 8 sec / Memory Limit: 1024 MB

配点 : 6

問題文

選手 1 、選手 2\ldots 、選手 NN 人の選手がゲームをしており、 選手 i の得点は最初 P_i 点です。

クエリが Q 個与えられるので、与えられた順番に処理してください。 クエリは次の 3 種類のいずれかです。

  • 1 a x : 選手 a の得点を x に変更する。
  • 2 a : 現在の選手 a の得点が、N 人のうち大きい方から何番目か出力する。具体的には r 番目であるとき整数 r を出力する。
  • 3 r : N 人のうち現在の得点が大きい方から r 番目の選手を出力する。具体的には選手 ar 番目であるとき整数 a を出力する。

ただし、最初に与えられる各選手の得点および、 1 つめの種類のクエリで与えられる変更後の選手の得点はすべて相異なることが保証されます。

制約

  • 1 \leq N,Q \leq 5\times 10^5
  • 0\leq P_i \leq 10^9
  • P_i \neq P_j (i\neq j)
  • 1\leq a \leq N
  • 0\leq x \leq 10^9
  • 1\leq r \leq N
  • 1 つめの種類のクエリの x はすべて互いに相異なり、また P_i のいずれとも相異なる。
  • 2 つめまたは 3 つめの種類のクエリが 1 つ以上存在する。
  • 入力は全て整数である。

入力

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

N Q
P_1 P_2 \ldots P_N
query_1
query_2
\vdots
query_Q

3 行目から Q+2 行目の各 query_i は次のいずれかの形で与えられる。

1 a x
2 a
3 r

出力

2 つめおよび 3 つめの種類のクエリに対する出力を、 与えられた順に改行区切りで出力せよ。


入力例 1

3 5
10 30 20
2 1
3 1
1 2 0
2 1
3 2

出力例 1

3
2
2
1

最初、選手 1 、選手 2 、選手 3 の得点はそれぞれ 10 , 30 , 20 です。

  • 1 つめのクエリについて、選手 1 の得点は 3 番目に大きいので 3 を出力します。
  • 2 つめのクエリについて、1 番目に得点が大きい選手は選手 2 であるので 2 を出力します。
  • 3 つめのクエリによって、選手 2 の得点は 0 に変更されます。 これによって選手 1 、選手 2 、選手 3 の得点はそれぞれ 10 , 0 , 20 になります。
  • 4 つめのクエリについて、選手 1 の得点は 2 番目に大きいので 2 を出力します。
  • 5 つめのクエリについて、2 番目に得点が大きい選手は選手 1 であるので 1 を出力します。

Score : 6 points

Problem Statement

N players called Player 1, Player 2, \ldots, Player N are playing a game. The initial score of Player i is P_i points.

Process Q queries in the order they are given. There are three kinds of queries, as follows.

  • 1 a x: Change the score of Player a to x.
  • 2 a: Print the current rank of Player a among the N players. Specifically, if Player a has the r-th highest score, print the integer r.
  • 3 r: Print the player who currently ranks r-th. Specifically, if Player a has the r-th highest score, print the integer a.

Here, it is guaranteed that the initial scores of the players, and the scores after the changes given as queries of the first kind, are all distinct.

Constraints

  • 1 \leq N,Q \leq 5\times 10^5
  • 0\leq P_i \leq 10^9
  • P_i \neq P_j (i\neq j)
  • 1\leq a \leq N
  • 0\leq x \leq 10^9
  • 1\leq r \leq N
  • All x in the queries of the first kind are distinct and different from all of P_i.
  • There is at least one query of the second or third kind.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
P_1 P_2 \ldots P_N
query_1
query_2
\vdots
query_Q

Each query_i in the 3-rd through (Q+2)-th lines is given in one of the following formats:

1 a x
2 a
3 r

Output

Print the response to the queries of the second and third kinds, in the order they are given, separated by newlines.


Sample Input 1

3 5
10 30 20
2 1
3 1
1 2 0
2 1
3 2

Sample Output 1

3
2
2
1

Initially, Player 1, Player 2, Player 3 has 10, 30, 20 points, respectively.

  • For the first query, Player 1 has the third highest score, so 3 should be printed.
  • For the second query, Player 2 has the first highest score, so 2 should be printed.
  • In the third query, the score of Player 2 is changed to 0. Now, Player 1, Player 2, Player 3 has 10, 0, 20 points, respectively.
  • For the fourth query, Player 1 has the second highest score, so 2 should be printed.
  • For the fifth query, Player 1 has the second highest score, so 1 should be printed.
N - 40B of calculations

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

長さ N の整数列 A が与えられます。
高橋君は、 N \times N のマス目の上から i 番目、左から j 番目のマスに A_i-A_j の値を書き込みました。
このマス目には何種類の値が書き込まれたでしょうか?

制約

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

入力

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

N
A_1 A_2 \dots A_N

出力

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


入力例 1

3
5 2 3

出力例 1

7

数が書き込まれた後のマス目の状態は以下の通りで、ここには 7 種類の値が含まれます。


入力例 2

10
13 21 34 55 89 1 2 3 5 8

出力例 2

75

Score : 6 points

Problem Statement

You are given an integer sequence A of length N.
In an N \times N grid, Takahashi has written the value A_i-A_j in the square at the i-th row from the top and j-th column from the left.
How many different values are written in the grid?

Constraints

  • All values in input are integers.
  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i \le 2 \times 10^5

Input

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

3
5 2 3

Sample Output 1

7

After the numbers are written, the grid looks as follows, containing 7 different values.


Sample Input 2

10
13 21 34 55 89 1 2 3 5 8

Sample Output 2

75
O - 3-Permutation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

1 以上 N 以下の相異なる整数のペア (A_i,B_i)M 個与えられます。
1 から N の順列 P=(P_1, P_2,\ldots , P_N) であって、 任意の 1\leq i\leq M について P_{A_i}+P_{B_i}3 の倍数となるものが存在するか判定してください。

制約

  • 2 \leq N \leq 1000
  • 0 \leq M \leq \min\left( \frac{N(N-1)}{2},2\times 10^5 \right)
  • 1 \leq A_i<B_i \leq N
  • (A_i, B_i) は全て異なる。
  • 入力は全て整数である。

入力

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

N M
A_1 B_1
:
A_M B_M

出力

問題文の条件をみたす順列が存在するならば Yes , そうでないならば No を出力せよ。


入力例 1

5 2
1 2
2 3

出力例 1

Yes

例えば P=(1,2,4,3,5) とすれば、 P_1+P_2=1+2=3 , P_2+P_3=2+4=6 であり、条件をみたしています。


入力例 2

3 2
1 2
2 3

出力例 2

No

順列として考えられるものは 3!=6 通りありますが、すべての条件をみたすような順列はありません。

Score : 6 points

Problem Statement

You are given M pairs (A_i,B_i) of distinct integers between 1 and N (inclusive).
Determine whether there is a permutation P=(P_1, P_2,\ldots , P_N) of the integers 1 to N such that P_{A_i}+P_{B_i} is a multiple of 3 for all 1\leq i\leq M.

Constraints

  • 2 \leq N \leq 1000
  • 0 \leq M \leq \min\left( \frac{N(N-1)}{2},2\times 10^5 \right)
  • 1 \leq A_i<B_i \leq N
  • The pairs (A_i, B_i) are all distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If there is a permutation that satisfies the conditions in the Problem Statement, print Yes; otherwise, print No.


Sample Input 1

5 2
1 2
2 3

Sample Output 1

Yes

If we choose P=(1,2,4,3,5), for example, we have P_1+P_2=1+2=3 and P_2+P_3=2+4=6, satisfying the conditions.


Sample Input 2

3 2
1 2
2 3

Sample Output 2

No

There are 3!=6 possible permutations, but none satisfies all of the conditions.