A - Integer Sum

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 個の整数 A_1,A_2,\dots,A_N が与えられます。

N 個の整数を合計した値を求めてください。

制約

  • 1 \le N \le 100
  • 1 \le A_i \le 100
  • 入力はすべて整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3
2 7 2

出力例 1

11

3 個の整数 2,7,2 が与えられます。

答えは 2 + 7 + 2 = 11 です。


入力例 2

1
3

出力例 2

3

Score : 100 points

Problem Statement

You are given N integers A_1,A_2,\dots, and A_N.

Find the sum of the N integers.

Constraints

  • 1 \le N \le 100
  • 1 \le A_i \le 100
  • All values in the input 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.


Sample Input 1

3
2 7 2

Sample Output 1

11

You are given three integers: 2, 7, and 2.

The answer is 2 + 7 + 2 = 11.


Sample Input 2

1
3

Sample Output 2

3
B - Everyone is Friends

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1,2,\ldots,N の番号がついた N 人の人がいます。

M 回の舞踏会が行われました。 i (1\leq i \leq M) 回目の舞踏会には k_i 人が参加し、参加した人は人 x_{i,1},x_{i,2},\ldots,x_{i,k_i} でした。

どの二人も少なくとも 1 回同じ舞踏会に参加したか判定してください。

制約

  • 2\leq N \leq 100
  • 1\leq M \leq 100
  • 2\leq k_i \leq N
  • 1\leq x_{i,1}<x_{i,2}<\ldots < x_{i,k_i}\leq N
  • 入力は全て整数

入力

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

N M
k_1 x_{1,1} x_{1,2} \ldots x_{1,k_1}
\vdots
k_M x_{M,1} x_{M,2} \ldots x_{M,k_M}

出力

どの二人も少なくとも 1 回同じ舞踏会に参加した場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

3 3
2 1 2
2 2 3
2 1 3

出力例 1

Yes

1 と人 2 は共に 1 回目の舞踏会に参加しています。

2 と人 3 は共に 2 回目の舞踏会に参加しています。

1 と人 3 は共に 3 回目の舞踏会に参加しています。

以上よりどの二人も少なくとも 1 回同じ舞踏会に参加したので、答えは Yes です。


入力例 2

4 2
3 1 2 4
3 2 3 4

出力例 2

No

1 と人 31 回も同じ舞踏会に参加していないので、答えは No です。

Score : 200 points

Problem Statement

There are N people numbered 1,2,\ldots,N.

M parties were held. k_i people attended the i-th (1\leq i \leq M) party, and they were People x_{i,1},x_{i,2},\ldots,x_{i,k_i}.

Determine if every two people attended the same party at least once.

Constraints

  • 2\leq N \leq 100
  • 1\leq M \leq 100
  • 2\leq k_i \leq N
  • 1\leq x_{i,1}<x_{i,2}<\ldots < x_{i,k_i}\leq N
  • All values in the input are integers.

Input

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

N M
k_1 x_{1,1} x_{1,2} \ldots x_{1,k_1}
\vdots
k_M x_{M,1} x_{M,2} \ldots x_{M,k_M}

Output

Print Yes if every two people attended the same party at least once; print No otherwise.


Sample Input 1

3 3
2 1 2
2 2 3
2 1 3

Sample Output 1

Yes

Both Person 1 and Person 2 attended the 1-st party.

Both Person 2 and Person 3 attended the 2-nd party.

Both Person 1 and Person 3 attended the 3-rd party.

Therefore, every two people attended the same party at least once, so the answer is Yes.


Sample Input 2

4 2
3 1 2 4
3 2 3 4

Sample Output 2

No

Person 1 and Person 3 did not attend the same party, so the answer is No.

C - Max Even

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

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

A の異なる 2 要素の和として表せる値の中に偶数が存在するか判定し、存在する場合その最大値を求めてください。

制約

  • 2\leq N \leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • A の要素は相異なる
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

A の異なる 2 要素の和として表せる値の中に偶数が存在しない場合、-1 を出力せよ。

偶数が存在する場合、その最大値を出力せよ。


入力例 1

3
2 3 4

出力例 1

6

A の異なる 2 要素の和として表せる値は 5,6,7 です。この中に偶数は存在し、その最大値は 6 です。


入力例 2

2
1 0

出力例 2

-1

A の異なる 2 要素の和として表せる値は 1 です。この中に偶数は存在しないので、 -1 を出力してください。

Score : 300 points

Problem Statement

You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N consisting of non-negative integers.

Determine if there is an even number represented as the sum of two different elements of A. If it exists, find the maximum such number.

Constraints

  • 2\leq N \leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • The elements of A are distinct.
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print -1 if there is no even number represented as the sum of two different elements of A.

If such an even number exists, print the maximum such number.


Sample Input 1

3
2 3 4

Sample Output 1

6

The values represented as the sum of two distinct elements of A are 5, 6, and 7. We have an even number here, and the maximum is 6.


Sample Input 2

2
1 0

Sample Output 2

-1

The value represented as the sum of two distinct elements of A is 1. We have no even number here, so -1 should be printed.

D - Root M Leaper

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

N \times N のマス目があります。上から i 行目、左から j 列目のマスを (i,j) と表します。

始め、(1,1) に駒が 1 個置かれています。あなたは以下の操作を何度でも行うことができます。

  • 今駒が置かれているマスと距離がちょうど \sqrt{M} であるマスに駒を移動させる。

ここで、マス (i,j) とマス (k,l) の距離は \sqrt{(i-k)^2+(j-l)^2} とします。

全てのマス (i,j) に対して、以下の問題を解いてください。

  • 駒を (1,1) から (i,j) に移動させることができるかを判定し、できる場合は操作回数の最小値を求めてください。

制約

  • 1 \le N \le 400
  • 1 \le M \le 10^6
  • 入力は全て整数

入力

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

N M

出力

N 行出力せよ。i 行目には N 個の整数を出力せよ。i 行目の j 個目の出力は、マス (i,j) に駒を移動させることができるのであれば操作回数の最小値を、そうでないのであれば -1 を出力せよ。


入力例 1

3 1

出力例 1

0 1 2
1 2 3
2 3 4

駒は上下左右の 4 個のマスに移動することができます。

例えば (2,2) に移動するには、以下のように 2 回の操作を行えばよいです。

  • 今駒は (1,1) に置かれている。(1,1)(1,2) の距離はちょうど \sqrt{1} であるため、駒を (1,2) に移動させる。
  • 今駒は (1,2) に置かれている。(1,2)(2,2) の距離はちょうど \sqrt{1} であるため、駒を (2,2) に移動させる。

入力例 2

10 5

出力例 2

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

Score : 400 points

Problem Statement

There is a grid with N \times N squares. We denote by (i, j) the square at the i-th row from the top and j-th column from the left.

Initially, a piece is placed on (1, 1). You may repeat the following operation any number of times:

  • Let (i, j) be the square the piece is currently on. Move the piece to the square whose distance from (i, j) is exactly \sqrt{M}.

Here, we define the distance between square (i, j) and square (k, l) as \sqrt{(i-k)^2+(j-l)^2}.

For all squares (i, j), determine if the piece can reach (i, j). If it can, find the minimum number of operations required to do so.

Constraints

  • 1 \le N \le 400
  • 1 \le M \le 10^6
  • All values in the input are integers.

Input

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

N M

Output

Print N lines. The i-th line should contain N integers. If the piece can reach (i, j), the j-th integer in the i-th line should be the minimum number of operations required to do so; otherwise, it should be -1.


Sample Input 1

3 1

Sample Output 1

0 1 2
1 2 3
2 3 4

You can move the piece to four adjacent squares.

For example, we can move the piece to (2,2) with two operations as follows.

  • The piece is now on (1,1). The distance between (1,1) and (1,2) is exactly \sqrt{1}, so move the piece to (1,2).
  • The piece is now on (1,2). The distance between (1,2) and (2,2) is exactly \sqrt{1}, so move the piece to (2,2).

Sample Input 2

10 5

Sample Output 2

0 3 2 3 2 3 4 5 4 5
3 4 1 2 3 4 3 4 5 6
2 1 4 3 2 3 4 5 4 5
3 2 3 2 3 4 3 4 5 6
2 3 2 3 4 3 4 5 4 5
3 4 3 4 3 4 5 4 5 6
4 3 4 3 4 5 4 5 6 5
5 4 5 4 5 4 5 6 5 6
4 5 4 5 4 5 6 5 6 7
5 6 5 6 5 6 5 6 7 6
E - Add and Mex

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

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

以下の操作を M 回行ってください。

  • i\ (1\leq i \leq N) について、 A_ii を加算する。その後 A に含まれない最小の非負整数を求める。

制約

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

入力

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

N M 
A_1 A_2 \ldots A_N

出力

M 行出力せよ。

i (1\leq i \leq M) 行目には i 回目の操作後に A に含まれない最小の非負整数を出力せよ。


入力例 1

3 3
-1 -1 -6

出力例 1

2
2
0

1 回目の操作では、数列 A

(-1 + 1, -1 +2 ,-6+3) = (0,1,-3)

になります。 A に含まれない最小の非負整数は 2 です。

2 回目の操作では、数列 A

(0 + 1, 1 +2 ,-3+3) = (1,3,0)

になります。 A に含まれない最小の非負整数は 2 です。

3 回目の操作では、数列 A

(1 + 1, 3 +2 ,0+3) = (2,5,3)

になります。 A に含まれない最小の非負整数は 0 です。


入力例 2

5 6
-2 -2 -5 -7 -15

出力例 2

1
3
2
0
0
0

Score : 500 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.

Perform the following operation M times:

  • For each i\ (1\leq i \leq N), add i to A_i. Then, find the minimum non-negative integer not contained in A.

Constraints

  • 1\leq N,M \leq 2\times 10^5
  • -10^9\leq A_i\leq 10^9
  • All values in the input are integers.

Input

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

N M 
A_1 A_2 \ldots A_N

Output

Print M lines.

The i-th (1\leq i \leq M) line should contain the minimum non-negative integer not contained in A after the i-th operation.


Sample Input 1

3 3
-1 -1 -6

Sample Output 1

2
2
0

The 1-st operation makes the sequence A

(-1 + 1, -1 +2 ,-6+3) = (0,1,-3).

The minimum non-negative integer not contained in A is 2.

The 2-nd operation makes the sequence A

(0 + 1, 1 +2 ,-3+3) = (1,3,0).

The minimum non-negative integer not contained in A is 2.

The 3-rd operation makes the sequence A

(1 + 1, 3 +2 ,0+3) = (2,5,3).

The minimum non-negative integer not contained in A is 0.


Sample Input 2

5 6
-2 -2 -5 -7 -15

Sample Output 2

1
3
2
0
0
0
F - Two Strings

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の英小文字からなる文字列 S,T が与えられます。

文字列 X と整数 i に対し、f(X,i)X に対して以下の操作を i 回行い得られる文字列とします。

  • X の先頭の文字を削除し、同じ文字を X の末尾に挿入する。

0 \le i,j \le N-1 を満たす正整数の組 (i,j) のうち、辞書順で f(S,i)f(T,j) より小さいか同じであるものの個数を求めてください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 S, T の大小を判定するアルゴリズムを以下に説明します。

以下では「 Si 文字目の文字」を S_i のように表します。また、 ST より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. S, T のうち長さが大きくない方の文字列の長さを L とします。i=1,2,\dots,L に対して S_iT_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_jT_j を比較して、S_jT_j よりアルファベット順で小さい場合は S \lt T 、そうでない場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、ST の長さを比較して、ST より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

制約

  • 1 \le N \le 2 \times 10^5
  • S,T は英小文字からなる長さ N の文字列
  • N は整数

入力

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

N
S
T

出力

答えを出力せよ。


入力例 1

3
adb
cab

出力例 1

4

条件を満たす (i,j) の組は (0,0),(0,2),(2,0),(2,2)4 個があります。

(i,j)=(1,2) は、f(S,i)=dba,f(T,j)=bca であるため条件を満たしません。


入力例 2

10
wsiuhwijsl
pwqoketvun

出力例 2

56

Score : 500 points

Problem Statement

You are given strings S and T of length N each, consisting of lowercase English letters.

For a string X and an integer i, let f(X,i) be the string obtained by performing on X the following operation i times:

  • Remove the first character of X and append the same character to the end of X.

Find the number of integer pairs (i,j) satisfying 0 \le i,j \le N-1 such that f(S,i) is lexicographically smaller than or equal to f(T,j).

What is the lexicographical order?

Simply put, the lexicographical order is the order in which the words are arranged in a dictionary. Let us define it formally by describing an algorithm of finding the ordering of two distinct strings S and T consisting of lowercase English letters.

Here, we denote "the i-th character of a string S" by S_i. Also, we write S \lt T and S \gt T if S is lexicographically smaller and larger than T, respectively.

  1. Let L be the smallest of the lengths of S and T. For i=1,2,\dots,L, we check if S_i equals T_i.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Comparing S_j and T_j, we terminate the algorithm by determining that S \lt T if S_j is smaller than T_j in the alphabetical order, and that S \gt T otherwise.
  3. If there is no i such that S_i \neq T_i, comparing the lengths of S and T, we terminate the algorithm by determining that S \lt T if S is shorter than T, and that S \gt T otherwise.

Constraints

  • 1 \le N \le 2 \times 10^5
  • S and T are strings of length N each, consisting of lowercase English letters.
  • N is an integer.

Input

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

N
S
T

Output

Print the answer.


Sample Input 1

3
adb
cab

Sample Output 1

4

There are 4 pairs of (i, j) satisfying the conditions: (0,0),(0,2),(2,0), and (2,2).

(i,j)=(1,2) does not satisfy the conditions because f(S,i)=dba and f(T,j)=bca.


Sample Input 2

10
wsiuhwijsl
pwqoketvun

Sample Output 2

56
G - Yet Another mod M

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます。ここで A の全ての要素は相異なります。

あなたは 3 以上 10^9 以下の正整数 M を選び、以下の操作を 1 回だけ行います。

  • 1 \le i \le N を満たす整数 i に対し、A_iA_i \bmod M で置き換える。

うまく M を選ぶことで、操作後の A を以下の条件を満たした状態にすることができますか? できるのであれば、そのような M1 個求めてください。

  • ある整数 x が存在して、xA の過半数を占める。

ここで、整数 xA の過半数を占めるとは、A_i = x を満たす整数 i の個数が A_i \neq x を満たす i の個数より多いことを言います。

制約

  • 3 \le N \le 5000
  • 1 \le A_i \le 10^9
  • A の全ての要素は相異なる
  • 入力はすべて整数

入力

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

N
A_1 A_2 \dots A_N

出力

条件を満たす M が存在するのであればそのような M1 個出力せよ。そうでないならば -1 を出力せよ。


入力例 1

5
3 17 8 14 10

出力例 1

7

M=7 として操作を行うと、A=(3,3,1,0,3) となり 3A の過半数を占めるため M=7 は条件を満たします。


入力例 2

10
822848257 553915718 220834133 692082894 567771297 176423255 25919724 849988238 85134228 235637759

出力例 2

37

入力例 3

10
1 2 3 4 5 6 7 8 9 10

出力例 3

-1

Score : 600 points

Problem Statement

You are given a sequence A=(A_1,A_2,\dots,A_N) of length N consisting of positive integers, where the elements of A are distinct.

You will choose a positive integer M between 3 and 10^9 (inclusive) to perform the following operation once:

  • For each integer i such that 1 \le i \le N, replace A_i with A_i \bmod M.

Can you choose an M so that A satisfies the following condition after the operation? If you can, find such an M.

  • There exists an integer x such that x is the majority in A.

Here, an integer x is said to be the majority in A if the number of integers i such that A_i = x is greater than the number of integers i such that A_i \neq x.

Constraints

  • 3 \le N \le 5000
  • 1 \le A_i \le 10^9
  • The elements of A are distinct.
  • All values in the input are integers.

Input

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

N
A_1 A_2 \dots A_N

Output

If there exists an M that satisfies the condition, print such an M. Otherwise, print -1.


Sample Input 1

5
3 17 8 14 10

Sample Output 1

7

If you let M=7 to perform the operation, you will have A=(3,3,1,0,3), where 3 is the majority in A, so M=7 satisfies the condition.


Sample Input 2

10
822848257 553915718 220834133 692082894 567771297 176423255 25919724 849988238 85134228 235637759

Sample Output 2

37

Sample Input 3

10
1 2 3 4 5 6 7 8 9 10

Sample Output 3

-1
Ex - Flipping Coins 2

Time Limit: 10 sec / Memory Limit: 1024 MB

配点 : 600

問題文

0,1,\ldots,N-1 の番号がついた N 枚のコインが並べられています。はじめ、全てのコインは表を向いています。また、 0 以上 N-1 以下の整数からなる長さ N の数列 A が与えられます。

すぬけ君は (1,\ldots,N) を並び替えて得られる順列 p=(p_1,p_2,\ldots,p_N) を等確率で 1 つ選び N 回操作を行います。 i\ (1\leq i \leq N) 回目の操作では以下の処理が行われます。

  • コイン (i-1) \bmod N、コイン(i-1+1 ) \bmod N\ldots、コイン (i -1+ A_{p_i}) \bmod N(A_{p_i}+1) 枚のコインを全てひっくり返す。

N 回の操作の後、表向きのコインの枚数を k としてすぬけ君はお母さんから k 円もらえます。

すぬけ君が得られるお金の期待値を \text{mod } 998244353 で求めてください。

期待値 \text{mod } 998244353 の定義

この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

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

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

2
0 1

出力例 1

1

p としてありうるのは (1,2)(2,1) です。

  • p として (1,2) が選ばれたとき

1 回目の操作ではコイン 0 をひっくり返します。 2 回目の操作ではコイン 1 とコイン 0 をひっくり返します。最終的に表向きのコインはコイン 01 枚なので、1 円もらえます。

  • p として (2,1) が選ばれたとき

1 回目の操作ではコイン 0 とコイン 1 をひっくり返します。 2 回目の操作ではコイン 1 をひっくり返します。最終的に表向きのコインはコイン 11 枚なので、1 円もらえます。

以上より、得られるお金の期待値は 1 円 です。


入力例 2

4
3 1 1 2

出力例 2

665496237

期待値を \text{mod } 998244353 で出力してください。

Score : 600 points

Problem Statement

N coins numbered 0,1,\ldots,N-1 are arranged in a row. Initially, all coins are face up. Also, you are given a sequence A of length N consisting of integers between 0 and N-1.

Snuke will choose a permutation p=(p_1,p_2,\ldots,p_N) of (1,\ldots,N) at equal probability and perform N operations. In the i-th (1\leq i \leq N) operation,

  • he flips (A_{p_i}+1) coins: coin (i-1) \bmod N, coin (i-1+1 ) \bmod N, \ldots, and coin (i -1+ A_{p_i}) \bmod N.

After the N operations, Snuke receives k yen (the currency in Japan) from his mother, where k is the number of face-up coins.

Find the expected value, modulo 998244353, of the money Snuke will receive.

Definition of expected value modulo 998244353

In this problem, we can prove that the sought expected value is always a rational number. Moreover, under the Constraints of this problem, when the sought expected value is represented as an irreducible fraction \frac{y}{x}, it is guaranteed that x is indivisible by 998244353.

Then, an integer z between 0 and 998244352 such that xz \equiv y \pmod{998244353} is uniquely determined. Find such z.

Constraints

  • 1 \leq N\leq 2\times 10^5
  • 0\leq A_i \leq N-1
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

2
0 1

Sample Output 1

1

p can be either (1,2) or (2,1).

  • If (1,2) is chosen as p:

In the first operation, coin 0 is flipped, and in the second operation, coin 1 and coin 0 are flipped. One coin, coin 0, results in being face up, so he receives 1 yen.

  • If (2,1) is chosen as p:

In the first operation, coin 0 and coin 1 are flipped, and in the second operation, coin 1 is flipped. One coin, coin 1, results in being face up, so he receives 1 yen.

Therefore, the expected value of the money he receives is 1 yen.


Sample Input 2

4
3 1 1 2

Sample Output 2

665496237

Print the expected value modulo 998244353.