Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 400 点
問題文
長さ 3N の文字列 S が与えられます。S は A
, B
, C
をそれぞれちょうど N 個ずつ含みます。
文字 A
, B
, C
からなる文字列 T が次の条件を満たすとき、T を 良い 文字列であると呼びます。
- T の長さは 3 で割り切れる。この長さを 3K とする。
- T_1 = T_2 = \ldots = T_K
- T_{K+1} = T_{K+2} = \ldots = T_{2K}
- T_{2K+1} = T_{2K+2} = \ldots = T_{3K}
- 文字 T_1, T_{K+1}, T_{2K+1} は互いに異なる。
良い文字列の例を挙げると、ABC
, BBAACC
, AAACCCBBB
です。
S を 6 個以下の(連続とは限らない)部分列に分解する方法であって、各部分列が良い文字列であるような方法を一つ見つけてください。
これは、この問題の制約下で必ず可能であることが証明できます。
制約
- 1 \le N \le 2\cdot 10^5
- 文字列 S は、文字
A
,B
,C
を N 個ずつ含む。
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
1
から 6
までの数字からなる長さ 3N の文字列を出力せよ。文字列に含まれる各 1\le i \le 6 について、i を出力した位置に対応する S の文字を並べると良い文字列が得られるようにすること。
なお、答えが複数通り存在する場合、そのどれを出力しても正解とみなされる。
入力例 1
2 ABCCBA
出力例 1
111222
S が部分列 ABC
, CBA
に分割されており、これらはそれぞれ良い文字列です。
入力例 2
4 AABCBCAACBCB
出力例 2
111211241244
1 の位置に対応する部分列は AABBCC
、2 の位置に対応する部分列は CAB
、4 の位置に対応する部分列は ACB
であり、これらは全て良い文字列です。
Score : 400 points
Problem Statement
You are given a string S of length 3N, containing exactly N letters A
, N letters B
, and N letters C
.
Let's call a string T consisting of letters A
, B
, and C
good, if the following conditions hold:
- The length of T is divisible by 3. Let's denote it by 3K.
- T_1 = T_2 = \ldots = T_K
- T_{K+1} = T_{K+2} = \ldots = T_{2K}
- T_{2K+1} = T_{2K+2} = \ldots = T_{3K}.
- Letters T_1, T_{K+1} and T_{2K+1} are different from each other.
Examples of good strings are ABC
, BBAACC
, and AAACCCBBB
.
Find a way to split S into at most 6 (not necessarily contiguous) subsequences so that the letters from each subsequence form a good string.
It can be proved that, under the constraints of the problem, it's always possible.
Constraints
- 1 \le N \le 2\cdot 10^5
- The string S contains N letters
A
, N lettersB
, and N lettersC
.
Input
Input is given from Standard Input in the following format:
N S
Output
Output a string of length 3N, containing only digits from 1 to 6. For every 1\le i \le 6 which appears in your string, characters of S at positions where you output i have to form a good string. If there are multiple possible solutions, printing any of them will be considered correct.
Sample Input 1
2 ABCCBA
Sample Output 1
111222
S is split into subsequences ABC
and CBA
, and each of them is a good string.
Sample Input 2
4 AABCBCAACBCB
Sample Output 2
111211241244
Positions of 1 form a subsequence AABBCC
, positions of 2 form a subsequence CAB
, and positions of 4 form a subsequence ACB
.
All of these strings are good.
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 700 点
問題文
A
, B
, C
からなる長さ N の文字列 S が与えられます。あなたは、次の操作を何回でも行うことができます。
- S_iS_{i+1}S_{i+2} が
ABC
,BCA
,CAB
のいずれかに等しいような 1 \le i \le N-2 を任意に選ぶ。そして、その三文字をABC
,BCA
,CAB
のいずれかで置換する。
例えば、文字列 AABC
に対して、以下の変換を行うことができます。
AABC
\toABCA
\toBCAA
上記の操作を有限回行うことで(0 回でもよい)、文字列 S から文字列 T を得ることが可能か判定してください。
制約
- 3\le N \le 5\cdot 10^5
- S は、
A
,B
,C
からなる長さ N の文字列である。 - T は、
A
,B
,C
からなる長さ N の文字列である。
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
上記の操作で S を T に変換することが可能であれば YES
、そうでなければ NO
と出力せよ。大文字、小文字は不問である。
入力例 1
4 AABC BCAA
出力例 1
YES
これは問題文で説明した例です。
入力例 2
4 ABCA BCAB
出力例 2
NO
Score : 700 points
Problem Statement
You are given a string S of length N, consisting of A
, B
, and C
.
You are allowed to perform the following operation any number of times:
- Choose any i with 1 \le i \le N-2, such that S_iS_{i+1}S_{i+2} is equal to
ABC
,BCA
, orCAB
. Then, replace the three characters withABC
,BCA
, orCAB
.
For example, you can perform the following transformations with string AABC
:
AABC
\toABCA
\toBCAA
Determine if you can obtain string T from string S with some finite (maybe zero) number of operations above.
Constraints
- 3\le N \le 5\cdot 10^5
- S is a string of length N consisting of
A
,B
, andC
. - T is a string of length N consisting of
A
,B
, andC
.
Input
Input is given from Standard Input in the following format:
N S T
Output
If you can transform S into T with the operations above, output YES
, otherwise output NO
.
The checker is case-insensitive: you can use either uppercase or lowercase letters.
Sample Input 1
4 AABC BCAA
Sample Output 1
YES
This example was explained in the statement.
Sample Input 2
4 ABCA BCAB
Sample Output 2
NO
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 900 点
問題文
整数 N, M が与えられます。次の条件を満たす長さ N の列 A=[A_1, A_2, \ldots, A_N] の個数を求めてください。
- 2 \le A_i \le M (1 \leq i \leq N)
- 1 から N までの整数の順列 P=[P_1,P_2,\ldots,P_N] であって次の性質を持つものが存在する。
- 1 から N までの各 i について、A_i は列 [P_1, P_2, \ldots, P_{i-1}, P_{i+1}, \ldots, P_{N-1}, P_N] の最長増加部分列の長さに等しい。
この個数は非常に大きい可能性があるため、これを素数 Q で割った余りを出力してください。
制約
- 3 \le N \le 5000
- 2 \le M \le N-1
- 10^8 \le Q \le 10^9
- Q は素数である。
入力
入力は以下の形式で標準入力から与えられる。
N M Q
出力
答えを Q で割った余りを出力せよ。
入力例 1
3 2 686926217
出力例 1
1
このような列は [2, 2, 2] のみです。ここで [1, 2, 3] という順列が存在して性質を満たします。
入力例 2
4 3 354817471
出力例 2
9
このような列は次の 9 個です: [2, 2, 2, 2], [2, 2, 2, 3], [2, 2, 3, 2], [2, 2, 3, 3], [2, 3, 2, 2], [2, 3, 3, 2], [3, 2, 2, 2], [3, 3, 2, 2], [3, 3, 3, 3]。
入力例 3
5 2 829412599
出力例 3
1
このような列は [2, 2, 2, 2, 2] のみです。
入力例 4
5 3 975576997
出力例 4
23
入力例 5
69 42 925171057
出力例 5
801835311
Score : 900 points
Problem Statement
You are given integers N and M. Find the number of arrays A=[A_1, A_2, \ldots, A_N] of length N such that the following conditions hold:
- 2 \le A_i \le M (1 \leq i \leq N)
- There exists a permutation P=[P_1,P_2,\ldots,P_N] of integers from 1 to N with the following property:
- For every i from 1 to N, A_i equals the length of the longest increasing subsequence of the sequence [P_1, P_2, \ldots, P_{i-1}, P_{i+1}, \ldots, P_{N-1}, P_N].
As this number can be very large, output it modulo some prime Q.
Constraints
- 3 \le N \le 5000.
- 2 \le M \le N-1.
- 10^8 \le Q \le 10^9
- Q is a prime.
Input
Input is given from Standard Input in the following format:
N M Q
Output
Print the answer modulo Q.
Sample Input 1
3 2 686926217
Sample Output 1
1
The only such array is [2, 2, 2], for which exists a permutation [1, 2, 3].
Sample Input 2
4 3 354817471
Sample Output 2
9
There are 9 such arrays: [2, 2, 2, 2], [2, 2, 2, 3], [2, 2, 3, 2], [2, 2, 3, 3], [2, 3, 2, 2], [2, 3, 3, 2], [3, 2, 2, 2], [3, 3, 2, 2], [3, 3, 3, 3].
Sample Input 3
5 2 829412599
Sample Output 3
1
The only such array is [2, 2, 2, 2, 2].
Sample Input 4
5 3 975576997
Sample Output 4
23
Sample Input 5
69 42 925171057
Sample Output 5
801835311
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 1200 点
問題文
A
, B
, C
をそれぞれちょうど N 個ずつ含む長さ 3N の文字列 T が次の条件を満たすとき、T を良い文字列と呼びます: T を N 個の長さ 3 の(連続とは限らない)部分列に分解する方法であって、各部分列が ABC
, BCA
, CAB
のいずれかであるような方法が存在する。
A
, B
, C
, ?
からなる長さ 3N の文字列 S が与えられます。各 ?
を A
, B
, C
のいずれかに置き換える方法であって、結果が良い文字列となるようなものの個数を求めてください。この個数は非常に大きい可能性があるため、これを 998244353 で割った余りを出力してください。
制約
- 1\le N \le 15
- S は、
A
,B
,C
,?
からなる長さ 3N の文字列である。
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを 998244353 で割った余りを出力せよ。
入力例 1
1 ???
出力例 1
3
得られる良い文字列は、ABC
, BCA
, CAB
の 3 個です。
入力例 2
2 AA????
出力例 2
2
得られる良い文字列は、AABBCC
, AABCBC
の 2 個です。
入力例 3
3 ?A?A?A?A?
出力例 3
0
A
が既に 4 個含まれるため、良い文字列を得ることはできません。
入力例 4
9 ?????????A??B??C???????????
出力例 4
331653164
Score : 1200 points
Problem Statement
Let's call a string T of length 3N, containing exactly N letters A
, N letters B
, and N letters C
, good, if it can be split into N (not necessarily contiguous) subsequences of length 3, so that the letters from each subsequence form ABC
, BCA
, or CAB
.
You are given a string S of length 3N, consisting of characters A
, B
, C
, and ?
.
Count the number of ways to replace each ?
with one of A
, B
, and C
, so that the resulting string is good. As this number can be very large, output it modulo 998244353.
Constraints
- 1\le N \le 15.
- S is a string of length 3N, consisting of
A
,B
,C
, and?
.
Input
Input is given from Standard Input in the following format:
N S
Output
Print the answer modulo 998244353.
Sample Input 1
1 ???
Sample Output 1
3
There are 3 good strings you can obtain: ABC
, BCA
, and CAB
.
Sample Input 2
2 AA????
Sample Output 2
2
There are 2 good strings you can obtain: AABBCC
and AABCBC
.
Sample Input 3
3 ?A?A?A?A?
Sample Output 3
0
It's impossible to obtain a good string, as there are already 4 A
's.
Sample Input 4
9 ?????????A??B??C???????????
Sample Output 4
331653164
Time Limit: 3 sec / Memory Limit: 1024 MB
配点 : 1500 点
問題文
N 個の集合 S_1, S_2, \ldots, S_N があります。はじめ、1 から N までの各 i について、集合 S_i は整数 i のみを含みます(すなわち、S_i = \{i\} です)。
あなたは、次の操作を行うことができます。
- 1 \le i \le N-1 であるような i を任意に選び、U = S_i \cup S_{i+1}(S_i と S_{i+1} の和集合)とする。 そして、S_i, S_{i+1} をともに U で置き換える。
あなたの目標は、有限回の操作を行い(0 回でもよい)、1 から N までの全ての i について S_i = \{L_i, L_i+1, \ldots, R_i-1, R_i\} であるような状態に至ることです。これは可能でしょうか。可能であるなら、それに必要な最小の操作回数も求めてください。
制約
- 1 \le N \le 5\cdot 10^5
- 1 \le L_i \le R_i \le N
入力
入力は以下の形式で標準入力から与えられる。
N L_1 R_1 L_2 R_2 \vdots L_N R_N
出力
目標状態が到達可能なら、到達に必要な最小の操作回数を出力せよ。そうでなければ、-1
を出力せよ。
入力例 1
3 1 2 1 2 1 3
出力例 1
-1
目標状態が到達不能であることを証明できます。
入力例 2
4 1 3 1 4 1 4 2 4
出力例 2
4
以下のように操作を行うことで到達可能です。
- i = 2 を選び、S_1 = \{1\}, S_2 = \{2, 3\}, S_3 = \{2, 3\}, S_4 = \{4\} とする
- i = 1 を選び、S_1 = \{1, 2, 3\}, S_2 = \{1, 2, 3\}, S_3 = \{2, 3\}, S_4 = \{4\} とする
- i = 3 を選び、S_1 = \{1, 2, 3\}, S_2 = \{1, 2, 3\}, S_3 = \{2, 3, 4\}, S_4 = \{2, 3, 4\} とする
- i = 2 を選び、S_1 = \{1, 2, 3\}, S_2 = \{1, 2, 3, 4\}, S_3 = \{1, 2, 3, 4\}, S_4 = \{2, 3, 4\} とする
Score : 1500 points
Problem Statement
You have N sets S_1, S_2, \ldots, S_N. Initially, for each i from 1 to N, set S_i contains only integer i (that is, S_i = \{i\}).
You are allowed to perform the following operation:
- Choose any i with 1 \le i \le N-1. Let U = S_i \cup S_{i+1} ー the union of sets S_i and S_{i+1}. Then, replace both S_i and S_{i+1} with U.
You want to perform a finite number of operations above (maybe zero), to get a configuration, in which S_i = \{L_i, L_i+1, \ldots, R_i-1, R_i\} for all i from 1 to N. Is it possible to reach this configuration? If it is possible, also find the smallest number of operations needed to reach it.
Constraints
- 1 \le N \le 5\cdot 10^5
- 1 \le L_i \le R_i \le N
Input
Input is given from Standard Input in the following format:
N L_1 R_1 L_2 R_2 \vdots L_N R_N
Output
If it is possible to reach the required configuration, output the smallest number of operations needed to do it. Otherwise, output -1
.
Sample Input 1
3 1 2 1 2 1 3
Sample Output 1
-1
It can be proved that it's impossible to obtain the required configuration.
Sample Input 2
4 1 3 1 4 1 4 2 4
Sample Output 2
4
We can perform the following sequence of operations:
- Choose i = 2, get S_1 = \{1\}, S_2 = \{2, 3\}, S_3 = \{2, 3\}, S_4 = \{4\}
- Choose i = 1, get S_1 = \{1, 2, 3\}, S_2 = \{1, 2, 3\}, S_3 = \{2, 3\}, S_4 = \{4\}
- Choose i = 3, get S_1 = \{1, 2, 3\}, S_2 = \{1, 2, 3\}, S_3 = \{2, 3, 4\}, S_4 = \{2, 3, 4\}
- Choose i = 2, get S_1 = \{1, 2, 3\}, S_2 = \{1, 2, 3, 4\}, S_3 = \{1, 2, 3, 4\}, S_4 = \{2, 3, 4\}
Time Limit: 4 sec / Memory Limit: 1024 MB
配点 : 1800 点
問題文
整数 N, K が与えられます。
長さ K の整数列 a=[a_1, a_2, \ldots, a_K] が全ての 1 \le i \le K について 1 \le a_i \le i を満たすとき、a を良い列と呼びます。
長さ NK の整数列 b=[b_1, b_2, \ldots, b_{NK}] が次の条件を満たすとき、b を素晴らしい列と呼びます: b を N 個の長さ K の(連続とは限らない)部分列に分解する方法であって、各部分列が良い列であるような方法が存在する。
f(pos, val) を、b_{pos}=val であるような素晴らしい列 b の個数と定義します。
全ての 1\le pos \le NK, 1 \le val \le K について f(pos, val) を求めてください。これらの数値は非常に大きい可能性があるため、これらを素数 P で割った余りを出力してください。
制約
- 1 \le N \le 20
- 1 \le K \le 20
- 10^8 \le P \le 10^9
- P は素数である。
入力
入力は以下の形式で標準入力から与えられる。
N K P
出力
NK 行出力せよ。i 行目の j 個目の数値が (f(i, j) \bmod P) となるようにすること。
入力例 1
2 2 965166677
出力例 1
6 0 4 2 4 2 3 3
以下の 6 個の素晴らしい列が存在します。
- [1, 1, 1, 1]: [b_1, b_2], [b_3, b_4] に分割できます。
- [1, 1, 1, 2]: [b_1, b_2], [b_3, b_4] に分割できます。
- [1, 2, 1, 1]: [b_1, b_2], [b_3, b_4] に分割できます。
- [1, 2, 1, 2]: [b_1, b_2], [b_3, b_4] に分割できます。
- [1, 1, 2, 1]: [b_1, b_3], [b_2, b_4] に分割できます。
- [1, 1, 2, 2]: [b_1, b_3], [b_2, b_4] に分割できます。
Score : 1800 points
Problem Statement
You are given integers N and K.
Let's call an integer array a=[a_1, a_2, \ldots, a_K] of length K good, if 1 \le a_i \le i for all 1 \le i \le K.
Let's call an integer array b=[b_1, b_2, \ldots, b_{NK}] of length NK amazing, if it can be split into N (not necessarily contiguous) subsequences of length K, each of which is good.
Let's define f(pos, val) as the number of amazing sequences b such that b_{pos}=val.
Find f(pos, val) for all 1\le pos \le NK, 1 \le val \le K. As these numbers can be very big, output them modulo some prime P.
Constraints
- 1 \le N \le 20.
- 1 \le K \le 20.
- 10^8 \le P \le 10^9
- P is a prime.
Input
Input is given from Standard Input in the following format:
N K P
Output
Output NK lines. The j-th number in the i-th line should be equal to (f(i, j) \bmod P).
Sample Input 1
2 2 965166677
Sample Output 1
6 0 4 2 4 2 3 3
There exist 6 amazing arrays:
- [1, 1, 1, 1] ー can be split into [b_1, b_2], [b_3, b_4].
- [1, 1, 1, 2] ー can be split into [b_1, b_2], [b_3, b_4].
- [1, 2, 1, 1] ー can be split into [b_1, b_2], [b_3, b_4].
- [1, 2, 1, 2] ー can be split into [b_1, b_2], [b_3, b_4].
- [1, 1, 2, 1] ー can be split into [b_1, b_3], [b_2, b_4].
- [1, 1, 2, 2] ー can be split into [b_1, b_3], [b_2, b_4].