Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
整数 N が与えられるので、以下の条件を全て満たす整数 X の個数を 998244353 で割った余りを求めてください。
- N 桁の正整数である。
- X の各桁を上の位から順に X_1,X_2,\dots,X_N とする。このとき以下の条件を全て満たす。
- 全ての整数 1 \le i \le N に対して、 1 \le X_i \le 9
- 全ての整数 1 \le i \le N-1 に対して、 |X_i-X_{i+1}| \le 1
制約
- N は整数
- 2 \le N \le 10^6
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを整数として出力せよ。
入力例 1
4
出力例 1
203
4 桁の整数として、例えば 1111,1234,7878,6545 が問題文中の条件を満たします。
入力例 2
2
出力例 2
25
入力例 3
1000000
出力例 3
248860093
998244353 で割った余りを求めることに注意してください。
Score : 300 points
Problem Statement
Given an integer N, find the number of integers X that satisfy all of the following conditions, modulo 998244353.
- X is an N-digit positive integer.
- Let X_1,X_2,\dots,X_N be the digits of X from top to bottom. They satisfy all of the following:
- 1 \le X_i \le 9 for all integers 1 \le i \le N;
- |X_i-X_{i+1}| \le 1 for all integers 1 \le i \le N-1.
Constraints
- N is an integer.
- 2 \le N \le 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer as an integer.
Sample Input 1
4
Sample Output 1
203
Some of the 4-digit integers satisfying the conditions are 1111,1234,7878,6545.
Sample Input 2
2
Sample Output 2
25
Sample Input 3
1000000
Sample Output 3
248860093
Be sure to find the count modulo 998244353.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
AtCoder 王国を治める高橋君は、英小文字のアルファベット順を変更することにしました。
新たなアルファベット順はa
, b
, \ldots, z
を並べ替えて得られる文字列 X を用いて表されます。X の i \, (1 \leq i \leq 26) 文字目は、新たな順番において i 番目に小さい英小文字を表します。
AtCoder 王国には N 人の国民がおり、それぞれの国民の名前は S_1, S_2, \dots, S_N です。ここで、S_i \, (1 \leq i \leq N) は英小文字からなります。
これらの名前を、高橋君の定めたアルファベット順に基づく辞書順に従って並べ替えてください。
辞書順とは?
辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 S, T の大小を判定するアルゴリズムを以下に説明します。
以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。
- S, T のうち長さが大きくない方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
- S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、S_j が T_j よりアルファベット順で小さい場合は S \lt T 、そうでない場合は S \gt T と決定して、アルゴリズムを終了します。
- S_i \neq T_i である i が存在しない場合、S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。
制約
- X は
a
,b
, \ldots,z
を並べ替えて得られる - 2 \leq N \leq 50000
- N は整数
- 1 \leq |S_i| \leq 10 \, (1 \leq i \leq N)
- S_i は英小文字からなる (1 \leq i \leq N)
- S_i \neq S_j (1 \leq i \lt j \leq N)
入力
入力は以下の形式で標準入力から与えられる。
X N S_1 S_2 \vdots S_N
出力
N 行出力せよ。i \, (1 \leq i \leq N) 行目には、高橋君の定めたアルファベット順に基づく辞書順に従って国民の名前を並べ替えたとき、i 番目に小さいものを出力すること。
入力例 1
bacdefghijklmnopqrstuvwxzy 4 abx bzz bzy caa
出力例 1
bzz bzy abx caa
高橋君が新たに設定したアルファベット順において、b
は a
より小さく、z
は y
より小さいです。そのため、国民の名前を辞書順に並べ替えると、小さい順に bzz
, bzy
, abx
, caa
となります。
入力例 2
zyxwvutsrqponmlkjihgfedcba 5 a ab abc ac b
出力例 2
b a ac ab abc
Score : 300 points
Problem Statement
Takahashi, who governs the Kingdom of AtCoder, has decided to change the alphabetical order of English lowercase letters.
The new alphabetical order is represented by a string X, which is a permutation of a
, b
, \ldots, z
. The i-th character of X (1 \leq i \leq 26) would be the i-th smallest English lowercase letter in the new order.
The kingdom has N citizens, whose names are S_1, S_2, \dots, S_N, where each S_i (1 \leq i \leq N) consists of lowercase English letters.
Sort these names lexicographically according to the alphabetical order decided by Takahashi.
What is the lexicographical order?
Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.
Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.
- Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
- If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
- If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.
Constraints
- X is a permutation of
a
,b
, \ldots,z
. - 2 \leq N \leq 50000
- N is an integer.
- 1 \leq |S_i| \leq 10 \, (1 \leq i \leq N)
- S_i consists of lowercase English letters. (1 \leq i \leq N)
- S_i \neq S_j (1 \leq i \lt j \leq N)
Input
Input is given from Standard Input in the following format:
X N S_1 S_2 \vdots S_N
Output
Print N lines. The i-th line (1 \leq i \leq N) should contain the i-th smallest name when the citizens' names are sorted according to the alphabetical order decided by Takahashi.
Sample Input 1
bacdefghijklmnopqrstuvwxzy 4 abx bzz bzy caa
Sample Output 1
bzz bzy abx caa
In the new alphabetical order set by Takahashi, b
is smaller than a
and z
is smaller than y
. Thus, sorting the citizens' names lexicographically would result in bzz
, bzy
, abx
, caa
in ascending order.
Sample Input 2
zyxwvutsrqponmlkjihgfedcba 5 a ab abc ac b
Sample Output 2
b a ac ab abc
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
この問題は G 問題の部分問題です。
0
と 1
からなる空でない文字列 S が次の条件を満たす時、S を美しい文字列と呼びます。
- (条件) 次の一連の操作を S の長さが 1 になるまで行い、S に残った唯一の文字を
1
にすることができる。- 1 \leq i \leq |S| - 1 を満たす整数 i を自由に選ぶ。
- 整数 x を次のように定義する。
- S_i =
0
かつ S_{i+1}=0
である場合、x = 1 とする。 - S_i =
0
かつ S_{i+1}=1
である場合、x = 0 とする。 - S_i =
1
かつ S_{i+1}=0
である場合、x = 0 とする。 - S_i =
1
かつ S_{i+1}=1
である場合、x = 1 とする。
- S_i =
- S_i と S_{i+1} を取り除き、それらがあった場所に x を数字とみなしたものを 1 個挿入する。
例えば S=10101
に対して i=2 を選んで操作を行った場合、操作後の文字列は1001
になる。
0
と 1
からなる長さ N の文字列 T があります。
T の部分文字列である美しい文字列の個数を求めてください。ただし、2 つの部分文字列が文字列として同じでも、取り出す位置が異なるならば別々に数えます。
部分文字列とは
S の部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。例えば、
10
は 101
の部分文字列ですが、11
は 101
の部分文字列ではありません。
制約
- 1 \leq N \leq 2 \times 10^5
- N は整数
- T は
0
と1
からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N T
出力
T の部分文字列である美しい文字列の個数を出力せよ。
入力例 1
3 110
出力例 1
3
T の 1 文字目から 2 文字目までを取り出してできる文字列 11
は美しい文字列です。なぜならば、i=1 として操作を行うと、操作後の文字列は 1
になるからです。
T の部分文字列である美しい文字列は次の 3 個です。
- T の 1 文字目を取り出してできる文字列
1
- T の 2 文字目を取り出してできる文字列
1
- T の 1 文字目から 2 文字目までを取り出してできる文字列
11
入力例 2
4 0000
出力例 2
4
入力例 3
30 011011100101110111100010011010
出力例 3
225
Score : 425 points
Problem Statement
This problem is a subproblem of Problem G.
A non-empty string S consisting of 0
and 1
is called a beautiful string when it satisfies the following condition:
- (Condition) You can perform the following sequence of operations until the length of S becomes 1 and make the only character remaining in S be
1
.- Choose any integer i satisfying 1 \leq i \leq |S| - 1.
- Define an integer x as follows:
- If S_i =
0
and S_{i+1} =0
, let x = 1. - If S_i =
0
and S_{i+1} =1
, let x = 0. - If S_i =
1
and S_{i+1} =0
, let x = 0. - If S_i =
1
and S_{i+1} =1
, let x = 1.
- If S_i =
- Remove S_i and S_{i+1}, and insert the digit corresponding to x in their place.
For example, if S=10101
and you choose i=2, the string after the operation is1001
.
You are given a string T of length N consisting of 0
and 1
.
Find the number of beautiful strings that are substrings of T. Even if two substrings are identical as strings, count them separately if they are taken from different positions.
What are substrings?
A substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.For example,
10
is a substring of 101
, but 11
is not a substring of 101
.
Constraints
- 1 \leq N \leq 2 \times 10^5
- N is an integer.
- T is a string of length N consisting of
0
and1
.
Input
The input is given from Standard Input in the following format:
N T
Output
Print the number of beautiful strings that are substrings of T.
Sample Input 1
3 110
Sample Output 1
3
The string 11
obtained by taking the 1st through 2nd characters of T is a beautiful string, because if you choose i=1 and perform the operation, the string becomes 1
.
The beautiful strings that are substrings of T are the following three strings:
- The string
1
obtained by taking the 1st character of T. - The string
1
obtained by taking the 2nd character of T. - The string
11
obtained by taking the 1st through 2nd characters of T.
Sample Input 2
4 0000
Sample Output 2
4
Sample Input 3
30 011011100101110111100010011010
Sample Output 3
225
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
長さ N の数列 A = (A_1, A_2, \dots, A_N) および整数 K があります。
A をいくつかの連続部分列に分割する方法は 2^{N-1} 通りありますが、そのうち分割後に要素の和が K である列が存在しない分割の方法は何通りありますか。答えを 998244353 で割った余りを求めてください。
ここで、「A をいくつかの連続部分列に分割する」とは次の手順のことを言います。
- 分割後の数列の個数 k (1 \leq k \leq N) および 1 = i_1 \lt i_2 \lt \dots \lt i_k \lt i_{k+1} = N+1 を満たす整数列 (i_1, i_2, \dots, i_k, i_{k+1}) を自由に選ぶ。
- 1 \leq n \leq k について、n 番目の数列を、A の i_n 番目から i_{n+1} - 1 番目までの要素を順番を保ったまま取り出して出来る数列とする。
A = (1, 2, 3, 4, 5) のときの分割の例を以下に挙げます。
- (1, 2, 3), (4), (5)
- (1, 2), (3, 4, 5)
- (1, 2, 3, 4, 5)
制約
- 1 \leq N \leq 2 \times 10^5
- -10^{15} \leq K \leq 10^{15}
- -10^9 \leq A_i \leq 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを 998244353 で割った余りを出力せよ。
入力例 1
3 3 1 2 3
出力例 1
2
問題文の条件を満たす分割は次の 2 通りです。
- (1), (2, 3)
- (1, 2, 3)
入力例 2
5 0 0 0 0 0 0
出力例 2
0
入力例 3
10 5 -5 -1 -7 6 -6 -2 -5 10 2 -10
出力例 3
428
Score : 475 points
Problem Statement
You are given a sequence A = (A_1, A_2, \dots, A_N) of length N and an integer K.
There are 2^{N-1} ways to divide A into several contiguous subsequences. How many of these divisions have no subsequence whose elements sum to K? Find the count modulo 998244353.
Here, "to divide A into several contiguous subsequences" means the following procedure.
- Freely choose the number k (1 \leq k \leq N) of subsequences and an integer sequence (i_1, i_2, \dots, i_k, i_{k+1}) satisfying 1 = i_1 \lt i_2 \lt \dots \lt i_k \lt i_{k+1} = N+1.
- For each 1 \leq n \leq k, the n-th subsequence is formed by taking the i_n-th through (i_{n+1} - 1)-th elements of A, maintaining their order.
Here are some examples of divisions for A = (1, 2, 3, 4, 5):
- (1, 2, 3), (4), (5)
- (1, 2), (3, 4, 5)
- (1, 2, 3, 4, 5)
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^{15} \leq K \leq 10^{15}
- -10^9 \leq A_i \leq 10^9
- 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 count modulo 998244353.
Sample Input 1
3 3 1 2 3
Sample Output 1
2
There are two divisions that satisfy the condition in the problem statement:
- (1), (2, 3)
- (1, 2, 3)
Sample Input 2
5 0 0 0 0 0 0
Sample Output 2
0
Sample Input 3
10 5 -5 -1 -7 6 -6 -2 -5 10 2 -10
Sample Output 3
428
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
長さ N の英小文字からなる文字列 S があります。以下のクエリを全部で Q 個処理してください。
- タイプ 1 : S の i 文字目を x に変更する。
- タイプ 2 : 現時点の S の l 文字目から r 文字目までを抜き出した文字列を t とする。この文字列に対して次のように定義される f(t) を求めよ。
- f(t) は t 中の同じ文字の連続の最大長である。
- 厳密には、 1 \le a \le b \le |t| かつ t の a 文字目から b 文字目までが全て等しいような整数 a,b を選ぶとき、 f(t) は b-a+1 の値としてありうる最大値である。
- 例えば f(
aaaccbbbb
)=4 、 f(bbaaabb
)=3 、 f(x
)=1 である。
制約
- N は 1 以上 5 \times 10^5 以下の整数
- S は英小文字からなる長さ N の文字列
- Q は 1 以上 5 \times 10^5 以下の整数
- タイプ 1 のクエリは以下の制約を満たす
- i は 1 以上 N 以下の整数
- x は英小文字
- タイプ 2 のクエリは以下の制約を満たす
- l,r は 1 \le l \le r \le N を満たす整数
入力
入力は以下の形式で標準入力から与えられる。
N Q S \rm{Query}_1 \rm{Query}_2 \vdots \rm{Query}_Q
但し、 \rm{Query}_i は i 個目のクエリを表す。
タイプ 1 のクエリは以下の形式で与えられる。
1 i x
タイプ 2 のクエリは以下の形式で与えられる。
2 l r
出力
タイプ 2 のクエリが現れる度に、その解答を 1 行に出力せよ。
なお、入出力が大きくなる場合があるので、高速な方法で入出力を行うことを推奨する。
入力例 1
10 5 babaacczcc 2 1 4 1 3 a 2 1 10 1 8 c 2 1 10
出力例 1
1 4 5
この入力には 5 個のクエリが含まれます。
- はじめ、文字列 S は
babaacczcc
です。 - 1 番目のクエリはタイプ 2 で、 l=1,r=4 です。
- 抜き出した文字列は
baba
であり、 f(baba
)=1 です。
- 抜き出した文字列は
- 2 番目のクエリはタイプ 1 で、 i=3,x=
a
です。- 変更後の文字列 S=
baaaacczcc
です。
- 変更後の文字列 S=
- 3 番目のクエリはタイプ 2 で、 l=1,r=10 です。
- 抜き出した文字列は
baaaacczcc
であり、 f(baaaacczcc
)=4 です。
- 抜き出した文字列は
- 4 番目のクエリはタイプ 1 で、 i=8,x=
c
です。- 変更後の文字列 S=
baaaaccccc
です。
- 変更後の文字列 S=
- 5 番目のクエリはタイプ 2 で、 l=1,r=10 です。
- 抜き出した文字列は
baaaaccccc
であり、 f(baaaaccccc
)=5 です。
- 抜き出した文字列は
入力例 2
1 1 a 1 1 z
出力例 2
出力が空である場合もあります。
Score : 525 points
Problem Statement
There is a string S of length N consisting of lowercase English letters. Process a total of Q queries as follows:
- Type 1 : Change the i-th character of S to x.
- Type 2 : Let t be the substring of the current S from the l-th character through the r-th character. Find f(t) defined as follows for this string:
- f(t) is the maximum length of consecutive identical characters in t.
- More precisely, when choosing integers a,b such that 1 \le a \le b \le |t| and all characters from the a-th through the b-th character of t are equal, f(t) is the maximum possible value of b-a+1.
- For example, f(
aaaccbbbb
)=4, f(bbaaabb
)=3, f(x
)=1.
Constraints
- N is an integer between 1 and 5 \times 10^5, inclusive.
- S is a string of length N consisting of lowercase English letters.
- Q is an integer between 1 and 5 \times 10^5, inclusive.
- Type 1 queries satisfy the following constraints:
- i is an integer between 1 and N, inclusive.
- x is a lowercase English letter.
- Type 2 queries satisfy the following constraints:
- l,r are integers satisfying 1 \le l \le r \le N.
Input
The input is given from Standard Input in the following format:
N Q S \rm{Query}_1 \rm{Query}_2 \vdots \rm{Query}_Q
Here, \rm{Query}_i represents the i-th query.
Type 1 queries are given in the following format:
1 i x
Type 2 queries are given in the following format:
2 l r
Output
Every time a type 2 query appears, output the answer on one line.
The use of fast input and output methods is recommended because of potentially large input and output.
Sample Input 1
10 5 babaacczcc 2 1 4 1 3 a 2 1 10 1 8 c 2 1 10
Sample Output 1
1 4 5
This input contains five queries.
- Initially, the string S is
babaacczcc
. - The 1st query is type 2 with l=1,r=4.
- The extracted string is
baba
, and f(baba
)=1.
- The extracted string is
- The 2nd query is type 1 with i=3,x=
a
.- The string S after the change is
baaaacczcc
.
- The string S after the change is
- The 3rd query is type 2 with l=1,r=10.
- The extracted string is
baaaacczcc
, and f(baaaacczcc
)=4.
- The extracted string is
- The 4th query is type 1 with i=8,x=
c
.- The string S after the change is
baaaaccccc
.
- The string S after the change is
- The 5th query is type 2 with l=1,r=10.
- The extracted string is
baaaaccccc
, and f(baaaaccccc
)=5.
- The extracted string is
Sample Input 2
1 1 a 1 1 z
Sample Output 2
The output may be empty.