実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
H 行 W 列のグリッドがあります。
グリッドの各マスは陸か海のどちらかであり、
その情報は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H で与えられます。
上から i 行目、左から j 列目のマスを (i, j) で表すと、
S_i の j 文字目が . のときマス (i, j) は陸であり、# のときマス (i, j) は海です。
ここで、グリッドの外周のマス(すなわち、i = 1 、i = H 、j = 1 、j = W のうち少なくとも 1 個以上を満たすマス (i, j) )については、すべて海であることが制約として保証されます。
高橋君が乗った宇宙船が、グリッド上のあるマスに不時着してしまいました。
その後、高橋君は L 、R 、U 、D のみからなる長さ N の文字列 T で表される手順に沿って、グリッド上を N 回移動しました。
i = 1, 2, \ldots, N について、T の i 文字目は i 回目の移動の内容を下記の通り表します。
Lのとき、左に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j-1) である。Rのとき、右に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j+1) である。Uのとき、上に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i-1, j) である。Dのとき、下に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i+1, j) である。
高橋君の移動経路上のマス(不時着したマスおよび現在いるマスを含む)はいずれも海でないことがわかっています。 高橋君が現在いるマスとしてあり得るものの個数を出力してください。
制約
- H, W, N は整数
- 3 \leq H, W \leq 500
- 1 \leq N \leq 500
- T は
L、R、U、Dのみからなる長さ N の文字列 - S_i は
.と#のみからなる長さ W の文字列 - 高橋君が現在いるマスとしてあり得るものが少なくとも 1 個存在する。
- グリッドの外周のマスはすべて海である。
入力
入力は以下の形式で標準入力から与えられる。
H W N T S_1 S_2 \vdots S_H
出力
答えを出力せよ。
入力例 1
6 7 5 LULDR ####### #...#.# ##...## #.#...# #...#.# #######
出力例 1
2
下記の 2 つの場合がありえるため、高橋君が現在いるマスとしてあり得るものは (3, 4) と (4, 5) の 2 個です。
- マス (3, 5) に不時着し、(3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4) と移動した場合
- マス (4, 6) に不時着し、(4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5) と移動した場合
入力例 2
13 16 9 ULURDLURD ################ ##..##.#..####.# ###.#..#.....#.# #..##..#####.### #...#..#......## ###.##.#..#....# ##.#####....##.# ###.###.#.#.#..# ######.....##..# #...#.#.######.# ##..###..#..#.## #...#.#.#...#..# ################
出力例 2
6
Score: 250 points
Problem Statement
There is a grid with H rows and W columns.
Each cell of the grid is land or sea, which is represented by H strings S_1, S_2, \ldots, S_H of length W. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left, and (i, j) is land if the j-th character of S_i is ., and (i, j) is sea if the character is #.
The constraints guarantee that all cells on the perimeter of the grid (that is, the cells (i, j) that satisfy at least one of i = 1, i = H, j = 1, j = W) are sea.
Takahashi's spaceship has crash-landed on a cell in the grid. Afterward, he moved N times on the grid following the instructions represented by a string T of length N consisting of L, R, U, and D. For i = 1, 2, \ldots, N, the i-th character of T describes the i-th move as follows:
Lindicates a move of one cell to the left. That is, if he is at (i, j) before the move, he will be at (i, j-1) after the move.Rindicates a move of one cell to the right. That is, if he is at (i, j) before the move, he will be at (i, j+1) after the move.Uindicates a move of one cell up. That is, if he is at (i, j) before the move, he will be at (i-1, j) after the move.Dindicates a move of one cell down. That is, if he is at (i, j) before the move, he will be at (i+1, j) after the move.
It is known that all cells along his path (including the cell where he crash-landed and the cell he is currently on) are not sea. Print the number of cells that could be his current position.
Constraints
- H, W, and N are integers.
- 3 \leq H, W \leq 500
- 1 \leq N \leq 500
- T is a string of length N consisting of
L,R,U, andD. - S_i is a string of length W consisting of
.and#. - There is at least one cell that could be Takahashi's current position.
- All cells on the perimeter of the grid are sea.
Input
The input is given from Standard Input in the following format:
H W N T S_1 S_2 \vdots S_H
Output
Print the answer.
Sample Input 1
6 7 5 LULDR ####### #...#.# ##...## #.#...# #...#.# #######
Sample Output 1
2
The following two cases are possible, so there are two cells that could be Takahashi's current position: (3, 4) and (4, 5).
- He crash-landed on cell (3, 5) and moved (3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4).
- He crash-landed on cell (4, 6) and moved (4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5).
Sample Input 2
13 16 9 ULURDLURD ################ ##..##.#..####.# ###.#..#.....#.# #..##..#####.### #...#..#......## ###.##.#..#....# ##.#####....##.# ###.###.#.#.#..# ######.....##..# #...#.#.######.# ##..###..#..#.## #...#.#.#...#..# ################
Sample Output 2
6
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
N 種類のビーンズが 1 粒ずつあります。 i 種類目のビーンズはおいしさが A_i で色が C_i です。ビーンズは混ぜられており、色でしか区別することができません。
あなたはビーンズの色を 1 つ選び、その色のビーンズをどれか 1 粒食べます。ビーンズの色をうまく選ぶことで、食べる可能性のあるビーンズのおいしさの最小値を最大化してください。
制約
- 1 \leq N \leq 2 \times 10^{5}
- 1 \leq A_i \leq 10^{9}
- 1 \leq C_i \leq 10^{9}
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N A_1 C_1 A_2 C_2 \vdots A_N C_N
出力
食べる可能性のあるビーンズのおいしさの最小値の最大値を整数として出力せよ。
入力例 1
4 100 1 20 5 30 5 40 1
出力例 1
40
同じ色のビーンズは互いに区別がつかないことに注意してください。
選ぶことができる色は 色 1 と 色 5 です。
- 色 1 のビーンズは 2 種類あり、美味しさはそれぞれ 100, 40 です。よって、色 1 を選んだときのおいしさの最小値は 40 です。
- 色 5 のビーンズは 2 種類あり、美味しさはそれぞれ 20, 30 です。よって、色 5 を選んだときのおいしさの最小値は 20 です。
おいしさの最小値を最大化するには 色 1 を選べばよいため、そのときの最小値である 40 を出力します。
入力例 2
10 68 3 17 2 99 2 92 4 82 4 10 3 100 2 78 1 3 1 35 4
出力例 2
35
Score: 250 points
Problem Statement
There are N types of beans, one bean of each type. The i-th type of bean has a deliciousness of A_i and a color of C_i. The beans are mixed and can only be distinguished by color.
You will choose one color of beans and eat one bean of that color. By selecting the optimal color, maximize the minimum possible deliciousness of the bean you eat.
Constraints
- 1 \leq N \leq 2 \times 10^{5}
- 1 \leq A_i \leq 10^{9}
- 1 \leq C_i \leq 10^{9}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 C_1 A_2 C_2 \vdots A_N C_N
Output
Print as an integer the maximum value of the minimum possible deliciousness of the bean you eat.
Sample Input 1
4 100 1 20 5 30 5 40 1
Sample Output 1
40
Note that beans of the same color cannot be distinguished from each other.
You can choose color 1 or color 5.
- There are two types of beans of color 1, with deliciousness of 100 and 40. Thus, the minimum deliciousness when choosing color 1 is 40.
- There are two types of beans of color 5, with deliciousness of 20 and 30. Thus, the minimum deliciousness when choosing color 5 is 20.
To maximize the minimum deliciousness, you should choose color 1, so print the minimum deliciousness in that case: 40.
Sample Input 2
10 68 3 17 2 99 2 92 4 82 4 10 3 100 2 78 1 3 1 35 4
Sample Output 2
35
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
あなたは以下の連続する操作をちょうど一度だけ行います。
-
整数 x\ (0\leq x \leq N) を選ぶ。x として 0 を選んだ場合何もしない。 x として 1 以上の整数を選んだ場合、A_1,A_2,\ldots,A_x をそれぞれ L で置き換える。
-
整数 y\ (0\leq y \leq N) を選ぶ。y として 0 を選んだ場合何もしない。 y として 1 以上の整数を選んだ場合、A_{N},A_{N-1},\ldots,A_{N-y+1} をそれぞれ R で置き換える。
操作後の A の要素の総和として考えられる最小値を求めてください。
制約
- 1 \leq N \leq 2\times 10^5
- -10^9 \leq L, R\leq 10^9
- -10^9 \leq A_i\leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N L R A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
5 4 3 5 5 0 6 3
出力例 1
14
x=2,y=2 として操作をすると、数列 A = (4,4,0,3,3) となり、要素の総和は 14 になります。
これが達成可能な最小値です。
入力例 2
4 10 10 1 2 3 4
出力例 2
10
x=0,y=0 として操作をすると、数列 A = (1,2,3,4) となり、要素の総和は 10 になります。
これが達成可能な最小値です。
入力例 3
10 -5 -3 9 -6 10 -1 2 10 -1 7 -15 5
出力例 3
-58
L,R,A_i は負であることがあります。
Score : 400 points
Problem Statement
You are given an integer sequence of length N: A=(A_1,A_2,\ldots,A_N).
You will perform the following consecutive operations just once:
-
Choose an integer x (0\leq x \leq N). If x is 0, do nothing. If x is 1 or greater, replace each of A_1,A_2,\ldots,A_x with L.
-
Choose an integer y (0\leq y \leq N). If y is 0, do nothing. If y is 1 or greater, replace each of A_{N},A_{N-1},\ldots,A_{N-y+1} with R.
Print the minimum possible sum of the elements of A after the operations.
Constraints
- 1 \leq N \leq 2\times 10^5
- -10^9 \leq L, R\leq 10^9
- -10^9 \leq A_i\leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N L R A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
5 4 3 5 5 0 6 3
Sample Output 1
14
If you choose x=2 and y=2, you will get A = (4,4,0,3,3), for the sum of 14, which is the minimum sum achievable.
Sample Input 2
4 10 10 1 2 3 4
Sample Output 2
10
If you choose x=0 and y=0, you will get A = (1,2,3,4), for the sum of 10, which is the minimum sum achievable.
Sample Input 3
10 -5 -3 9 -6 10 -1 2 10 -1 7 -15 5
Sample Output 3
-58
L, R, and A_i may be negative.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の単純無向グラフが与えられます。このグラフの頂点には 1 から N の番号が付けられており、辺には 1 から M の番号が付けられています。辺 i は頂点 U_i と頂点 V_i の間を結んでいます。
整数 K, S, T, X が与えられます。以下の条件を満たす数列 A = (A_0, A_1, \dots, A_K) は何通りありますか?
- A_i は 1 以上 N 以下の整数
- A_0 = S
- A_K = T
- 頂点 A_i と頂点 A_{i + 1} の間を直接結ぶ辺が存在する
- 数列 A の中に整数 X\ (X≠S,X≠T) は偶数回出現する ( 0 回でも良い)
ただし、答えは非常に大きくなることがあるので、答えを 998244353 で割ったあまりを求めてください。
制約
- 入力は全て整数
- 2≤N≤2000
- 1≤M≤2000
- 1≤K≤2000
- 1≤S,T,X≤N
- X≠S
- X≠T
- 1≤U_i<V_i≤N
- i≠j ならば (U_i, V_i)≠(U_j, V_j)
入力
入力は以下の形式で標準入力から与えられる。
N M K S T X U_1 V_1 U_2 V_2 \vdots U_M V_M
出力
答えを 998244353 で割ったあまりを出力せよ。
入力例 1
4 4 4 1 3 2 1 2 2 3 3 4 1 4
出力例 1
4
- (1, 2, 1, 2, 3)
- (1, 2, 3, 2, 3)
- (1, 4, 1, 4, 3)
- (1, 4, 3, 4, 3)
の 4 個が条件を満たします。(1, 2, 3, 4, 3) や (1, 4, 1, 2, 3) は 2 が奇数回出現するため、条件を満たしません。
入力例 2
6 5 10 1 2 3 2 3 2 4 4 6 3 6 1 5
出力例 2
0
グラフは連結であるとは限りません。
入力例 3
10 15 20 4 4 6 2 6 2 7 5 7 4 5 2 4 3 7 1 7 1 4 2 9 5 10 1 3 7 8 7 9 1 6 1 2
出力例 3
952504739
998244353 で割ったあまりを求めてください。
Score : 500 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges. The vertices are numbered from 1 through N, and the edges are numbered from 1 through M. Edge i connects Vertex U_i and Vertex V_i.
You are given integers K, S, T, and X. How many sequences A = (A_0, A_1, \dots, A_K) are there satisfying the following conditions?
- A_i is an integer between 1 and N (inclusive).
- A_0 = S
- A_K = T
- There is an edge that directly connects Vertex A_i and Vertex A_{i+1}.
- Integer X\ (X≠S,X≠T) appears even number of times (possibly zero) in sequence A.
Since the answer can be very large, find the answer modulo 998244353.
Constraints
- All values in input are integers.
- 2≤N≤2000
- 1≤M≤2000
- 1≤K≤2000
- 1≤S,T,X≤N
- X≠S
- X≠T
- 1≤U_i<V_i≤N
- If i ≠ j, then (U_i, V_i) ≠ (U_j, V_j).
Input
Input is given from Standard Input in the following format:
N M K S T X U_1 V_1 U_2 V_2 \vdots U_M V_M
Output
Print the answer modulo 998244353.
Sample Input 1
4 4 4 1 3 2 1 2 2 3 3 4 1 4
Sample Output 1
4
The following 4 sequences satisfy the conditions:
- (1, 2, 1, 2, 3)
- (1, 2, 3, 2, 3)
- (1, 4, 1, 4, 3)
- (1, 4, 3, 4, 3)
On the other hand, (1, 2, 3, 4, 3) and (1, 4, 1, 2, 3) do not, since there are odd number of occurrences of 2.
Sample Input 2
6 5 10 1 2 3 2 3 2 4 4 6 3 6 1 5
Sample Output 2
0
The graph is not necessarily connected.
Sample Input 3
10 15 20 4 4 6 2 6 2 7 5 7 4 5 2 4 3 7 1 7 1 4 2 9 5 10 1 3 7 8 7 9 1 6 1 2
Sample Output 3
952504739
Find the answer modulo 998244353.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 行 M 列のグリッドがあり、上から i 行目、左から j 列目のマス (i,j) には整数 (i-1) \times M + j が書かれています。
このグリッドに、以下の操作を施します。
- 全てのマス (i,j) について、 i+j が奇数ならそのマスに書かれている数字を 0 に書き換える。
操作後のグリッドについて、Q 個の質問に答えてください。
i 個目の質問は以下の通りです:
- 以下の条件を全て満たすマス (p,q) 全てについて、そこに書かれている整数を全て足し合わせた値を 998244353 で割った余りを求めよ。
- A_i \le p \le B_i
- C_i \le q \le D_i
制約
- 入力は全て整数
- 1 \le N,M \le 10^9
- 1 \le Q \le 2 \times 10^5
- 1 \le A_i \le B_i \le N
- 1 \le C_i \le D_i \le M
入力
入力は以下の形式で標準入力から与えられる。
N M Q A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_Q B_Q C_Q D_Q
出力
Q 行出力せよ。
そのうち i 行目には、 i 個目の質問に対する答えを整数として出力せよ。
入力例 1
5 4 6 1 3 2 4 1 5 1 1 5 5 1 4 4 4 2 2 5 5 4 4 1 5 1 4
出力例 1
28 27 36 14 0 104
この入力では、グリッドは以下の通りです。

この入力には 6 つの質問が含まれます。
- 1 個目の質問の答えは 0+3+0+6+0+8+0+11+0=28 です。
- 2 個目の質問の答えは 1+0+9+0+17=27 です。
- 3 個目の質問の答えは 17+0+19+0=36 です。
- 4 個目の質問の答えは 14 です。
- 5 個目の質問の答えは 0 です。
- 6 個目の質問の答えは 104 です。
入力例 2
1000000000 1000000000 3 1000000000 1000000000 1000000000 1000000000 165997482 306594988 719483261 992306147 1 1000000000 1 1000000000
出力例 2
716070898 240994972 536839100
1 個目の質問について、マス (10^9,10^9) に書かれている整数は 10^{18} ですが、それを 998244353 で割った余りを求めることに注意してください。
入力例 3
999999999 999999999 3 999999999 999999999 999999999 999999999 216499784 840031647 84657913 415448790 1 999999999 1 999999999
出力例 3
712559605 648737448 540261130
Score : 500 points
Problem Statement
We have a grid with N rows and M columns. The square (i,j) at the i-th row from the top and j-th column from the left has an integer (i-1) \times M + j written on it.
Let us perform the following operation on this grid.
- For every square (i,j) such that i+j is odd, replace the integer on that square with 0.
Answer Q questions on the grid after the operation.
The i-th question is as follows:
- Find the sum of the integers written on all squares (p,q) that satisfy all of the following conditions, modulo 998244353.
- A_i \le p \le B_i.
- C_i \le q \le D_i.
Constraints
- All values in the input are integers.
- 1 \le N,M \le 10^9
- 1 \le Q \le 2 \times 10^5
- 1 \le A_i \le B_i \le N
- 1 \le C_i \le D_i \le M
Input
The input is given from Standard Input in the following format:
N M Q A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_Q B_Q C_Q D_Q
Output
Print Q lines.
The i-th line should contain the answer to the i-th question as an integer.
Sample Input 1
5 4 6 1 3 2 4 1 5 1 1 5 5 1 4 4 4 2 2 5 5 4 4 1 5 1 4
Sample Output 1
28 27 36 14 0 104
The grid in this input is shown below.

This input contains six questions.
- The answer to the first question is 0+3+0+6+0+8+0+11+0=28.
- The answer to the second question is 1+0+9+0+17=27.
- The answer to the third question is 17+0+19+0=36.
- The answer to the fourth question is 14.
- The answer to the fifth question is 0.
- The answer to the sixth question is 104.
Sample Input 2
1000000000 1000000000 3 1000000000 1000000000 1000000000 1000000000 165997482 306594988 719483261 992306147 1 1000000000 1 1000000000
Sample Output 2
716070898 240994972 536839100
For the first question, note that although the integer written on the square (10^9,10^9) is 10^{18}, you are to find it modulo 998244353.
Sample Input 3
999999999 999999999 3 999999999 999999999 999999999 999999999 216499784 840031647 84657913 415448790 1 999999999 1 999999999
Sample Output 3
712559605 648737448 540261130