A - Hamming Distance

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

配点 : 100

問題文

正整数 N および、英小文字からなる長さ N の文字列 S,T が与えられます。

ST のハミング距離を求めてください。 すなわち、1 以上 N 以下の整数 i であって、Si 文字目と Ti 文字目が異なるようなものの個数を求めてください。

制約

  • 1\leq N \leq 100
  • N は整数
  • S,T はそれぞれ英小文字からなる長さ N の文字列

入力

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

N
S
T

出力

答えを出力せよ。


入力例 1

6
abcarc
agcahc

出力例 1

2

ST2,5 文字目が異なり、それ以外が等しいです。よって答えは 2 です。


入力例 2

7
atcoder
contest

出力例 2

7

入力例 3

8
chokudai
chokudai

出力例 3

0

入力例 4

10
vexknuampx
vzxikuamlx

出力例 4

4

Score : 100 points

Problem Statement

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

Find the Hamming distance between S and T. That is, find the number of integers i such that 1 \leq i \leq N and the i-th character of S is different from the i-th character of T.

Constraints

  • 1\leq N \leq 100
  • N is an integer.
  • Each of S and T is a string of length N consisting of lowercase English letters.

Input

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

N
S
T

Output

Print the answer.


Sample Input 1

6
abcarc
agcahc

Sample Output 1

2

S and T differ in the 2nd and 5th characters, but not in other characters. Thus, the answer is 2.


Sample Input 2

7
atcoder
contest

Sample Output 2

7

Sample Input 3

8
chokudai
chokudai

Sample Output 3

0

Sample Input 4

10
vexknuampx
vzxikuamlx

Sample Output 4

4
B - Ranking with Ties

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

配点 : 200

問題文

1 から N までの番号が付けられた N 人の人がとあるコンテストに参加し、人 i\ (1\leq i\leq N)得点P_i でした。

このコンテストでは、以下の手順によって N 人それぞれの 順位 が定まります。

  1. 変数 r を用意し、r=1 と初期化する。最初、N 人の順位はすべて未確定である。
  2. N 人全員の順位が確定するまで以下の操作を繰り返す。
    • 順位が未確定である人の中での得点の最大値を x とし、得点が x である人の数を k とおく。得点が x である k 人すべての順位を r 位と確定させたのち、rk を足す。

N 人それぞれの順位を出力してください。

制約

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

入力

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

N
P_1 P_2 \dots P_N

出力

N 行出力せよ。 i\ (1\leq i \leq N) 行目には、人 i の順位を整数として出力せよ。


入力例 1

4
3 12 9 9

出力例 1

4
1
2
2

以下のようにして N\ (=4) 人それぞれの順位が定まります。

  1. 変数 r を用意し、r=1 と初期化する。最初、4 人の順位はすべて未確定である。
  2. 現時点で順位が未確定なのは人 1,2,3,4 であり、その中での得点の最大値は P_2\ (=12) である。よって、人 2 の順位を r\ (=1) 位と確定させたのち、r1 を足して r=2 とする。
  3. 現時点で順位が未確定なのは人 1,3,4 であり、その中での得点の最大値は P_3=P_4\ (=9) である。よって、人 3,4 の順位を r\ (=2) 位と確定させたのち、r2 を足して r=4 とする。
  4. 現時点で順位が未確定なのは人 1 であり、その中での得点の最大値は P_1\ (=3) である。よって、人 1 の順位を r\ (=4) 位と確定させたのち、r1 を足して r=5 とする。
  5. 4 人全員の順位が確定したため、手順を終了する。

入力例 2

3
3 9 6

出力例 2

3
1
2

入力例 3

4
100 100 100 100

出力例 3

1
1
1
1

入力例 4

8
87 87 87 88 41 38 41 38

出力例 4

2
2
2
1
5
7
5
7

Score : 200 points

Problem Statement

N people labeled from 1 to N participated in a certain contest. The score of person i (1 \leq i \leq N) was P_i.

In this contest, the rank of each of the N people is determined by the following procedure:

  1. Prepare a variable r, and initialize r = 1. Initially, the ranks of the N people are all undetermined.
  2. Repeat the following operation until the ranks of all N people are determined:
    • Let x be the maximum score among the people whose ranks are currently undetermined, and let k be the number of people whose score is x. Determine the rank of those k people with score x to be r, and then add k to r.

Print the rank of each of the N people.

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 N lines. The i-th line (1 \leq i \leq N) should contain the rank of person i as an integer.


Sample Input 1

4
3 12 9 9

Sample Output 1

4
1
2
2

The ranks of the N\ (=4) people are determined as follows:

  1. Prepare a variable r and initialize r=1. At first, the ranks of all 4 people are undetermined.
  2. Currently, persons 1, 2, 3, 4 have undetermined ranks. The maximum score among them is P_2\ (=12). Therefore, determine the rank of person 2 to be r\ (=1), and then add 1 to r, making r=2.
  3. Currently, persons 1, 3, 4 have undetermined ranks. The maximum score among them is P_3=P_4\ (=9). Therefore, determine the ranks of persons 3 and 4 to be r\ (=2), and then add 2 to r, making r=4.
  4. Currently, person 1 has an undetermined rank. The maximum score among them is P_1\ (=3). Therefore, determine the rank of person 1 to be r\ (=4), and then add 1 to r, making r=5.
  5. The ranks of all 4 people are now determined, so the process ends.

Sample Input 2

3
3 9 6

Sample Output 2

3
1
2

Sample Input 3

4
100 100 100 100

Sample Output 3

1
1
1
1

Sample Input 4

8
87 87 87 88 41 38 41 38

Sample Output 4

2
2
2
1
5
7
5
7
C - Make it Forest

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

配点 : 350

問題文

頂点に 1 から N の番号がついた N 頂点 M 辺の単純無向グラフが与えられます。i 番目の辺は頂点 u_i と頂点 v_i を結ぶ辺です。
グラフを森にするためには辺を最低何本削除する必要がありますか?

森とは 単純無向グラフ F が森であるとは、F が閉路を含まないことを言います。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min \left( \frac{N(N-1)}{2}, 2 \times 10^5\right)
  • 1 \leq u_i \lt v_i \leq N
  • 入力で与えられるグラフは単純
  • 入力される値は全て整数

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

答えを出力せよ。


入力例 1

4 4
1 2
1 3
2 4
3 4

出力例 1

1

例えば 1 番目の辺を削除すると、グラフは森になります。


入力例 2

5 0

出力例 2

0

入力例 3

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

出力例 3

2

Score : 350 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges, where the vertices are labeled 1 to N. The i-th edge connects vertices u_i and v_i.
What is the minimum number of edges that need to be deleted from this graph so that the graph becomes a forest?

What is a forest? A simple undirected graph F is called a forest if and only if F does not contain any cycle.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min \left( \frac{N(N-1)}{2}, 2 \times 10^5\right)
  • 1 \leq u_i < v_i \leq N
  • The given graph is simple.
  • All input values are integers.

Input

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

Print the answer.


Sample Input 1

4 4
1 2
1 3
2 4
3 4

Sample Output 1

1

For example, if you delete the first edge, the graph becomes a forest.


Sample Input 2

5 0

Sample Output 2

0

Sample Input 3

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

Sample Output 3

2
D - Switch Seats

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

配点 : 400

問題文

N 組のカップルが一列に座っています。
2 組のカップルであって、もともと両方のカップルは隣り合わせで座っておらず、かつ 4 人の間で席を交換することで両方のカップルが隣り合わせで座れるようになる組の個数を数えてください。

長さ 2N の数列 A=(A_1,A_2,\dots,A_{2N}) があります。A には 1, 2, \dots, N がそれぞれ 2 回ずつ登場します。

1 \leq a \lt b \leq N を満たす整数対 (a, b) であって以下の条件を全て満たすものの個数を求めてください。

  • A 内において a 同士は隣接していない。
  • A 内において b 同士は隣接していない。
  • 次の操作を 1 回以上自由な回数行うことで、A 内において a 同士が隣接していて、かつ b 同士が隣接している状態にすることができる。
    • A_i = a, A_j = b を満たす整数対 (i, j) (1 \leq i \leq 2N, 1 \leq j \leq 2N) を選び、A_iA_j を入れ替える。

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

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • A には 1, 2, \dots, N がそれぞれ 2 回ずつ登場する
  • 全てのテストケースに対する N の総和は 2 \times 10^5 以下
  • 入力される値は全て整数

入力

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

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

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

N
A_1 A_2 \dots A_{2N}

出力

T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。


入力例 1

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

出力例 1

1
0
4

1 番目のテストケースについて考えます。
(a, b)=(1, 2) は問題文の条件を満たします。理由は次の通りです。

  • A 内において 1 同士は隣接していない。
  • A 内において 2 同士は隣接していない。
  • (i,j)=(1, 6) として A_1A_6 を入れ替える操作を行うことで、1 同士が隣接していて、かつ 2 同士が隣接している状態にすることができる。

問題文の条件を満たす (a, b)(1, 2) のみです。

Score : 400 points

Problem Statement

N couples are seated in a line.
Count the number of pairs of couples such that neither couple was originally sitting next to each other, and both couples can end up sitting next to each other by swapping seats among those four people.

There is a sequence A = (A_1, A_2, \dots, A_{2N}) of length 2N. Each of the integers 1, 2, \dots, N appears exactly twice in A.

Find the number of integer pairs (a, b) satisfying 1 \leq a < b \leq N and all of the following conditions:

  • The two occurrences of a in A are not adjacent.
  • The two occurrences of b in A are not adjacent.
  • By performing the following operation one or more times in any order, it is possible to reach a state where the two occurrences of a in A are adjacent and the two occurrences of b in A are also adjacent.
    • Choose an integer pair (i, j) (1 \leq i \leq 2N, 1 \leq j \leq 2N) such that A_i = a and A_j = b, and swap A_i with A_j.

You are given T test cases; solve each of them.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • Each of 1, 2, \dots, N appears exactly twice in A.
  • The sum of N over all test cases is at most 2 \times 10^5.
  • 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
A_1 A_2 \dots A_{2N}

Output

Print T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

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

Sample Output 1

1
0
4

Consider the first test case.
(a, b) = (1, 2) satisfies the conditions in the problem statement, for the following reasons:

  • The two occurrences of 1 in A are not adjacent.
  • The two occurrences of 2 in A are not adjacent.
  • By performing the operation where (i, j) = (1, 6) and swapping A_1 with A_6, you can reach a state where the two occurrences of 1 are adjacent and the two occurrences of 2 are also adjacent.

(1, 2) is the only pair (a, b) that satisfies the conditions.

E - Replace

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

配点 : 500

問題文

正整数 N および、英小文字からなる長さ N の文字列 S,T が与えられます。

以下の操作を繰り返し(0 回でも良い)行うことで、ST と一致させることが可能かどうか判定してください。 可能な場合は、そのために必要な操作回数の最小値も求めてください。

  • 英小文字 x,y を選び、S に含まれる 全てx をそれぞれ y で置き換える。

制約

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

入力

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

N
S
T

出力

ST と一致させることが可能ならばそのために必要な操作回数の最小値を、不可能ならば -1 を出力せよ。


入力例 1

6
afbfda
bkckbb

出力例 1

4

以下のように操作を 4 回行うことで、ST と一致させることができます。

  1. x= b, y= c として操作を行う。S= afcfda となる。
  2. x= a, y= b として操作を行う。S= bfcfdb となる。
  3. x= f, y= k として操作を行う。S= bkckdb となる。
  4. x= d, y= b として操作を行う。S= bkckbb となり、T と一致する。

3 回以下の操作で ST と一致させることはできないため、必要な操作回数の最小値は 4 です。


入力例 2

4
abac
abac

出力例 2

0

ST が既に一致しているため、操作を行う必要はありません。


入力例 3

4
abac
abrc

出力例 3

-1

どのように操作を繰り返し行っても、ST と一致させることはできません。


入力例 4

4
abac
bcba

出力例 4

4

Score : 500 points

Problem Statement

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

Determine whether it is possible to make S identical to T by repeating the operation below any number of times (possibly zero). If it is possible, also find the minimum number of operations required.

  • Choose two lowercase English letters x, y and replace every occurrence of x in S with y.

Constraints

  • 1\leq N \leq 2\times 10^5
  • N is an integer.
  • Each of S and T is a string of length N, consisting of lowercase English letters.

Input

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

N
S
T

Output

If it is possible to make S identical to T, print the minimum number of operations required. Otherwise, print -1.


Sample Input 1

6
afbfda
bkckbb

Sample Output 1

4

By performing the operation four times in the following way, you can make S identical to T:

  1. Choose x= b and y= c. S becomes afcfda.
  2. Choose x= a and y= b. S becomes bfcfdb.
  3. Choose x= f and y= k. S becomes bkckdb.
  4. Choose x= d and y= b. S becomes bkckbb, which is identical to T.

It cannot be done with fewer than four operations, so the minimum number of operations required is 4.


Sample Input 2

4
abac
abac

Sample Output 2

0

S and T are already identical, so no operations are required.


Sample Input 3

4
abac
abrc

Sample Output 3

-1

No matter how you repeat the operation, it is impossible to make S identical to T.


Sample Input 4

4
abac
bcba

Sample Output 4

4
F - Range Power Sum

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

配点 : 550

問題文

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

\displaystyle \sum_{1\leq l\leq r\leq N} \Bigg(\sum_{l\leq i\leq r} A_i\Bigg)^K の値を 998244353 で割った余りを求めてください。

制約

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

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3 2
3 1 2

出力例 1

75

求める値は A_1^2+A_2^2+A_3^2+(A_1+A_2)^2+(A_2+A_3)^2+(A_1+A_2+A_3)^2=3^2+1^2+2^2+4^2+3^2+6^2=75 です。


入力例 2

1 10
0

出力例 2

0

入力例 3

10 5
91 59 85 60 57 72 12 3 27 16

出力例 3

428633385

998244353 で割った余りを求めることに注意してください。

Score : 550 points

Problem Statement

You are given positive integers N, K, and an integer sequence of length N: A = (A_1, A_2, \dots, A_N).

Find \displaystyle \sum_{1\leq l\leq r\leq N} \Bigg(\sum_{l\leq i\leq r} A_i\Bigg)^K, modulo 998244353.

Constraints

  • 1\leq N \leq 2\times 10^5
  • 1\leq K \leq 10
  • 0 \leq A_i < 998244353
  • All input values are integers.

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

3 2
3 1 2

Sample Output 1

75

The value is A_1^2+A_2^2+A_3^2+(A_1+A_2)^2+(A_2+A_3)^2+(A_1+A_2+A_3)^2=3^2+1^2+2^2+4^2+3^2+6^2=75.


Sample Input 2

1 10
0

Sample Output 2

0

Sample Input 3

10 5
91 59 85 60 57 72 12 3 27 16

Sample Output 3

428633385

Be sure to find the sum modulo 998244353.

G - Colorful Spanning Tree

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

配点 : 675

問題文

頂点に 1 から N の番号がついた N 頂点 M 辺の連結無向グラフが与えられます。グラフは自己ループを含みませんが、多重辺を含む可能性があります。辺には色がついていて、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ色 c_i (1 \leq c_i \leq C) の辺です。 また、数列 A = (A_1, A_2, \dots, A_C) が与えられます。

このグラフの全域木のうち次の条件を満たすものを カラフル全域木 と呼びます。

  • 1 \leq i \leq C を満たす全ての整数 i について、全域木に含まれる色 i の辺の本数は A_i 本以下である。

1 \leq L \leq R \leq C を満たす整数の組 (L, R) のうち次の条件を満たすものの個数を求めてください。

  • カラフル全域木 T であって、T に含まれる任意の辺の色 cL \leq c \leq R を満たすようなものが存在する。

制約

  • 2 \leq N \leq 150
  • N - 1 \leq M \leq \min\left(\frac{CN(N-1)}{2}, 5 \times 10^5\right)
  • 1 \leq C \leq 300
  • 1 \leq A_i \leq N-1
  • \sum_{i=1}^C A_i \leq 300
  • 1 \leq u_i \lt v_i \leq N
  • 1 \leq c_i \leq C
  • i \neq j ならば (u_i,v_i,c_i) \neq (u_j,v_j,c_j)
  • 入力で与えられるグラフは連結
  • 入力される値は全て整数

入力

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

N M C
A_1 A_2 \dots A_C
u_1 v_1 c_1
u_2 v_2 c_2
\vdots
u_M v_M c_M

出力

1 \leq L \leq R \leq C を満たす整数の組 (L, R) のうち問題文の条件を満たすものの個数を出力せよ。


入力例 1

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

出力例 1

4

例えば (L, R) = (1, 2) は問題文の条件を満たします。これは 1 番目の辺と 4 番目の辺からなる全域木 T はカラフル全域木で、かつ T に含まれる任意の辺の色が L 以上 R 以下であるからです。
問題文の条件を満たす (L, R)(1, 2), (1, 3), (2, 2), (2, 3)4 個です。


入力例 2

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

出力例 2

11

入力例 3

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

出力例 3

2

Score : 675 points

Problem Statement

You are given a connected undirected graph with N vertices and M edges, where the vertices are labeled 1 to N. The graph does not contain self-loops, but it may contain multi-edges. Each edge has a color, and the i-th edge has a color c_i (1 \leq c_i \leq C) and connects vertices u_i and v_i. Also, a sequence A = (A_1, A_2, \dots, A_C) is given.

Among the spanning trees of this graph, those satisfying the following condition are called colorful spanning trees:

  • For every integer i such that 1 \leq i \leq C, the number of edges of color i included in the spanning tree is at most A_i.

Find the number of integer pairs (L, R) with 1 \leq L \leq R \leq C that satisfy the following condition:

  • There exists a colorful spanning tree T such that every edge in T has a color c with L \leq c \leq R.

Constraints

  • 2 \leq N \leq 150
  • N - 1 \leq M \leq \min\left(\frac{C N (N-1)}{2}, 5 \times 10^5\right)
  • 1 \leq C \leq 300
  • 1 \leq A_i \leq N-1
  • \sum_{i=1}^C A_i \leq 300
  • 1 \leq u_i < v_i \leq N
  • 1 \leq c_i \leq C
  • If i \neq j, then (u_i, v_i, c_i) \neq (u_j, v_j, c_j)
  • The given graph is connected.
  • All input values are integers.

Input

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

N M C
A_1 A_2 \dots A_C
u_1 v_1 c_1
u_2 v_2 c_2
\vdots
u_M v_M c_M

Output

Print the number of integer pairs (L, R) with 1 \leq L \leq R \leq C that satisfy the condition in the problem statement.


Sample Input 1

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

Sample Output 1

4

For example, (L, R) = (1, 2) satisfies the condition, because the spanning tree T formed by the 1st and 4th edges is a colorful spanning tree, and all edges in T have colors c with 1 \leq c \leq 2.
There are four pairs (L, R) that satisfy the condition: (1, 2), (1, 3), (2, 2), (2, 3).


Sample Input 2

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

Sample Output 2

11

Sample Input 3

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

Sample Output 3

2