A - Middle Letter

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

英小文字からなる長さが奇数の文字列 S が与えられます。

S の中央の文字を出力してください。

中央の文字とは ある長さが奇数の文字列 T について、 T の長さを |T| として、T の前から \frac{|T|+1}{2} 番目の文字を中央の文字とします。

制約

  • S は英小文字からなる長さが奇数の文字列
  • S の長さは 1 以上 99 以下

入力

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

S

出力

答えを出力せよ。


入力例 1

atcoder

出力例 1

o

atcoder の中央の文字は o です。


入力例 2

a

出力例 2

a

Score : 100 points

Problem Statement

You are given an odd-length string S consisting of lowercase English letters.

Print the central character of S.

What is the central character? For an odd-length string T, its central character is the \frac{|T|+1}{2}-th character from the beginning, where |T| is the length of T.

Constraints

  • S is an odd-length string consisting of lowercase English letters.
  • The length of S is between 1 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

atcoder

Sample Output 1

o

The central character of atcoder is o.


Sample Input 2

a

Sample Output 2

a
B - Modulo Number

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

-10^{18} 以上 10^{18} 以下の整数 N が与えられます。

以下の条件を満たす 0 以上 998244353 未満の整数 x を求めてください。なお、答えが一意に定まることが証明できます。

  • N-x998244353 の倍数

制約

  • N-10^{18} 以上 10^{18} 以下の整数

入力

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

N

出力

答えを出力せよ。


入力例 1

998244354

出力例 1

1

998244354-1 = 998244353998244353 の倍数なので条件を満たします。


入力例 2

-9982443534

出力例 2

998244349

-9982443534-998244349= -10980687883998244353 の倍数なので条件を満たします。

Score : 200 points

Problem Statement

You are given an integer N between -10^{18} and 10^{18} (inclusive).

Find an integer x between 0 and 998244353 - 1 (inclusive) that satisfies the following condition. It can be proved that such an integer is unique.

  • N-x is a multiple of 998244353.

Constraints

  • N is an integer between -10^{18} and 10^{18} (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

998244354

Sample Output 1

1

998244354-1 = 998244353 is a multiple of 998244353, so the condition is satisfied.


Sample Input 2

-9982443534

Sample Output 2

998244349

-9982443534-998244349= -10980687883 is a multiple of 998244353, so the condition is satisfied.

C - Convex Quadrilateral

Time Limit: 2 sec / Memory Limit: 1024 MB

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

D - Snuke Panic (1D)

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君はすぬけ君たちを捕まえようとしています。

数直線上の座標 0,1,2,3,45 箇所に穴があり、すぬけ君たちの巣につながっています。

これから N 匹のすぬけ君が穴から出てきます。i 番目のすぬけ君は時刻 T_i に座標 X_i の穴から出てきて、大きさは A_i であることがわかっています。

高橋君は時刻 0 に座標 0 におり、数直線上を単位時間あたり 1 以下の速さで移動することができます。
すぬけ君が穴から出てきたのと同じ時刻に同じ座標に高橋君がいるとき、かつ、そのときに限り、高橋君はすぬけ君を捕まえることができます。
すぬけ君を捕まえるのにかかる時間は無視できます。

高橋君が適切に行動したとき、捕まえることができるすぬけ君の大きさの合計の最大値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 < T_1 < T_2 < \ldots < T_N \leq 10^5
  • 0 \leq X_i \leq 4
  • 1 \leq A_i \leq 10^9
  • 入力に含まれる値は全て整数である

入力

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

N
T_1 X_1 A_1
T_2 X_2 A_2
\vdots
T_N X_N A_N

出力

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


入力例 1

3
1 0 100
3 3 10
5 4 1

出力例 1

101

次のように行動するのが最適です。

  • 座標 0 で待機し、時刻 11 番目のすぬけ君を捕まえる
  • 座標 4 へ移動し、時刻 53 番目のすぬけ君を捕まえる

1 番目と 2 番目のすぬけ君を両方とも捕まえることはできないので、これが最大です。


入力例 2

3
1 4 1
2 4 1
3 4 1

出力例 2

0

高橋君はすぬけ君を 1 匹も捕まえることができません。


入力例 3

10
1 4 602436426
2 1 623690081
3 3 262703497
4 4 628894325
5 3 450968417
6 1 161735902
7 1 707723857
8 2 802329211
9 0 317063340
10 2 125660016

出力例 3

2978279323

Score : 400 points

Problem Statement

Takahashi is trying to catch many Snuke.

There are five pits at coordinates 0, 1, 2, 3, and 4 on a number line, connected to Snuke's nest.

Now, N Snuke will appear from the pits. It is known that the i-th Snuke will appear from the pit at coordinate X_i at time T_i, and its size is A_i.

Takahashi is at coordinate 0 at time 0 and can move on the line at a speed of at most 1.
He can catch a Snuke appearing from a pit if and only if he is at the coordinate of that pit exactly when it appears.
The time it takes to catch a Snuke is negligible.

Find the maximum sum of the sizes of Snuke that Takahashi can catch by moving optimally.

Constraints

  • 1 \leq N \leq 10^5
  • 0 < T_1 < T_2 < \ldots < T_N \leq 10^5
  • 0 \leq X_i \leq 4
  • 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
T_1 X_1 A_1
T_2 X_2 A_2
\vdots
T_N X_N A_N

Output

Print the answer as an integer.


Sample Input 1

3
1 0 100
3 3 10
5 4 1

Sample Output 1

101

The optimal strategy is as follows.

  • Wait at coordinate 0 to catch the first Snuke at time 1.
  • Go to coordinate 4 to catch the third Snuke at time 5.

It is impossible to catch both the first and second Snuke, so this is the best he can.


Sample Input 2

3
1 4 1
2 4 1
3 4 1

Sample Output 2

0

Takahashi cannot catch any Snuke.


Sample Input 3

10
1 4 602436426
2 1 623690081
3 3 262703497
4 4 628894325
5 3 450968417
6 1 161735902
7 1 707723857
8 2 802329211
9 0 317063340
10 2 125660016

Sample Output 3

2978279323
E - Throwing the Die

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

サイコロを使ったゲームをします。ゲームは最大 N 回のターンからなり、各ターンは次のように進行します。

  • 1,\ldots,6 の目が等確率で出る 6 面ダイスを振り、出目を X とする(出目は各ターンで独立とする)。
  • 現在が N ターン目なら、スコアX とし、ゲームを終了する。
  • そうでないとき、ゲームを続行するか終了するか選択する。
    • ゲームを終了する場合、スコアを X とし、残りのターンは行わずにゲームを終了する。

スコアの期待値が最大になるように行動したとき、スコアの期待値を求めてください。

制約

  • 1 \leq N \leq 100

入力

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

N

出力

答えを出力せよ。
なお、真の解との絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。


入力例 1

1

出力例 1

3.5000000000

入力例 2

2

出力例 2

4.2500000000

入力例 3

10

出力例 3

5.6502176688

Score : 500 points

Problem Statement

Let us play a game using a die. The game consists of at most N turns, each of which goes as follows.

  • Throw a 6-sided die that shows 1,\ldots,6 with equal probability, and let X be the number shown (each throw is independent of the others).
  • If it is the N-th turn now, your score is X, and the game ends.
  • Otherwise, choose whether to continue or end the game.
    • If you end the game, your score is X, and there is no more turn.

Find the expected value of your score when you play the game to maximize this expected value.

Constraints

  • 1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.
Your output is considered correct if its absolute or relative error from the true answer is at most 10^{-6}.


Sample Input 1

1

Sample Output 1

3.5000000000

Sample Input 2

2

Sample Output 2

4.2500000000

Sample Input 3

10

Sample Output 3

5.6502176688
F - Well-defined Path Queries on a Namori

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

頂点に 1 から N の番号がついた N 頂点 N 辺の連結な単純無向グラフ G が与えられます。i 番目の辺は頂点 u_i と頂点 v_i を双方向に結んでいます。

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

  • 頂点 x_i から頂点 y_i に向かう単純パス(同じ頂点を 2 度通らないパス)が一意に定まるか判定せよ。

制約

  • 3 \leq N \leq 2 \times 10^5
  • 1 \leq u_i < v_i\leq N
  • i \neq j ならば (u_i,v_i) \neq (u_j,v_j)
  • GN 頂点 N 辺の連結な単純無向グラフ
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq x_i < y_i\leq N
  • 入力は全て整数

入力

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

N
u_1 v_1
u_2 v_2
\vdots
u_N v_N
Q
x_1 y_1
x_2 y_2
\vdots
x_Q y_Q

出力

Q 行出力せよ。

i\ (1 \leq i \leq Q) 行目には、頂点 x_i から頂点 y_i に向かう単純パスが一意に定まる場合 Yes、そうでない場合 No を出力せよ。


入力例 1

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

出力例 1

No
Yes
No

頂点 1 から 2 に向かう単純パスは (1,2),(1,3,2) と一意に定まらないので、 1 個目のクエリの答えは No です。

頂点 1 から 4 に向かう単純パスは (1,4) と一意に定まるので、2 個目のクエリの答えは Yes です。

頂点 1 から 5 に向かう単純パスは (1,2,5),(1,3,2,5) と一意に定まらないので、3 個目のクエリの答えは No です。


入力例 2

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

出力例 2

Yes
No
Yes
Yes
No
No
Yes
No
Yes
No

Score : 500 points

Problem Statement

You are given a connected simple undirected graph G with N vertices numbered 1 to N and N edges. The i-th edge connects Vertex u_i and Vertex v_i bidirectionally.

Answer the following Q queries.

  • Determine whether there is a unique simple path from Vertex x_i to Vertex y_i (a simple path is a path without repetition of vertices).

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 1 \leq u_i < v_i\leq N
  • (u_i,v_i) \neq (u_j,v_j) if i \neq j.
  • G is a connected simple undirected graph with N vertices and N edges.
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq x_i < y_i\leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
u_1 v_1
u_2 v_2
\vdots
u_N v_N
Q
x_1 y_1
x_2 y_2
\vdots
x_Q y_Q

Output

Print Q lines.

The i-th line (1 \leq i \leq Q) should contain Yes if there is a unique simple path from Vertex x_i to Vertex y_i, and No otherwise.


Sample Input 1

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

Sample Output 1

No
Yes
No

The simple paths from Vertex 1 to 2 are (1,2) and (1,3,2), which are not unique, so the answer to the first query is No.

The simple path from Vertex 1 to 4 is (1,4), which is unique, so the answer to the second query is Yes.

The simple paths from Vertex 1 to 5 are (1,2,5) and (1,3,2,5), which are not unique, so the answer to the third query is No.


Sample Input 2

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

Sample Output 2

Yes
No
Yes
Yes
No
No
Yes
No
Yes
No
G - Yet Another RGB Sequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

整数 R,G,B,K が与えられます。R, G, B からなる文字列 S であって、以下の条件をすべて満たすものの個数を 998244353 で割った余りを求めてください。

  • S に含まれる R, G, B の個数はそれぞれ R,G,B 個である。
  • S に(連続する)部分文字列として含まれる RG の個数は K 個である。

制約

  • 1 \leq R,G,B\leq 10^6
  • 0 \leq K \leq \mathrm{min}(R,G)
  • 入力は全て整数

入力

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

R G B K

出力

答えを出力せよ。


入力例 1

2 1 1 1

出力例 1

6

条件を満たす文字列は以下の 6 個です。

  • RRGB
  • RGRB
  • RGBR
  • RBRG
  • BRRG
  • BRGR

入力例 2

1000000 1000000 1000000 1000000

出力例 2

80957240

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

Score : 600 points

Problem Statement

You are given integers R, G, B, and K. How many strings S consisting of R, G, and B satisfy all of the conditions below? Find the count modulo 998244353.

  • The number of occurrences of R, G, and B in S are R, G, and B, respectively.
  • The number of occurrences of RG as (contiguous) substrings in S is K.

Constraints

  • 1 \leq R,G,B\leq 10^6
  • 0 \leq K \leq \mathrm{min}(R,G)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

R G B K

Output

Print the answer.


Sample Input 1

2 1 1 1

Sample Output 1

6

The following six strings satisfy the conditions.

  • RRGB
  • RGRB
  • RGBR
  • RBRG
  • BRRG
  • BRGR

Sample Input 2

1000000 1000000 1000000 1000000

Sample Output 2

80957240

Find the count modulo 998244353.

Ex - Snuke Panic (2D)

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋君はすぬけ君たちを捕まえようとしています。

2 次元座標平面上にいくつか穴があいており、すぬけ君たちの巣につながっています。

これから N 匹のすぬけ君が穴から出てきます。i 番目のすぬけ君は時刻 T_i に座標 (X_i,Y_i) の穴から出てきて、大きさは A_i であることがわかっています。

高橋君は時刻 0 に座標 (0,0) におり、以下の 2 種類の移動ができます。

  • x 軸方向に単位時間あたり 1 以下の速さで移動する
  • y方向に単位時間あたり 1 以下の速さで移動する

y 軸負方向に移動することはできません。

すぬけ君が穴から出てきたのと同じ時刻に同じ座標に高橋君がいるとき、かつ、そのときに限り、高橋君はすぬけ君を捕まえることができます。
すぬけ君を捕まえるのにかかる時間は無視できます。

高橋君が適切に行動したとき、捕まえることができるすぬけ君の大きさの合計の最大値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq T_i \leq 10^9
  • 0 \leq X_i,Y_i \leq 10^9
  • 1 \leq A_i \leq 10^9
  • (T_i,X_i,Y_i) は相異なる
  • 入力に含まれる値は全て整数である

入力

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

N
T_1 X_1 Y_1 A_1
T_2 X_2 Y_2 A_2
\vdots
T_N X_N Y_N A_N

出力

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


入力例 1

3
1 0 0 100
3 2 1 10
5 3 1 1

出力例 1

101
  • 座標 (0,0) で待機し、時刻 11 番目のすぬけ君を捕まえる
  • 座標 (3,1) へ移動し、時刻 53 番目のすぬけ君を捕まえる

1 番目と 2 番目のすぬけ君を両方とも捕まえることはできないので、これが最大です。


入力例 2

2
100 0 1 1
200 1 0 10

出力例 2

10

y 軸負方向には移動できないため、1 番目のすぬけ君を捕まえた後、2 番目のすぬけ君を捕まえることはできません。


入力例 3

10
797829355 595605750 185676190 353195922
913575467 388876063 395940406 533206504
810900084 201398242 159760440 87027328
889089200 220046203 85488350 325976483
277429832 161055688 73308100 940778720
927999455 429014248 477195779 174616807
673419335 415891345 81019893 286986530
989248231 147792453 417536200 219371588
909664305 22150727 414107912 317441890
988670052 140275628 468278658 67181740

出力例 3

1553741733

Score : 600 points

Problem Statement

Takahashi is trying to catch many Snuke.

There are some pits in a two-dimensional coordinate plane, connected to Snuke's nest.

Now, N Snuke will appear from the pits. It is known that the i-th Snuke will appear from the pit at coordinates (X_i,Y_i) at time T_i, and its size is A_i.

Takahashi is at coordinates (0,0) at time 0 and can do the following two kinds of moves.

  • Move at a speed of at most 1 in the x-direction (positive or negative).
  • Move at a speed of at most 1 in the positive y-direction.

Moving in the negative y-direction is not allowed.

He can catch a Snuke appearing from a pit if and only if he is at the coordinates of that pit exactly when it appears.
The time it takes to catch a Snuke is negligible.

Find the maximum sum of the sizes of Snuke that Takahashi can catch by moving optimally.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq T_i \leq 10^9
  • 0 \leq X_i,Y_i \leq 10^9
  • 1 \leq A_i \leq 10^9
  • The triples (T_i,X_i,Y_i) are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
T_1 X_1 Y_1 A_1
T_2 X_2 Y_2 A_2
\vdots
T_N X_N Y_N A_N

Output

Print the answer as an integer.


Sample Input 1

3
1 0 0 100
3 2 1 10
5 3 1 1

Sample Output 1

101

The optimal strategy is as follows.

  • Wait at coordinates (0,0) to catch the first Snuke at time 1.
  • Go to coordinates (3,1) to catch the third Snuke at time 5.

It is impossible to catch both the first and second Snuke, so this is the best he can.


Sample Input 2

2
100 0 1 1
200 1 0 10

Sample Output 2

10

Moving in the negative y-direction is not allowed, so he cannot catch the first Snuke and then the second.


Sample Input 3

10
797829355 595605750 185676190 353195922
913575467 388876063 395940406 533206504
810900084 201398242 159760440 87027328
889089200 220046203 85488350 325976483
277429832 161055688 73308100 940778720
927999455 429014248 477195779 174616807
673419335 415891345 81019893 286986530
989248231 147792453 417536200 219371588
909664305 22150727 414107912 317441890
988670052 140275628 468278658 67181740

Sample Output 3

1553741733