A - Closed interval

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

配点 : 100 点

問題文

整数 L,R が与えられます。

L 以上 R 以下の整数がいくつあるか求めてください。

制約

  • 1\le L\le R\le 100
  • 入力される値は全て整数

入力

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

L R

出力

答えを出力せよ。


入力例 1

3 5

出力例 1

3

3 以上 5 以下の整数は 3,4,5 の 3 つです。したがって、3 を出力してください。


入力例 2

1 7

出力例 2

7

入力例 3

14 79

出力例 3

66

Score : 100 points

Problem Statement

You are given integers L and R.

Find how many integers are between L and R, inclusive.

Constraints

  • 1\le L\le R\le 100
  • All input values are integers.

Input

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

L R

Output

Output the answer.


Sample Input 1

3 5

Sample Output 1

3

The integers between 3 and 5, inclusive, are 3,4,5, which is three integers. Thus, output 3.


Sample Input 2

1 7

Sample Output 2

7

Sample Input 3

14 79

Sample Output 3

66
B - Chompers

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

配点 : 100 点

問題文

英小文字からなる文字列 S と正の整数 N が与えられます。S の長さは 2N+1 以上です。

S の先頭と末尾から N 文字ずつ取り除いて得られる文字列を求めてください。

制約

  • S は英小文字からなる文字列
  • N は整数
  • 2N+1 \leq |S| \leq 30
  • 1 \leq N \leq 10

入力

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

S
N

出力

答えを出力せよ。


入力例 1

chemotherapy
3

出力例 1

mother

chemotherapy の先頭 3 文字 (che) と末尾 3 文字 (apy) を取り除くと、mother が得られます。


入力例 2

thermometer
4

出力例 2

mom

入力例 3

burger
1

出力例 3

urge

Score : 100 points

Problem Statement

You are given a string S consisting of lowercase English letters and a positive integer N. The length of S is at least 2N+1.

Find the string obtained by removing N characters from the beginning and N characters from the end of S.

Constraints

  • S is a string consisting of lowercase English letters.
  • N is an integer.
  • 2N+1 \leq |S| \leq 30
  • 1 \leq N \leq 10

Input

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

S
N

Output

Output the answer.


Sample Input 1

chemotherapy
3

Sample Output 1

mother

Removing the first three characters (che) and the last three characters (apy) from chemotherapy gives mother.


Sample Input 2

thermometer
4

Sample Output 2

mom

Sample Input 3

burger
1

Sample Output 3

urge
C - Frequency

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

配点 : 200 点

問題文

英小文字からなる文字列 S が与えられます。S に最も多く出現する文字を求めてください。そのような文字が複数ある場合は、そのうちアルファベット順で最も早いものを答えてください。

制約

  • 1 \leq |S| \leq 1000(|S| は文字列 S の長さ)
  • S の各文字は英小文字である。

入力

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

S

出力

S に最も多く出現する文字のうちアルファベット順で最も早いものを出力せよ。


入力例 1

frequency

出力例 1

e

frequency には e が 2 回出現し、これは他のどの文字よりも多いため e を出力します。


入力例 2

atcoder

出力例 2

a

atcoder には a, t, c, o, d, e, r が 1 回ずつ出現するため、このうちアルファベット順で最も早い a を出力します。


入力例 3

pseudopseudohypoparathyroidism

出力例 3

o

Score: 200 points

Problem Statement

You are given a string S consisting of lowercase English letters. Find the character that appears most frequently in S. If multiple such characters exist, report the one that comes earliest in alphabetical order.

Constraints

  • 1 \leq |S| \leq 1000 (|S| is the length of the string S.)
  • Each character in S is a lowercase English letter.

Input

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

S

Output

Among the characters that appear most frequently in S, print the one that comes earliest in alphabetical order.


Sample Input 1

frequency

Sample Output 1

e

In frequency, the letter e appears twice, which is more than any other character, so you should print e.


Sample Input 2

atcoder

Sample Output 2

a

In atcoder, each of the letters a, t, c, o, d, e, and r appears once, so you should print the earliest in alphabetical order, which is a.


Sample Input 3

pseudopseudohypoparathyroidism

Sample Output 3

o
D - Permute to Minimize

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

配点 : 200 点

問題文

正整数 X が与えられます。

X を(先頭に 0 を含まない形で)十進表記した際に現れる数字を、先頭に 0 が来ないように 並び替えることで得られる正整数のうち、値が最小のものを求めてください。

制約

  • 1\leq X < 10^5
  • X は整数

入力

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

X

出力

答えを出力せよ。


入力例 1

903

出力例 1

309

X を十進表記した際に現れる数字を先頭に 0 が来ないように並び替えることで得られる正整数は、903, 930, 309, 390 の 4 通りであり、このうち値が最小のものは 309 です。


入力例 2

432

出力例 2

234

入力例 3

100

出力例 3

100

Score : 200 points

Problem Statement

You are given a positive integer X.

Find the minimum value among all positive integers that can be obtained by rearranging the digits appearing in the decimal representation of X (without leading zeros) such that there is no leading zero.

Constraints

  • 1\leq X < 10^5
  • X is an integer.

Input

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

X

Output

Output the answer.


Sample Input 1

903

Sample Output 1

309

There are four positive integers that can be obtained by rearranging the digits appearing in the decimal representation of X such that there is no leading zero: 903, 930, 309, 390; the minimum value among them is 309.


Sample Input 2

432

Sample Output 2

234

Sample Input 3

100

Sample Output 3

100
E - Except and Min

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

配点 : 300 点

問題文

1 から N の番号がついた N 個のボールが袋に入っています。ボール i には整数 A_i が書かれています。
Q 個のクエリを処理してください。
クエリでは長さ K の数列 B_1, B_2, \dots, B_{K} が与えられるので以下の一連の操作を行ってください。ここで、B_i は全て 1 以上 N 以下で、かつ相異なります。

  • まず、ボール B_1, ボール B_2, \dots, ボール B_K を全て袋から取り出す。
  • そして、現在の袋に入っているボールに書かれた整数の最小値を出力する。(この時、袋は空でないことが制約から保証されている。)
  • その後、取り出した K 個のボールを全て袋に戻す。

制約

  • 6 \leq N \leq 3 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq K \leq 5
  • 1 \leq B_1 \lt B_2 \lt \dots \lt B_K \leq N
  • 全てのクエリに対する K の総和は 4 \times 10^5 以下
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{query}_i は i 番目のクエリを意味する。

N Q
A_1 A_2 \dots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

クエリは以下の形式で与えられる。

K
B_1 B_2 \dots B_K

出力

Q 行出力せよ。i 行目には i 番目のクエリの答えを出力せよ。


入力例 1

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

出力例 1

2
1
3
5
2
1

例えば 1 番目のクエリでは、ボール 4 とボール 5 を袋から取り出します。この時、袋には以下の 4 個のボールが残っています:

  • 3 が書き込まれたボール 1
  • 2 が書き込まれたボール 2
  • 5 が書き込まれたボール 3
  • 2 が書き込まれたボール 6

よってこの時の袋に入っているボールに書かれた整数の最小値は 2 です。

Score : 300 points

Problem Statement

A bag contains N balls numbered 1 to N. Ball i has the integer A_i written on it.
Process Q queries.
For each query, a sequence B_1, B_2, \dots, B_{K} of length K is given, and you should perform the following sequence of operations. Here, all B_i are between 1 and N inclusive and are distinct.

  • First, remove balls B_1, B_2, \dots, B_K from the bag.
  • Then, output the minimum value among the integers written on the balls currently in the bag. (It is guaranteed by the constraints that the bag is not empty at this point.)
  • Afterwards, return all K removed balls to the bag.

Constraints

  • 6 \leq N \leq 3 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq K \leq 5
  • 1 \leq B_1 \lt B_2 \lt \dots \lt B_K \leq N
  • The sum of K over all queries is at most 4 \times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{query}_i denotes the i-th query.

N Q
A_1 A_2 \dots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in the following format.

K
B_1 B_2 \dots B_K

Output

Output Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

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

Sample Output 1

2
1
3
5
2
1

For example, in the first query, balls 4 and 5 are removed from the bag. At this point, the following four balls remain in the bag:

  • Ball 1 with 3 written on it
  • Ball 2 with 2 written on it
  • Ball 3 with 5 written on it
  • Ball 6 with 2 written on it

Thus, the minimum value among the integers written on the balls in the bag is 2.

F - AtCoder Riko

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

配点 : 350 点

問題文

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

以下のようなことが起こりうる正整数 L をすべて求めてください。

AtCoder 社は棒状のスナック菓子「AtCoderりこ」を発売しました。 カップの中に長さ L の AtCoderりこが何本か入っています。 高橋君がこのカップをシェイクしたところ、それぞれの AtCoderりこは以下のいずれかの状態になりました。

  • 長さが L である 1 本の AtCoderりことしてそのまま残った。
  • 長さの和が L であるような 2 本の AtCoderりこに分かれた。ただし、各 AtCoderりこの長さは正整数である。
カップをシェイクした後、カップの中には N 本の AtCoderりこが入っており、i 本目の AtCoderりこの長さは A_i でした。

ただし、このようなことが起こりうる正整数 L が少なくとも 1 つ存在するような入力が与えられます。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 条件を満たすような L が少なくとも 1 つ存在する
  • 入力される値は全て整数

入力

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

N  
A_1 A_2 \ldots A_N  

出力

条件を満たすような L を、空白区切りで昇順に 1 行で出力せよ。


入力例 1

4
10 5 5 10

出力例 1

10 15

最初、カップには長さ 10 の AtCoderりこが 3 本入っていて、そのうち 1 本が 長さ 5 の 2 本の AtCoderりこに分かれると、条件を満たします。
最初、カップには長さ 15 の AtCoderりこが 2 本入っていて、それぞれの AtCoderりこが長さ 5,10 の 2 本の AtCoderりこに分かれると、条件を満たします。
これ以外の L では条件を満たしません。


入力例 2

3
4 4 4

出力例 2

4

入力例 3

6
10 187 344 100 434 257

出力例 3

444

Score : 350 points

Problem Statement

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

Find all positive integers L for which the following can occur:

AtCoder Inc. has released a stick-shaped snack called "AtCoderiko." A cup contains one or more AtCoderikos, each of length L. When Takahashi shook the cup, each AtCoderiko ended up in one of the following states:

  • It remained as one AtCoderiko of length L.
  • It broke into two AtCoderikos whose lengths sum to L. Here, the length of each AtCoderiko is a positive integer.
After shaking the cup, there were N AtCoderikos in the cup, and the length of the i-th AtCoderiko was A_i.

The given input guarantees that there exists at least one positive integer L for which this can occur.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • There exists at least one L satisfying the condition.
  • All input values are integers.

Input

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

N  
A_1 A_2 \ldots A_N  

Output

Output all values of L satisfying the condition in ascending order, separated by spaces, in one line.


Sample Input 1

4
10 5 5 10

Sample Output 1

10 15

If the cup initially contained three AtCoderikos of length 10, and one of them broke into two AtCoderikos of length 5, the condition is satisfied.
If the cup initially contained two AtCoderikos of length 15, and each of them broke into two AtCoderikos of lengths 5 and 10, the condition is satisfied.
No other values of L satisfy the condition.


Sample Input 2

3
4 4 4

Sample Output 2

4

Sample Input 3

6
10 187 344 100 434 257

Sample Output 3

444
G - Minimum Steiner Tree

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

配点 : 425 点

問題文

頂点に 1 から N の番号がついた N 頂点の木が与えられます。i 番目の辺は頂点 A_i と頂点 B_i を結んでいます。

このグラフからいくつかの(0 個でもよい)辺と頂点を削除してできる木のうち、指定された K 個の頂点、頂点 V_1,\ldots,V_K を全て含むようなものの頂点数の最小値を求めてください。

制約

  • 1 \leq K \leq N \leq 2\times 10^5
  • 1 \leq A_i,B_i \leq N
  • 1 \leq V_1 < V_2 < \ldots < V_K \leq N
  • 与えられるグラフは木である
  • 入力は全て整数

入力

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

N K
A_1 B_1
\vdots
A_{N-1} B_{N-1}
V_1 \ldots V_K

出力

答えを出力せよ。


入力例 1

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

出力例 1

4

与えられた木は下図左の通りであり、そこからいくつかの辺と頂点を削除してできる木のうち頂点 1,3,5 を全て含むような頂点数最小のものは下図右の通りです。

図


入力例 2

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

出力例 2

4

入力例 3

5 1
1 4
2 3
5 2
1 2
1

出力例 3

1

Score : 425 points

Problem Statement

You are given a tree with N vertices numbered 1 to N. The i-th edge connects vertices A_i and B_i.

Consider a tree that can be obtained by removing some (possibly zero) edges and vertices from this graph. Find the minimum number of vertices in such a tree that includes all of K specified vertices V_1,\ldots,V_K.

Constraints

  • 1 \leq K \leq N \leq 2\times 10^5
  • 1 \leq A_i,B_i \leq N
  • 1 \leq V_1 < V_2 < \ldots < V_K \leq N
  • The given graph is a tree.
  • All input values are integers.

Input

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

N K
A_1 B_1
\vdots
A_{N-1} B_{N-1}
V_1 \ldots V_K

Output

Print the answer.


Sample Input 1

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

Sample Output 1

4

The given tree is shown on the left in the figure below. The tree with the minimum number of vertices that includes all of vertices 1,3,5 is shown on the right.

Figure


Sample Input 2

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

Sample Output 2

4

Sample Input 3

5 1
1 4
2 3
5 2
1 2
1

Sample Output 3

1
H - x + y ≡ x + y

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

配点 : 450 点

問題文

正整数 a,b に対して、a と b を繋げて書いた時に出来る整数を \mathrm{concat}(a, b) と呼びます。厳密に述べると、\mathrm{concat}(a, b) を次のように定義します。

  • a, b を 10 進表記して出来る文字列を A, B とする。A, B をこの順に結合して出来る文字列を C とする。C を 10 進表記された整数とみなした時の値を \mathrm{concat}(a, b) とする。

例えば a = 123, b = 45 の時 \mathrm{concat}(a, b)=12345 です。

正整数 N, M が与えられます。
N 以下の正整数の組 (x,y) であって \mathrm{concat}(x, y) \equiv x + y \pmod{M} であるものの個数を 998244353 で割った余りを求めてください。

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

制約

  • 1 \leq T \leq 10^4
  • 1 \leq N \leq 10^{18}
  • 2 \leq M \leq 10^9
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_i は i 番目のテストケースを意味する。

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

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

N M

出力

T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
各テストケースでは、条件を満たす (x,y) の個数を 998244353 で割った余りを出力せよ。


入力例 1

4
3 2
123 456
20260530 460
123456789123456789 998244353

出力例 1

3
0
922576091
422081792

1 番目のテストケースについて、条件を満たす (x,y) は (2,1),(2,2),(2,3) の 3 個です。

Score : 450 points

Problem Statement

For positive integers a and b, define \mathrm{concat}(a, b) as the integer formed by writing a and b one after another. More formally, \mathrm{concat}(a, b) is defined as follows.

  • Let A and B be the strings formed by writing a and b in decimal, respectively. Let C be the string formed by concatenating A and B in this order. The value of C interpreted as an integer in decimal notation is \mathrm{concat}(a, b).

For example, if a = 123 and b = 45, then \mathrm{concat}(a, b) = 12345.

You are given positive integers N and M.
Find the number, modulo 998244353, of pairs (x, y) of positive integers not greater than N such that \mathrm{concat}(x, y) \equiv x + y \pmod{M}.

You are given T test cases; solve each one.

Constraints

  • 1 \leq T \leq 10^4
  • 1 \leq N \leq 10^{18}
  • 2 \leq M \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

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

Each test case is given in the following format:

N M

Output

Output T lines. The i-th line should contain the answer for the i-th test case.
For each test case, output the number, modulo 998244353, of pairs (x, y) satisfying the condition.


Sample Input 1

4
3 2
123 456
20260530 460
123456789123456789 998244353

Sample Output 1

3
0
922576091
422081792

For the first test case, three pairs (x, y) satisfy the condition: (2, 1), (2, 2), (2, 3).

I - Oddly Similar

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

配点 : 550 点

問題文

N 個の長さ M の数列 A_1, A_2, \ldots, A_N があります。i 番目の数列は M 個の整数 A_{i,1}, A_{i,2}, \ldots, A_{i,M} で表されます。

それぞれの長さが M の数列 X,Y について、X_i = Y_i となるような i(1 \leq i \leq M) の個数が奇数であるときに、X と Y は似ていると言います。

1 \leq i < j \leq N を満たす整数の組 (i,j) のうち、A_i と A_j が似ているものの個数を求めてください。

制約

  • 1 \leq N \leq 2000
  • 1 \leq M \leq 2000
  • 1 \leq A_{i,j} \leq 999
  • 入力は全て整数である。

入力

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

N M
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}

出力

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


入力例 1

3 3
1 2 3
1 3 4
2 3 4

出力例 1

1

(i,j) = (1,2) は条件を満たします。なぜならば、A_{1,k} = A_{2,k} となるような k は k=1 の 1 個だけだからです。

(i,j) = (1,3) ,(2,3) は条件を満たさないため、条件を満たす (i,j) の組は (1,2) だけです。


入力例 2

6 5
8 27 27 10 24
27 8 2 4 5
15 27 26 17 24
27 27 27 27 27
27 7 22 11 27
19 27 27 27 27

出力例 2

5

Score: 550 points

Problem Statement

There are N sequences of length M, denoted as A_1, A_2, \ldots, A_N. The i-th sequence is represented by M integers A_{i,1}, A_{i,2}, \ldots, A_{i,M}.

Two sequences X and Y of length M are said to be similar if and only if the number of indices i (1 \leq i \leq M) such that X_i = Y_i is odd.

Find the number of pairs of integers (i,j) satisfying 1 \leq i < j \leq N such that A_i and A_j are similar.

Constraints

  • 1 \leq N \leq 2000
  • 1 \leq M \leq 2000
  • 1 \leq A_{i,j} \leq 999
  • All input values are integers.

Input

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

N M
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}

Output

Print the answer as an integer.


Sample Input 1

3 3
1 2 3
1 3 4
2 3 4

Sample Output 1

1

The pair (i,j) = (1,2) satisfies the condition because there is only one index k such that A_{1,k} = A_{2,k}, which is k=1.

The pairs (i,j) = (1,3), (2,3) do not satisfy the condition, making (1,2) the only pair that does.


Sample Input 2

6 5
8 27 27 10 24
27 8 2 4 5
15 27 26 17 24
27 27 27 27 27
27 7 22 11 27
19 27 27 27 27

Sample Output 2

5