実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N かつ全ての要素が 1 以上 M 以下である整数列のうち、狭義単調増加であるものを全て辞書順に出力してください。
注記
ある 2 個の異なる長さの等しい整数列 A_1,A_2,\dots,A_N と B_1,B_2,\dots,B_N が以下を満たすとき、またその時に限り辞書順で A が B より早いと定義されます。
- ある整数 i(1 \le i \le N) が存在し、1 \le j < i である全ての整数 j に対し A_j=B_j が成り立ち、かつ A_i < B_i が成り立つ。
ある整数列 A_1,A_2,\dots,A_N は以下を満たすとき、またその時に限り狭義単調増加です。
- 全ての整数 i(1 \le i \le N-1) に対し A_i < A_{i+1} が成り立つ。
制約
- 1 \le N \le M \le 10
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
条件を満たす整数列を一行に一つずつ、辞書順に出力せよ(出力例を参考にせよ)。
入力例 1
2 3
出力例 1
1 2 1 3 2 3
条件を満たす数列は (1,2),(1,3),(2,3) の 3 個です。これらを辞書順で早い方から出力します。
入力例 2
3 5
出力例 2
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
Score : 300 points
Problem Statement
Print all strictly increasing integer sequences of length N where all elements are between 1 and M (inclusive), in lexicographically ascending order.
Notes
For two integer sequences of the same length A_1,A_2,\dots,A_N and B_1,B_2,\dots,B_N, A is said to be lexicographically earlier than B if and only if:
- there is an integer i (1 \le i \le N) such that A_j=B_j for all integers j satisfying 1 \le j < i, and A_i < B_i.
An integer sequence A_1,A_2,\dots,A_N is said to be strictly increasing if and only if:
- A_i < A_{i+1} for all integers i (1 \le i \le N-1).
Constraints
- 1 \le N \le M \le 10
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the sought sequences in lexicographically ascending order, each in its own line (see Sample Outputs).
Sample Input 1
2 3
Sample Output 1
1 2 1 3 2 3
The sought sequences are (1,2),(1,3),(2,3), which should be printed in lexicographically ascending order.
Sample Input 2
3 5
Sample Output 2
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
実行時間制限: 2 sec / メモリ制限: 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
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
長さ N の英小文字からなる文字列 S=S_1S_2\dots S_N が与えられます。あなたは、S に対して以下の操作をちょうど 1 回行います。
- S の長さ 1 以上の連続部分文字列を選んで、左に 1 だけ巡回シフトする。すなわち、整数 1 \leq \ell \leq r \leq N を選んで、S の r 文字目の右に S_\ell を挿入したのち、S の \ell 番目の文字を削除する。
操作後の S としてありえる文字列のうち、辞書順最小のものを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えてください。
制約
- 1 \leq T \leq 10^5
- 1 \leq N \leq 10^5
- S は英小文字からなる長さ N の文字列
- T,N は整数
- 1 つの入力ファイルに含まれる N の総和は 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケース \mathrm{case}_i\;(1 \leq i \leq T) は以下の形式で与えられる。
N S
出力
T 行出力せよ。i \; (1 \leq i \leq T) 行目には、\mathrm{case}_i に対する答えを出力せよ。
入力例 1
3 7 atcoder 1 x 5 snuke
出力例 1
acodert x nsuke
- 1 番目のテストケースでは、2 文字目から 7 文字目を巡回シフトして
acodertとするのが辞書順最小です。 - 2 番目のテストケースでは、どのように操作しても
xが得られます。 - 3 番目のテストケースでは、1 文字目から 2 文字目を巡回シフトして
nsukeとするのが辞書順最小です。
Score : 400 points
Problem Statement
You are given a string S=S_1S_2\dots S_N of length N consisting of lowercase English letters. You will perform the following operation on S exactly once:
- Choose a contiguous substring of S with length at least 1 and cyclically shift it to the left by 1. That is, choose integers 1 \leq \ell \leq r \leq N, insert S_\ell to the right of the r-th character of S, and then delete the \ell-th character of S.
Find the lexicographically smallest string among all possible strings that S can become after the operation.
You are given T test cases, so solve each of them.
Constraints
- 1 \leq T \leq 10^5
- 1 \leq N \leq 10^5
- S is a string of length N consisting of lowercase English letters.
- T and N are integers.
- The sum of N over all test cases in a single input file is at most 10^5.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case \mathrm{case}_i\;(1 \leq i \leq T) is given in the following format:
N S
Output
Output T lines. The i-th (1 \leq i \leq T) line should contain the answer for \mathrm{case}_i.
Sample Input 1
3 7 atcoder 1 x 5 snuke
Sample Output 1
acodert x nsuke
- In the first test case, cyclically shifting from the 2nd to the 7th character gives
acodert, which is lexicographically smallest. - In the second test case, no matter how you operate, you get
x. - In the third test case, cyclically shifting from the 1st to the 2nd character gives
nsuke, which is lexicographically smallest.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
1 以上 N-1 以下の整数からなる長さ M の数列 A=(A_1,A_2,\dots,A_M) が与えられます。 i=1,2,\dots,M について、以下の質問に答えてください。
- 数列 B=(B_1,B_2,\dots,B_N) がある。最初、各 j について B_j=j である。今から、k=1,2,\dots,i-1,i+1,\dots,M の順に以下の操作を行う
(すなわち、i を除いた 1 以上 M 以下の整数 k について、昇順に以下の操作を行う)。
- B_{A_k} と B_{A_k+1} の値を入れ替える。
- 全ての操作が終了した段階で、B_j=1 を満たす j の値を S_i と定義する。S_i を求めよ。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1 \leq A_i \leq N-1\ (1\leq i \leq M)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_M
出力
M 行出力せよ。 i\ (1\leq i \leq M) 行目には、S_i の値を整数として出力せよ。
入力例 1
5 4 1 2 3 2
出力例 1
1 3 2 4
i = 2 のとき、操作によって B は以下のように変化します。
- 最初、B = (1,2,3,4,5)
- k=1 として操作を行う。すなわち B_1 と B_2 の値を入れ替えて、B = (2,1,3,4,5)
- k=3 として操作を行う。すなわち B_3 と B_4 の値を入れ替えて、B = (2,1,4,3,5)
- k=4 として操作を行う。すなわち B_2 と B_3 の値を入れ替えて、B = (2,4,1,3,5)
全ての操作が終了した段階で B_3=1 であるため、S_2 = 3 です。
同様に、
- i=1 のとき:k=2,3,4 の順に操作を行うと B=(1,4,3,2,5) になるので、S_1=1
- i=3 のとき:k=1,2,4 の順に操作を行うと B=(2,1,3,4,5) になるので、S_3=2
- i=4 のとき:k=1,2,3 の順に操作を行うと B=(2,3,4,1,5) になるので、S_4=4
です。
入力例 2
3 3 2 2 2
出力例 2
1 1 1
入力例 3
10 10 1 1 1 9 4 4 2 1 3 3
出力例 3
2 2 2 3 3 3 1 3 4 4
Score : 500 points
Problem Statement
You are given a sequence of length M consisting of integers between 1 and N-1, inclusive: A=(A_1,A_2,\dots,A_M). Answer the following question for i=1, 2, \dots, M.
- There is a sequence B=(B_1,B_2,\dots,B_N). Initially, we have B_j=j for each j. Let us perform the following operation for k=1, 2, \dots, i-1, i+1, \dots, M in this order (in other words, for integers k between 1 and M except i in ascending order).
- Swap the values of B_{A_k} and B_{A_k+1}.
- After all the operations, let S_i be the value of j such that B_j=1. Find S_i.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1 \leq A_i \leq N-1\ (1\leq i \leq M)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_M
Output
Print M lines. The i-th line (1\leq i \leq M) should contain the value S_i as an integer.
Sample Input 1
5 4 1 2 3 2
Sample Output 1
1 3 2 4
For i = 2, the operations change B as follows.
- Initially, B = (1,2,3,4,5).
- Perform the operation for k=1. That is, swap the values of B_1 and B_2, making B = (2,1,3,4,5).
- Perform the operation for k=3. That is, swap the values of B_3 and B_4, making B = (2,1,4,3,5).
- Perform the operation for k=4. That is, swap the values of B_2 and B_3, making B = (2,4,1,3,5).
After all the operations, we have B_3=1, so S_2 = 3.
Similarly, we have the following.
- For i=1: performing the operation for k=2,3,4 in this order makes B=(1,4,3,2,5), so S_1=1.
- For i=3: performing the operation for k=1,2,4 in this order makes B=(2,1,3,4,5), so S_3=2.
- For i=4: performing the operation for k=1,2,3 in this order makes B=(2,3,4,1,5), so S_4=4.
Sample Input 2
3 3 2 2 2
Sample Output 2
1 1 1
Sample Input 3
10 10 1 1 1 9 4 4 2 1 3 3
Sample Output 3
2 2 2 3 3 3 1 3 4 4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点の木があります。頂点には 1 から N までの番号が付いており、i 番目の辺は頂点 a_i と頂点 b_i を結んでいます。
x=1,2,\ldots,N に対して次の問題を解いてください。
- 木の頂点の部分集合 V であって空でないものは 2^N-1 通り存在するが、そのうち V による誘導部分グラフの連結成分数が x であるようなものは何通りあるかを 998244353 で割った余りを求めよ。
誘導部分グラフとは
S をグラフ G の頂点の部分集合とします。このとき、G の S による誘導部分グラフとは、頂点集合が S で、辺集合が「G の辺であって両端が S に含まれるものすべて」であるようなグラフです。制約
- 1 \leq N \leq 5000
- 1 \leq a_i \lt b_i \leq N
- 与えられるグラフは木
入力
入力は以下の形式で標準入力から与えられる。
N
a_1 b_1
\vdots
a_{N-1} b_{N-1}
出力
N 行出力せよ。
i 行目には x=i に対する出力をせよ。
入力例 1
4 1 2 2 3 3 4
出力例 1
10 5 0 0
以下の 5 通りでは誘導部分グラフの連結成分数が 2、これら以外では 1 になります。
- V = \{1,2,4\}
- V = \{1,3\}
- V = \{1,3,4\}
- V = \{1,4\}
- V = \{2,4\}
入力例 2
2 1 2
出力例 2
3 0
入力例 3
10 3 4 3 6 6 9 1 3 2 4 5 6 6 10 1 8 5 7
出力例 3
140 281 352 195 52 3 0 0 0 0
Score : 500 points
Problem Statement
You are given a tree with N vertices. The vertices are numbered 1 through N, and the i-th edge connects vertex a_i and vertex b_i.
Solve the following problem for each x=1,2,\ldots,N:
- There are (2^N-1) non-empty subsets V of the tree's vertices. Find the number, modulo 998244353, of such V that the subgraph induced by V has exactly x connected components.
What is an induced subgraph?
Let S be the subset of the vertices of a graph G; then the subgraph of G induced by S is a graph whose vertex set is S and whose edge set consists of all edges of G whose both ends are contained in S.Constraints
- 1 \leq N \leq 5000
- 1 \leq a_i \lt b_i \leq N
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
N
a_1 b_1
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The i-th line should contain the answer for x=i.
Sample Input 1
4 1 2 2 3 3 4
Sample Output 1
10 5 0 0
The induced subgraph will have two connected components in the following five cases and one in the others.
- V = \{1,2,4\}
- V = \{1,3\}
- V = \{1,3,4\}
- V = \{1,4\}
- V = \{2,4\}
Sample Input 2
2 1 2
Sample Output 2
3 0
Sample Input 3
10 3 4 3 6 6 9 1 3 2 4 5 6 6 10 1 8 5 7
Sample Output 3
140 281 352 195 52 3 0 0 0 0