A - Compromise

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

配点 : 100

問題文

N 個の選択肢があります。

あなたは、これらの中から 1 つの選択肢を選びます。あなたが i 番目の選択肢を選んだときの嬉しさは X_i です。

どの選択肢を選んでも嬉しさが負になる場合は Yes を、そうでない場合は No を出力してください。

制約

  • 2 \leq N \leq 10
  • -100 \leq X_i \leq 100
  • 入力される値はすべて整数

入力

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

N
X_1 X_2 \ldots X_N

出力

問題文中の指示に従って YesNo のいずれかを出力せよ。


入力例 1

4
2 0 -1 2

出力例 1

No

選択肢 1, 2, 4 のいずれかを選んだとき、嬉しさは負になりません。


入力例 2

3
-5 -2 -1

出力例 2

Yes

入力例 3

4
0 -2 0 -1

出力例 3

No

Score : 100 points

Problem Statement

There are N choices.

You will choose one of these choices. If you choose the i-th choice, your happiness will be X_i.

If your happiness will be negative no matter which choice you choose, output Yes; otherwise, output No.

Constraints

  • 2 \leq N \leq 10
  • -100 \leq X_i \leq 100
  • All input values are integers.

Input

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

N
X_1 X_2 \ldots X_N

Output

Output Yes or No according to the instructions in the problem statement.


Sample Input 1

4
2 0 -1 2

Sample Output 1

No

If you choose choice 1, 2, or 4, the happiness will not be negative.


Sample Input 2

3
-5 -2 -1

Sample Output 2

Yes

Sample Input 3

4
0 -2 0 -1

Sample Output 3

No
B - Representative Balls

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

配点 : 200

問題文

N 個のボールがあります。

i 番目のボールの色は C_i、大きさは S_i です。ここで、色は 1, 2, \ldots, M の整数で表されます。

k = 1, 2, \ldots, M について、色 k のボールの大きさの最大値を出力してください。ただし、色 k のボールが存在しない場合は -1 と出力してください。

制約

  • 1 \leq N, M \leq 100
  • 1 \leq C_i \leq M
  • 1 \leq S_i \leq 100
  • 入力される値はすべて整数

入力

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

N M
C_1 S_1
C_2 S_2
\vdots
C_N S_N

出力

k = 1, 2, \ldots, M の順に、色 k のボールが存在するならば色 k のボールの大きさの最大値を、存在しないならば -1 を空白区切りで出力せよ。


入力例 1

4 5
1 3
2 10
1 7
4 9

出力例 1

7 10 -1 9 -1

1 のボールの大きさの最大値は 7、色 2 のボールの大きさの最大値は 10、色 4 のボールの大きさの最大値は 9 です。

3 のボール、色 5 のボールは存在しません。


入力例 2

5 5
2 6
5 12
5 2
5 9
2 7

出力例 2

-1 7 -1 -1 12

Score : 200 points

Problem Statement

There are N balls.

The color of the i-th ball is C_i, and its size is S_i. Here, the colors are represented by integers 1, 2, \ldots, M.

For k = 1, 2, \ldots, M, output the maximum size of a ball of color k, or -1 if there is no ball of color k.

Constraints

  • 1 \leq N, M \leq 100
  • 1 \leq C_i \leq M
  • 1 \leq S_i \leq 100
  • All input values are integers.

Input

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

N M
C_1 S_1
C_2 S_2
\vdots
C_N S_N

Output

In the order k = 1, 2, \ldots, M, output the maximum size of a ball of color k if such a ball exists, or -1 otherwise, separated by spaces.


Sample Input 1

4 5
1 3
2 10
1 7
4 9

Sample Output 1

7 10 -1 9 -1

The maximum size of a ball of color 1 is 7, that of a ball of color 2 is 10, and that of a ball of color 4 is 9.

There is no ball of color 3 or color 5.


Sample Input 2

5 5
2 6
5 12
5 2
5 9
2 7

Sample Output 2

-1 7 -1 -1 12
C - Count Close Pairs

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

配点 : 300

問題文

この問題は インタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。

数直線上に、点 1, 2, \ldots, Nこの順に 左から右に並んでいます。

最初、あなたには整数 N のみが与えられます。

その後、あなたはジャッジに以下の質問を \bm{2N} 回まで行うことができます。

  • 1 \leq i < j \leq N を満たす整数 i,j を選び、点 i と点 j の距離が 1 以下であるか質問する。

距離が 1 以下の 2 点組の個数、すなわち 1\leq i<j\leq N をみたす整数の組 (i,j) であって、点 i と点 j の距離が 1 以下であるようなものの個数を出力してください。

制約

  • 2 \leq N \leq 10^3
  • N は整数

入出力

最初に、点の個数を表す整数 N を標準入力から受け取ってください。

N

次に、あなたはジャッジに対して問題文中の質問を 2N 回まで繰り返すことができます。

質問は、以下の形式で標準出力に出力してください。 ここで、i,j1 \leq i<j \leq N を満たす整数である必要があります。

? i j

これに対する応答として、以下のどちらかが標準入力から与えられます。

Yes
No

ここで、Yes ならば点 i と点 j の距離が 1 以下であることを、No ならば点 i と点 j の距離が 1 より大きいことを表します。

問題の答え X が求まったら、解答を以下の形式で出力してください。 その後、ただちにプログラムを終了してください。

! X

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
  • 解答を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
  • 点の配置および答えはあなたとジャッジの対話の開始時に固定され、あなたが行った質問などに応じて変更されることはありません。

入出力例

以下は、N = 3、点 1,2,3 の座標がそれぞれ 0,0.7,1.5 の場合の入出力例です。(点 1,2,3 の座標は入力として与えられません。)

入力 出力 説明
3 N が与えられます。
? 1 2 1,2 の間の距離が 1 以下かをジャッジに質問します。
Yes ジャッジから、点 1,2 の間の距離が 1 以下であるという返答を得ます。
? 1 3 1,3 の間の距離が 1 以下かをジャッジに質問します。
No ジャッジから、点 1,3 の間の距離が 1 以下でないという返答を得ます。
? 2 3 2,3 の間の距離が 1 以下かをジャッジに質問します。
Yes ジャッジから、点 2,3 の間の距離が 1 以下であるという返答を得ます。
! 2 条件をみたす組の数として 2 を解答します。

質問回数は 3 回であり、特に 2N=6 回以下です。
解答した組の数も正しいため、この後ただちにプログラムを終了することで、正解と判定されます。

Score : 300 points

Problem Statement

This is an interactive problem (in which your program and the judge program communicate via Standard Input and Output).

On a number line, points 1, 2, \ldots, N are arranged from left to right in that order.

Initially, you are given only the integer N.

Then, you can ask the judge the following question at most \bm{2N} times.

  • Choose integers i and j satisfying 1 \leq i < j \leq N, and ask whether the distance between points i and j is at most 1.

Output the number of pairs of points whose distance is at most 1, that is, the number of pairs of integers (i,j) satisfying 1 \leq i < j \leq N such that the distance between points i and j is at most 1.

Constraints

  • 2 \leq N \leq 10^3
  • N is an integer.

Interaction

First, receive the integer N representing the number of points from Standard Input:

N

Then, you can repeat the question described in the problem statement to the judge at most 2N times.

Output the question to Standard Output in the following format, where i and j must be integers satisfying 1 \leq i<j \leq N:

? i j

As a response to this, one of the following will be given from Standard Input:

Yes
No

Here, Yes indicates that the distance between points i and j is at most 1, and No indicates that the distance between points i and j is greater than 1.

Once you have found the answer X to the problem, output your answer in the following format, and then terminate the program immediately:

! X

Notes

  • Each time you output something, insert a newline at the end and flush Standard Output. Otherwise, the judge result may be TLE.
  • The judge result is indeterminate if you output something invalid during the interaction, or if the program terminates prematurely.
  • Terminate the program immediately after outputting your answer. Otherwise, the judge result is indeterminate.
  • The arrangement of the points and the answer are fixed at the start of the interaction between you and the judge, and do not change depending on, for example, the questions you make.

Sample Interaction

Below is an example of interaction where N = 3 and the coordinates of points 1,2,3 are 0, 0.7, 1.5, respectively. (The coordinates of points 1,2,3 are not given as input.)

Input Output Explanation
3 N is given.
? 1 2 Ask the judge whether the distance between points 1 and 2 is at most 1.
Yes Obtain the response from the judge that the distance between points 1 and 2 is at most 1.
? 1 3 Ask the judge whether the distance between points 1 and 3 is at most 1.
No Obtain the response from the judge that the distance between points 1 and 3 is not at most 1.
? 2 3 Ask the judge whether the distance between points 2 and 3 is at most 1.
Yes Obtain the response from the judge that the distance between points 2 and 3 is at most 1.
! 2 Answer 2 as the number of pairs satisfying the condition.

The number of questions is 3, which is in particular at most 2N=6.
Since the answered number of pairs is also correct, the program will be judged as correct if it terminates immediately after this.

D - Placing Rooks

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

配点 : 400

問題文

NN 列のマス目があります。

最初、マス目の上には何も置かれていません。
その状態から始めて、高橋君がマス目に対して M 回の操作を順に行います。i 回目 (1\leq i\leq M) の操作は以下のとおりです。

  • 上から R_i 行目のマスに置かれているコマをすべて取り除く。
  • 次に、左から C_i 列目のマスに置かれているコマをすべて取り除く。
  • 最後に、上から R_i 行目かつ左から C_i 列目のマスにコマを置く。

M 回の操作の後でマス目に置かれているコマの個数を出力してください。

制約

  • 1 \leq N \leq 3\times 10^5
  • 1 \leq M \leq 3\times 10^5
  • 1 \leq R_i \leq N
  • 1 \leq C_i \leq N
  • 入力はすべて整数

入力

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

N M
R_1 C_1
R_2 C_2
\vdots
R_M C_M

出力

M 回の操作の後でマス目に置かれているコマの個数を出力せよ。


入力例 1

3 6
1 1
1 2
3 3
3 2
1 3
1 3

出力例 1

2

最初、33 列のマス目の上に何も置かれていないところから、それぞれの操作によって、次のようにコマが取り除かれ、置かれます。
以下では、上から i 行目かつ左から j 列目のマスをマス (i,j) で表します。

  • 1 回目の操作では、マス (1,1) にコマが置かれます。
  • 2 回目の操作では、マス (1,1) からコマが取り除かれ、マス (1,2) にコマが置かれます。
  • 3 回目の操作では、マス (3,3) にコマが置かれます。
  • 4 回目の操作では、マス (1,2) およびマス (3,3) からコマが取り除かれ、マス (3,2) にコマが置かれます。
  • 5 回目の操作では、マス (1,3) にコマが置かれます。
  • 6 回目の操作では、マス (1,3) からコマが取り除かれ、マス (1,3) に再度コマが置かれます。

最終状態において、マス (1,3) およびマス (3,2)1 つずつコマが置かれているため、2 を出力します。


入力例 2

2 3
1 2
2 1
1 1

出力例 2

1

Score : 400 points

Problem Statement

There is a grid with N rows and N columns.

Initially, nothing is placed on the grid.
Starting from this state, Takahashi performs M operations on the grid in order. The i-th operation (1\leq i\leq M) is as follows.

  • Remove all pieces placed on the cells in the R_i-th row from the top.
  • Next, remove all pieces placed on the cells in the C_i-th column from the left.
  • Finally, place a piece on the cell at the R_i-th row from the top and the C_i-th column from the left.

Output the number of pieces placed on the grid after the M operations.

Constraints

  • 1 \leq N \leq 3\times 10^5
  • 1 \leq M \leq 3\times 10^5
  • 1 \leq R_i \leq N
  • 1 \leq C_i \leq N
  • All input values are integers.

Input

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

N M
R_1 C_1
R_2 C_2
\vdots
R_M C_M

Output

Output the number of pieces placed on the grid after the M operations.


Sample Input 1

3 6
1 1
1 2
3 3
3 2
1 3
1 3

Sample Output 1

2

Initially, nothing is placed on the grid with three rows and three columns, and pieces are removed and placed by each operation as follows.
Below, the cell at the i-th row from the top and the j-th column from the left is denoted as cell (i,j).

  • In the first operation, a piece is placed on cell (1,1).
  • In the second operation, the piece is removed from cell (1,1), and a piece is placed on cell (1,2).
  • In the third operation, a piece is placed on cell (3,3).
  • In the fourth operation, the pieces are removed from cell (1,2) and cell (3,3), and a piece is placed on cell (3,2).
  • In the fifth operation, a piece is placed on cell (1,3).
  • In the sixth operation, the piece is removed from cell (1,3), and a piece is placed on cell (1,3) again.

In the final state, there is one piece each on cell (1,3) and cell (3,2), so output 2.


Sample Input 2

2 3
1 2
2 1
1 1

Sample Output 2

1
E - Range Flip

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

配点 : 450

問題文

N 枚のカードが並んでいます。カードには 1, 2, \ldots, N の番号が付けられています。

カード i の表面には整数 A_i が、裏面には整数 B_i が書かれています。はじめ、すべてのカードは表面が上を向いています。

あなたは、以下の操作を高々 K 回行うことができます。

  • 1 \leq l \leq r \leq N なる整数 l, r を選ぶ。l \leq i \leq r なる各整数 i について、カード i を裏返す。ただし、カードを裏返すとは、操作を行う前に下を向いている面を上に向けることを指す。

操作を終えた後、各カードの上を向いている面に書かれている数の総和として考えられる最大値を求めてください。

制約

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

入力

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

N K
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

答えを出力せよ。


入力例 1

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

出力例 1

45

1 回目の操作で l = 2, r = 5 とし、2 回目の操作で l = 4, r = 4 とすると、上を向いている面に書かれている数はカードの番号の順に 2, 9, 5, 9, 8, 7, 5 となり、和は 45 です。


入力例 2

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

出力例 2

35

1 回も操作を行わなくてもよいです。


入力例 3

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

出力例 3

47

Score : 450 points

Problem Statement

There are N cards lined up. The cards are numbered 1, 2, \ldots, N.

On the front side of card i, an integer A_i is written, and on the back side, an integer B_i is written. Initially, every card is facing front side up.

You can perform the following operation at most K times.

  • Choose integers l and r satisfying 1 \leq l \leq r \leq N. For each integer i satisfying l \leq i \leq r, flip card i. Here, flipping a card means turning the side that was facing down before the operation to face up.

After finishing the operations, find the maximum possible value of the sum of the numbers written on the side of the cards that is facing up.

Constraints

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

Input

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

N K
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Output the answer.


Sample Input 1

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

Sample Output 1

45

If you choose l = 2, r = 5 in the first operation and l = 4, r = 4 in the second operation, the numbers written on the side facing up, in order of the card numbers, become 2, 9, 5, 9, 8, 7, 5, and their sum is 45.


Sample Input 2

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

Sample Output 2

35

It is fine if you perform no operation.


Sample Input 3

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

Sample Output 3

47
F - Many Mod Calculation

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

配点 : 500

問題文

整数 N,X と長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

非負整数 x に対し、 f(x)=(\ldots((x \bmod A_1) \bmod A_2) \ldots ) \bmod A_N と定義します。

f(x)=0 となる 1 以上 X 以下の整数 x がいくつ存在するか求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\le T\le 2\times 10^5
  • 1\le N\le 2\times 10^5
  • 全てのテストケースにおける N の総和は 2\times 10^5 以下
  • 1\le X\le 10^{18}
  • 1\le A_i\le 10^{18}
  • 入力される値は全て整数

入力

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

各テストケースは以下の形式で与えられる。

N X
A_1 A_2 \ldots A_N

出力

各テストケースに対する答えを順に改行区切りで出力せよ。


入力例 1

4
3 7
5 2 3
9 31415
9 9 8 2 4 4 3 5 3
1 1000000000000000000
1
9 20260405
3141 5926 5358 9793 2384 6264 3383 2795 288

出力例 1

4
17452
1000000000000000000
77403

1 番目のテストケースについて考えます。

例えば x=7 のとき f(7)=(((7 \bmod 5) \bmod 2)\bmod 3)=(2\bmod 2)\bmod 3=0\bmod 3=0 となります。

f(x)=0 となる 1 以上 7 以下の整数 xx=2,4,5,74 つです。

Score : 500 points

Problem Statement

You are given integers N,X and a length-N sequence of positive integers A=(A_1,A_2,\ldots,A_N).

For a non-negative integer x, define f(x)=(\ldots((x \bmod A_1) \bmod A_2) \ldots ) \bmod A_N.

Find the number of integers x between 1 and X, inclusive, such that f(x)=0.

You are given T test cases; solve each of them.

Constraints

  • 1\le T\le 2\times 10^5
  • 1\le N\le 2\times 10^5
  • The sum of N over all test cases is at most 2\times 10^5.
  • 1\le X\le 10^{18}
  • 1\le A_i\le 10^{18}
  • All input values are integers.

Input

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Each test case is given in the following format:

N X
A_1 A_2 \ldots A_N

Output

Output the answers for the test cases in order, separated by newlines.


Sample Input 1

4
3 7
5 2 3
9 31415
9 9 8 2 4 4 3 5 3
1 1000000000000000000
1
9 20260405
3141 5926 5358 9793 2384 6264 3383 2795 288

Sample Output 1

4
17452
1000000000000000000
77403

Consider the first test case.

For example, when x=7, f(7)=(((7 \bmod 5) \bmod 2)\bmod 3)=(2\bmod 2)\bmod 3=0\bmod 3=0.

There are four integers x between 1 and 7 such that f(x)=0: x=2,4,5,7.

G - Segment Sum Constraints

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

配点 : 600

問題文

3 つの整数の組 (L_i,R_i,S_i)M 個与えられます。
N 個の正の整数の組 A=(A_1,A_2,\ldots,A_N) であって、次の条件をすべてみたすものを考えます。

  • A_{L_i}, A_{L_i+1}, \ldots, A_{R_i} の総和は S_i である。

条件をみたす組が無限に存在する場合は Infinity を、そうでない場合はその数を 998244353 で割った余りを出力してください。

制約

  • 1 \leq N \leq 8
  • 1 \leq M \leq 36
  • 1\leq L_i\leq R_i\leq N
  • 1\leq S_i\leq 10^9
  • (L_i,R_i) はすべて異なる。
  • 入力される値はすべて整数

入力

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

N M
L_1 R_1 S_1
L_2 R_2 S_2
\vdots
L_M R_M S_M

出力

条件をみたす組が無限に存在する場合は Infinity を、そうでない場合はその数を 998244353 で割った余りを出力せよ。


入力例 1

3 2
1 2 7
2 3 10

出力例 1

6

条件は以下の 2 つです。

  • A_1+A_2=7
  • A_2+A_3=10

A=(1,6,4), (2,5,5), (3,4,6), (4,3,7), (5,2,8), (6,1,9)6 つが条件をみたすため、6998244353 で割った余りである 6 を出力します。


入力例 2

2 1
1 1 10

出力例 2

Infinity

条件をみたす A は無数に存在します。


入力例 3

2 2
1 1 10
1 2 1

出力例 3

0

条件をみたす A は存在しません。

Score : 600 points

Problem Statement

You are given M triples of integers (L_i,R_i,S_i).
Consider tuples A=(A_1,A_2,\ldots,A_N) of N positive integers satisfying all of the following conditions.

  • The sum of A_{L_i}, A_{L_i+1}, \ldots, A_{R_i} is S_i.

If there are infinitely many tuples satisfying the conditions, output Infinity; otherwise, output the number of such tuples, modulo 998244353.

Constraints

  • 1 \leq N \leq 8
  • 1 \leq M \leq 36
  • 1\leq L_i\leq R_i\leq N
  • 1\leq S_i\leq 10^9
  • All (L_i,R_i) are distinct.
  • All input values are integers.

Input

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

N M
L_1 R_1 S_1
L_2 R_2 S_2
\vdots
L_M R_M S_M

Output

If there are infinitely many tuples satisfying the conditions, output Infinity; otherwise, output the number of such tuples, modulo 998244353.


Sample Input 1

3 2
1 2 7
2 3 10

Sample Output 1

6

We have the following two conditions.

  • A_1+A_2=7
  • A_2+A_3=10

The six tuples A=(1,6,4), (2,5,5), (3,4,6), (4,3,7), (5,2,8), (6,1,9) satisfy the conditions, so output 6 modulo 998244353, that is, output 6.


Sample Input 2

2 1
1 1 10

Sample Output 2

Infinity

There are infinitely many tuples A satisfying the conditions.


Sample Input 3

2 2
1 1 10
1 2 1

Sample Output 3

0

There is no tuple A satisfying the conditions.