Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の整数からなる数列 A=(A_1,\ldots,A_N) であって、以下の条件を全て満たすものは何通りありますか?
-
1\le A_i \le M (1 \le i \le N)
-
\displaystyle\sum _{i=1}^N A_i \leq K
ただし、答えは非常に大きくなることがあるので、答えを 998244353 で割った余りを求めてください。
制約
- 1 \leq N, M \leq 50
- N \leq K \leq NM
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K
出力
答えを 998244353 で割った余りを出力せよ。
入力例 1
2 3 4
出力例 1
6
条件を満たす数列は以下の 6 つです。
- (1,1)
- (1,2)
- (1,3)
- (2,1)
- (2,2)
- (3,1)
入力例 2
31 41 592
出力例 2
798416518
答えを 998244353 で割った余りを出力してください。
Score : 300 points
Problem Statement
How many integer sequences of length N, A=(A_1, \ldots, A_N), satisfy all of the conditions below?
-
1\le A_i \le M (1 \le i \le N)
-
\displaystyle\sum _{i=1}^N A_i \leq K
Since the count can get enormous, find it modulo 998244353.
Constraints
- 1 \leq N, M \leq 50
- N \leq K \leq NM
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M K
Output
Print the answer.
Sample Input 1
2 3 4
Sample Output 1
6
The following six sequences satisfy the conditions.
- (1,1)
- (1,2)
- (1,3)
- (2,1)
- (2,2)
- (3,1)
Sample Input 2
31 41 592
Sample Output 2
798416518
Be sure to print the count modulo 998244353.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
0, 1 のみからなる長さ N の文字列 S が与えられます。
S の中で先頭から K 番目の 1 の塊を K-1 番目の 1 の塊の直後まで移動した文字列を出力してください。
なお、S には 1 の塊が K 個以上含まれることが保証されます。
より正確な説明は以下の通りです。
- S の l 文字目から r 文字目までからなる部分文字列を S_{l\ldots r} と表す。
- S の部分文字列 S_{l\ldots r} が
1の塊であるとは、以下の条件を全て満たすことと定める。- S_l=S_{l+1}=\cdots=S_r=
1 - l=1 または S_{l-1}=
0 - r=N または S_{r+1}=
0
- S_l=S_{l+1}=\cdots=S_r=
- S に含まれる
1の塊が S_{l_1\ldots r_1},\ldots,S_{l_m\ldots r_m} で全てであり、l_1 < \cdots < l_m を満たしているとする。
このとき、以下で定義される長さ N の文字列 T を、「S の中で先頭から K 番目の1の塊を K-1 番目の1の塊の直後まで移動した文字列」と定める- 1 \leq i \leq r_{K-1} のとき T_i = S_i
- r_{K-1}+1 \leq i \leq r_{K-1}+(r_K-l_K)+1 のとき T_i=
1 - r_{K-1}+(r_K-l_K)+2 \leq i \leq r_K のとき T_i=
0 - r_K+1 \leq i \leq N のとき T_i=S_i
制約
- 1 \leq N \leq 5\times 10^5
- S は
0,1のみからなる長さ N の文字列 - 2 \leq K
- S には
1の塊が K 個以上含まれる
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
答えを出力せよ。
入力例 1
15 3 010011100011001
出力例 1
010011111000001
S には、2 文字目から 2 文字目、5 文字目から 7 文字目、11 文字目から 12 文字目、15 文字目から 15 文字目の 4 つの 1 の塊があります。
入力例 2
10 2 1011111111
出力例 2
1111111110
Score : 300 points
Problem Statement
You are given a string S of length N consisting of 0 and 1.
Move the K-th 1-block from the beginning in S to immediately after the (K-1)-th 1-block, and print the resulting string.
It is guaranteed that S contains at least K 1-blocks.
Here is a more precise description.
- Let S_{l\ldots r} denote the substring of S from the l-th character through the r-th character.
- We define a substring S_{l\ldots r} of S to be a
1-block if it satisfies all of the following conditions:- S_l = S_{l+1} = \cdots = S_r =
1 - l = 1 or S_{l-1} =
0 - r = N or S_{r+1} =
0
- S_l = S_{l+1} = \cdots = S_r =
-
Suppose that all
1-blocks in S are S_{l_1\ldots r_1}, \ldots, S_{l_m\ldots r_m}, where l_1 < l_2 < \cdots < l_m.Then, we define the length N string T, obtained by moving the K-th
1-block to immediately after the (K-1)-th1-block, as follows:- T_i = S_i for 1 \leq i \leq r_{K-1}
- T_i =
1for r_{K-1} + 1 \leq i \leq r_{K-1} + (r_K - l_K) + 1 - T_i =
0for r_{K-1} + (r_K - l_K) + 2 \leq i \leq r_K - T_i = S_i for r_K + 1 \leq i \leq N
Constraints
- 1 \leq N \leq 5 \times 10^5
- S is a string of length N consisting of
0and1. - 2 \leq K
- S contains at least K
1-blocks.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the answer.
Sample Input 1
15 3 010011100011001
Sample Output 1
010011111000001
S has four 1-blocks: from the 2nd to the 2nd character, from the 5th to the 7th character, from the 11th to the 12th character, and from the 15th to the 15th character.
Sample Input 2
10 2 1011111111
Sample Output 2
1111111110
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は何も書かれていないたくさんのボールと 1 つの袋を持っています。 最初、袋は空で、高橋君は Q 回の操作を行います。 それぞれの操作は以下の 3 種類のうちのいずれかです。
- 操作 1 : まだ何も書かれていないボール 1 つに整数 X_i を書き込み、袋に入れる。
- 操作 2 : 袋に入っているすべてのボールについて、そこに書かれている数を、それに X_i を加えたものに書き換える。
- 操作 3 : 袋に入っているボールのうち書かれている数が最小のもの(複数ある場合はそのうちの 1 つ)を取り出し、そこに書かれている数を記録する。その後、そのボールを捨てる。
1\leq i\leq Q について i 回目の操作の種類 P_i および操作 1 , 2 における X_i の値が与えられるので、操作 3 において記録された数を順に出力してください。
制約
- 1 \leq Q \leq 2\times 10^5
- 1 \leq P_i \leq 3
- 1 \leq X_i \leq 10^9
- 入力は全て整数である。
- P_i=3 であるような i が 1 つ以上存在する。
- P_i=3 であるとき、 i 回目の操作の直前の時点で、袋には 1 つ以上のボールが入っている。
入力
入力は以下の形式で標準入力から与えられる。
Q query_1 query_2 : query_Q
2 行目から Q+1 行目の各 query_i は次のいずれかの形で与えられる。
1 X_i
2 X_i
3
まず、1\leq P_i\leq 3 が与えられる。これは操作の種類を表す。 P_i=1 または P_i=2 ならば、その後に空白区切りで X_i が与えられる。
出力
Q 回の操作のうち操作の種類が P_i=3 であるような各操作について、記録された数を改行区切りで出力せよ。
入力例 1
5 1 3 1 5 3 2 2 3
出力例 1
3 7
高橋君は次のように操作を行います。
- 3 の書かれたボールを袋に入れる。
- 5 の書かれたボールを袋に入れる。
- 今、袋には 3 の書かれたボールと 5 の書かれたボールが入っているため、このうち小さい 3 の書かれたボールを取り出し、 3 を記録した後に捨てる。
- 今、袋には 5 の書かれたボールのみが入っているため、この数を 5+2=7 に書き換える。
- 今、袋には 7 の書かれたボールのみが入っているため、このボールを取り出し、 7 を記録した後に捨てる。
よって、記録された順に 3 , 7 を出力します。
入力例 2
6 1 1000000000 2 1000000000 2 1000000000 2 1000000000 2 1000000000 3
出力例 2
5000000000
答えが 32 bit整数に収まらないことがある事に注意してください。
Score : 400 points
Problem Statement
Takahashi has many balls, on which nothing is written, and one bag. Initially, the bag is empty. Takahashi will do Q operations, each of which is of one of the following three types.
- Type 1: Write an integer X_i on a blank ball and put it in the bag.
- Type 2: For each ball in the bag, replace the integer written on it with that integer plus X_i.
- Type 3: Pick up the ball with the smallest integer in the bag (if there are multiple such balls, pick up one of them). Record the integer written on this ball and throw it away.
For each 1\leq i\leq Q, you are given the type P_i of the i-th operation and the value of X_i if the operation is of Type 1 or 2. Print the integers recorded in the operations of Type 3 in order.
Constraints
- 1 \leq Q \leq 2\times 10^5
- 1 \leq P_i \leq 3
- 1 \leq X_i \leq 10^9
- All values in input are integers.
- There is one or more i such that P_i=3.
- If P_i=3, the bag contains at least one ball just before the i-th operation.
Input
Input is given from Standard Input in the following format:
Q query_1 query_2 : query_Q
Each query_i in the 2-nd through (Q+1)-th lines is in the following format:
1 X_i
2 X_i
3
The first number in each line is 1\leq P_i\leq 3, representing the type of the operation. If P_i=1 or P_i=2, it is followed by a space, and then by X_i.
Output
For each operation with P_i=3 among the Q operations, print the recorded integer in its own line.
Sample Input 1
5 1 3 1 5 3 2 2 3
Sample Output 1
3 7
Takahashi will do the following operations.
- Write 3 on a ball and put it in the bag.
- Write 5 on a ball and put it in the bag.
- The bag now contains a ball with 3 and another with 5. Pick up the ball with the smaller of them, or 3. Record 3 and throw it away.
- The bag now contains just a ball with 5. Replace this integer with 5+2=7.
- The bag now contains just a ball with 7. Pick up this ball, record 7, and throw it away.
Therefore, we should print 3 and 7, in the order they are recorded.
Sample Input 2
6 1 1000000000 2 1000000000 2 1000000000 2 1000000000 2 1000000000 3
Sample Output 2
5000000000
Note that the outputs may not fit into a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
頂点に 1 から N の番号がついた N 頂点 N 辺の有向グラフがあります。
全ての頂点の出次数は 1 で、頂点 i から出ている辺は頂点 a_i へ伸びています。
頂点の組 (u, v) であって、頂点 u から頂点 v へ到達可能であるものの個数を求めてください。
ここで、頂点 u から頂点 v へ到達可能であるとは、ある長さ K+1 の頂点の列 w_0, w_1, \dots, w_K であって次の条件を全て満たすものが存在することをいいます。特に、u = v の時は常に到達可能です。
- w_0 = u
- w_K = v
- 0 \leq i \lt K を満たす全ての i について、頂点 w_i から頂点 w_{i+1} へ伸びる辺が存在する。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq a_i \leq N
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 a_2 \dots a_N
出力
頂点の組 (u, v) であって、頂点 u から頂点 v へ到達可能であるものの個数を出力せよ。
入力例 1
4 2 1 1 4
出力例 1
8
頂点 1 から到達可能である頂点は頂点 1, 2 です。
頂点 2 から到達可能である頂点は頂点 1, 2 です。
頂点 3 から到達可能である頂点は頂点 1, 2, 3 です。
頂点 4 から到達可能である頂点は頂点 4 のみです。
よって、頂点の組 (u, v) であって頂点 u から頂点 v へ到達可能であるものの個数は 8 個です。
頂点 4 から出ている辺は自己ループ、すなわち頂点 4 自身へ伸びている辺である点に注意してください。
入力例 2
5 2 4 3 1 2
出力例 2
14
入力例 3
10 6 10 4 1 5 9 8 6 5 1
出力例 3
41
Score : 450 points
Problem Statement
There is a directed graph with N vertices numbered 1 to N and N edges.
The out-degree of every vertex is 1, and the edge from vertex i points to vertex a_i.
Count the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u.
Here, vertex v is reachable from vertex u if there exists a sequence of vertices w_0, w_1, \dots, w_K of length K+1 that satisfies the following conditions. In particular, if u = v, it is always reachable.
- w_0 = u.
- w_K = v.
- For every 0 \leq i \lt K, there is an edge from vertex w_i to vertex w_{i+1}.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq a_i \leq N
- 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 number of pairs of vertices (u, v) such that vertex v is reachable from vertex u.
Sample Input 1
4 2 1 1 4
Sample Output 1
8
The vertices reachable from vertex 1 are vertices 1, 2.
The vertices reachable from vertex 2 are vertices 1, 2.
The vertices reachable from vertex 3 are vertices 1, 2, 3.
The vertex reachable from vertex 4 is vertex 4.
Therefore, the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u is 8.
Note that the edge from vertex 4 is a self-loop, that is, it points to vertex 4 itself.
Sample Input 2
5 2 4 3 1 2
Sample Output 2
14
Sample Input 3
10 6 10 4 1 5 9 8 6 5 1
Sample Output 3
41
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
英大文字・英小文字からなる長さ 4 の文字列で、以下の 2 条件をともに満たすものを DDoS 型文字列と呼ぶことにします。
- 1,2,4 文字目が英大文字で、3 文字目が英小文字である
- 1,2 文字目が等しい
例えば DDoS, AAaA は DDoS 型文字列であり、ddos, IPoE は DDoS 型文字列ではありません。
英大文字・英小文字および ? からなる文字列 S が与えられます。
S に含まれる ? を独立に英大文字・英小文字に置き換えてできる文字列は、S に含まれる ? の個数を q として 52^q 通りあります。
このうち DDoS 型文字列を部分列に含まないものの個数を 998244353 で割ったあまりを求めてください。
注記
文字列の部分列とは、文字列から 0 個以上の文字を取り除いた後、残りの文字を元の順序で連結して得られる文字列のことをいいます。
例えば、AC は ABC の部分列であり、RE は ECR の部分列ではありません。
制約
- S は英大文字・英小文字および
?からなる - S の長さは 4 以上 3\times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
DD??S
出力例 1
676
? の少なくとも一方が英小文字のとき、DDoS 型文字列を部分列に含みます。
入力例 2
????????????????????????????????????????
出力例 2
858572093
998244353 で割ったあまりを求めてください。
入力例 3
?D??S
出力例 3
136604
Score : 500 points
Problem Statement
A DDoS-type string is a string of length 4 consisting of uppercase and lowercase English letters satisfying both of the following conditions.
- The first, second, and fourth characters are uppercase English letters, and the third character is a lowercase English letter.
- The first and second characters are equal.
For instance, DDoS and AAaA are DDoS-type strings, while neither ddos nor IPoE is.
You are given a string S consisting of uppercase and lowercase English letters and ?.
Let q be the number of occurrences of ? in S. There are 52^q strings that can be obtained by independently replacing each ? in S with an uppercase or lowercase English letter.
Among these strings, find the number of ones that do not contain a DDoS-type string as a subsequence, modulo 998244353.
Notes
A subsequence of a string is a string obtained by removing zero or more characters from the string and concatenating the remaining characters without changing the order.
For instance, AC is a subsequence of ABC, while RE is not a subsequence of ECR.
Constraints
- S consists of uppercase English letters, lowercase English letters, and
?. - The length of S is between 4 and 3\times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
DD??S
Sample Output 1
676
When at least one of the ?s is replaced with a lowercase English letter, the resulting string will contain a DDoS-type string as a subsequence.
Sample Input 2
????????????????????????????????????????
Sample Output 2
858572093
Find the count modulo 998244353.
Sample Input 3
?D??S
Sample Output 3
136604