A - Three Cards

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 枚のカードがあります。カードには 1 から N の番号がついています。

カード i には正整数 A_i が書かれています。

この中から 3 枚のカードを選び、好きな順で書かれている整数を連結し新しく整数を作ります。例えば、1,23,4 が書かれたカードを選んだとき、12344231 を作ることができます。

作ることのできる整数の最大値を求めてください。

制約

  • 3 \le N \le 2 \times 10^5
  • 1 \le A_i < 10^6
  • 入力は全て整数である。

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

5
1 4 3 5 8

出力例 1

854

4,5,8 が書かれたカードを選んだ場合、458,485,548,584,845,854 を作ることができます。

855 以上の整数を作ることはできないので解は 854 です。


入力例 2

8
813 921 481 282 120 900 555 409

出力例 2

921900813

Score : 300 points

Problem Statement

There are N cards, numbered 1 to N.

Card i has a positive integer A_i written on it.

You can choose three of these cards and concatenate the integers written on them in any order you like to make a new integer. For example, if you choose cards with 1, 23, and 4 written on them, you can make integers such as 1234 and 4231.

Find the maximum integer you can make.

Constraints

  • 3 \le N \le 2 \times 10^5
  • 1 \le A_i < 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

5
1 4 3 5 8

Sample Output 1

854

If you choose cards with 4, 5, and 8 written on them, you can make 458, 485, 548, 584, 845, or 854.

You can make nothing greater than 854, so the answer is 854.


Sample Input 2

8
813 921 481 282 120 900 555 409

Sample Output 2

921900813
B - Plus and AND

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の非負整数列 A=(A_1,A_2,\dots,A_N) が与えられます。あなたは以下の操作を M 回以下行うことができます。(1 回も行わなくてもよいです。)

  • 1 \le i \le N を満たす整数 i を選び、A_i1 増やす。

その後、A の中から K 要素を選びます。

選んだ K 要素のビット単位 \mathrm{AND} の最大値を求めてください。

ビット単位 \mathrm{AND} 演算とは

整数 A, B のビット単位 \mathrm{AND}A\ \mathrm{AND}\ B は以下のように定義されます。

  • A\ \mathrm{AND}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち両方が 1 であれば 1、そうでなければ 0 である。
例えば、3\ \mathrm{AND}\ 5 = 1 となります (二進表記すると: 011\ \mathrm{AND}\ 101 = 001)。
一般に k 個の整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{AND}(\dots ((p_1\ \mathrm{AND}\ p_2)\ \mathrm{AND}\ p_3)\ \mathrm{AND}\ \dots\ \mathrm{AND}\ p_k) と定義され、これは p_1, p_2, p_3, \dots p_k の順番によらないことが証明できます。

制約

  • 1 \le K \le N \le 2 \times 10^5
  • 0 \le M < 2^{30}
  • 0 \le A_i < 2^{30}
  • 入力は全て整数である。

入力

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

N M K
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4 8 2
1 2 4 8

出力例 1

10

以下のような手順を踏むことで 選んだ 2 要素の \mathrm{AND} として 10 を達成できます。

  • A_3 を選ぶ操作を 6 回行う。A_3 = 10 となる。
  • A_4 を選ぶ操作を 2 回行う。A_4 = 10 となる。
  • A_3,A_4 を選ぶ。2 要素の \mathrm{AND}10 である。

選んだ 2 要素の \mathrm{AND}11 以上にすることはできないので、解は 10 です。


入力例 2

5 345 3
111 192 421 390 229

出力例 2

461

Score : 500 points

Problem Statement

You are given a sequence of N non-negative integers: A=(A_1,A_2,\dots,A_N). You may perform the following operation at most M times (possibly zero):

  • choose an integer i such that 1 \le i \le N and add 1 to A_i.

Then, you will choose K of the elements of A.

Find the maximum possible value of the bitwise \mathrm{AND} of the elements you choose.

What is bitwise {\rm AND}?

The bitwise {\rm AND} of non-negative integers A and B, A\ \mathrm{AND}\ B, is defined as follows:

  • When A\ {\rm AND}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if both of the digits in that place of A and B are 1, and 0 otherwise.
For example, 3\ {\rm AND}\ 5 = 1. (In base two, 011\ {\rm AND}\ 101 = 001.)
Generally, the bitwise {\rm AND} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1\ \mathrm{AND}\ p_2)\ \mathrm{AND}\ p_3)\ \mathrm{AND}\ \dots\ \mathrm{AND}\ p_k). We can prove that this value does not depend on the order of p_1, p_2, p_3, \dots, p_k.

Constraints

  • 1 \le K \le N \le 2 \times 10^5
  • 0 \le M < 2^{30}
  • 0 \le A_i < 2^{30}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

4 8 2
1 2 4 8

Sample Output 1

10

If you do the following, the bitwise \mathrm{AND} of the elements you choose will be 10.

  • Perform the operation on A_3 six times. Now, A_3 = 10.
  • Perform the operation on A_4 twice. Now, A_4 = 10.
  • Choose A_3 and A_4, whose bitwise \mathrm{AND} is 10.

There is no way to choose two elements whose bitwise \mathrm{AND} is greater than 10, so the answer is 10.


Sample Input 2

5 345 3
111 192 421 390 229

Sample Output 2

461
C - Even XOR

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

0 以上 2^N 未満の非負整数からなる集合 S のうち、以下の条件を満たすものの個数を 998244353 で割ったあまりを出力してください。

  • S の空でない部分集合 T は以下のどちらかを満たす。
    • T の要素数が奇数
    • T の全要素の \mathrm{XOR}0 でない
\mathrm{XOR} とは

非負整数 A, B のビット単位 \mathrm{XOR}A \oplus B は、以下のように定義されます。

  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。
一般に k 個の整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR}(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots p_k の順番によらないことが証明できます。

制約

  • 1 \le N \le 2 \times 10^5
  • 入力は全て整数である。

入力

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

N

出力

答えを出力せよ。


入力例 1

2

出力例 1

15

\lbrace 0,2,3 \rbrace\lbrace 1 \rbrace\lbrace \rbrace は条件を満たします。

\lbrace 0,1,2,3 \rbrace は条件を満たしません。

なぜなら、\lbrace 0,1,2,3 \rbrace は部分集合 \lbrace 0,1,2,3 \rbrace が要素数が偶数であり、全要素の \mathrm{XOR}0 であるからです。


入力例 2

146

出力例 2

743874490

Score : 600 points

Problem Statement

How many sets S consisting of non-negative integers between 0 and 2^N-1 (inclusive) satisfy the following condition? Print the count modulo 998244353.

  • Every non-empty subset T of S satisfies at least one of the following:
    • The number of elements in T is odd.
    • The \mathrm{XOR} of the elements in T is not zero.
What is \mathrm{XOR}?

The bitwise \mathrm{XOR} of non-negative integers A and B, A \oplus B, is defined as follows:

  • When A \oplus B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of the digits in that place of A and B is 1, and 0 otherwise.
For example, we have 3 \oplus 5 = 6 (in base two: 011 \oplus 101 = 110).
Generally, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k). We can prove that this value does not depend on the order of p_1, p_2, p_3, \dots, p_k.

Constraints

  • 1 \le N \le 2 \times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

2

Sample Output 1

15

Sets such as \lbrace 0,2,3 \rbrace, \lbrace 1 \rbrace, and \lbrace \rbrace satisfy the condition.

On the other hand, \lbrace 0,1,2,3 \rbrace does not.

This is because the subset \lbrace 0,1,2,3 \rbrace of \lbrace 0,1,2,3 \rbrace has an even number of elements, whose bitwise \mathrm{XOR} is 0.


Sample Input 2

146

Sample Output 2

743874490
D - >=<

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 800

問題文

長さ N かつ全要素が 1 以上 M 以下の整数列 A=(A_1,A_2,\dots,A_N) であって、以下の条件を全て満たすものを「素晴らしい整数列」と呼びます。

  • 1 \le i \le K を満たす整数 i に対して、以下のうちのいずれかが成り立つ。
    • A_{P_i} < X_i かつ A_{Q_i} < Y_i
    • A_{P_i} = X_i かつ A_{Q_i} = Y_i
    • A_{P_i} > X_i かつ A_{Q_i} > Y_i

「素晴らしい整数列」が存在するか判定し、存在するならば「素晴らしい整数列」の要素の総和としてあり得る最小値を求めてください。

制約

  • 1 \le N,M,K \le 2 \times 10^5
  • 1 \le P_i,Q_i \le N
  • 1 \le X_i,Y_i \le M
  • P_i \neq Q_i
  • 入力は全て整数である。

入力

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

N M K
P_1 X_1 Q_1 Y_1
P_2 X_2 Q_2 Y_2
\vdots
P_K X_K Q_K Y_K

出力

「素晴らしい整数列」が存在する場合は「素晴らしい整数列」の要素の総和としてあり得る最小値を、「素晴らしい整数列」が存在しない場合は -1 を出力せよ。


入力例 1

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

出力例 1

6

A=(2,3,1) は全ての条件を満たすので「素晴らしい整数列」です。この場合要素の総和は 6 です。

要素の総和が 5 以下の「素晴らしい整数列」は存在しないため、解は 6 です。


入力例 2

2 2 2
1 1 2 2
2 1 1 2

出力例 2

-1

「素晴らしい整数列」は存在しないため、-1 を出力します。


入力例 3

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

出力例 3

12

Score : 800 points

Problem Statement

A fantastic IS is an integer sequence of length N whose every element is between 1 and M (inclusive) that satisfies the following condition.

  • For every integer i such that 1 \le i \le K, one of the following holds.
    • A_{P_i} < X_i and A_{Q_i} < Y_i;
    • A_{P_i} = X_i and A_{Q_i} = Y_i;
    • A_{P_i} > X_i and A_{Q_i} > Y_i.

Determine whether a fantastic IS exists. If it does, find the minimum possible sum of the elements in a fantastic IS.

Constraints

  • 1 \le N,M,K \le 2 \times 10^5
  • 1 \le P_i,Q_i \le N
  • 1 \le X_i,Y_i \le M
  • P_i \neq Q_i
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
P_1 X_1 Q_1 Y_1
P_2 X_2 Q_2 Y_2
\vdots
P_K X_K Q_K Y_K

Output

If a fantastic IS exists, print the minimum possible sum of the elements in a fantastic IS; otherwise, print -1.


Sample Input 1

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

Sample Output 1

6

A=(2,3,1) fully satisfies the condition and thus is a fantastic IS, whose sum of the elements is 6.

There is no fantastic IS whose sum of the elements is less than 6, so the answer is 6.


Sample Input 2

2 2 2
1 1 2 2
2 1 1 2

Sample Output 2

-1

There is no fantastic IS, so -1 should be printed.


Sample Input 3

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

Sample Output 3

12
E - Simple Speed

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

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

1 以上 N 以下の整数からなる整数列 B のうち、以下の条件を全て満たすものの個数を 998244353 で割ったあまりを出力してください。

  • 1 \le i \le N を満たす整数 i に対し、B の中に i はちょうど A_i 個存在する。
  • 1 \le i \le |B|-1 を満たす整数 i に対し、|B_i - B_{i+1}|=1 が成り立つ。

制約

  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i \le 2 \times 10^5
  • 入力はすべて整数である。

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3
2 3 1

出力例 1

6

B としてあり得るものは、以下の 6 通りがあります。

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

よって、解は 6 です。


入力例 2

1
200000

出力例 2

0

条件を満たす B が存在しないこともあります。


入力例 3

6
12100 31602 41387 41498 31863 12250

出力例 3

750337372

Score : 800 points

Problem Statement

You are given a sequence of N positive integers: A=(A_1,A_2,\dots,A_N).

How many integer sequences B consisting of integers between 1 and N (inclusive) satisfy all of the following conditions? Print the count modulo 998244353.

  • For each integer i such that 1 \le i \le N, there are exactly A_i occurrences of i in B.
  • For each integer i such that 1 \le i \le |B|-1, it holds that |B_i - B_{i+1}|=1.

Constraints

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

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

3
2 3 1

Sample Output 1

6

B can be the following six sequences.

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

Thus, the answer is 6.


Sample Input 2

1
200000

Sample Output 2

0

There may be no sequence that satisfies the conditions.


Sample Input 3

6
12100 31602 41387 41498 31863 12250

Sample Output 3

750337372
F - Simple Solitaire

Time Limit: 10 sec / Memory Limit: 1024 MB

配点 : 1200

問題文

(1,2,\dots,N) の順列 P を用意して、次の操作を行います。

N 枚のカードがあります。これらのカードには 1 から N の番号がついてます。カード i には P_i が書かれています。

整数 X=1 があります。はじめ PCT 君は何も持っていません。PCT 君は以下の操作を i=1,2,\dots,N の順で行います。

  • カード i を手に入れる。その後、X が書かれたカードを持っている限り以下の行動を繰り返す。
    • X の書かれているカードを食べ、X1 増やす。
  • 現在持っているカードの枚数が M 枚以上の場合、持っているカードを全て捨てて操作を終了する。これ以降も操作は行わない。

ここで、以下のように順列 P のスコアを定義します。

  • カードが捨てられて操作が終わった場合、P のスコアを 0 とする。
  • カードが捨てられずに操作が最後まで行われた場合、P のスコアを \prod_{i=1}^{N-1} (i 回目の操作終了時に PCT 君が持っているカードの枚数 ) とする。

P としてあり得るものは N! 通りありますが、そのすべてに対してスコアの総和を 998244353 で割ったあまりを出力してください。

制約

  • 2 \le N \le 2 \times 10^5
  • 2 \le M \le N
  • 入力は全て整数である。

入力

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

N M

出力

答えを出力せよ。


入力例 1

3 2

出力例 1

1

P=(3,1,2) の場合、以下のように操作が行われます。

  • 1 回目の操作
    • PCT 君がカード 1 を手に入れる。
    • 現在持っているカードの枚数は 1 枚なので、操作を続行する。
  • 2 回目の操作
    • PCT 君がカード 2 を手に入れる。
    • カード 2 を食べ、X2 にする。
    • 現在持っているカードの枚数は 1 枚なので、操作を続行する。
  • 3 回目の操作
    • PCT 君がカード 3 を手に入れる。
    • カード 1,3 を食べ、X4 にする。
    • 現在持っているカードの枚数は 0 枚なので、操作を続行する。

操作が最後まで行われたため、(3,1,2) のスコアは 1 \times 1 = 1 です。

(3,1,2) 以外にスコアが 1 以上になる順列は存在しないため、解は 1 です。


入力例 2

3 3

出力例 2

5

入力例 3

146146 146

出力例 3

103537573

Score : 1200 points

Problem Statement

The following process is carried out on a permutation P of (1,2,\dots,N).

We have N cards, numbered 1 to N. Card i has the integer P_i written on it.

There are an integer X=1 and a boy called PCT, who initially has nothing. PCT does the following procedure for each i=1,2,\dots,N in this order.

  • Get Card i. Then, repeat the following action as long as he has a card with X written on it:
    • eat the card with X written on it, and then add 1 to X.
  • If PCT currently has M or more cards, throw away all cards he has and terminate the process, without performing any more procedures.

Here, let us define the score of the permutation P as follows:

  • if the process is terminated by throwing away cards, the score of P is 0;
  • if the process is carried out through the end without throwing away cards, the score of P is \prod_{i=1}^{N-1} ( the number of cards PCT has at the end of the i-th procedure ).

There are N! permutations P of (1,2,\dots,N). Find the sum of the scores of all those permutations, modulo 998244353.

Constraints

  • 2 \le N \le 2 \times 10^5
  • 2 \le M \le N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.


Sample Input 1

3 2

Sample Output 1

1

For P=(3,1,2), the process goes as follows.

  • The first procedure:
    • PCT gets Card 1.
    • PCT currently has 1 card, so he goes on.
  • The second procedure:
    • PCT gets Card 2.
    • PCT eats Card 2 and make X = 2.
    • PCT currently has 1 card, so he goes on.
  • The third procedure:
    • PCT gets Card 3.
    • PCT eats Cards 1,3 and make X = 4.
    • PCT currently has 0 cards, so he goes on.

The process is carried out through the end, so the score of (3,1,2) is 1 \times 1 = 1.

Other than (3,1,2), there is no permutation with a score of 1 or greater, so the answer is 1.


Sample Input 2

3 3

Sample Output 2

5

Sample Input 3

146146 146

Sample Output 3

103537573