A - Make 10

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さが 2 の棒が N_2 個、長さが 3 の棒が N_3 個、長さが 4 の棒が N_4 個あります。あなたは、これらの棒に対して次の操作を何度でも行うことができます:

  • 2 つの棒を選ぶ。
  • 選んだ棒の長さが x, y であるとき、これらを接着することで、長さ x+y の棒を作る。

長さがちょうど 10 に等しい棒を最大でいくつ作れるかを求めてください。

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

制約

  • 1\leq T\leq 100
  • 0\leq N_2, N_3, N_4\leq 10^{15}

入力

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

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

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

N_2 N_3 N_4

出力

T 行出力してください。i 行目には、\text{case}_i に対する答えを出力してください。


入力例 1

5
3 4 1
7 0 0
0 0 7
0 0 0
1000000000000000 1000000000000000 1000000000000000

出力例 1

2
1
0
0
900000000000000

ひとつめのテストケースについて説明します。 長さ 2 の棒が 3 個、長さ 3 の棒が 4 個、長さ 4 の棒が 1 個あります。

例えば以下のようにして、長さがちょうど 10 の棒を 2 個作ることができます。

  • 長さが 2, 2, 3, 3 の棒を適当な順序で接着することで、長さが 10 の棒をひとつ作ることができます。
  • 長さが 3, 3, 4 の棒を適当な順序で接着することで、長さが 10 の棒をひとつ作ることができます。
  • これらの操作の後、長さが 2, 10, 10 の棒が手元に残ります。

Score : 300 points

Problem Statement

We have N_2 sticks of length 2 each, N_3 sticks of length 3 each, and N_4 sticks of length 4 each. You can do the following operation any number of times.

  • Choose two sticks.
  • Let x and y be the lengths of these sticks. Bond them to form a stick of length x+y.

Find the maximum number of sticks that can be made whose lengths are exactly 10.

Given T test cases, solve each of them.

Constraints

  • 1\leq T\leq 100
  • 0\leq N_2, N_3, N_4\leq 10^{15}

Input

Input is given from Standard Input in the following format:

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

Each case is in the following format:

N_2 N_3 N_4

Output

Print T lines; the i-th line should contain the answer for \text{case}_i.


Sample Input 1

5
3 4 1
7 0 0
0 0 7
0 0 0
1000000000000000 1000000000000000 1000000000000000

Sample Output 1

2
1
0
0
900000000000000

Let us describe the first case. We have three sticks of length 2, four sticks of length 3, and one stick of length 4.

One way to make two sticks of length exactly 10 is as follows.

  • Bond four sticks of length 2, 2, 3, 3 in some order to make one stick of length 10.
  • Bond three sticks of length 3, 3, 4 in some order to make one stick of length 10.
  • Now we have three sticks of length 2, 10, 10.
B - Cross-free Matching

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

座標平面上に、x 座標が 1, 2, \ldots, Ny 座標が 0 または 1 であるような合計 2N 個の頂点 (1, 0),\ldots, (N,0), (1,1), \ldots, (N,1) があります。 これらのうちの 2 頂点を結ぶ線分が M 個あり、i 番目の線分は (a_i, 0)(b_i, 1) を結んでいます。

これら M 個の線分から K 個の線分を選び、選んだ線分のうちどの 2 個の線分も同一の点を含まないようにすることを考えます。ただし、線分の両端点も線分に含まれる点として扱います。可能な K の最大値を求めてください。

制約

  • 1\leq N, M \leq 2\times 10^5
  • 1\leq a_i, b_i\leq N
  • i\neq j ならば、a_i\neq a_j または b_i\neq b_j

入力

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

N M
a_1 b_1
\vdots
a_M b_M

出力

可能な K の最大値を出力してください。


入力例 1

3 3
1 2
2 3
3 1

出力例 1

2

1, 2 番目の線分を選ぶことが、最適解のひとつです。

例えば 1 番目の線分と 3 番目の線分は同一の点 \left(\frac53, \frac23\right) を含むため、同時に選ぶことはできません。


入力例 2

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

出力例 2

3

1, 3, 5 番目の線分を選ぶことが、最適解のひとつです。

例えば 1 番目の線分と 2 番目の線分は同一の点 (1, 1) を含むため、同時に選ぶことはできません。


入力例 3

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

出力例 3

1

Score : 400 points

Problem Statement

In the coordinate plane, we have 2N vertices whose x-coordinates are 1, 2, \ldots, N and whose y-coordinates are 0 or 1: (1, 0),\ldots, (N,0), (1,1), \ldots, (N,1). There are M segments that connect two of these vertices: the i-th segment connects (a_i, 0) and (b_i, 1).

Consider choosing K of these M segments so that no two chosen segments contain the same point. Here, we consider both endpoints of a segment to be contained in that segment. Find the maximum possible value of K.

Constraints

  • 1\leq N, M \leq 2\times 10^5
  • 1\leq a_i, b_i\leq N
  • a_i\neq a_j or b_i\neq b_j, if i\neq j.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
\vdots
a_M b_M

Output

Print the maximum possible value of K.


Sample Input 1

3 3
1 2
2 3
3 1

Sample Output 1

2

One optimal solution is to choose the first and second segments.

The first and third segments, for example, contain the same point \left(\frac53, \frac23\right), so we cannot choose them both.


Sample Input 2

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

Sample Output 2

3

One optimal solution is to choose the first, third, and fifth segments.

The first and second segments, for example, contain the same point (1, 1), so we cannot choose them both.


Sample Input 3

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

Sample Output 3

1

C - Maximize GCD

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 項からなる正整数列 A = (A_1, A_2, \ldots, A_N) が与えられます。あなたはこの数列に対して、次の操作を 0 回以上 K 回以下行うことができます:

  • i\in \{1,2,\ldots,N\} をひとつ選び、A_i1 を加える。

操作後の \gcd(A_1, A_2, \ldots, A_N) としてありうる最大値を求めてください。

制約

  • 2\leq N\leq 3\times 10^5
  • 1\leq K\leq 10^{18}
  • 1 \leq A_i\leq 3\times 10^5

入力

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

N K
A_1 A_2 \ldots A_N

出力

操作後の \gcd(A_1, A_2, \ldots, A_N) としてありうる最大値を出力してください。


入力例 1

3 6
3 4 9

出力例 1

5

例えば以下のようにして、\gcd(A_1, A_2, A_3) = 5 を実現できます。

  • i = 1 に対して 2 回、i = 2 に対して 1 回、i=3 に対して 1 回の操作を行う。合計の操作回数は 4 回で、K=6 以下である。
  • 操作の結果、A_1 = 5, A_2 = 5, A_3 = 10 となり、\gcd(A_1, A_2, A_3) = 5 である。

入力例 2

3 4
30 10 20

出力例 2

10

操作を一度も行わないことで、\gcd(A_1, A_2, A_3) = 10 を実現できます。


入力例 3

5 12345
1 2 3 4 5

出力例 3

2472

Score : 600 points

Problem Statement

Given is a sequence of N positive integers: A = (A_1, A_2, \ldots, A_N). You can do the following operation on this sequence at least zero and at most K times:

  • choose i\in \{1,2,\ldots,N\} and add 1 to A_i.

Find the maximum possible value of \gcd(A_1, A_2, \ldots, A_N) after your operations.

Constraints

  • 2\leq N\leq 3\times 10^5
  • 1\leq K\leq 10^{18}
  • 1 \leq A_i\leq 3\times 10^5

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print the maximum possible value of \gcd(A_1, A_2, \ldots, A_N) after your operations.


Sample Input 1

3 6
3 4 9

Sample Output 1

5

One way to achieve \gcd(A_1, A_2, A_3) = 5 is as follows.

  • Do the operation with i = 1 twice, with i = 2 once, and with i = 3 once, for a total of four times, which is not more than K=6.
  • Now we have A_1 = 5, A_2 = 5, A_3 = 10, for which \gcd(A_1, A_2, A_3) = 5.

Sample Input 2

3 4
30 10 20

Sample Output 2

10

Doing no operation achieves \gcd(A_1, A_2, A_3) = 10.


Sample Input 3

5 12345
1 2 3 4 5

Sample Output 3

2472
D - Pure Straight

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 項からなる正整数列 A = (A_1, A_2, \ldots, A_N) が与えられます。各 A_i1, 2, \ldots, K のいずれかです。

あなたはこの数列に対して、次の操作を何度でも行うことができます:

  • 隣接する 2 項を入れ替える。つまり、|i-j|=1 となる i, j を選び、A_iA_j を入れ替える。

数列 A が以下の条件を満たすようにするために必要な操作回数の最小値を求めてください。

  • 数列 A は、連続部分列として (1, 2, \ldots, K) を含む。 つまり、A_n = 1, A_{n+1} = 2, \ldots, A_{n+K-1} = K が成り立つような N-K+1 以下の正整数 n が存在する。

制約

  • 2\leq K\leq 16
  • K \leq N\leq 200
  • A_i1, 2, \ldots, K のいずれかに等しい
  • 数列 A は、1, 2, \ldots, K のそれぞれを少なくともひとつ含む

入力

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

N K
A_1 A_2 \ldots A_N

出力

数列 A が条件を満たすようにするために必要な操作回数の最小値を出力してください。


入力例 1

4 3
3 1 2 1

出力例 1

2

例えば次のように操作を行うのが最適です。

  • A_1A_2 を入れ替える。A(1,3,2,1) へ変化する。
  • A_2A_3 を入れ替える。A(1,2,3,1) へ変化する。
  • A_1 = 1, A_2 = 2, A_3 = 3 が成り立ち、条件を満たす。

入力例 2

5 5
4 1 5 2 3

出力例 2

5

入力例 3

8 4
4 2 3 2 4 2 1 4

出力例 3

5

Score : 600 points

Problem Statement

Given is a sequence of N positive integers A = (A_1, A_2, \ldots, A_N), where each A_i is 1, 2, \ldots, or K.

You can do the following operation on this sequence any number of times.

  • Swap two adjacent elements, that is, choose i and j such that |i-j|=1 and swap A_i and A_j.

Find the minimum number of operations needed to make A satisfy the following condition.

  • A contains (1, 2, \ldots, K) as a contiguous subsequence, that is, there is a positive integer n at most N-K+1 such that A_n = 1, A_{n+1} = 2, \ldots, and A_{n+K-1} = K.

Constraints

  • 2\leq K\leq 16
  • K \leq N\leq 200
  • A_i is 1, 2, \ldots, or K.
  • A contains at least one occurrence of each of 1, 2, \ldots, K.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print the minimum number of operations needed to make A satisfy the condition.


Sample Input 1

4 3
3 1 2 1

Sample Output 1

2

One optimal sequence of operations is as follows.

  • Swap A_1 and A_2, changing A to (1,3,2,1).
  • Swap A_2 and A_3, changing A to (1,2,3,1).
  • Now we have A_1 = 1, A_2 = 2, A_3 = 3, satisfying the condition.

Sample Input 2

5 5
4 1 5 2 3

Sample Output 2

5

Sample Input 3

8 4
4 2 3 2 4 2 1 4

Sample Output 3

5
E - Infinite Operations

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 800

問題文

N 項からなる正整数列 A = (A_1, A_2, \ldots, A_N)Q 個のクエリが与えられます。i 番目のクエリは、以下のようなものです:

  • 整数 x_i, y_i (ただし 1\leq x_i\leq N)が与えられる。A_{x_i}y_i に変更する。

クエリで数列が変更されるたびに、以下の問題の答えを \mod 998244353 で求めてください(注記参照)。

数列 A に対して以下の操作を n 回行うとき、獲得できる総得点の最大値を f(n) とする。

  • A_i\leq A_j となる i, j および A_i + 2x \leq A_j となる非負実数 x を選ぶ。
  • A_ix を加え、A_j から x を引く。
  • x 点を獲得する。

極限 \displaystyle \lim_{n\to\infty} f(n) が存在することが証明できる。この値を求めよ。

注記

求める極限は必ず有理数となることが証明できます。またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R\times Q\equiv P\pmod{998244353} かつ 0\leq R < 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 2\leq N\leq 3\times 10^5
  • 1\leq Q\leq 3\times 10^5
  • 1\leq A_i \leq 10^9
  • 1\leq x_i\leq N
  • 1\leq y_i\leq 10^9

入力

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

N Q
A_1 A_2 \ldots A_N
x_1 y_1
\vdots
x_Q y_Q

出力

Q 行出力してください。i 行目には、i 番目のクエリで数列を変更した時点での、問題の答えを出力してください。


入力例 1

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

出力例 1

0
1
2
2

1 つめのクエリにより、数列は (5, 5, 5) へと変更されます。この場合任意の n に対して f(n) = 0 となり、答えは 0 となります。

2 つめのクエリにより、数列は (5,6,5) へと変更されます。操作は例えば以下のように進行します。

  • (i,j,x) = (3,2,0.4) と選ぶ。数列を (5, 5.6, 5.4) へ変更し、0.4 点を獲得する。
  • (i,j,x) = (1,2,0.3) と選ぶ。数列を (5.3, 5.3, 5.4) へ変更し、0.3 点を獲得する。

上の方法では 2 回の操作により 0.7 点を獲得しており、f(2) \geq 0.7 であることがわかります。

この場合、獲得できる総得点は 1 を超えることはなく、操作回数を増やしていき最適な方法で操作を行うことで、獲得できる総得点を限りなく 1 に近づけることが可能であることが証明できます。したがって \displaystyle \lim_{n\to\infty} f(n) = 1 となります。


入力例 2

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

出力例 2

2
1
499122178
499122177

Score : 800 points

Problem Statement

Given are a sequence of N positive integers A = (A_1, A_2, \ldots, A_N) and Q queries. The i-th query is as follows:

  • given integers x_i and y_i (where 1\leq x_i\leq N), change A_{x_i} to y_i.

Each time the query changes the sequence, find the answer to the following problem modulo 998244353 (see Notes).

Let f(n) be the maximum total number of points when doing the following operation n times on A.

  • Choose i, j such that A_i\leq A_j and choose non-negative real number x such that A_i + 2x \leq A_j.
  • Add x to A_i and subtract x from A_j.
  • Gain x points.

It can be proved that the limit \displaystyle \lim_{n\to\infty} f(n) exists. Find this limit.

Notes

We can prove that the limit in question is always a rational number. Additionally, under the Constraints of this problem, when that number is represented as \frac{P}{Q} using two coprime integers P and Q, we can prove that there is a unique integer R such that R\times Q\equiv P\pmod{998244353} and 0\leq R < 998244353. Find this R.

Constraints

  • 2\leq N\leq 3\times 10^5
  • 1\leq Q\leq 3\times 10^5
  • 1\leq A_i \leq 10^9
  • 1\leq x_i\leq N
  • 1\leq y_i\leq 10^9

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 \ldots A_N
x_1 y_1
\vdots
x_Q y_Q

Output

Print Q lines; the i-th line should contain the answer to the problem just after the change by the i-th query.


Sample Input 1

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

Sample Output 1

0
1
2
2

The first query changes the sequence to (5, 5, 5). Here, we have f(n) = 0 for any n, so the answer is 0.

The second query changes the sequence to (5, 6, 5). Here, one possible way to do the operations is as follows.

  • Choose (i,j,x) = (3,2,0.4), changing the sequence to (5, 5.6, 5.4) and gaining 0.4 points.
  • Choose (i,j,x) = (1,2,0.3), changing the sequence to (5.3, 5.3, 5.4) and gaining 0.3 points.

The above two operations gain 0.7 points, from which we can see that f(2) \geq 0.7.

We can prove that it is impossible to gain more than 1 point in total, and that the total number of points that can be gained by the optimal way to do the operations tends to 1 as the number of operations increases. Thus, we have \displaystyle \lim_{n\to\infty} f(n) = 1.


Sample Input 2

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

Sample Output 2

2
1
499122178
499122177
F - Affine Sort

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 1100

問題文

N 項からなる正整数列 X = (X_1, X_2, \ldots, X_N) が与えられます。

正の整数 K に対して、整数の組 (a,b,c) のうちで以下の条件がすべて成り立つものの個数を f(K) と書くことにします。

  • 1\leq c \leq K
  • 0\leq a < c かつ 0\leq b < c
  • i に対して aX_i + bc で割った余りを Y_i とするとき、Y_1 < Y_2 < \cdots < Y_N が成り立つ。

極限 \displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} が存在することが証明できます。 この値を \mod 998244353 で求めてください(注記参照)。

注記

求める極限は必ず有理数となることが証明できます。またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R\times Q\equiv P\pmod{998244353} かつ 0\leq R < 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 2\leq N\leq 10^3
  • X_i は正の整数で、\sum_{i=1}^N X_i \leq 5\times 10^5 を満たす
  • i\neq j ならば X_i\neq X_j

入力

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

N
X_1 X_2 \ldots X_N

出力

\displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3}\mod 998244353 で出力してください。


入力例 1

3
3 1 2

出力例 1

291154603
  • 例えば (a,b,c) = (3,5,7) とすると、Y_1 = 0, Y_2 = 1, Y_3 = 4 となり、Y_1 < Y_2 < Y_3 が成り立ちます。
  • f(1) = 0, f(2) = 0, f(3) = 1, f(4) = 2, f(5) = 5 が成り立ちます。
  • \displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = \frac{1}{24} が成り立ちます。

入力例 2

3
5 9 2

出力例 2

832860616

\displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = \frac{55}{1008} が成り立ちます。


入力例 3

2
2 3

出力例 3

166374059

\displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = \frac{1}{6} が成り立ちます。


入力例 4

4
4 5 3 2

出力例 4

0

\displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = 0 が成り立ちます。

Score : 1100 points

Problem Statement

Given is a sequence of N positive integers X = (X_1, X_2, \ldots, X_N).

For a positive integer K, let f(K) be the number of triples of integers (a,b,c) that satisfy all of the following conditions.

  • 1\leq c \leq K.
  • 0\leq a < c and 0\leq b < c.
  • For each i, let Y_i be the remainder when aX_i + b is divided by c. Then, Y_1 < Y_2 < \cdots < Y_N holds.

It can be proved that the limit \displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} exists. Find this value modulo 998244353 (see Notes).

Notes

We can prove that the limit in question is always a rational number. Additionally, under the Constraints of this problem, when that number is represented as \frac{P}{Q} using two coprime integers P and Q, we can prove that there is a unique integer R such that R\times Q\equiv P\pmod{998244353} and 0\leq R < 998244353. Find this R.

Constraints

  • 2\leq N\leq 10^3
  • X_i are positive integers such that \sum_{i=1}^N X_i \leq 5\times 10^5.
  • X_i\neq X_j if i\neq j.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 \ldots X_N

Output

Print \displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} modulo 998244353.


Sample Input 1

3
3 1 2

Sample Output 1

291154603
  • For example, when (a,b,c) = (3,5,7), we have Y_1 = 0, Y_2 = 1, Y_3 = 4, which satisfy Y_1 < Y_2 < Y_3.
  • We have f(1) = 0, f(2) = 0, f(3) = 1, f(4) = 2, f(5) = 5.
  • We have \displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = \frac{1}{24}.

Sample Input 2

3
5 9 2

Sample Output 2

832860616

We have \displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = \frac{55}{1008} .


Sample Input 3

2
2 3

Sample Output 3

166374059

We have \displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = \frac{1}{6}.


Sample Input 4

4
4 5 3 2

Sample Output 4

0

We have \displaystyle \lim_{K\to\infty} \frac{f(K)}{K^3} = 0.