実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
1,2,\ldots,N の番号のついた N 人の人に合計 K 枚のカードを配ります。
人 A から始めて 人 A,A+1,A+2,\ldots,N,1,2,\ldots の順に 1 枚ずつカードを配るとき、最後のカードは誰に配られるでしょうか?
厳密には、人 x(1 \leq x < N) の次には人 x+1 にカードを配り、人 N の次には人 1 にカードを配ります。
制約
- 1 \leq N,K \leq 1000
- 1 \leq A \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A
出力
最後のカードが配られた人の番号を出力せよ。
入力例 1
3 3 2
出力例 1
1
人 2、人 3、人 1 の順にカードを配ります。
入力例 2
1 100 1
出力例 2
1
入力例 3
3 14 2
出力例 3
3
Score : 100 points
Problem Statement
We will hand out a total of K cards to N people numbered 1, 2, \ldots, N.
Beginning with Person A, we will give the cards one by one to the people in this order: A, A+1, A+2, \ldots, N, 1, 2, \ldots. Who will get the last card?
Formally, after Person x(1 \leq x < N) gets a card, Person x+1 will get a card. After Person N gets a card, Person 1 gets a card.
Constraints
- 1 \leq N,K \leq 1000
- 1 \leq A \leq N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K A
Output
Print a number representing the person who will get the last card.
Sample Input 1
3 3 2
Sample Output 1
1
The cards are given to Person 2, 3, 1 in this order.
Sample Input 2
1 100 1
Sample Output 2
1
Sample Input 3
3 14 2
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
関数 f を f(x) = x^2 + 2x + 3 と定義します。
整数 t が入力されるので、 f(f(f(t)+t)+f(f(t))) を求めてください。
ただし、答えは 2 \times 10^9 以下の整数であることが保証されます。
制約
- t は 0 以上 10 以下の整数である
入力
入力は以下の形式で標準入力から与えられる。
t
出力
答えを整数として出力せよ。
入力例 1
0
出力例 1
1371
答えは以下の手順によって計算されます。
- f(t) = t^2 + 2t + 3 = 0 \times 0 + 2 \times 0 + 3 = 3
- f(t)+t = 3 + 0 = 3
- f(f(t)+t) = f(3) = 3 \times 3 + 2 \times 3 + 3 = 18
- f(f(t)) = f(3) = 18
- f(f(f(t)+t)+f(f(t))) = f(18+18) = f(36) = 36 \times 36 + 2 \times 36 + 3 = 1371
入力例 2
3
出力例 2
722502
入力例 3
10
出力例 3
1111355571
Score : 100 points
Problem Statement
Let us define a function f as f(x) = x^2 + 2x + 3.
Given an integer t, find f(f(f(t)+t)+f(f(t))).
Here, it is guaranteed that the answer is an integer not greater than 2 \times 10^9.
Constraints
- t is an integer between 0 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
t
Output
Print the answer as an integer.
Sample Input 1
0
Sample Output 1
1371
The answer is computed as follows.
- f(t) = t^2 + 2t + 3 = 0 \times 0 + 2 \times 0 + 3 = 3
- f(t)+t = 3 + 0 = 3
- f(f(t)+t) = f(3) = 3 \times 3 + 2 \times 3 + 3 = 18
- f(f(t)) = f(3) = 18
- f(f(f(t)+t)+f(f(t))) = f(18+18) = f(36) = 36 \times 36 + 2 \times 36 + 3 = 1371
Sample Input 2
3
Sample Output 2
722502
Sample Input 3
10
Sample Output 3
1111355571
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1, \dots, N の番号が付けられており、i \, (1 \leq i \leq M) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。
以下の条件を全て満たす整数 a, b, c の組の総数を求めてください。
- 1 \leq a \lt b \lt c \leq N
- 頂点 a と頂点 b を結ぶ辺が存在する。
- 頂点 b と頂点 c を結ぶ辺が存在する。
- 頂点 c と頂点 a を結ぶ辺が存在する。
制約
- 3 \leq N \leq 100
- 1 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
- (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M U_1 V_1 \vdots U_M V_M
出力
答えを出力せよ。
入力例 1
5 6 1 5 4 5 2 3 1 4 3 5 2 5
出力例 1
2
(a, b, c) = (1, 4, 5), (2, 3, 5) が条件を満たします。
入力例 2
3 1 1 2
出力例 2
0
入力例 3
7 10 1 7 5 7 2 5 3 6 4 7 1 5 2 4 1 3 1 6 2 7
出力例 3
4
Score : 200 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, \dots, N, and the i-th (1 \leq i \leq M) edge connects Vertex U_i and Vertex V_i.
Find the number of tuples of integers a, b, c that satisfy all of the following conditions:
- 1 \leq a \lt b \lt c \leq N
- There is an edge connecting Vertex a and Vertex b.
- There is an edge connecting Vertex b and Vertex c.
- There is an edge connecting Vertex c and Vertex a.
Constraints
- 3 \leq N \leq 100
- 1 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
- (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M U_1 V_1 \vdots U_M V_M
Output
Print the answer.
Sample Input 1
5 6 1 5 4 5 2 3 1 4 3 5 2 5
Sample Output 1
2
(a, b, c) = (1, 4, 5), (2, 3, 5) satisfy the conditions.
Sample Input 2
3 1 1 2
Sample Output 2
0
Sample Input 3
7 10 1 7 5 7 2 5 3 6 4 7 1 5 2 4 1 3 1 6 2 7
Sample Output 3
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
整数 1, 2, \dots, N が書かれたカードが 4 枚ずつ、合計 4N 枚あります。
高橋君は、これらのカードをシャッフルしたのち 1 枚のカードを選んで抜き取り、残りの 4N - 1 枚を束にしてあなたに渡しました。渡された束の i \, (1 \leq i \leq 4N - 1) 枚目のカードには、整数 A_i が書かれています。
高橋君が抜き取ったカードに書かれていた整数を求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
- 各 k \, (1 \leq k \leq N) に対し、A_i = k となる i は 4 個以下である。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \ldots A_{4N - 1}
出力
答えを出力せよ。
入力例 1
3 1 3 2 3 3 2 2 1 1 1 2
出力例 1
3
高橋君が抜き取ったカードには 3 が書かれています。
入力例 2
1 1 1 1
出力例 2
1
入力例 3
4 3 2 1 1 2 4 4 4 4 3 1 3 2 1 3
出力例 3
2
Score : 200 points
Problem Statement
We have 4 cards with an integer 1 written on it, 4 cards with 2, \ldots, 4 cards with N, for a total of 4N cards.
Takahashi shuffled these cards, removed one of them, and gave you a pile of the remaining 4N-1 cards. The i-th card (1 \leq i \leq 4N - 1) of the pile has an integer A_i written on it.
Find the integer written on the card removed by Takahashi.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
- For each k \, (1 \leq k \leq N), there are at most 4 indices i such that A_i = k.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_{4N - 1}
Output
Print the answer.
Sample Input 1
3 1 3 2 3 3 2 2 1 1 1 2
Sample Output 1
3
Takahashi removed a card with 3 written on it.
Sample Input 2
1 1 1 1
Sample Output 2
1
Sample Input 3
4 3 2 1 1 2 4 4 4 4 3 1 3 2 1 3
Sample Output 3
2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
9\times 9 のマス目 A があり、各マスには 1 以上 9 以下の整数が書き込まれています。
具体的には、 A の上から i 行目、左から j 列目のマスには A_{i,j} が書き込まれています。
A が次の条件をすべてみたしているならば Yes を、そうでないならば No を出力してください。
- A の各行について、その行に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
- A の各列について、その列に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
- A の行を上から 3 行ずつ 3 つに分け、同様に列も左から 3 列ずつ 3 つに分ける。 これによって A を 9 つの 3\times 3 のマス目に分けたとき、それぞれの 3\times 3 のマス目には 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
制約
- 1\leq A_{i,j}\leq 9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
A_{1,1} A_{1,2} \ldots A_{1,9}
A_{2,1} A_{2,2} \ldots A_{2,9}
\vdots
A_{9,1} A_{9,2} \ldots A_{9,9}
出力
マス目 A が問題文の条件をすべてみたすならば Yes を、
そうでないならば No を出力せよ。
入力例 1
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 2 3 4 5 6 7 8 9 1 5 6 7 8 9 1 2 3 4 8 9 1 2 3 4 5 6 7 3 4 5 6 7 8 9 1 2 6 7 8 9 1 2 3 4 5 9 1 2 3 4 5 6 7 8
出力例 1
Yes
マス目 A は次のようになっています。

マス目 A は 3 つの条件をすべてみたしているため、Yes を出力します。
入力例 2
1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 1 3 4 5 6 7 8 9 1 2 4 5 6 7 8 9 1 2 3 5 6 7 8 9 1 2 3 4 6 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 6 8 9 1 2 3 4 5 6 7 9 1 2 3 4 5 6 7 8
出力例 2
No
マス目 A は次のようになっています。

例えば左上の 3\times 3 のマス目に注目すると 3 つめの条件をみたしていないことが分かるため、No を出力します。
入力例 3
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6
出力例 3
No
マス目 A は次のようになっています。

例えば一番左の列に注目すると 2 つめの条件をみたしていないことが分かるため、No を出力します。
Score : 250 points
Problem Statement
There is a 9\times 9 grid A, where each cell contains an integer between 1 and 9, inclusive.
Specifically, the cell at the i-th row from the top and j-th column from the left contains A_{i,j}.
If A satisfies all of the following conditions, print Yes. Otherwise, print No.
- For each row of A, the nine cells in that row contain each integer from 1 to 9 exactly once.
- For each column of A, the nine cells in that column contain each integer from 1 to 9 exactly once.
- Divide the rows of A into three groups, each of three rows, from top to bottom, and similarly divide the columns into three groups, each of three columns, from left to right. Each 3\times 3 grid obtained from A in this way contains each integer from 1 to 9 exactly once.
Constraints
- 1\leq A_{i,j}\leq 9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A_{1,1} A_{1,2} \ldots A_{1,9}
A_{2,1} A_{2,2} \ldots A_{2,9}
\vdots
A_{9,1} A_{9,2} \ldots A_{9,9}
Output
If the grid A satisfies all the conditions in the problem statement, print Yes; otherwise, print No.
Sample Input 1
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 2 3 4 5 6 7 8 9 1 5 6 7 8 9 1 2 3 4 8 9 1 2 3 4 5 6 7 3 4 5 6 7 8 9 1 2 6 7 8 9 1 2 3 4 5 9 1 2 3 4 5 6 7 8
Sample Output 1
Yes
The grid A is shown below.

The grid A satisfies all three conditions, so print Yes.
Sample Input 2
1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 1 3 4 5 6 7 8 9 1 2 4 5 6 7 8 9 1 2 3 5 6 7 8 9 1 2 3 4 6 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 6 8 9 1 2 3 4 5 6 7 9 1 2 3 4 5 6 7 8
Sample Output 2
No
The grid A is shown below.

For example, if you look at the top left 3\times 3 grid, you can see that the third condition is unsatisfied, so print No.
Sample Input 3
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6
Sample Output 3
No
The grid A is shown below.

For example, if you look at the leftmost column, you can see that the second condition is unsatisfied, so print No.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は黒いマスと透明なマスからなるシート A,B を 1 枚ずつと、透明なマスのみからなる無限に広がるシート C を持っています。
また、高橋君には黒いマスと透明なマスからなる、理想とするシート X が存在します。
シート A,B,X の大きさはそれぞれ縦 H_A マス \times 横 W_A マス、縦 H_B マス \times 横 W_B マス、縦 H_X マス \times 横 W_X マスです。
シート A の各マスは . と # からなる長さ W_A の文字列 H_A 個 A_1,A_2,\ldots,A_{H_A} によって表され、
A_i (1\leq i\leq H_A) の j 文字目 (1\leq j\leq W_A) が、
. のときシート A の上から i 行目かつ左から j 列目のマスは透明なマスであり、
# のとき黒いマスです。
シート B,X の各マスも、同様に長さ W_B の文字列 H_B 個 B_1,B_2,\ldots,B_{H_B} および長さ W_X の文字列 H_X 個 X_1,X_2,\ldots,X_{H_X} によって表されます。
高橋君の目標は、次の手順で、シート A,B,C から、A,B に存在する すべての黒いマスを使って シート X を作り出すことです。
- シート A,B をマス目に沿ってシート C に貼り付ける。この時、シート A,B はそれぞれ好きな場所に平行移動させて貼って良いが、シートを切り分けたり、回転させたりしてはいけない。
- シート C からマス目に沿って H_X\times W_X マスの領域を切り出す。ここで、切り出されたシートの各マスは、シート A または B の黒いマスが貼り付けられていれば黒いマスに、そうでなければ透明なマスとなる。
このとき、貼り付ける位置と切り出す領域をうまくとることで高橋君は目標を達成できるか、すなわち次の条件をともにみたすことにできるか判定してください。
- 切り出されたシートはシート A,B の 黒いマスをすべて 含む。切り出されたシートの上でシート A,B の黒いマスどうしが重なって存在していても構わない。
- 切り出されたシートは、回転させたり裏返したりすることなくシート X と一致する。
制約
- 1\leq H_A,W_A,H_B,W_B,H_X,W_X\leq 10
- H_A,W_A,H_B,W_B,H_X,W_X は整数
- A_i は
.と#のみからなる長さ W_A の文字列 - B_i は
.と#のみからなる長さ W_B の文字列 - X_i は
.と#のみからなる長さ W_X の文字列 - シート A,B,X はそれぞれ少なくとも 1 つ以上の黒いマスを含む。
入力
入力は以下の形式で標準入力から与えられる。
H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}
出力
高橋君が問題文中の目標を達成できるならば Yes を、できないならば No を出力せよ。
入力例 1
3 5 #.#.. ..... .#... 2 2 #. .# 5 3 ... #.# .#. .#. ...
出力例 1
Yes
まず、シート A をシート C に貼り付けると下図のようになります。
\vdots
.......
.#.#...
\cdots.......\cdots
..#....
.......
\vdots
さらに、シート B をシート A と左上を合わせて貼ってみると下図のようになります。
\vdots
.......
.#.#...
\cdots..#....\cdots
..#....
.......
\vdots
ここで、上で具体的に図示されている範囲のうち、上から 1 行目かつ左から 2 列目のマスを左上として 5\times 3 マスを切り出すと下図のようになります。
... #.# .#. .#. ...
これはシート A,B のすべての黒いマスを含んでおり、また、シート X と一致しているため条件を満たしています。
よって、Yes を出力します。
入力例 2
2 2 #. .# 2 2 #. .# 2 2 ## ##
出力例 2
No
シート A や B を回転させて貼ってはいけないことに注意してください。
入力例 3
1 1 # 1 2 ## 1 1 #
出力例 3
No
どのように貼ったり切り出したりしても、シート B の黒いマスをすべて含むように切り出すことはできないため、1 つめの条件をみたすことができません。
よって、No を出力します。
入力例 4
3 3 ### ... ... 3 3 #.. #.. #.. 3 3 ..# ..# ###
出力例 4
Yes
Score : 300 points
Problem Statement
Takahashi has two sheets A and B, each composed of black squares and transparent squares, and an infinitely large sheet C composed of transparent squares.
There is also an ideal sheet X for Takahashi composed of black squares and transparent squares.
The sizes of sheets A, B, and X are H_A rows \times W_A columns, H_B rows \times W_B columns, and H_X rows \times W_X columns, respectively.
The squares of sheet A are represented by H_A strings of length W_A, A_1, A_2, \ldots, A_{H_A} consisting of . and #.
If the j-th character (1\leq j\leq W_A) of A_i (1\leq i\leq H_A) is ., the square at the i-th row from the top and j-th column from the left is transparent; if it is #, that square is black.
Similarly, the squares of sheets B and X are represented by H_B strings of length W_B, B_1, B_2, \ldots, B_{H_B}, and H_X strings of length W_X, X_1, X_2, \ldots, X_{H_X}, respectively.
Takahashi's goal is to create sheet X using all black squares in sheets A and B by following the steps below with sheets A, B, and C.
- Paste sheets A and B onto sheet C along the grid. Each sheet can be pasted anywhere by translating it, but it cannot be cut or rotated.
- Cut out an H_X\times W_X area from sheet C along the grid. Here, a square of the cut-out sheet will be black if a black square of sheet A or B is pasted there, and transparent otherwise.
Determine whether Takahashi can achieve his goal by appropriately choosing the positions where the sheets are pasted and the area to cut out, that is, whether he can satisfy both of the following conditions.
- The cut-out sheet includes all black squares of sheets A and B. The black squares of sheets A and B may overlap on the cut-out sheet.
- The cut-out sheet coincides sheet X without rotating or flipping.
Constraints
- 1\leq H_A, W_A, H_B, W_B, H_X, W_X\leq 10
- H_A, W_A, H_B, W_B, H_X, W_X are integers.
- A_i is a string of length W_A consisting of
.and#. - B_i is a string of length W_B consisting of
.and#. - X_i is a string of length W_X consisting of
.and#. - Sheets A, B, and X each contain at least one black square.
Input
The input is given from Standard Input in the following format:
H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}
Output
If Takahashi can achieve the goal described in the problem statement, print Yes; otherwise, print No.
Sample Input 1
3 5 #.#.. ..... .#... 2 2 #. .# 5 3 ... #.# .#. .#. ...
Sample Output 1
Yes
First, paste sheet A onto sheet C, as shown in the figure below.
\vdots
.......
.#.#...
\cdots.......\cdots
..#....
.......
\vdots
Next, paste sheet B so that its top-left corner aligns with that of sheet A, as shown in the figure below.
\vdots
.......
.#.#...
\cdots..#....\cdots
..#....
.......
\vdots
Now, cut out a 5\times 3 area with the square in the first row and second column of the range illustrated above as the top-left corner, as shown in the figure below.
... #.# .#. .#. ...
This includes all black squares of sheets A and B and matches sheet X, satisfying the conditions.
Therefore, print Yes.
Sample Input 2
2 2 #. .# 2 2 #. .# 2 2 ## ##
Sample Output 2
No
Note that sheets A and B may not be rotated or flipped when pasting them.
Sample Input 3
1 1 # 1 2 ## 1 1 #
Sample Output 3
No
No matter how you paste or cut, you cannot cut out a sheet that includes all black squares of sheet B, so you cannot satisfy the first condition.
Therefore, print No.
Sample Input 4
3 3 ### ... ... 3 3 #.. #.. #.. 3 3 ..# ..# ###
Sample Output 4
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
正整数 A,B が与えられます。
あなたは、A=B になるまで以下の操作を繰り返します。
- A,B の大小関係に応じて、次の 2 個のうちどちらかの処理を行う。
- A > B ならば、A を A-B で置き換える。
- A < B ならば、B を B-A で置き換える。
A=B になるまで、操作を何回行うか求めてください。ただし、有限回の操作で A=B になることが保証されます。
制約
- 1 \le A,B \le 10^{18}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
答えを出力せよ。
入力例 1
3 8
出力例 1
4
始め、A=3,B=8 です。操作は以下のように行われます。
- A<B であるため、B を B-A=5 で置き換える。A=3,B=5 となる。
- A<B であるため、B を B-A=2 で置き換える。A=3,B=2 となる。
- A>B であるため、A を A-B=1 で置き換える。A=1,B=2 となる。
- A<B であるため、B を B-A=1 で置き換える。A=1,B=1 となる。
よって、操作回数は 4 回です。
入力例 2
1234567890 1234567890
出力例 2
0
入力が 32bit 整数型に収まらないことがあることに注意してください。
入力例 3
1597 987
出力例 3
15
Score : 400 points
Problem Statement
You are given positive integers A and B.
You will repeat the following operation until A=B:
- compare A and B to perform one of the following two:
- if A > B, replace A with A-B;
- if A < B, replace B with B-A.
How many times will you repeat it until A=B? It is guaranteed that a finite repetition makes A=B.
Constraints
- 1 \le A,B \le 10^{18}
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Print the answer.
Sample Input 1
3 8
Sample Output 1
4
Initially, A=3 and B=8. You repeat the operation as follows:
- A<B, so replace B with B-A=5, making A=3 and B=5.
- A<B, so replace B with B-A=2, making A=3 and B=2.
- A>B, so replace A with A-B=1, making A=1 and B=2.
- A<B, so replace B with B-A=1, making A=1 and B=1.
Thus, you repeat it four times.
Sample Input 2
1234567890 1234567890
Sample Output 2
0
Note that the input may not fit into a 32-bit integer type.
Sample Input 3
1597 987
Sample Output 3
15
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
ある国には N 個の都市と M 個の発電所があります。これらを総称して地点と呼びます。
地点には 1,2,\dots,N+M の番号がつけられており、そのうち都市は地点 1,2,\dots,N で発電所は地点 N+1,N+2,\dots,N+M です。
この国には電線が E 本あり、電線 i ( 1 \le i \le E ) は地点 U_i と地点 V_i を双方向に結びます。
また、ある都市に 電気が通っている とは、ある都市から電線をいくつか辿って少なくともひとつの発電所に辿り着くことができる状態を言います。
今、 Q 個のイベントが起こります。そのうち i ( 1 \le i \le Q ) 番目のイベントでは電線 X_i が切れ、その電線を辿ることができなくなります。一度切れた電線は、その後のイベントにおいても切れたままです。
全てのイベントについて、そのイベントが終わった直後に電気が通っている都市の数を求めてください。
制約
- 入力は全て整数
- 1 \le N,M
- N+M \le 2 \times 10^5
- 1 \le Q \le E \le 5 \times 10^5
- 1 \le U_i < V_i \le N+M
- i \neq j ならば、 U_i \neq U_j または V_i \neq V_j
- 1 \le X_i \le E
- X_i は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N M E U_1 V_1 U_2 V_2 \vdots U_E V_E Q X_1 X_2 \vdots X_Q
出力
Q 行出力せよ。
そのうち i ( 1 \le i \le Q ) 行目には i 番目のイベントが終わった直後に電気が通っている都市の数を出力せよ。
入力例 1
5 5 10 2 3 4 10 5 10 6 9 2 9 4 8 1 7 3 6 8 10 1 8 6 3 5 8 10 2 7
出力例 1
4 4 2 2 2 1
はじめ、全ての都市に電気が通っています。
- 1 番目のイベントによって地点 5 と地点 10 を結ぶ電線 3 が切れます。
- これにより、都市 5 に電気が通らなくなり、電気が通っている都市の数は 4 となります。
- 2 番目のイベントによって地点 2 と地点 9 を結ぶ電線 5 が切れます。
- 3 番目のイベントによって地点 3 と地点 6 を結ぶ電線 8 が切れます。
- これにより、都市 2,3 に電気が通らなくなり、電気が通っている都市の数は 2 となります。
- 4 番目のイベントによって地点 1 と地点 8 を結ぶ電線 10 が切れます。
- 5 番目のイベントによって地点 4 と地点 10 を結ぶ電線 2 が切れます。
- 6 番目のイベントによって地点 1 と地点 7 を結ぶ電線 7 が切れます。
- これにより、都市 1 に電気が通らなくなり、電気が通っている都市の数は 1 となります。
Score : 500 points
Problem Statement
A country has N cities and M power plants, which we collectively call places.
The places are numbered 1,2,\dots,N+M, among which Places 1,2,\dots,N are the cities and Places N+1,N+2,\dots,N+M are the power plants.
This country has E power lines. Power Line i (1 \le i \le E) connects Place U_i and Place V_i bidirectionally.
A city is said to be electrified if one can reach at least one of the power plants from the city using some power lines.
Now, Q events will happen. In the i-th (1 \le i \le Q) event, Power Line X_i breaks, making it unusable. Once a power line breaks, it remains broken in the succeeding events.
Find the number of electrified cities right after each events.
Constraints
- All values in input are integers.
- 1 \le N,M
- N+M \le 2 \times 10^5
- 1 \le Q \le E \le 5 \times 10^5
- 1 \le U_i < V_i \le N+M
- If i \neq j, then U_i \neq U_j or V_i \neq V_j.
- 1 \le X_i \le E
- X_i are distinct.
Input
Input is given from Standard Input in the following format:
N M E U_1 V_1 U_2 V_2 \vdots U_E V_E Q X_1 X_2 \vdots X_Q
Output
Print Q lines.
The i-th line should contain the number of electrified cities right after the i-th event.
Sample Input 1
5 5 10 2 3 4 10 5 10 6 9 2 9 4 8 1 7 3 6 8 10 1 8 6 3 5 8 10 2 7
Sample Output 1
4 4 2 2 2 1
Initially, all cities are electrified.
- The 1-st event breaks Power Line 3 that connects Point 5 and Point 10.
- Now City 5 is no longer electrified, while 4 cities remain electrified.
- The 2-nd event breaks Power Line 5 that connects Point 2 and Point 9.
- The 3-rd event breaks Power Line 8 that connects Point 3 and Point 6.
- Now Cities 2 and 3 are no longer electrified, while 2 cities remain electrified.
- The 4-th event breaks Power Line 10 that connects Point 1 and Point 8.
- The 5-th event breaks Power Line 2 that connects Point 4 and Point 10.
- The 6-th event breaks Power Line 7 that connects Point 1 and Point 7.
- Now City 1 is no longer electrified, while 1 city remains electrified.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
縦 H 行、横 W 列のマス目があります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。
1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i, j) それぞれについて、マス (i, j) には正整数 A_{i, j} が書かれています。また、すべてのマスは白色に塗られています。
高橋君は、縦 h_1 行、横 w_1 列の長方形の黒いスタンプを持っており、青木君は、縦 h_2 行、横 w_2 列の長方形の白いスタンプを持っています。
2 人はこれらのスタンプとマス目を使って対戦ゲームをします。
まず高橋君が、持っている黒いスタンプを 1 回使って、マス目の縦 h_1 行、横 w_1 列の長方形領域を黒色に塗りつぶします。
すなわち、1 \leq i \leq H - h_1 + 1 かつ 1 \leq j \leq W - w_1 + 1 を満たす整数の組 (i, j) を一つ選び、
i \leq r \leq i + h_1 - 1 かつ j \leq c \leq j + w_1 - 1 を満たすすべてのマス (r, c) を黒色に塗りつぶします。
次に青木君が、持っている白いスタンプを 1 回使って、マス目の縦 h_2 行、横 w_2 列の長方形領域を白色に塗りつぶします。
すなわち、1 \leq i \leq H - h_2 + 1 かつ 1 \leq j \leq W - w_2 + 1 を満たす整数の組 (i, j) を一つ選び、
i \leq r \leq i + h_2 - 1 かつ j \leq c \leq j + w_2 - 1 を満たすすべてのマス (r, c) を白色に塗りつぶします。
このとき、青木君が白色に塗るマスがすでに高橋君によって黒色に塗られていた場合は、そのマスの色は白で上書きされます。
上記の通りに高橋君と青木君がスタンプを 1 回ずつ使った後の、黒色に塗られたマスに書かれた整数の合計を、ゲームの「スコア」とします。 高橋君はスコアが出来るだけ大きくなるように、青木君はスコアが出来るだけ小さくなるように、それぞれ最適な戦略をとります。 ゲームのスコアがいくらになるかを求めて下さい。
制約
- 2 \leq H, W \leq 1000
- 1 \leq h_1, h_2 \leq H
- 1 \leq w_1, w_2 \leq W
- 1 \leq A_{i, j} \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W h_1 w_1 h_2 w_2
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
出力
高橋君はスコアが出来るだけ大きくなるように、青木君はスコアが出来るだけ小さくなるように、それぞれ最適な戦略をとるときの、ゲームのスコアを出力せよ。
入力例 1
3 4 2 3 3 1 3 1 4 1 5 9 2 6 5 3 5 8
出力例 1
19
ゲームは以下の通りに進行します。
- はじめ、すべてのマスは白色で塗られています。
- まず高橋君が、持っている縦 2 行横 3 列の黒いスタンプを使って、マス (2, 2), (2, 3), (2 ,4), (3, 2), (3, 3), (3, 4) の 6 つのマスを黒色で塗りつぶします。
- 次に青木君が、持っている縦 3 行横 1 列の白いスタンプを使って、マス (1, 4), (2, 4), (3, 4) を白色で塗りつぶします。
- 最終的に黒色で塗られているマスは、マス (2, 2), (2, 3), (3, 2), (3, 3) の 4 つであるため、ゲームのスコアは 9 + 2 + 3 + 5 = 19 です。
入力例 2
3 4 2 3 3 4 3 1 4 1 5 9 2 6 5 3 5 8
出力例 2
0
青木君がスタンプを使った後、すべてのマスは白色であり、ゲームのスコアは 0 となります。
入力例 3
10 10 3 7 2 3 9 7 19 7 10 4 13 9 4 8 10 15 16 3 18 19 17 12 13 2 12 18 4 9 13 13 6 13 5 2 16 12 2 14 18 17 14 7 8 12 12 13 17 12 14 15 19 7 13 15 5 2 16 10 4 6 1 2 7 8 10 14 14 10 9 13 11 4 9 19 16 12 3 19 19 6 2 19 14 20 15 3 19 19 2 10 1 4 3 15 13 20 5 6 19 1 7 17 10 19
出力例 3
180
Score : 500 points
Problem Statement
There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
For each pair of integers (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, (i, j) contains a positive integer A_{i,j}. Additionally, every square is painted white.
Takahashi has a rectangular black stamp that can cover h_1 rows and w_1 columns, and Aoki has a rectangular white stamp that can cover h_2 rows and w_2 columns. Using these stamps and the grid, they will play a game against each other.
First, Takahashi presses his black stamp to paint a rectangular region with h_1 rows and w_1 columns black.
That is, he chooses a pair of integers (i, j) such that 1 \leq i \leq H - h_1 + 1 and 1 \leq j \leq W - w_1 + 1, and paints black every square (r, c) such that i \leq r \leq i + h_1 - 1 and j \leq c \leq j + w_1 - 1.
Second, Aoki presses his white stamp to paint a rectangular region with h_2 rows and w_2 columns white.
That is, he chooses a pair of integers (i, j) such that 1 \leq i \leq H - h_2 + 1 and 1 \leq j \leq W - w_2 + 1, and paints white every square (r, c) such that i \leq r \leq i + h_2 - 1 and j \leq c \leq j + w_2 - 1.
If some of these squares are already painted black by Takahashi, they will also become white.
The game's score is defined as the sum of the integers written on the squares painted black after Takahashi and Aoki press their stamps as described above. Takahashi follows the optimal strategy to make the score as large as possible, while Aoki follows the optimal strategy to make the score as small as possible. What will the game's score be?
Constraints
- 2 \leq H, W \leq 1000
- 1 \leq h_1, h_2 \leq H
- 1 \leq w_1, w_2 \leq W
- 1 \leq A_{i, j} \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H W h_1 w_1 h_2 w_2
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
Output
Print the game's score when Takahashi follows the optimal strategy to make the score as large as possible and Aoki follows the optimal strategy to make the score as small as possible.
Sample Input 1
3 4 2 3 3 1 3 1 4 1 5 9 2 6 5 3 5 8
Sample Output 1
19
The game will go as follows.
- Initially, all squares are painted white.
- First, Takahashi presses his 2 \times 3 black stamp to paint the following six squares black: (2, 2), (2, 3), (2 ,4), (3, 2), (3, 3), (3, 4).
- Second, Aoki presses his 3 \times 1 white stamp to paint the following three squares white: (1, 4), (2, 4), (3, 4).
- Eventually, the following four squares are painted black: (2, 2), (2, 3), (3, 2), (3, 3), for a score of 9 + 2 + 3 + 5 = 19.
Sample Input 2
3 4 2 3 3 4 3 1 4 1 5 9 2 6 5 3 5 8
Sample Output 2
0
After Aoki presses his stamp, all squares will be white, for a score of 0.
Sample Input 3
10 10 3 7 2 3 9 7 19 7 10 4 13 9 4 8 10 15 16 3 18 19 17 12 13 2 12 18 4 9 13 13 6 13 5 2 16 12 2 14 18 17 14 7 8 12 12 13 17 12 14 15 19 7 13 15 5 2 16 10 4 6 1 2 7 8 10 14 14 10 9 13 11 4 9 19 16 12 3 19 19 6 2 19 14 20 15 3 19 19 2 10 1 4 3 15 13 20 5 6 19 1 7 17 10 19
Sample Output 3
180