E - Counting Squares

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

二次元平面があります。1 以上 9 以下の整数 r,c について、S_{r}c 番目の文字が # であるとき座標 (r,c) にポーンが置いてあり、S_{r}c 番目の文字が . であるとき座標 (r,c) に何も置かれていません。

この平面上の正方形であって、4 頂点全てにポーンが置いてあるものの個数を求めてください。

制約

  • S_1,\ldots,S_9 はそれぞれ #. からなる長さ 9 の文字列

入力

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

S_1
S_2
\vdots
S_9

出力

答えを出力せよ。


入力例 1

##.......
##.......
.........
.......#.
.....#...
........#
......#..
.........
.........

出力例 1

2

座標 (1,1),(1,2),(2,2),(2,1) を頂点とする正方形は、4 頂点全てにポーンが置かれています。

座標 (4,8),(5,6),(7,7),(6,9) を頂点とする正方形も、4 頂点全てにポーンが置かれています。

よって答えは 2 です。


入力例 2

.#.......
#.#......
.#.......
.........
....#.#.#
.........
....#.#.#
........#
.........

出力例 2

3

Score : 300 points

Problem Statement

There is a two-dimensional plane. For integers r and c between 1 and 9, there is a pawn at the coordinates (r,c) if the c-th character of S_{r} is #, and nothing if the c-th character of S_{r} is ..

Find the number of squares in this plane with pawns placed at all four vertices.

Constraints

  • Each of S_1,\ldots,S_9 is a string of length 9 consisting of # and ..

Input

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

S_1
S_2
\vdots
S_9

Output

Print the answer.


Sample Input 1

##.......
##.......
.........
.......#.
.....#...
........#
......#..
.........
.........

Sample Output 1

2

The square with vertices (1,1), (1,2), (2,2), and (2,1) have pawns placed at all four vertices.

The square with vertices (4,8), (5,6), (7,7), and (6,9) also have pawns placed at all four vertices.

Thus, the answer is 2.


Sample Input 2

.#.......
#.#......
.#.......
.........
....#.#.#
.........
....#.#.#
........#
.........

Sample Output 2

3
F - Approximate Equalization 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 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
G - Change Usernames

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

あなたの運営する Web サービスには N 人のユーザがいます。

i 番目のユーザの現在のユーザ名は S_i ですが、T_i への変更を希望しています。
ここで、S_1,\ldots,S_N は相異なり、T_1,\ldots,T_N も相異なります。

ユーザ名を変更する順序を適切に定めることで、以下の条件を全て満たすように、全てのユーザのユーザ名を希望通り変更することができるか判定してください。

  • ユーザ名の変更は 1 人ずつ行う
  • どのユーザもユーザ名の変更は一度だけ行う
  • ユーザ名の変更を試みる時点で他のユーザが使っているユーザ名に変更することはできない

制約

  • 1 \leq N \leq 10^5
  • S_i,T_i は英小文字からなる 1 文字以上 8 文字以下の文字列
  • S_i \neq T_i
  • S_i は相異なる
  • T_i は相異なる

入力

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

N
S_1 T_1
S_2 T_2
\vdots
S_N T_N

出力

条件を全て満たすように全てのユーザのユーザ名を希望通り変更することができるとき Yes、できないとき No と出力せよ。


入力例 1

2
b m
m d

出力例 1

Yes

1 番目のユーザの現在のユーザ名は b であり、m への変更を希望しています。
2 番目のユーザの現在のユーザ名は m であり、d への変更を希望しています。

まず、2 番目のユーザのユーザ名を m から d に変更し、 その後 1 番目のユーザのユーザ名を b から m に変更することで、条件を満たしながら変更することができます。

最初の時点では 2 番目のユーザのユーザ名が m なので、1 番目のユーザのユーザ名を同じ m に変更することはできません。


入力例 2

3
a b
b c
c a

出力例 2

No

1 番目のユーザの現在のユーザ名は a であり、b への変更を希望しています。
2 番目のユーザの現在のユーザ名は b であり、c への変更を希望しています。
3 番目のユーザの現在のユーザ名は c であり、a への変更を希望しています。

条件を満たしながらユーザ名の変更を行うことはできません。


入力例 3

5
aaa bbb
yyy zzz
ccc ddd
xxx yyy
bbb ccc

出力例 3

Yes

Score : 400 points

Problem Statement

You run a web service with N users.

The i-th user with a current handle S_i wants to change it to T_i.
Here, S_1,\ldots, and S_N are pairwise distinct, and so are T_1,\ldots, and T_N.

Determine if there is an appropriate order to change their handles to fulfill all of their requests subject to the following conditions:

  • you change only one user's handle at a time;
  • you change each user's handle only once;
  • when changing the handle, the new handle should not be used by other users at that point.

Constraints

  • 1 \leq N \leq 10^5
  • S_i and T_i are strings of length between 1 and 8 (inclusive) consisting of lowercase English letters.
  • S_i \neq T_i
  • S_i are pairwise distinct.
  • T_i are pairwise distinct.

Input

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

N
S_1 T_1
S_2 T_2
\vdots
S_N T_N

Output

Print Yes if they can change their handles to fulfill all of their requests subject to the conditions; print No otherwise.


Sample Input 1

2
b m
m d

Sample Output 1

Yes

The 1-st user with a current handle b wants to change it to m.
The 2-nd user with a current handle m wants to change it to d.

First, you change the 2-nd user's handle from m to d; then you change the 1-st user's handle from b to m. This way, you can achieve the objective.

Note that you cannot change the 1-st user's handle to m at first, because it is used by the 2-nd user at that point.


Sample Input 2

3
a b
b c
c a

Sample Output 2

No

The 1-st user with a current handle a wants to change it to b.
The 2-nd user with a current handle b wants to change it to c.
The 3-rd user with a current handle c wants to change it to a.

We cannot change their handles subject to the conditions.


Sample Input 3

5
aaa bbb
yyy zzz
ccc ddd
xxx yyy
bbb ccc

Sample Output 3

Yes
H - Chain Contestant

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

別世界の AtCoder では現在、 AAC, ..., AJC の 10 種類のコンテストが開催されており、これから N 回のコンテストが開催されます。
各コンテストの種類は文字列 S として与えられ、 Si 文字目が x なら i 回目には AxC が開催されます。
シカの AtCoDeer くんは、 N 個のコンテストから 1 個以上いくつか選んで、以下の条件を満たすように選んで出場します。

  • 出るコンテストを順番を保ったまま抜き出したとき、コンテストの種類ごとにひとかたまりとなっている。
    • 厳密には、 AtCoDeer くんが x 個のコンテストに出場し、そのうち i 回目のコンテストの種類が T_i であるとき、全ての 1 \le i < j < k \le x を満たす整数組 (i,j,k) に対して、 T_i=T_k であるならば T_i=T_j でなければならない。

AtCoDeer くんが出場するコンテストの選び方として考えられるものの総数を 998244353 で割った余りを求めてください。
ただし、 2 つのコンテストの選び方が異なるとは、あるコンテスト c が存在して、片方の選び方では c に出場するがもう片方の選び方では出場しないということを指します。

制約

  • 1 \le N \le 1000
  • |S|=N
  • SA から J までの英大文字のみからなる

入力

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

N
S

出力

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


入力例 1

4
BGBH

出力例 1

13

例えば、 1,3 回目のコンテストに出場する、 2,4 回目のコンテストに出場するという選び方は条件を満たします。
一方、 1,2,3,4 回目のコンテストに出場する場合、 ABC への出場がひとかたまりになっておらず、整数組 (i,j,k)=(1,2,3) について条件に違反します。
また、全てのコンテストに出場しないということも認められません。
問題文の条件に適する出場するコンテストの選び方は 13 通りあります。


入力例 2

100
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBIEIJEIJIJCGCCFGIEBIHFCGFBFAEJIEJAJJHHEBBBJJJGJJJCCCBAAADCEHIIFEHHBGF

出力例 2

330219020

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

Score : 500 points

Problem Statement

AtCoder in another world holds 10 types of contests called AAC, ..., AJC. There will be N contests from now on.
The types of these N contests are given to you as a string S: if the i-th character of S is x, the i-th contest will be AxC.
AtCoDeer will choose and participate in one or more contests from the N so that the following condition is satisfied.

  • In the sequence of contests he will participate in, the contests of the same type are consecutive.
    • Formally, when AtCoDeer participates in x contests and the i-th of them is of type T_i, for every triple of integers (i,j,k) such that 1 \le i < j < k \le x, T_i=T_j must hold if T_i=T_k.

Find the number of ways for AtCoDeer to choose contests to participate in, modulo 998244353.
Two ways to choose contests are considered different when there is a contest c such that AtCoDeer participates in c in one way but not in the other.

Constraints

  • 1 \le N \le 1000
  • |S|=N
  • S consists of uppercase English letters from A through J.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the answer as an integer.


Sample Input 1

4
BGBH

Sample Output 1

13

For example, participating in the 1-st and 3-rd contests is valid, and so is participating in the 2-nd and 4-th contests.
On the other hand, participating in the 1-st, 2-nd, 3-rd, and 4-th contests is invalid, since the participations in ABCs are not consecutive, violating the condition for the triple (i,j,k)=(1,2,3).
Additionally, it is not allowed to participate in zero contests.
In total, there are 13 valid ways to participate in some contests.


Sample Input 2

100
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBIEIJEIJIJCGCCFGIEBIHFCGFBFAEJIEJAJJHHEBBBJJJGJJJCCCBAAADCEHIIFEHHBGF

Sample Output 2

330219020

Be sure to find the count modulo 998244353.

I - Simultaneous Swap

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

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

高橋君は次の操作を好きなだけ (0 回でも良い) 繰り返す事ができます。

1 以上 N 以下の、どの 2 つも互いに相異なる 3 つの整数 i,j,k を選ぶ。
Ai 番目の要素と j 番目の要素を交換し、Bi 番目の要素と k 番目の要素を交換する。

高橋君がうまく操作を繰り返すことによって、 AB を一致させる事が可能ならば Yes を、不可能ならば No を出力してください。
ただし、AB が一致しているとは、任意の 1\leq i\leq N について Ai 番目の要素と Bi 番目の要素が等しいことを言います。

制約

  • 3 \leq N \leq 2\times 10^5
  • 1\leq A_i,B_i\leq N
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

操作を繰り返すことによって、高橋君が AB を一致させる事が可能ならば Yes を、不可能ならば No を出力せよ。


入力例 1

3
1 2 1
1 1 2

出力例 1

Yes

(i,j,k)=(1,2,3) として 1 回操作を行うことで、A_1A_2B_1B_3 がそれぞれ交換され、
A,B はともに (2,1,1) となって一致します。よって、Yes を出力します。


入力例 2

3
1 2 2
1 1 2

出力例 2

No

どのように操作を行っても AB を一致させることはできません。よって、No を出力します。


入力例 3

5
1 2 3 2 1
3 2 2 1 1

出力例 3

Yes

入力例 4

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

出力例 4

No

Score : 500 points

Problem Statement

You are given two sequences of N numbers: A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N).

Takahashi can repeat the following operation any number of times (possibly zero).

Choose three pairwise distinct integers i, j, and k between 1 and N.
Swap the i-th and j-th elements of A, and swap the i-th and k-th elements of B.

If there is a way for Takahashi to repeat the operation to make A and B equal, print Yes; otherwise, print No.
Here, A and B are said to be equal when, for every 1\leq i\leq N, the i-th element of A and that of B are equal.

Constraints

  • 3 \leq N \leq 2\times 10^5
  • 1\leq A_i,B_i\leq N
  • 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
B_1 B_2 \ldots B_N

Output

Print Yes if there is a way for Takahashi to repeat the operation to make A and B equal, and print No otherwise.


Sample Input 1

3
1 2 1
1 1 2

Sample Output 1

Yes

Performing the operation once with (i,j,k)=(1,2,3) swaps A_1 and A_2, and swaps B_1 and B_3,
making both A and B equal to (2,1,1). Thus, you should print Yes.


Sample Input 2

3
1 2 2
1 1 2

Sample Output 2

No

There is no way to perform the operation to make A and B equal, so you should print No.


Sample Input 3

5
1 2 3 2 1
3 2 2 1 1

Sample Output 3

Yes

Sample Input 4

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

Sample Output 4

No