A - To Be Saikyo

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

1 から N までの番号が付けられた N 人の人がいます。 それぞれの人にはプログラミング力という整数値が定まっており、人 i のプログラミング力は P_i です。 人 1 が最強になるためには、あといくつプログラミング力を上げる必要がありますか? すなわち、すべての i \neq 1 に対して P_1 + x > P_i を満たすような最小の非負整数 x は何ですか?

制約

  • 1\leq N \leq 100
  • 1\leq P_i \leq 100
  • 入力は全て整数

入力

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

N
P_1 P_2 \dots P_N

出力

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


入力例 1

4
5 15 2 10

出力例 1

11

1 が最強になるためには、プログラミング力を 16 以上にする必要があります。 よって、答えは 16-5=11 です。


入力例 2

4
15 5 2 10

出力例 2

0

1 は既に最強なので、これ以上プログラミング力を上げる必要はありません。


入力例 3

3
100 100 100

出力例 3

1

Score : 100 points

Problem Statement

There are N people numbered 1 through N. Each person has a integer score called programming ability; person i's programming ability is P_i points. How many more points does person 1 need, so that person 1 becomes the strongest? In other words, what is the minimum non-negative integer x such that P_1 + x > P_i for all i \neq 1?

Constraints

  • 1\leq N \leq 100
  • 1\leq P_i \leq 100
  • All input values are integers.

Input

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

N
P_1 P_2 \dots P_N

Output

Print the answer as an integer.


Sample Input 1

4
5 15 2 10

Sample Output 1

11

Person 1 becomes the strongest when their programming skill is 16 points or more, so the answer is 16-5=11.


Sample Input 2

4
15 5 2 10

Sample Output 2

0

Person 1 is already the strongest, so no more programming skill is needed.


Sample Input 3

3
100 100 100

Sample Output 3

1
B - Who is Saikyo?

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 人の競技プログラマーがいます。順に 人 1, 人 2, \dots, 人 N と呼びます。
競技プログラマーの間には 強さ と呼ばれる関係性があり、相異なる 2 人組 (X, 人 Y) 全てに対して 「人 X は人 Y より強い」または「人 Y は人 X より強い」のどちらか一方が成り立ちます。
強さ は 推移律 が成り立ちます。言い換えると、相異なる 3 人組 (X, 人 Y, 人 Z) 全てに対して次の条件が成り立ちます。

  • X が人 Y よりも強く、かつ人 Y が人 Z よりも強いとき、人 X は人 Z よりも強い。

X が自分以外のどの人 Y に対しても「人 X は人 Y より強い」という関係が成り立つ時、人 X最強のプログラマー と呼びます。(上記の制約下においてそのような人がちょうど 1 人存在することが証明できます)

あなたは M 個の強さに関する情報を知っています。i 個目の情報は「人 A_i は人 B_i より強い」という情報です。
あなたは情報を元に N 人の中から最強のプログラマーを特定することができますか?
最強のプログラマーを特定できる場合はその人の番号を出力してください。特定できない場合、つまり最強のプログラマーとしてあり得る人が複数人いる場合は -1 を出力してください。

制約

  • 2 \leq N \leq 50
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i, B_i \leq N
  • A_i \neq B_i
  • i \neq j ならば (A_i, B_i) \neq (A_j, B_j)
  • 全ての情報が正しくなるように、全ての相異なる 2 人組にどちらが強いかを割り当てる方法が少なくとも 1 通り存在する

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

出力

最強のプログラマーを特定できる場合はその人の番号を出力せよ。特定できない場合は -1 を出力せよ。


入力例 1

3 2
1 2
2 3

出力例 1

1

あなたは「人 1 は人 2 より強い」「人 2 は人 3 より強い」という情報が分かっています。
推移律から「人 1 は人 3 より強い」という情報もわかるので、人 1 は最強のプログラマーです。


入力例 2

3 2
1 3
2 3

出力例 2

-1

最強のプログラマーである可能性がある人は人 1 と人 2 です。最強のプログラマーを一意に特定できないので -1 を出力してください。


入力例 3

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

出力例 3

-1

Score : 300 points

Problem Statement

There are N competitive programmers numbered person 1, person 2, \ldots, and person N.
There is a relation called superiority between the programmers. For all pairs of distinct programmers (person X, person Y), exactly one of the following two relations holds: "person X is stronger than person Y" or "person Y is stronger than person X."
The superiority is transitive. In other words, for all triplets of distinct programmers (person X, person Y, person Z), it holds that:

  • if person X is stronger than person Y and person Y is stronger than person Z, then person X is stronger than person Z.

A person X is said to be the strongest programmer if person X is stronger than person Y for all people Y other than person X. (Under the constraints above, we can prove that there is always exactly one such person.)

You have M pieces of information on their superiority. The i-th of them is that "person A_i is stronger than person B_i."
Can you determine the strongest programmer among the N based on the information?
If you can, print the person's number. Otherwise, that is, if there are multiple possible strongest programmers, print -1.

Constraints

  • 2 \leq N \leq 50
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i, B_i \leq N
  • A_i \neq B_i
  • If i \neq j, then (A_i, B_i) \neq (A_j, B_j).
  • There is at least one way to determine superiorities for all pairs of distinct programmers, that is consistent with the given information.

Input

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

If you can uniquely determine the strongest programmer, print the person's number; otherwise, print -1.


Sample Input 1

3 2
1 2
2 3

Sample Output 1

1

You have two pieces of information: "person 1 is stronger than person 2" and "person 2 is stronger than person 3."
By the transitivity, you can also infer that "person 1 is stronger than person 3," so person 1 is the strongest programmer.


Sample Input 2

3 2
1 3
2 3

Sample Output 2

-1

Both person 1 and person 2 may be the strongest programmer. Since you cannot uniquely determine which is the strongest, you should print -1.


Sample Input 3

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

Sample Output 3

-1
C - Approximate Equalization 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

整数列 A=(A_1,A_2,\dots,A_N) があります。 あなたは次の操作を好きな回数(0 回でもよい)行うことができます。

  • 1\leq i,j \leq N を満たす整数 i,j を選ぶ。A_i1 減らし、A_j1 増やす。

A の最小値と最大値の差を 1 以下にするために必要な最小の操作回数を求めてください。

制約

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

入力

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

N
A_1 A_2 \dots A_N

出力

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


入力例 1

4
4 7 3 7

出力例 1

3

以下のように 3 回の操作を行うことで、A の最小値と最大値の差を 1 以下にすることができます。

  • i=2,j=3 として操作を行う。A=(4,6,4,7) になる。
  • i=4,j=1 として操作を行う。A=(5,6,4,6) になる。
  • i=4,j=3 として操作を行う。A=(5,6,5,5) になる。

3 回未満の操作で A の最小値と最大値の差を 1 以下にすることはできません。よって答えは 3 です。


入力例 2

1
313

出力例 2

0

入力例 3

10
999999997 999999999 4 3 2 4 999999990 8 999999991 999999993

出力例 3

2499999974

Score : 400 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N). You can perform the following operation any number of times (possibly zero).

  • Choose integers i and j with 1\leq i,j \leq N. Decrease A_i by one and increase A_j by one.

Find the minimum number of operations required to make the difference between the minimum and maximum values of A at most one.

Constraints

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

Input

The 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

4
4 7 3 7

Sample Output 1

3

By the following three operations, the difference between the minimum and maximum values of A becomes at most one.

  • Choose i=2 and j=3 to make A=(4,6,4,7).
  • Choose i=4 and j=1 to make A=(5,6,4,6).
  • Choose i=4 and j=3 to make A=(5,6,5,5).

You cannot make the difference between maximum and minimum values of A at most one by less than three operations, so the answer is 3.


Sample Input 2

1
313

Sample Output 2

0

Sample Input 3

10
999999997 999999999 4 3 2 4 999999990 8 999999991 999999993

Sample Output 3

2499999974
D - Odd or Even

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 550

問題文

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

整数 N および N 未満の 奇数 K が与えられます。
ジャッジシステムは、0 および 1 からなる長さ N の数列 A = (A_1, A_2, \dots, A_N) を隠し持っています。

あなたは数列 A の要素の値を直接知ることはできません。
その代わりに、ジャッジシステムに対して以下の質問を N 回まで行うことができます。

  • 1 以上 N 以下の相異なる整数 x_1, x_2, \dots, x_K を選ぶ。そして、A_{x_1} + A_{x_2} + \dots + A_{x_K} の偶奇を聞く。

N 回以下の質問で (A_1, A_2, \dots, A_N) を全て特定して、答えを出力してください。
ただし、ジャッジは適応的です。言い換えると、ジャッジシステムは今までの質問の回答に矛盾しない範囲でA の内容を自由に変更することができます。
そのため、出力が次の条件を満たす場合にあなたの作成したプログラムは正解とみなされます。それ以外の場合は不正解とみなされます。

  • ここまでの質問の回答と矛盾しないような数列が一意に定まっており、かつそれがプログラムが出力した数列と一致している。

制約

  • 1 \leq K \lt N \leq 1000
  • K は奇数
  • A_i0 または 1

入出力

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

最初に、N および K を標準入力から受け取ってください。

N K

次に、(A_1, A_2, \dots, A_N) を全て特定できるまで質問を繰り返してください。
質問は、以下の形式で標準出力に出力してください。ここで x_1, x_2, \dots, x_K1 以上 N 以下の相異なる K 個の整数です。

? x_1 x_2 \dots x_K

これに対する応答は、次の形式で標準入力から与えられます。

T

ここで、T は質問に対する答えで、

  • T0 である場合は A_{x_1} + A_{x_2} + \dots + A_{x_K} は偶数であることを、
  • T1 である場合は A_{x_1} + A_{x_2} + \dots + A_{x_K} は奇数であることを意味します。

ただし、x_1, x_2, \dots, x_K が制約を満たしていないか、質問の回数が N 回を超えた場合は T-1 となります。

ジャッジが -1 を返した場合、プログラムはすでに不正解とみなされています。この場合、ただちにプログラムを終了してください。

A の要素を全て特定できたら、特定した A の要素を以下の形式で出力してください。その後、ただちにプログラムを終了してください。

! A_1 A_2 \dots A_N

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で誤った出力形式による出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
  • 解答を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
  • ジャッジは適応的です。言い換えると、ジャッジシステムは今までの質問の回答に矛盾しない範囲で A の内容を変更することができます。

入出力例

以下の入出力例は N=5, K=3 の場合の入出力例です。この入出力例の通りに出力するとジャッジ結果は WA になることに注意してください。
入出力例では、プログラムが出力した A = (1, 0, 1, 1, 0) はここまでの質問の回答に矛盾しない数列ですが、例えば (0, 0, 1, 0, 0) もここまでの質問の回答に矛盾しない数列であるため、数列 A は一意に定まっていません。そのため、このプログラムは不正解とみなされます。

入力 出力 説明
5 3 まず整数 N および K が与えられます。
? 2 4 1 (x_1, x_2, x_3) = (2, 4, 1) として質問を行います。
0 質問の答えは 0 なので、ジャッジはその値を返します。
? 5 3 2 (x_1, x_2, x_3) = (5, 3, 2) として質問を行います。
1 質問の答えは 1 なので、ジャッジはその値を返します。
! 1 0 1 1 0 A の答えとして (1, 0, 1, 1, 0) を出力します。A を一意に特定できていないのでジャッジ結果は WA になります。

Score : 550 points

Problem Statement

This is an interactive task (where your program and the judge interact via Standard Input and Output).

You are given an integer N and an odd number K.
The judge has a hidden length-N sequence A = (A_1, A_2, \dots, A_N) consisting of 0 and 1.

While you cannot directly access the elements of sequence A, you are allowed to ask the judge the following query at most N times.

  • Choose distinct integers x_1, x_2, \dots, and x_K between 1 and N, inclusive, to ask the parity of A_{x_1} + A_{x_2} + \dots + A_{x_K}.

Determine (A_1, A_2, \dots, A_N) by at most N queries, and print the answer.
Here, the judge is adaptive. In other words, the judge may modify the contents of A as long as it is consistent with the responses to the past queries.
Therefore, your program is considered correct if the output satisfies the following condition, and incorrect otherwise:

  • your program prints a sequence consistent with the responses to the queries so far, and that is the only such sequence.

Constraints

  • 1 \leq K \lt N \leq 1000
  • K is odd.
  • A_i is 0 or 1.

Input and Output

This is an interactive task (where your program and the judge interact via Standard Input and Output).

First of all, receive N and K from Standard Input.

N K

Then, repeat asking queries until you can uniquely determine (A_1, A_2, \dots, A_N).
Each query should be printed to Standard Output in the following format, where x_1, x_2, \dots, and x_K are K distinct integers between 1 and N, inclusive.

? x_1 x_2 \dots x_K

The response to the query is given from Standard Input in the following format.

T

Here, T denotes the response to the query.

  • T is 0 when A_{x_1} + A_{x_2} + \dots + A_{x_K} is even, and
  • T is 1 when A_{x_1} + A_{x_2} + \dots + A_{x_K} is odd.

However, if x_1, x_2, \dots and x_K do not satisfy the constraints, or the number of queries exceeds N, then T is -1.

If the judge returns -1, your program is already considered incorrect, so terminate the program immediately.

When you can determine all the elements of A, print those elements in the following format, and terminate the program immediately.

! A_1 A_2 \dots A_N

Notes

  • Print a newline and flush Standard Output at the end of each message. Otherwise, you may get a TLE verdict.
  • The verdict will be indeterminate if there is malformed output during the interaction or your program quits prematurely.
  • Terminate the program immediately after printing the answer, or the verdict will be indeterminate.
  • The judge for this problem is adaptive. This means that the judge may modify the contents of A as long as it is consistent with the responses to the past queries.

Sample Interaction

In the following interaction, N=5 and K=3. Note that the following output itself will result in WA.
Here, A = (1, 0, 1, 1, 0) is indeed consistent with the responses, but so is (0, 0, 1, 0, 0), so sequence A is not uniquely determined. Thus, this program is considered incorrect.

Input Output Description
5 3 First, you are given integers N and K.
? 2 4 1 You ask a query with (x_1, x_2, x_3) = (2, 4, 1).
0 The response to the query is 0, so the judge returns that value.
? 5 3 2 You ask a query with (x_1, x_2, x_3) = (5, 3, 2).
1 The response to the query is 1, so the judge returns that value.
! 1 0 1 1 0 You print (1, 0, 1, 1, 0) to guess A. Since sequence A is not uniquely determined, the verdict will be WA.
E - Duplicate

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 から 9 までの数字からなる文字列 S に対して、 f(S) を次の手順によって得られる文字列 T とします。(S_iSi 番目の文字を意味します)

  • 文字列 T がある。はじめ、T は空文字列である。
  • i=1, 2, \dots, |S| - 1 の順に次の操作を行う。
    • S_{i+1} を整数として解釈したときの値を n とする。T の末尾に S_in 個追加する。

例えば S = 313 のとき、以下の手順によって f(S) = 3111 に決まります。

  • はじめ T は空文字列である。
  • i=1 のとき n = 1 である。T31 個追加する。T3 になる。
  • i=2 のとき n = 3 である。T13 個追加する。T3111 になる。
  • 操作を終了する。T として 3111 を得る。

1 から 9 までの数字からなる長さ N の文字列 S が与えられます。
あなたは「Sf(S) に置き換える」という操作を S の長さが 1 になるまで繰り返します。
操作が終了するまでに行う操作を行う回数を 998244353 で割った余りを求めてください。ただし、操作が無限に続く場合は -1 を出力してください。

制約

  • 2 \leq N \leq 10^6
  • S1, 2, 3, 4, 5, 6, 7, 8, 9 からなる長さ N の文字列

入力

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

N
S

出力

操作が終了するまでに行う操作を行う回数を 998244353 で割った余りを出力せよ。ただし、操作が無限に続く場合は -1 を出力せよ。


入力例 1

3
313

出力例 1

4

S = 313 の場合、操作を 4 回行うと S の長さが 1 になります。

  • f(S) = 3111 である。S3111 に置き換える。
  • f(S) = 311 である。S311 に置き換える。
  • f(S) = 31 である。S31 に置き換える。
  • f(S) = 3 である。S3 に置き換える。
  • S の長さが 1 になったので操作を終了する。

入力例 2

9
123456789

出力例 2

-1

S = 123456789 の場合、操作が無限に続きます。この場合は -1 を出力してください。


入力例 3

2
11

出力例 3

1

Score : 600 points

Problem Statement

For a string S consisting of digits from 1 through 9, let f(S) be the string T obtained by the following procedure. (S_i denotes the i-th character of S.)

  • Let T be an initially empty string.
  • For i=1, 2, \dots, |S| - 1, perform the following operation:
    • Append n copies of S_i to the tail of T, where n is the value when S_{i+1} is interpreted as an integer.

For example, S = 313 yields f(S) = 3111 by the following steps.

  • T is initially empty.
  • For i=1, we have n = 1. Append one copy of 3 to T, which becomes 3.
  • For i=2, we have n = 3. Append three copies of 1 to T, which becomes 3111.
  • Terminate the procedure. We obtain T = 3111.

You are given a length-N string S consisting of digits from 1 through 9.
You repeat the following operation until the length of S becomes 1: replace S with f(S).
Find how many times, modulo 998244353, you perform the operation until you complete it. If you will repeat the operation indefinitely, print -1 instead.

Constraints

  • 2 \leq N \leq 10^6
  • S is a length-N string consisting of 1, 2, 3, 4, 5, 6, 7, 8, and 9.

Input

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

N
S

Output

Print the number of times, modulo 998244353, that you perform the operation until you complete it. If you will repeat the operation indefinitely, print -1 instead.


Sample Input 1

3
313

Sample Output 1

4

If S = 313, the length of S be comes 1 after four operations.

  • We have f(S) = 3111. Replace S with 3111.
  • We have f(S) = 311. Replace S with 311.
  • We have f(S) = 31. Replace S with 31.
  • We have f(S) = 3. Replace S with 3.
  • Now that the length of S is 1, terminate the repetition.

Sample Input 2

9
123456789

Sample Output 2

-1

If S = 123456789, you indefinitely repeat the operation. In this case, -1 should be printed.


Sample Input 3

2
11

Sample Output 3

1
F - Flip Machines

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 625

問題文

1 から N までの番号が付けられた N 枚のカードがあります。 カードのそれぞれの面には整数が書かれており、カード i の表には A_i が、裏には B_i が書かれています。 最初、全てのカードは表を向いています。

今ここに M 台のマシーンがあり、1 から M までの番号が付けられています。 マシーン j は(相異なるとは限らない)2 つの 1 以上 N 以下の整数 X_j,Y_j を持っており、マシーン j が起動されると、 \frac{1}{2} の確率でカード X_j を、残りの \frac{1}{2} の確率でカード Y_j を裏返します。 この確率は各起動ごとに独立です。

すぬけくんは今から以下の操作を順に行います。

  1. 1 以上 M 以下の整数からなる集合 S を選ぶ。
  2. S に含まれる番号のマシーンを、番号が小さい順に 1 度ずつ起動する。

すぬけくんがうまく S を選んだとき、「すべての操作が終了した後に各カードが向いている面に書かれた整数の合計」の期待値が最大でいくつになるか求めてください。

制約

  • 1\leq N \leq 40
  • 1\leq M \leq 10^5
  • 1\leq A_i,B_i \leq 10^4
  • 1\leq X_j,Y_j \leq N
  • 入力は全て整数

入力

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

N M
A_1 B_1
\vdots
A_N B_N
X_1 Y_1
\vdots
X_M Y_M

出力

答えを出力せよ。 出力は、真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。


入力例 1

3 1
3 10
10 6
5 2
1 2

出力例 1

19.500000

S として空集合を選んだ場合、どのマシーンも起動されないので、「すべての操作が終了した後に各カードが向いている面に書かれた整数の合計」の期待値は 3+10+5=18 です。

S として \lbrace 1 \rbrace を選んだ場合、マシーン 1 が起動され、

  • カード X_1 = 1 が裏返された場合、「すべての操作が終了した後に各カードが向いている面に書かれた整数の合計」は 10+10+5=25
  • カード Y_1 = 2 が裏返された場合、「すべての操作が終了した後に各カードが向いている面に書かれた整数の合計」は 3+6+5=14

なので、その期待値は \frac{25+14}{2} = 19.5 です。

よって、「すべての操作が終了した後に各カードが向いている面に書かれた整数の合計」の期待値の最大値は 19.5 です。


入力例 2

1 3
5 100
1 1
1 1
1 1

出力例 2

100.000000

同じ (X_j,Y_j) を持つマシーンが複数存在することもあります。


入力例 3

8 10
6918 9211
16 1868
3857 8537
3340 8506
6263 7940
1449 4593
5902 1932
310 6991
4 4
8 6
3 5
1 1
4 2
5 6
7 5
3 3
1 5
3 1

出力例 3

45945.000000

Score : 625 points

Problem Statement

There are N cards numbered 1 through N. Each face of a card has an integer written on it; card i has A_i on its front and B_i on its back. Initially, all cards are face up.

There are M machines numbered 1 through M. Machine j has two (not necessarily distinct) integers X_j and Y_j between 1 and N. If you power up machine j, it flips card X_j with the probability of \frac{1}{2}, and flips card Y_j with the remaining probability of \frac{1}{2}. This probability is independent for each power-up.

Snuke will perform the following procedure.

  1. Choose a set S consisting of integers from 1 through M.
  2. For each element in S in ascending order, power up the machine with that number.

Among Snuke's possible choices of S, find the maximum expected value of the sum of the integers written on the face-up sides of the cards after the procedure.

Constraints

  • 1\leq N \leq 40
  • 1\leq M \leq 10^5
  • 1\leq A_i,B_i \leq 10^4
  • 1\leq X_j,Y_j \leq N
  • All input values are integers.

Input

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

N M
A_1 B_1
\vdots
A_N B_N
X_1 Y_1
\vdots
X_M Y_M

Output

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


Sample Input 1

3 1
3 10
10 6
5 2
1 2

Sample Output 1

19.500000

If S is chosen to be an empty set, no machine is powered up, so the expected sum of the integers written on the face-up sides of the cards after the procedure is 3+10+5=18.

If S is chosen to be \lbrace 1 \rbrace, machine 1 is powered up.

  • If card X_1 = 1 is flipped, the expected sum of the integers written on the face-up sides of the cards after the procedure is 10+10+5=25.
  • If card Y_1 = 2 is flipped, the expected sum of the integers written on the face-up sides of the cards after the procedure is 3+6+5=14.

Thus, the expected value is \frac{25+14}{2} = 19.5.

Therefore, the maximum expected value of the sum of the integers written on the face-up sides of the cards after the procedure is 19.5.


Sample Input 2

1 3
5 100
1 1
1 1
1 1

Sample Output 2

100.000000

Different machines may have the same (X_j,Y_j).


Sample Input 3

8 10
6918 9211
16 1868
3857 8537
3340 8506
6263 7940
1449 4593
5902 1932
310 6991
4 4
8 6
3 5
1 1
4 2
5 6
7 5
3 3
1 5
3 1

Sample Output 3

45945.000000
G - Redistribution of Piles

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 625

問題文

1 から N までの番号がついた N 枚の皿があります。皿 i には a_i 個の石が載っています。また、空の袋があります。
あなたは以下の 2 種類の操作を好きな順番で 0 回以上何度でも行うことができます。

  • 石が 1 個以上載っている皿全てから石を 1 個ずつ取る。取った石は袋に移動する。
  • 袋から石を N 個取り出し、全ての皿に 1 個ずつ石を載せる。ただし、この操作は袋に石が N 個以上ある場合にのみ行うことができる。

操作後に皿 i に載っている石の個数を b_i とします。b_i を並べてできる長さ N の整数列 (b_1, b_2, \dots, b_N) としてあり得るものの個数を 998244353 で割った余りを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq a_i \leq 10^9

入力

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

N
a_1 a_2 \dots a_N

出力

(b_1, b_2, \dots, b_N) としてあり得るものの個数を 998244353 で割った余りを出力せよ。


入力例 1

3
3 1 3

出力例 1

7

例えば以下の手順で操作を行うと b(2, 1, 2) になります。

  • 1 番目の操作を行う。b(2, 0, 2) になる。
  • 1 番目の操作を行う。b(1, 0, 1) になる。
  • 2 番目の操作を行う。b(2, 1, 2) になる。

操作後の b としてあり得る列は次の 7 種類です。

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

入力例 2

1
0

出力例 2

1

操作後の b としてあり得るものは (0)1 種類です。


入力例 3

5
1 3 5 7 9

出力例 3

36

入力例 4

10
766294629 440423913 59187619 725560240 585990756 965580535 623321125 550925213 122410708 549392044

出力例 4

666174028

Score : 625 points

Problem Statement

There are N plates numbered 1 through N. Dish i has a_i stones on it. There is also an empty bag.
You can perform the following two kinds of operations any number of times (possibly zero) in any order.

  • Remove one stone from each plate with one or more stones. Put the removed stones into the bag.
  • Take N stones out of the bag, and put one stone to each plate. This operation can be performed only when the bag has N or more stones.

Let b_i be the number of stones on plate i after you finished the operations. Print the number, modulo 998244353, of sequences of integers (b_1, b_2, \dots, b_N) of length N that can result from the operations.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq a_i \leq 10^9

Input

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

N
a_1 a_2 \dots a_N

Output

Print the number, modulo 998244353, of possible sequences (b_1, b_2, \dots, b_N).


Sample Input 1

3
3 1 3

Sample Output 1

7

For example, b becomes (2, 1, 2) by the following procedure.

  • Perform the first operation. b becomes (2, 0, 2).
  • Perform the first operation. b becomes (1, 0, 1).
  • Perform the second operation. b becomes (2, 1, 2).

The following seven sequences can be the resulting b.

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

Sample Input 2

1
0

Sample Output 2

1

There are one sequence, (0), that can be the resulting b.


Sample Input 3

5
1 3 5 7 9

Sample Output 3

36

Sample Input 4

10
766294629 440423913 59187619 725560240 585990756 965580535 623321125 550925213 122410708 549392044

Sample Output 4

666174028
Ex - Group Photo

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 650

問題文

2N+1 人の人が前列と後列の 2 列に並んで集合写真を取ろうとしています。 前列には N 人の人がいて、i 人目の身長は A_i です。 後列には N+1 人の人がいて、i 人目の身長は B_i です。 ここで、2N+1 人の身長は互いに相異なることが保証されます。 前列・後列のそれぞれの列の中では、人が並ぶ順番を自由に決めることができます。

今、前列に並んでいる人の身長が左から順に a_1,a_2,\dots,a_N であり、後列に並んでいる人の身長が左から順に b_1,b_2,\dots,b_{N+1} であるとします。 以下の条件をすべて満たすとき、この並び方は良い並び方であると定義します。

  • すべての i\ (2 \leq i \leq N) について、a_i < b_i または a_{i-1} < b_i
  • a_1 < b_1
  • a_N < b_{N+1}

前列の並び方は N! 通りありますが、そのうち後列の並び方をうまく定めることで良い並び方にできるものの数を 998244353 で割ったあまりを求めてください。

制約

  • 1\leq N \leq 5000
  • 1 \leq A_i,B_i \leq 10^9
  • A_i \neq A_j\ (1 \leq i < j \leq N)
  • B_i \neq B_j\ (1 \leq i < j \leq N+1)
  • A_i \neq B_j\ (1 \leq i \leq N, 1 \leq j \leq N+1)
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_{N+1}

出力

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


入力例 1

3
1 12 6
4 3 10 9

出力例 1

2
  • a = (1,12,6) のとき、b = (4,3,10,9) などとすることで良い並び方になります。
  • a = (6,12,1) のとき、b = (10,9,4,3) などとすることで良い並び方になります。
  • a がそれ以外の 4 通りであるとき、後列をどのように並べても(すなわち、24 通りある b のうちどれを持ってきても)良い並び方にはなりません。

よって答えは 2 です。


入力例 2

1
5
1 10

出力例 2

0

入力例 3

10
189330739 910286918 802329211 923078537 492686568 404539679 822804784 303238506 650287940 1
125660016 430302156 982631932 773361868 161735902 731963982 317063340 880895728 1000000000 707723857 450968417

出力例 3

3542400

Score : 650 points

Problem Statement

(2N+1) people are forming two rows to take a group photograph. There are N people in the front row; the i-th of them has a height of A_i. There are (N+1) people in the back row; the i-th of them has a height of B_i. It is guaranteed that the heights of the (2N+1) people are distinct. Within each row, we can freely rearrange the people.

Suppose that the heights of the people in the front row are a_1,a_2,\dots,a_N from the left, and those in the back row are b_1,b_2,\dots,b_{N+1} from the left. This arrangement is said to be good if all of the following conditions are satisfied:

  • a_i < b_i or a_{i-1} < b_i for all i\ (2 \leq i \leq N).
  • a_1 < b_1.
  • a_N < b_{N+1}.

Among the N! ways to rearrange the front row, how many of them, modulo 998244353, are such ways that we can rearrange the back row to make the arrangement good?

Constraints

  • 1\leq N \leq 5000
  • 1 \leq A_i,B_i \leq 10^9
  • A_i \neq A_j\ (1 \leq i < j \leq N)
  • B_i \neq B_j\ (1 \leq i < j \leq N+1)
  • A_i \neq B_j\ (1 \leq i \leq N, 1 \leq j \leq N+1)
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_{N+1}

Output

Print the answer as an integer.


Sample Input 1

3
1 12 6
4 3 10 9

Sample Output 1

2
  • When a = (1,12,6), we can let, for example, b = (4,3,10,9) to make the arrangement good.
  • When a = (6,12,1), we can let, for example, b = (10,9,4,3) to make the arrangement good.
  • When a is one of the other four ways, no rearrangement of the back row (that is, none of the possible 24 candidates of b) makes the arrangement good.

Thus, the answer is 2.


Sample Input 2

1
5
1 10

Sample Output 2

0

Sample Input 3

10
189330739 910286918 802329211 923078537 492686568 404539679 822804784 303238506 650287940 1
125660016 430302156 982631932 773361868 161735902 731963982 317063340 880895728 1000000000 707723857 450968417

Sample Output 3

3542400