実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の狭義単調増加列 A=(A _ 1,A _ 2,\ldots,A _ N) と、長さ M の狭義単調増加列 B=(B _ 1,B _ 2,\ldots,B _ M) が与えられます。 ここで、すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j が成り立っています。
長さ N+M の狭義単調増加列 C=(C _ 1,C _ 2,\ldots,C _ {N+M}) を、次の操作を行った結果得られる列として定めます。
- C を A と B を連結した列とする。厳密には、i=1,2,\ldots,N について C _ i=A _ i とし、i=N+1,N+2,\ldots,N+M について C _ i=B _ {i-N} とする。
- C を昇順にソートする。
A _ 1,A _ 2,\ldots,A _ N と B _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるか求めてください。 より厳密には、まず i=1,2,\ldots,N について C _ k=A _ i を満たす k を順に求めたのち、j=1,2,\ldots,M について C _ k=B _ j を満たす k を順に求めてください。
制約
- 1\leq N,M\leq 10^5
- 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
- 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
- すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A _ 1 A _ 2 \ldots A _ N B _ 1 B _ 2 \ldots B _ M
出力
答えを 2 行で出力せよ。
1 行目には A _ 1,A _ 2,\ldots,A _ N がそれぞれ C では何番目にあるかを空白区切りで出力せよ。
2 行目には B _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるかを空白区切りで出力せよ。
入力例 1
4 3 3 14 15 92 6 53 58
出力例 1
1 3 4 7 2 5 6
C は (3,6,14,15,53,58,92) となります。 A=(3,14,15,92) の要素はそれぞれ 1,3,4,7 番目にあり、B=(6,53,58) の要素はそれぞれ 2,5,6 番目にあります。
入力例 2
4 4 1 2 3 4 100 200 300 400
出力例 2
1 2 3 4 5 6 7 8
入力例 3
8 12 3 4 10 15 17 18 22 30 5 7 11 13 14 16 19 21 23 24 27 28
出力例 3
1 2 5 9 11 12 15 20 3 4 6 7 8 10 13 14 16 17 18 19
Score : 300 points
Problem Statement
You are given strictly increasing sequences of length N and M: A=(A _ 1,A _ 2,\ldots,A _ N) and B=(B _ 1,B _ 2,\ldots,B _ M). Here, A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M).
Let C=(C _ 1,C _ 2,\ldots,C _ {N+M}) be a strictly increasing sequence of length N+M that results from the following procedure.
- Let C be the concatenation of A and B. Formally, let C _ i=A _ i for i=1,2,\ldots,N, and C _ i=B _ {i-N} for i=N+1,N+2,\ldots,N+M.
- Sort C in ascending order.
For each of A _ 1,A _ 2,\ldots,A _ N, B _ 1,B _ 2,\ldots,B _ M, find its position in C. More formally, for each i=1,2,\ldots,N, find k such that C _ k=A _ i, and for each j=1,2,\ldots,M, find k such that C _ k=B _ j.
Constraints
- 1\leq N,M\leq 10^5
- 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
- 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
- A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\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 \ldots A _ N B _ 1 B _ 2 \ldots B _ M
Output
Print the answer in two lines.
The first line should contain the positions of A _ 1,A _ 2,\ldots,A _ N in C, with spaces in between.
The second line should contain the positions of B _ 1,B _ 2,\ldots,B _ M in C, with spaces in between.
Sample Input 1
4 3 3 14 15 92 6 53 58
Sample Output 1
1 3 4 7 2 5 6
C will be (3,6,14,15,53,58,92). Here, the 1-st, 3-rd, 4-th, 7-th elements are from A=(3,14,15,92), and the 2-nd, 5-th, 6-th elements are from B=(6,53,58).
Sample Input 2
4 4 1 2 3 4 100 200 300 400
Sample Output 2
1 2 3 4 5 6 7 8
Sample Input 3
8 12 3 4 10 15 17 18 22 30 5 7 11 13 14 16 19 21 23 24 27 28
Sample Output 3
1 2 5 9 11 12 15 20 3 4 6 7 8 10 13 14 16 17 18 19
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A があり、最初 A_i = i です。 以下のクエリを全部で Q 個処理してください。
- タイプ 1 : A_p を x に変更する。
- タイプ 2 : A_p を出力する。
- タイプ 3 : 「 A の先頭の要素を末尾にする」という操作を k 回繰り返す。
- 厳密には、 A を (A_2,A_3,\dots,A_N,A_1) に置き換えることを k 回繰り返す。
制約
- 入力は全て整数
- 1 \le N \le 10^6
- 1 \le Q \le 3 \times 10^5
- 全てのクエリは以下の制約を満たす
- 1 \le p \le N
- 1 \le x \le 10^6
- 1 \le k \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N Q
\rm{Query}_1
\rm{Query}_2
\vdots
\rm{Query}_Q
但し、 \rm{Query}_i は i 個目のクエリを表す。
タイプ 1 のクエリは以下の形式で与えられる。
1 p x
タイプ 2 のクエリは以下の形式で与えられる。
2 p
タイプ 3 のクエリは以下の形式で与えられる。
3 k
出力
タイプ 2 のクエリが現れる度に、その解答を 1 行に出力せよ。
入力例 1
5 5 2 3 1 2 1000000 3 4 2 2 2 3
出力例 1
3 1 1000000
この入力には 5 個のクエリが含まれます。
- 最初、 A=(1,2,3,4,5) です。
- 1 番目のクエリはタイプ 2 で、 A_3=3 を出力します。
- 2 番目のクエリはタイプ 1 で、 A_2=1000000 に置き換えます。
- クエリの結果、 A=(1,1000000,3,4,5) となります。
- 3 番目のクエリはタイプ 3 で、「 A の先頭の要素を末尾にする」という操作を 4 回繰り返します。
- クエリの結果、 A=(5,1,1000000,3,4) となります。
- 4 番目のクエリはタイプ 2 で、 A_2=1 を出力します。
- 5 番目のクエリはタイプ 2 で、 A_3=1000000 を出力します。
入力例 2
1000000 2 1 1000000 999999 3 1000000000
出力例 2
出力が空である場合もあります。
Score : 300 points
Problem Statement
There is an integer sequence A of length N, initially A_i = i. Process a total of Q queries of the following types:
- Type 1: Change A_p to x.
- Type 2: Output A_p.
- Type 3: Repeat the operation "move the first element of A to the end" k times.
- Formally, replace A with (A_2,A_3,\dots,A_N,A_1) k times.
Constraints
- All input values are integers.
- 1 \le N \le 10^6
- 1 \le Q \le 3 \times 10^5
- All queries satisfy the following constraints:
- 1 \le p \le N
- 1 \le x \le 10^6
- 1 \le k \le 10^9
Input
The input is given from Standard Input in the following format:
N Q
\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 p x
Type 2 queries are given in the following format:
2 p
Type 3 queries are given in the following format:
3 k
Output
For each Type 2 query, output the answer on a line.
Sample Input 1
5 5 2 3 1 2 1000000 3 4 2 2 2 3
Sample Output 1
3 1 1000000
This input contains 5 queries.
- Initially, A=(1,2,3,4,5).
- The 1st query is Type 2: output A_3=3.
- The 2nd query is Type 1: replace A_2 with 1000000.
- After the query, A=(1,1000000,3,4,5).
- The 3rd query is Type 3: repeat the operation "move the first element of A to the end" 4 times.
- After the query, A=(5,1,1000000,3,4).
- The 4th query is Type 2: output A_2=1.
- The 5th query is Type 2: output A_3=1000000.
Sample Input 2
1000000 2 1 1000000 999999 3 1000000000
Sample Output 2
The output may be empty.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
N 頂点 M 辺の単純無向グラフ G があります。頂点には 1, 2, \ldots, N の番号が付けられており、i 番目の辺は頂点 A_i と頂点 B_i を結ぶ無向辺です。
あなたは、以下の 2 つの操作を好きな順序で好きな回数繰り返すことができます。
- G に無向辺を 1 つ追加する
- G から無向辺を 1 つ削除する
G をすべての頂点の次数が 2 である単純無向グラフにするために行う操作回数として考えられる最小値を求めてください。
単純無向グラフとは
単純無向グラフとは、自己ループと多重辺を持たない無向グラフのことを指します。
制約
- 3 \leq N \leq 8
- 0 \leq M \leq \frac{N(N-1)}{2}
- 入力で与えられるグラフ G は単純無向グラフ
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
答えを出力せよ。
入力例 1
5 4 1 2 1 5 2 4 4 5
出力例 1
3
例えば以下のようにすることで、3 回の操作で G をすべての頂点の次数が 2 の単純無向グラフにすることができます。
- 頂点 2 と頂点 3 を結ぶ辺を追加する。
- 頂点 2 と頂点 4 を結ぶ辺を削除する。
- 頂点 3 と頂点 4 を結ぶ辺を追加する。
入力例 2
3 0
出力例 2
3
入力例 3
6 8 1 4 1 5 2 3 2 6 3 4 3 6 4 5 4 6
出力例 3
2
入力例 4
8 21 1 4 5 7 8 4 3 4 2 5 8 1 5 1 2 8 2 1 2 4 3 1 6 7 5 8 2 7 6 8 5 4 3 8 7 3 7 8 5 3 7 4
出力例 4
13
Score : 425 points
Problem Statement
There is a simple undirected graph G with N vertices and M edges. The vertices are numbered 1, 2, \ldots, N, and the i-th edge is an undirected edge connecting vertices A_i and B_i.
You can repeat the following two operations in any order and any number of times:
- Add one undirected edge to G
- Remove one undirected edge from G
Find the minimum number of operations to make G a simple undirected graph where all vertices have degree 2.
What is a simple undirected graph?
A simple undirected graph refers to an undirected graph that has no self-loops and no multi-edges.
Constraints
- 3 \leq N \leq 8
- 0 \leq M \leq \frac{N(N-1)}{2}
- The graph G given in the input is a simple undirected graph.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
Output the answer.
Sample Input 1
5 4 1 2 1 5 2 4 4 5
Sample Output 1
3
For example, the following three operations make G a simple undirected graph where all vertices have degree 2.
- Add an edge connecting vertices 2 and 3.
- Remove the edge connecting vertices 2 and 4.
- Add an edge connecting vertices 3 and 4.
Sample Input 2
3 0
Sample Output 2
3
Sample Input 3
6 8 1 4 1 5 2 3 2 6 3 4 3 6 4 5 4 6
Sample Output 3
2
Sample Input 4
8 21 1 4 5 7 8 4 3 4 2 5 8 1 5 1 2 8 2 1 2 4 3 1 6 7 5 8 2 7 6 8 5 4 3 8 7 3 7 8 5 3 7 4
Sample Output 4
13
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
この問題は問題 G と似た設定です。問題文の相違点を赤字で示します。
H 行 W 列のグリッドがあり、グリッドの各マスは赤色あるいは緑色に塗られています。
グリッドの上から i 行目、左から j 列目のマスをマス (i,j) と表記します。
マス (i,j) の色は文字 S_{i,j} で表され、S_{i,j} = . のときマス (i,j) は赤色、S_{i,j} = # のときマス (i,j) は緑色に塗られています。
グリッドにおいて、緑色に塗られたマスを頂点集合、隣り合った 2 つの緑色のマスを結ぶ辺全体を辺集合としたグラフにおける連結成分の個数を 緑の連結成分数 と呼びます。ただし、2 つのマス (x,y) と (x',y') が隣り合っているとは、|x-x'| + |y-y'| = 1 であることを指します。
赤色に塗られたマスを一様ランダムに 1 つ選び、緑色に塗り替えたとき、塗り替え後のグリッドの緑の連結成分数の期待値を \text{mod } 998244353 で出力してください。
「期待値を \text{mod } 998244353 で出力」とは
求める期待値は必ず有理数となることが証明できます。 またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、 R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ 1 つ存在することが証明できます。この R を出力してください。制約
- 1 \leq H,W \leq 1000
- S_{i,j} =
.または S_{i,j} =# - S_{i,j} =
.なる (i,j) が存在する。
入力
入力は以下の形式で標準入力から与えられる。
H W
S_{1,1}S_{1,2}\ldotsS_{1,W}
S_{2,1}S_{2,2}\ldotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\ldotsS_{H,W}
出力
答えを出力せよ。
入力例 1
3 3 ##. #.# #..
出力例 1
499122178
マス (1,3) を緑色に塗り替えたとき、緑の連結成分数は 1 となります。
マス (2,2) を緑色に塗り替えたとき、緑の連結成分数は 1 となります。
マス (3,2) を緑色に塗り替えたとき、緑の連結成分数は 2 となります。
マス (3,3) を緑色に塗り替えたとき、緑の連結成分数は 2 となります。
よって、赤色に塗られたマスを一様ランダムに 1 つ選び、緑色に塗り替えた後の緑の連結成分数の期待値は (1+1+2+2)/4 = 3/2 となります。
入力例 2
4 5 ..#.. .###. ##### ..#..
出力例 2
598946613
入力例 3
3 4 #... .#.# ..##
出力例 3
285212675
Score : 450 points
Problem Statement
This problem has a similar setting to Problem G. Differences in the problem statement are indicated in red.
There is a grid with H rows and W columns, where each cell is painted red or green.
Let (i,j) denote the cell in the i-th row from the top and the j-th column from the left.
The color of cell (i,j) is represented by the character S_{i,j}, where S_{i,j} = . means cell (i,j) is red, and S_{i,j} = # means cell (i,j) is green.
The number of green connected components in the grid is the number of connected components in the graph with the vertex set being the green cells and the edge set being the edges connecting two adjacent green cells. Here, two cells (x,y) and (x',y') are considered adjacent when |x-x'| + |y-y'| = 1.
Consider choosing one red cell uniformly at random and repainting it green. Print the expected value of the number of green connected components in the grid after repainting, modulo 998244353.
What does "print the expected value modulo 998244353" mean?
It can be proved that the sought expected value is always rational. Furthermore, the constraints of this problem guarantee that if that value is expressed as \frac{P}{Q} using two coprime integers P and Q, there is exactly one integer R such that R \times Q \equiv P \pmod{998244353} and 0 \leq R < 998244353. Print this R.Constraints
- 1 \leq H,W \leq 1000
- S_{i,j} =
.or S_{i,j} =#. - There is at least one (i,j) such that S_{i,j} =
..
Input
The input is given from Standard Input in the following format:
H W
S_{1,1}S_{1,2}\ldotsS_{1,W}
S_{2,1}S_{2,2}\ldotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\ldotsS_{H,W}
Output
Print the answer.
Sample Input 1
3 3 ##. #.# #..
Sample Output 1
499122178
If cell (1,3) is repainted green, the number of green connected components becomes 1.
If cell (2,2) is repainted green, the number of green connected components becomes 1.
If cell (3,2) is repainted green, the number of green connected components becomes 2.
If cell (3,3) is repainted green, the number of green connected components becomes 2.
Therefore, the expected value of the number of green connected components after choosing one red cell uniformly at random and repainting it green is (1+1+2+2)/4 = 3/2.
Sample Input 2
4 5 ..#.. .###. ##### ..#..
Sample Output 2
598946613
Sample Input 3
3 4 #... .#.# ..##
Sample Output 3
285212675
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
英小文字のみからなる文字列 S が与えられます。 下記の条件を満たす空でない文字列 T の個数を 998244353 で割ったあまりを出力してください。
T を 2 つ連結して得られる文字列 TT が、S に(連続とは限らない)部分列として含まれる。
制約
- S は英小文字のみからなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
ababbaba
出力例 1
8
問題文中の条件を満たす文字列 T は、a 、aa 、ab 、aba 、b 、ba 、bab 、bb の 8 個です。
入力例 2
zzz
出力例 2
1
問題文中の条件を満たす文字列 T は、z のみです。
S = S_1S_2S_3 = zzz から、文字列 zz を部分列として得る方法は、
S_1S_2 = zz 、S_1S_3 = zz 、S_2S_3 = zz の 3 通りありますが、文字列 z は答えに 1 回だけ寄与することに注意してください。
入力例 3
ppppqqppqqqpqpqppqpqqqqpppqppq
出力例 3
580
Score : 500 points
Problem Statement
You are given a string S consisting of lowercase English letters. Print the number of non-empty strings T that satisfy the following condition, modulo 998244353.
The concatenation TT of two copies of T is a subsequence of S (not necessarily contiguous).
Constraints
- S is a string consisting of lowercase English letters whose length is between 1 and 100, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
ababbaba
Sample Output 1
8
The eight strings satisfying the condition are a, aa, ab, aba, b, ba, bab, and bb.
Sample Input 2
zzz
Sample Output 2
1
The only string satisfying the condition is z.
Note that this string contributes to the answer just once, although there are three ways to extract the subsequence zz from S = S_1S_2S_3 = zzz: S_1S_2 = zz, S_1S_3 = zz, and S_2S_3 = zz.
Sample Input 3
ppppqqppqqqpqpqppqpqqqqpppqppq
Sample Output 3
580