A - ><

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

配点 : 300

問題文

長さ N-1 の文字列 S が与えられます. S の各文字は < または > です.

長さ N の非負整数列 a_1,a_2,\cdots,a_N は, すべての i (1 \leq i \leq N-1) について次の条件をみたす時,良い非負整数列と呼ばれます.

  • S_i= < のとき: a_i<a_{i+1}
  • S_i= > のとき: a_i>a_{i+1}

良い非負整数列の要素の総和としてありうる最小の値を求めてください.

制約

  • 2 \leq N \leq 5 \times 10^5
  • S<> のみから成る長さ N-1 の文字列.

入力

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

S

出力

良い非負整数列の要素の総和としてありうる最小の値を出力せよ.


入力例 1

<>>

出力例 1

3

a=(0,2,1,0) は良い非負整数列であり, この場合の要素の総和は 3 になります. 要素の総和が 3 より小さい良い非負整数列は存在しません.


入力例 2

<>>><<><<<<<>>><

出力例 2

28

Score : 300 points

Problem Statement

Given is a string S of length N-1. Each character in S is < or >.

A sequence of N non-negative integers, a_1,a_2,\cdots,a_N, is said to be good when the following condition is satisfied for all i (1 \leq i \leq N-1):

  • If S_i= <: a_i<a_{i+1}
  • If S_i= >: a_i>a_{i+1}

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • S is a string of length N-1 consisting of < and >.

Input

Input is given from Standard Input in the following format:

S

Output

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.


Sample Input 1

<>>

Sample Output 1

3

a=(0,2,1,0) is a good sequence whose sum is 3. There is no good sequence whose sum is less than 3.


Sample Input 2

<>>><<><<<<<>>><

Sample Output 2

28
B - Two Contests

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

配点 : 600

問題文

1 から 10^9 までの番号のついた 10^9 人が参加する大会があります. この大会では,2 回のコンテストが行われます.

コンテストで出題する問題として,1 から N までの番号のついた N 問が準備されています. 問題 i が出題された場合,番号が L_i 以上 R_i 以下の参加者は全員正解し,逆にそれ以外の参加者は誰も解けません.

これらの N 問を,2 回のコンテストに分けて出題します. どの問題も,ちょうど 1 回のコンテストで出題されなくてはいけません. また,どちらのコンテストも,少なくとも 1 問以上の問題が出題される必要があります.

それぞれのコンテストの楽しさは,そのコンテストの全ての問題を解く参加者の人数です. 2 回のコンテストの楽しさの和としてありうる最大の値を求めてください.

制約

  • 2 \leq N \leq 10^5
  • 1 \leq L_i \leq R_i \leq 10^9
  • 入力される値はすべて整数である.

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

出力

2 回のコンテストの楽しさの和としてありうる最大の値を出力せよ.


入力例 1

4
4 7
1 4
5 8
2 5

出力例 1

6

以下のようにするのが最適です.

  • 1 回目のコンテストで問題 1,3 を出題する.人 5,6,7 がこのコンテストの問題を全て解くので,コンテストの楽しさは 3 である.
  • 2 回目のコンテストで問題 2,4 を出題する.人 2,3,4 がこのコンテストの問題を全て解くので,コンテストの楽しさは 3 である.
  • 2 回のコンテストの楽しさの和が 6 になる.楽しさの和を 6 より大きくすることは出来ない.

入力例 2

4
1 20
2 19
3 18
4 17

出力例 2

34

入力例 3

10
457835016 996058008
456475528 529149798
455108441 512701454
455817105 523506955
457368248 814532746
455073228 459494089
456651538 774276744
457667152 974637457
457293701 800549465
456580262 636471526

出力例 3

540049931

Score : 600 points

Problem Statement

10^9 contestants, numbered 1 to 10^9, will compete in a competition. There will be two contests in this competition.

The organizer prepared N problems, numbered 1 to N, to use in these contests. When Problem i is presented in a contest, it will be solved by all contestants from Contestant L_i to Contestant R_i (inclusive), and will not be solved by any other contestants.

The organizer will use these N problems in the two contests. Each problem must be used in exactly one of the contests, and each contest must have at least one problem.

The joyfulness of each contest is the number of contestants who will solve all the problems in the contest. Find the maximum possible total joyfulness of the two contests.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq L_i \leq R_i \leq 10^9
  • All values in input are integers.

Input

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 maximum possible total joyfulness of the two contests.


Sample Input 1

4
4 7
1 4
5 8
2 5

Sample Output 1

6

The optimal choice is:

  • Use Problem 1 and 3 in the first contest. Contestant 5, 6, and 7 will solve both of them, so the joyfulness of this contest is 3.
  • Use Problem 2 and 4 in the second contest. Contestant 2, 3, and 4 will solve both of them, so the joyfulness of this contest is 3.
  • The total joyfulness of these two contests is 6. We cannot make the total joyfulness greater than 6.

Sample Input 2

4
1 20
2 19
3 18
4 17

Sample Output 2

34

Sample Input 3

10
457835016 996058008
456475528 529149798
455108441 512701454
455817105 523506955
457368248 814532746
455073228 459494089
456651538 774276744
457667152 974637457
457293701 800549465
456580262 636471526

Sample Output 3

540049931
C - Neither AB nor BA

実行時間制限: 4 sec / メモリ制限: 1024 MB

配点 : 800

問題文

正の偶数 N が与えられます. A,B,C のみからなる長さ N の文字列 s であって,次の条件を満たすものの個数を求めてください.

  • 以下の操作を繰り返すことで,s を空文字列へと変換できる.
    • s の中で連続した 2 文字を選び,消す.ただし,選んだ 2 文字が AB または BA であってはいけない.

例えば,N=4 のとき,ABBC は条件をみたします. ABBC →( BB を消去)→ AC →( AC を消去 )→ 空文字列 と操作すれば良いです.

なお,答えは非常に大きくなることがあるので 998244353 で割ったあまりを求めてください.

制約

  • 2 \leq N \leq 10^7
  • N は偶数

入力

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

N

出力

条件をみたす文字列が何通りあるかを 998244353 で割ったあまりを出力せよ.


入力例 1

2

出力例 1

7

s=AB,BA 以外の文字列は条件を満たします.


入力例 2

10

出力例 2

50007

入力例 3

1000000

出力例 3

210055358

Score : 800 points

Problem Statement

Given is a positive even number N.

Find the number of strings s of length N consisting of A, B, and C that satisfy the following condition:

  • s can be converted to the empty string by repeating the following operation:
    • Choose two consecutive characters in s and erase them. However, choosing AB or BA is not allowed.

For example, ABBC satisfies the condition for N=4, because we can convert it as follows: ABBC → (erase BB) → AC → (erase AC) → (empty).

The answer can be enormous, so compute the count modulo 998244353.

Constraints

  • 2 \leq N \leq 10^7
  • N is an even number.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of strings that satisfy the conditions, modulo 998244353.


Sample Input 1

2

Sample Output 1

7

Except AB and BA, all possible strings satisfy the conditions.


Sample Input 2

10

Sample Output 2

50007

Sample Input 3

1000000

Sample Output 3

210055358
D - Balance Beam

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

配点 : 1100

問題文

1 から N までの番号がついた N 個の平均台があります. どの平均台の長さも 1 メートルです. すぬけくんが平均台 i の上をあるくスピードは 1 秒あたり 1/A_i メートルです. また,りんごさんが平均台 i の上をあるくスピードは 1 秒あたり 1/B_i メートルです.

すぬけくんとりんごさんが,以下のゲームを行います.

  • まず,すぬけくんが N 個の平均台を好きな順序で横一列に連結し,長さ N メートルの平均台を作る.
  • すぬけくんはこの平均台の左端からスタートする. りんごさんはこの平均台の上で一様ランダムに選ばれた一点からスタートする. 2 人は同時にスタートし,平均台の右端を目指して進む。
  • すぬけくんの勝利条件は,りんごさんが平均台の右端に到着する前にりんごさんに追いつくことである. つまり,すぬけくんとりんごさんの位置が同じになる瞬間があればすぬけくんの勝ち,そうでないならりんごさんの勝ちである.

すぬけくんが勝率を最大化するように平均台を並べたときの勝率を求めてください.

なお,この問題の答えは有理数になります. そこで,答えを既約分数 P/Q として表したときの P,Q を求めてください. ただし,答えが 0 の時は P=0,Q=1 としてください.

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i,B_i \leq 10^9
  • 入力される値はすべて整数である.

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

すぬけくんの勝率の最大値を表す既約分数の分子と分母を出力せよ.


入力例 1

2
3 2
1 2

出力例 1

1 4

平均台 2,1 の順に連結するとすぬけくんの勝率は 1/4 になり,これが最大です.


入力例 2

4
1 5
4 7
2 1
8 4

出力例 2

1 2

入力例 3

3
4 1
5 2
6 3

出力例 3

0 1

入力例 4

10
866111664 178537096
705445072 318106937
472381277 579910117
353498483 865935868
383133839 231371336
378371075 681212831
304570952 16537461
955719384 267238505
844917655 218662351
550309930 62731178

出力例 4

697461712 2899550585

Score : 1100 points

Problem Statement

We have N balance beams numbered 1 to N. The length of each beam is 1 meters. Snuke walks on Beam i at a speed of 1/A_i meters per second, and Ringo walks on Beam i at a speed of 1/B_i meters per second.

Snuke and Ringo will play the following game:

  • First, Snuke connects the N beams in any order of his choice and makes a long beam of length N meters.
  • Then, Snuke starts at the left end of the long beam. At the same time, Ringo starts at a point chosen uniformly at random on the long beam. Both of them walk to the right end of the long beam.
  • Snuke wins if and only if he catches up to Ringo before Ringo reaches the right end of the long beam. That is, Snuke wins if there is a moment when Snuke and Ringo stand at the same position, and Ringo wins otherwise.

Find the probability that Snuke wins when Snuke arranges the N beams so that the probability of his winning is maximized.

This probability is a rational number, so we ask you to represent it as an irreducible fraction P/Q (to represent 0, use P=0, Q=1).

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i,B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the numerator and denominator of the irreducible fraction that represents the maximum probability of Snuke's winning.


Sample Input 1

2
3 2
1 2

Sample Output 1

1 4

When the beams are connected in the order 2,1 from left to right, the probability of Snuke's winning is 1/4, which is the maximum possible value.


Sample Input 2

4
1 5
4 7
2 1
8 4

Sample Output 2

1 2

Sample Input 3

3
4 1
5 2
6 3

Sample Output 3

0 1

Sample Input 4

10
866111664 178537096
705445072 318106937
472381277 579910117
353498483 865935868
383133839 231371336
378371075 681212831
304570952 16537461
955719384 267238505
844917655 218662351
550309930 62731178

Sample Output 4

697461712 2899550585
E - Prefix Suffix Addition

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

配点 : 1400

問題文

すぬけくんは,長さ N の整数列 x_1,x_2,\cdots,x_N を持っています. 最初,x の全ての要素は 0 です.

すぬけくんは,以下の 2 種類の操作を好きな順序で好きな回数行うことができます.

  • 操作 1: 整数 k (1 \leq k \leq N),及び長さ k の非負整数列 c_1,c_2,\cdots,c_k を自由に選ぶ. ただし,c広義単調増加でなくてはならない. そして,すべての i (1 \leq i \leq k) について,x_ix_i+c_i で置き換える.
  • 操作 2: 整数 k (1 \leq k \leq N),及び長さ k の非負整数列 c_1,c_2,\cdots,c_k を自由に選ぶ. ただし,c広義単調減少な数列でなくてはならない. そして,すべての i (1 \leq i \leq k) について,x_{N-k+i}x_{N-k+i}+c_i で置き換える.

すぬけくんの目標は,全ての i について,x_i=A_i となるようにすることです. すぬけくんが目標を達成するために行う操作回数の最小値を求めてください.

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力される値はすべて整数である.

入力

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

N
A_1 A_2 \cdots A_N

出力

すぬけくんが目標を達成するために行う操作回数の最小値を出力せよ.


入力例 1

5
1 2 1 2 1

出力例 1

3

例えば,以下のように 3 回の操作を行えば良いです. 3 回未満の操作で目標は達成できません.

  • k=2,c=(1,2) として,操作 1 を行う.操作後,x=(1,2,0,0,0) となる.
  • k=3,c=(0,0,1) として,操作 1 を行う.操作後,x=(1,2,1,0,0) となる.
  • k=2,c=(2,1) として,操作 2 を行う.操作後,x=(1,2,1,2,1) となる.

入力例 2

5
2 1 2 1 2

出力例 2

2

入力例 3

15
541962451 761940280 182215520 378290929 211514670 802103642 28942109 641621418 380343684 526398645 81993818 14709769 139483158 444795625 40343083

出力例 3

7

Score : 1400 points

Problem Statement

Snuke has a sequence of N integers x_1,x_2,\cdots,x_N. Initially, all the elements are 0.

He can do the following two kinds of operations any number of times in any order:

  • Operation 1: choose an integer k (1 \leq k \leq N) and a non-decreasing sequence of k non-negative integers c_1,c_2,\cdots,c_k. Then, for each i (1 \leq i \leq k), replace x_i with x_i+c_i.
  • Operation 2: choose an integer k (1 \leq k \leq N) and a non-increasing sequence of k non-negative integers c_1,c_2,\cdots,c_k. Then, for each i (1 \leq i \leq k), replace x_{N-k+i} with x_{N-k+i}+c_i.

His objective is to have x_i=A_i for all i. Find the minimum number of operations required to achieve it.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 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 A_2 \cdots A_N

Output

Print the minimum number of operations required to achieve Snuke's objective.


Sample Input 1

5
1 2 1 2 1

Sample Output 1

3

One way to achieve the objective in three operations is shown below. The objective cannot be achieved in less than three operations.

  • Do Operation 1 and choose k=2,c=(1,2). Now we have x=(1,2,0,0,0).
  • Do Operation 1 and choose k=3,c=(0,0,1). Now we have x=(1,2,1,0,0).
  • Do Operation 2 and choose k=2,c=(2,1). Now we have x=(1,2,1,2,1).

Sample Input 2

5
2 1 2 1 2

Sample Output 2

2

Sample Input 3

15
541962451 761940280 182215520 378290929 211514670 802103642 28942109 641621418 380343684 526398645 81993818 14709769 139483158 444795625 40343083

Sample Output 3

7
F - Two Pieces

実行時間制限: 4 sec / メモリ制限: 1024 MB

配点 : 2200

問題文

数直線上に,区別できない 2 つの駒が置かれています. どちらの駒も最初,座標 0 にあります.(駒は同じ座標に同時に存在できます)

これらの駒に対して,以下の 2 種類の操作が可能です.

  • 好きな駒を 1 つ選び,1 大きい座標に移動する.
  • 座標の小さい駒を,座標の大きい駒の位置へと移動する. なお,もともと 2 つの駒が同じ座標に置いてある場合は何も起きないが,その場合でも 1 回の操作として数える.

以上の操作を好きな順番で N 回繰り返して,2 つの駒の一方が座標 A,他方が座標 B にあるようにしたいです. このような動かし方が何通りあるかを求めてください. ただし答えは非常に大きくなることがあるので,998244353 で割ったあまりを求めてください.

なお,ある 2 つの動かし方 x,y が異なるとは,整数 i (1 \leq i \leq N) であって, ( 動かし方 xi 回目の操作後に駒の置いてある座標の集合 )( 動かし方 yi 回目の操作後に駒の置いてある座標の集合 ) が異なるものが存在することを意味します.

制約

  • 1 \leq N \leq 10^7
  • 0 \leq A \leq B \leq N
  • 入力される値はすべて整数である.

入力

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

N A B

出力

条件をみたす駒の動かし方が何通りあるかを 998244353 で割ったあまりを出力せよ.


入力例 1

5 1 3

出力例 1

4

以下の 4 通りの動かし方があります. なお,(x,y) で,駒の座標がそれぞれ x,y にある状態を表しています.

  • (0,0)→(0,0)→(0,1)→(0,2)→(0,3)→(1,3)
  • (0,0)→(0,0)→(0,1)→(0,2)→(1,2)→(1,3)
  • (0,0)→(0,0)→(0,1)→(1,1)→(1,2)→(1,3)
  • (0,0)→(0,1)→(1,1)→(1,1)→(1,2)→(1,3)

入力例 2

10 0 0

出力例 2

1

入力例 3

10 4 6

出力例 3

197

入力例 4

1000000 100000 200000

出力例 4

758840509

Score : 2200 points

Problem Statement

We have two indistinguishable pieces placed on a number line. Both pieces are initially at coordinate 0. (They can occupy the same position.)

We can do the following two kinds of operations:

  • Choose a piece and move it to the right (the positive direction) by 1.
  • Move the piece with the smaller coordinate to the position of the piece with the greater coordinate. If two pieces already occupy the same position, nothing happens, but it still counts as doing one operation.

We want to do a total of N operations of these kinds in some order so that one of the pieces will be at coordinate A and the other at coordinate B. Find the number of ways to move the pieces to achieve it. The answer can be enormous, so compute the count modulo 998244353.

Two ways to move the pieces are considered different if and only if there exists an integer i (1 \leq i \leq N) such that the set of the coordinates occupied by the pieces after the i-th operation is different in those two ways.

Constraints

  • 1 \leq N \leq 10^7
  • 0 \leq A \leq B \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of ways to move the pieces to achieve our objective, modulo 998244353.


Sample Input 1

5 1 3

Sample Output 1

4

Shown below are the four ways to move the pieces, where (x,y) represents the state where the two pieces are at coordinates x and y.

  • (0,0)→(0,0)→(0,1)→(0,2)→(0,3)→(1,3)
  • (0,0)→(0,0)→(0,1)→(0,2)→(1,2)→(1,3)
  • (0,0)→(0,0)→(0,1)→(1,1)→(1,2)→(1,3)
  • (0,0)→(0,1)→(1,1)→(1,1)→(1,2)→(1,3)

Sample Input 2

10 0 0

Sample Output 2

1

Sample Input 3

10 4 6

Sample Output 3

197

Sample Input 4

1000000 100000 200000

Sample Output 4

758840509