A - Permutation Grid

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

1,\dots,n の順列 R_1,\dots,R_nC_1,\dots,C_n が与えられます。

あなたは縦 n 行、横 n 列からなるマス目を次の条件を満たすように白か黒で塗ります。

  • i=1,\dots,n について、上から i 行目の黒マスの数はちょうど R_i
  • j=1,\dots,n について、左から j 列目の黒マスの数はちょうど C_j

なお、この問題の制約のもとで、条件を満たすような塗り方がちょうど一通り存在することが示せます。

q 個のクエリ (r_1,c_1),\dots,(r_q,c_q) が与えられます。 各 i=1,\dots,q について、上から r_i 行目、左から c_i 列目にあるマスの色が黒であれば # を、白であれば . を出力してください。

制約

  • 1\le n,q\le 10^5
  • R_1,\dots,R_nC_1,\dots,C_n はそれぞれ 1,\dots,n の順列
  • 1\le r_i,c_i \le n
  • 入力はすべて整数

入力

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

n
R_1 \dots R_n
C_1 \dots C_n
q
r_1 c_1
\vdots
r_q c_q

出力

i 文字目が i 番目のクエリの答えであるような、#. からなる長さ q の文字列を出力せよ。


入力例 1

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

出力例 1

#.#.#.#

次のような塗り方が条件を満たします。

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

Score : 400 points

Problem Statement

Given are two permutations of 1,\dots,n: R_1,\dots,R_n and C_1,\dots,C_n.

We have a grid with n horizontal rows and n vertical columns. You will paint each square black or white to satisfy the following conditions.

  • For each i=1,\dots,n, the i-th row from the top has exactly R_i black squares.
  • For each j=1,\dots,n, the j-th column from the left has exactly C_j black squares.

It can be proved that, under the Constraints of this problem, there is exactly one way to paint the grid to satisfy the conditions.

You are given q queries (r_1,c_1),\dots,(r_q,c_q). For each i=1,\dots,q, print # if the square at the r_i-th row from the top and c_i-th column from the left is painted black; print . if that square is painted white.

Constraints

  • 1\le n,q\le 10^5
  • R_1,\dots,R_n and C_1,\dots,C_n are each permutations of 1,\dots,n.
  • 1\le r_i,c_i \le n
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

n
R_1 \dots R_n
C_1 \dots C_n
q
r_1 c_1
\vdots
r_q c_q

Output

Print a string of length q whose i-th character is the answer to the i-th query.


Sample Input 1

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

Sample Output 1

#.#.#.#

The conditions are satisfied by painting the grid as follows.

#####
#...#
#.#.#
###.#
....#
B - Shift and Reverse

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

1,\dots, n の順列 p_1,\dots,p_n が与えられます。 この順列に対して以下の操作を、好きな順で何度でも行えます。

  • 全体をひっくりかえす。つまり、p_1,p_2,\dots,p_np_n,p_{n-1},\dots,p_1 に並び替える。
  • 先頭の項を末尾に移動させる。つまり、p_1, p_2, \dots,p_np_2,\dots, p_n, p_1 に並び替える。

順列を昇順に並び替えるのに必要な操作回数の最小値を求めてください。 ただし、与えられる入力について、これらの操作によって順列を昇順に並び替えられることが保証されています。

制約

  • 2 \leq n \leq 10^5
  • p_1,\dots,p_n1,\dots,n の順列
  • 問題文中の操作によって p_1,\dots,p_n を昇順に並び替えられる

入力

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

n
p_1 \dots p_n

出力

順列を昇順に並び替えるのに必要な操作回数の最小値を出力せよ。


入力例 1

3
1 3 2

出力例 1

2

次のように操作すると 2 回で昇順に並び替えできます。

  1. 先頭の項を末尾に移動させ、 3, 2, 1 に並び替える。
  2. 全体をひっくりかえし、1, 2, 3 に並び替える。

2 回未満の操作で昇順に並び替えることはできないため、答えは 2 です。


入力例 2

2
2 1

出力例 2

1

どちらの操作をしても 1 回で昇順に並び替えできます。

1 回未満の操作で昇順に並び替えることはできないため、答えは 1 です。


入力例 3

10
2 3 4 5 6 7 8 9 10 1

出力例 3

3

次のように操作すると 3 回で昇順に並び替えできます。

  1. 全体をひっくりかえし、1,10,9,8,7,6,5,4,3,2 に並び替える。
  2. 先頭の項を末尾に移動させ、 10,9,8,7,6,5,4,3,2,1 に並び替える。
  3. 全体をひっくりかえし、 1,2,3,4,5,6,7,8,9,10 に並び替える。

3 回未満の操作で昇順に並び替えることはできないため、答えは 3 です。


入力例 4

12
1 2 3 4 5 6 7 8 9 10 11 12

出力例 4

0

一度も操作する必要がありません。

Score : 400 points

Problem Statement

Given is a permutation p_1,\dots,p_n of 1,\dots,n. On this permutation, you can do the operations below any number of times in any order.

  • Reverse the entire permutation. That is, rearrange p_1,p_2,\dots,p_n to p_n,p_{n-1},\dots,p_1.
  • Move the term at the beginning to the end. That is, rearrange p_1,p_2,\dots,p_n to p_2,\dots, p_n, p_1.

Find the minimum number of operations needed to sort the permutation in ascending order. In the given input, it is guaranteed that these operations can sort the permutation in ascending order.

Constraints

  • 2 \leq n \leq 10^5
  • p_1,\dots,p_n is a permutation of 1,\dots,n.
  • The operations in Problem Statement can sort p_1,\dots,p_n in ascending order.

Input

Input is given from Standard Input in the following format:

n
p_1 \dots p_n

Output

Print the minimum number of operations needed to sort the permutation in ascending order.


Sample Input 1

3
1 3 2

Sample Output 1

2

You can sort it in ascending order in two operations as follows.

  1. Move the term at the beginning to the end: now you have 3, 2, 1.
  2. Reverse the whole permutation: now you have 1, 2, 3.

You cannot sort it in less than two operations, so the answer is 2.


Sample Input 2

2
2 1

Sample Output 2

1

Doing either operation once will sort it in ascending order.

You cannot sort it in less than one operation, so the answer is 1.


Sample Input 3

10
2 3 4 5 6 7 8 9 10 1

Sample Output 3

3

You can sort it in ascending order in three operations as follows.

  1. Reverse the whole permutation: now you have 1,10,9,8,7,6,5,4,3,2.
  2. Move the term at the beginning to the end: now you have 10,9,8,7,6,5,4,3,2,1.
  3. Reverse the whole permutation: now you have 1,2,3,4,5,6,7,8,9,10.

You cannot sort it in less than three operations, so the answer is 3.


Sample Input 4

12
1 2 3 4 5 6 7 8 9 10 11 12

Sample Output 4

0

No operation is needed.

C - Almost Sorted

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1,\dots, n-1 からなる数列 a_1,\dots,a_n と整数 d が与えられます。 以下の条件を満たす数列 p_1,\dots,p_n はいくつありますか? 答えを 998244353 で割ったあまりを出力してください。

  • p_1,\dots,p_n1,\dots, n の順列
  • i=1,\dots,n について、 a_i\neq -1 ならば a_i=p_i (つまり、a_1,\dots,a_n-1 の項を適切に置き換えることで p_1,\dots,p_n に書き換えできる)
  • i=1,\dots,n について、 |p_i - i|\le d

制約

  • 1 \le d \le 5
  • d < n \le 500
  • 1\le a_i \le n または a_i=-1
  • a_i\neq -1 ならば |a_i-i|\le d
  • i\neq j かつ a_i, a_j \neq -1 ならば a_i\neq a_j
  • 入力はすべて整数

入力

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

n d
a_1 \dots a_n

出力

条件を満たす数列の数を 998244353 で割ったあまりを出力せよ。


入力例 1

4 2
3 -1 1 -1

出力例 1

2

(3,2,1,4)(3,4,1,2) が条件を満たします。


入力例 2

5 1
2 3 4 5 -1

出力例 2

0

-1 を置き換えて得られる 1,2,3,4,5 の順列は (2,3,4,5,1) のみです。 この順列は、5 項目が条件を満たさないため、答えは 0 です。


入力例 3

16 5
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

出力例 3

794673086

998244353 で割ったあまりを出力してください。

Score : 600 points

Problem Statement

Given is a sequence a_1,\dots,a_n consisting of 1,\dots, n and -1, along with an integer d. How many sequences p_1,\dots,p_n satisfy the conditions below? Print the count modulo 998244353.

  • p_1,\dots,p_n is a permutation of 1,\dots, n.
  • For each i=1,\dots,n, a_i=p_i if a_i\neq -1. (That is, p_1,\dots,p_n can be obtained by replacing the -1s in a_1,\dots,a_n in some way.)
  • For each i=1,\dots,n, |p_i - i|\le d.

Constraints

  • 1 \le d \le 5
  • d < n \le 500
  • 1\le a_i \le n or a_i=-1.
  • |a_i-i|\le d if a_i\neq -1.
  • a_i\neq a_j, if i\neq j and a_i, a_j \neq -1.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

n d
a_1 \dots a_n

Output

Print the number of sequences that satisfy the conditions, modulo 998244353.


Sample Input 1

4 2
3 -1 1 -1

Sample Output 1

2

The conditions are satisfied by (3,2,1,4) and (3,4,1,2).


Sample Input 2

5 1
2 3 4 5 -1

Sample Output 2

0

The only permutation of 1,2,3,4,5 that can be obtained by replacing the -1 is (2,3,4,5,1), whose fifth term violates the condition, so the answer is 0.


Sample Input 3

16 5
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

Sample Output 3

794673086

Print the count modulo 998244353.

D - Between Two Binary Strings

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

文字列の 美しさ を、その文字列のなかで同じ 2 文字が隣り合っている位置の個数として定義します。 例えば、00011 の美しさは 3 で、10101 の美しさは 0 です。

S_{n,m}n 文字の 0m 文字の 1 からなる長さ n+m の文字列全体の集合とします。

s_1,s_2 \in S_{n,m} について、s_1,s_2距離 d(s_1,s_2) を 「隣り合った 2 文字を入れ替える操作によって文字列 s_1 を文字列 s_2 に並び替えるのに必要な最小の操作回数」 と定義します。

また、s_1,s_2,s_3\in S_{n,m} について、s_2s_1s_3間にある ことを、d(s_1,s_3)=d(s_1,s_2)+d(s_2,s_3) で定義します。

s,t\in S_{n,m} が与えられるので、st の間にある文字列の美しさの最大値を出力してください。

制約

  • 2 \le n + m\le 3\times 10^5
  • 0 \le n,m
  • s,tn 文字の 0m 文字の 1 からなる長さ n+m の文字列

入力

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

n m
s
t

出力

st の間にある文字列の美しさの最大値を出力せよ。


入力例 1

2 3
10110
01101

出力例 1

2

1011001101 の距離は 2 で、これらの間にある文字列は、10110, 01110, 01101, 10101 です。 それぞれの美しさは 1,2,1,0 であるため、答えは 2 です。


入力例 2

4 2
000011
110000

出力例 2

4

000011110000 の距離は 8 です。 美しさが最大になる文字列は 000011110000 で、答えは 4 です。


入力例 3

12 26
01110111101110111101001101111010110110
10011110111011011001111011111101001110

出力例 3

22

Score : 700 points

Problem Statement

Let us define the beauty of the string as the number of positions where two adjacent characters are the same. For example, the beauty of 00011 is 3, and the beauty of 10101 is 0.

Let S_{n,m} be the set of all strings of length n+m formed of n 0s and m 1s.

For s_1,s_2 \in S_{n,m}, let us define the distance of s_1 and s_2, d(s_1,s_2), as the minimum number of swaps needed when rearranging the string s_1 to s_2 by swaps of two adjacent characters.

Additionally, for s_1,s_2,s_3\in S_{n,m}, s_2 is said to be between s_1 and s_3 when d(s_1,s_3)=d(s_1,s_2)+d(s_2,s_3).

Given s,t\in S_{n,m}, print the greatest beauty of a string between s and t.

Constraints

  • 2 \le n + m\le 3\times 10^5
  • 0 \le n,m
  • Each of s and t is a string of length n+m formed of n 0s and m 1s.

Input

Input is given from Standard Input in the following format:

n m
s
t

Output

Print the greatest beauty of a string between s and t.


Sample Input 1

2 3
10110
01101

Sample Output 1

2

Between 10110 and 01101, whose distance is 2, we have the following strings: 10110, 01110, 01101, 10101. Their beauties are 1, 2, 1, 0, respectively, so the answer is 2.


Sample Input 2

4 2
000011
110000

Sample Output 2

4

Between 000011 and 110000, whose distance is 8, the strings with the greatest beauty are 000011 and 110000, so the answer is 4.


Sample Input 3

12 26
01110111101110111101001101111010110110
10011110111011011001111011111101001110

Sample Output 3

22
E - Paw

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

n 個のマスが一列に並んでいます。それぞれのマスには右向きか左向きの足跡がついているか、穴が空いているかのいずれかです。 各マスの最初の状態は ., <, > からなる文字列 s で表され、si 文字目が . であるとき左から i マス目に穴が空いていることを、< であるとき左向きの足跡がついていることを、> であるとき右向きの足跡がついていることを表します。

猫のすぬけくんは、穴の空いているマスがなくなるまで次の操作を繰り返します。

  • Step 1: 穴の空いているマスのうち 1 マスを等確率でランダムに選ぶ。
  • Step 2: 選んだマスの穴を埋め、そこに立ち、等確率でランダムに左か右を向く。
  • Step 3: 穴の空いているマスの上に乗るか、マスの外に出るまで、向いている方向に歩き続ける

ただし、マスの選び方も向く方向の選び方も、互いに独立とします。

すぬけくんが(穴の空いていない)マスの上を歩くとき、そのマスには歩いた向きに足跡が付きます。 すでに足跡がついているマスであっても、もともとついている足跡が消えて、新しく足跡が付きます。 例えば、>.<><.>< という状態で、すぬけくんが左から 6 マス目を選んで左を向いたとき、左から 6,5,4,3 マス目に左向きの足跡がつき、>.<<<<>< となります。

すぬけくんが操作を終えた時の、左向きの足跡がついたマスの数の期待値を \mathrm{mod~} 998244353 で求めてください。

注意

求める期待値が既約分数 p/q で表されるとき、rq\equiv p ~(\text{mod } 998244353) かつ 0 \leq r \lt 998244353 を満たす整数 r がこの問題の制約のもとで一意に定まります。 この r が求める値です。

制約

  • 1 \leq n \leq 10^5
  • s., <, > からなる長さ n の文字列
  • s.1 文字以上含む

入力

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

n
s

出力

左向きの足跡がついたマスの数の期待値を \mathrm{mod~} 998244353 で出力せよ。


入力例 1

5
><.<<

出力例 1

3

Step 1 では、すぬけくんは穴の空いている唯一のマスを必ず選びます。

Step 2 ですぬけくんが左を向いたとき、操作後のマスの状態は <<<<< となります。 このとき、左向きの足跡がついたマスの数は 5 です。

Step 2 ですぬけくんが右を向いたとき、操作後のマスの状態は ><>>> となります。 このとき、左向きの足跡がついたマスの数は 1 です。

よって、答えは \frac{5+1}{2}=3 です。


入力例 2

20
>.>.<>.<<.<>.<..<>><

出力例 2

848117770

Score : 800 points

Problem Statement

There are n squares arranged in a row. Each square has a left- or right-pointing footprint or a hole. The initial state of each square is described by a string s consisting of ., <, >. If the i-th character of s is ., the i-th square from the left has a hole; if that character is <, that square has a left-pointing footprint; if that character is >, that square has a right-pointing footprint.

Snuke, the cat, will repeat the following procedure until there is no more square with a hole.

  • Step 1: Choose one square with a hole randomly with equal probability.
  • Step 2: Fill the hole in the chosen square, stand there, and face to the left or right randomly with equal probability.
  • Step 3: Keep walking in the direction Snuke is facing until he steps on a square with a hole or exits the row of squares.

Here, the choices of squares and directions are independent of each other.

When Snuke walks on a square (without a hole), that square gets a footprint in the direction he walks in. If the square already has a footprint, it gets erased and replaced by a new one. For example, in the situation >.<><.><, if Snuke chooses the 6-th square from the left and faces to the left, the 6-th, 5-th, 4-th, 3-rd squares get left-pointing footprints: >.<<<<><.

Find the expected value of the number of left-pointing footprints when Snuke finishes the procedures, modulo 998244353.

Notes

When the sought expected value is represented as an irreducible fraction p/q, there uniquely exists an integer r such that rq\equiv p ~(\text{mod } 998244353) and 0 \leq r \lt 998244353 under the Constraints of this problem. This r is the value to be found.

Constraints

  • 1 \leq n \leq 10^5
  • s is a string of length n consisting of ., <, >.
  • s contains at least one ..

Input

Input is given from Standard Input in the following format:

n
s

Output

Print the expected value of the number of left-pointing footprints, modulo 998244353.


Sample Input 1

5
><.<<

Sample Output 1

3

In Step 1, Snuke always chooses the only square with a hole.

If Snuke faces to the left in Step 2, the squares become <<<<<, where 5 squares have left-pointing footprints.

If Snuke faces to the right in Step 2, the squares become ><>>>, where 1 square has a left-pointing footprint.

Therefore, the answer is \frac{5+1}{2}=3.


Sample Input 2

20
>.>.<>.<<.<>.<..<>><

Sample Output 2

848117770
F - Takahashi The Strongest

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 900

問題文

高橋くん、青木くん、すぬけくんの 3 人が、じゃんけんを k 回するゲームで対戦します。

P, R, S からなる長さ k の文字列を 作戦 と呼びます。ゲームは次のような流れで進行します。

  • 参加者がそれぞれ作戦を選ぶ。
  • じゃんけんを k 回行う。i 回目では、それぞれの参加者は、選んだ作戦の i 文字目に応じた手を出す。具体的には、P であればパーを、R であればグーを、S であればチョキを出す。

青木くんは n 個の作戦 a_1,\dots,a_n のうち 1 つを等確率でランダムに選びます。 すぬけくんは m 個の作戦 b_1,\dots,b_m のうち 1 つを等確率でランダムに選びます。 ただし、2 人の選び方は独立であるとします。

k 回のじゃんけんのうち、高橋くんだけが勝った回が 1 回でもあった場合、高橋くんは喜びます。 ありうる 3^k 通りの作戦それぞれについて、高橋くんがその作戦を選んだときに喜ぶ確率を求め、その nm 倍を整数として出力してください(この値は整数となることが証明できます)。

注意

3 人でじゃんけんをしたとき、高橋くんだけが勝つ場合は次の 3 通りです。

  • 高橋くんがパーを出し、青木くんとすぬけくんがグーを出す
  • 高橋くんがグーを出し、青木くんとすぬけくんがチョキを出す
  • 高橋くんがチョキを出し、青木くんとすぬけくんがパーを出す

制約

  • 1 \leq k \leq 12
  • 1 \leq n,m \leq 3^k
  • a_i,b_iP, R, S からなる長さ k の文字列
  • a_1,\dots,a_n は相異なる
  • b_1,\dots,b_m は相異なる

入力

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

k n m
a_1
\vdots
a_n
b_1
\vdots
b_m

出力

3^k 個の値を出力せよ。i 個目には、ありうる作戦のうち辞書順で i 番目のものを高橋くんが選んだときの答えを出力せよ。


入力例 1

2 1 3
RS
RP
RR
RS

出力例 1

3
3
3
0
1
0
0
1
0

青木くんが選ぶ作戦は RS です。

すぬけくんが作戦として RP を選んだ場合、条件を満たす高橋くんの作戦は PP, PR, PS です。

すぬけくんが作戦として RR を選んだ場合、条件を満たす高橋くんの作戦は PP, PR, PS です。

すぬけくんが作戦として RS を選んだ場合、条件を満たす高橋くんの作戦は PP, PR, PS, RR, SR です。

以上より、高橋くんの作戦が PP, PR, PS, RP, RR, RS, SP, SR, SS であるときの確率はそれぞれ 1,1,1,0,\frac 13,0,0,\frac 13,0 です。 これらを 3 倍した値を出力してください。


入力例 2

3 5 4
RRP
SSS
RSR
PPP
RSS
PPS
SRP
SSP
RRS

出力例 2

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

Score : 900 points

Problem Statement

Takahashi, Aoki, and Snuke will play a game with k rounds of rock-paper-scissors.

Let us call a string of length k consisting of P, R, S a strategy. The game proceeds as follows.

  • Each participant chooses a strategy.
  • Play k rounds of rock-paper-scissors. In the i-th round, each participant plays the hand corresponding to the i-th character in the chosen strategy: paper for P, rock for R, and scissors for S.

Aoki will randomly choose one strategy from the n strategies a_1,\dots,a_n with equal probability. Snuke will randomly choose one strategy from the m strategies b_1,\dots,b_m with equal probability. Their choices are independent of each other.

Takahashi will be happy if he is the only winner in at least one of the k rounds. For each of the 3^k possible strategies, find the probability that he becomes happy when choosing that strategy and print it multiplied by nm as an integer (it can be proved that this value is an integer).

Notes

In the game of rock-paper-scissors with three players, the following three scenarios make Takahashi the only winner.

  • Takahashi plays paper, while Aoki and Snuke play rock.
  • Takahashi plays rock, while Aoki and Snuke play scissors.
  • Takahashi plays scissors, while Aoki and Snuke play paper.

Constraints

  • 1 \leq k \leq 12
  • 1 \leq n,m \leq 3^k
  • Each of a_i and b_i is a string of length k consisting of P, R, S.
  • a_1,\dots,a_n are distinct.
  • b_1,\dots,b_m are distinct.

Input

Input is given from Standard Input in the following format:

k n m
a_1
\vdots
a_n
b_1
\vdots
b_m

Output

Print 3^k values. The i-th value should be the answer when Takahashi chooses the i-th lexicographically smallest possible strategy.


Sample Input 1

2 1 3
RS
RP
RR
RS

Sample Output 1

3
3
3
0
1
0
0
1
0

Aoki chooses the strategy RS.

If Snuke chooses the strategy RP, the strategies that can meet Takahashi's objective are PP, PR, PS.

If Snuke chooses the strategy RR, the strategies that can meet Takahashi's objective are PP, PR, PS.

If Snuke chooses the strategy RS, the strategies that can meet Takahashi's objective are PP, PR, PS, RR, SR.

Therefore, the probabilities when Takahashi chooses PP, PR, PS, RP, RR, RS, SP, SR, SS are 1, 1, 1, 0, \frac 13, 0, 0, \frac 13, 0, respectively. Print them multiplied by 3.


Sample Input 2

3 5 4
RRP
SSS
RSR
PPP
RSS
PPS
SRP
SSP
RRS

Sample Output 2

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