Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
xy 平面上に長方形があります。この長方形の各辺は x 軸または y 軸に平行であり、面積は 0 ではありません。
この長方形の 4 つの頂点のうち異なる 3 つの頂点の座標 (x_1, y_1), (x_2, y_2), (x_3, y_3) が与えられるので、残る 1 つの頂点の座標を求めてください。
制約
- -100 \leq x_i, y_i \leq 100
- (x_1, y_1), (x_2, y_2), (x_3, y_3) のすべてを頂点に持つ長方形がただ一つ存在し、その各辺は x 軸または y 軸に平行であり、面積は 0 ではない。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
x_1 y_1 x_2 y_2 x_3 y_3
出力
答えとなる頂点の座標 (x, y) を下記の形式にしたがい空白区切りで出力せよ。
x y
入力例 1
-1 -1 -1 2 3 2
出力例 1
3 -1
(-1, -1), (-1, 2), (3, 2) を頂点とする長方形の残る 1 つの頂点は (3, -1) です。
入力例 2
-60 -40 -60 -80 -20 -80
出力例 2
-20 -40
Score : 100 points
Problem Statement
There is a rectangle in the xy-plane. Each edge of this rectangle is parallel to the x- or y-axis, and its area is not zero.
Given the coordinates of three of the four vertices of this rectangle, (x_1, y_1), (x_2, y_2), and (x_3, y_3), find the coordinates of the other vertex.
Constraints
- -100 \leq x_i, y_i \leq 100
- There uniquely exists a rectangle with all of (x_1, y_1), (x_2, y_2), (x_3, y_3) as vertices, edges parallel to the x- or y-axis, and a non-zero area.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
x_1 y_1 x_2 y_2 x_3 y_3
Output
Print the sought coordinates (x, y) separated by a space in the following format:
x y
Sample Input 1
-1 -1 -1 2 3 2
Sample Output 1
3 -1
The other vertex of the rectangle with vertices (-1, -1), (-1, 2), (3, 2) is (3, -1).
Sample Input 2
-60 -40 -60 -80 -20 -80
Sample Output 2
-20 -40
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
(1,2,3,4,5) を並び替えた整数列 A=(A_1,A_2,A_3,A_4,A_5) が与えられます。
A の隣り合う 2 つの項を入れ替える操作を ちょうど 1 回 行うことで A を昇順にすることができるか判定してください。
制約
- A は (1,2,3,4,5) を並び替えてできる長さ 5 の整数列
入力
入力は以下の形式で標準入力から与えられる。
A_1 A_2 A_3 A_4 A_5
出力
ちょうど 1 回の操作で A を昇順にすることができるならば Yes を、できないならば No を出力せよ。
入力例 1
1 2 4 3 5
出力例 1
Yes
A_3 と A_4 を入れ替えることで A=(1,2,3,4,5) となり、 A を昇順に並び替えることができます。したがって、 Yes を出力してください。
入力例 2
5 3 2 4 1
出力例 2
No
どのような操作をしても A を昇順に並び替えることはできません。
入力例 3
1 2 3 4 5
出力例 3
No
ちょうど 1 回操作をする必要があります。
入力例 4
2 1 3 4 5
出力例 4
Yes
Score : 150 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,A_3,A_4,A_5) obtained by permuting (1,2,3,4,5).
Determine whether A can be sorted in ascending order by performing exactly one operation of swapping two adjacent elements in A.
Constraints
- A is an integer sequence of length 5 obtained by permuting (1,2,3,4,5).
Input
The input is given from Standard Input in the following format:
A_1 A_2 A_3 A_4 A_5
Output
If A can be sorted in ascending order by exactly one operation, print Yes; otherwise, print No.
Sample Input 1
1 2 4 3 5
Sample Output 1
Yes
By swapping A_3 and A_4, A becomes (1,2,3,4,5), so it can be sorted in ascending order. Therefore, print Yes.
Sample Input 2
5 3 2 4 1
Sample Output 2
No
No matter what operation is performed, it is impossible to sort A in ascending order.
Sample Input 3
1 2 3 4 5
Sample Output 3
No
You must perform exactly one operation.
Sample Input 4
2 1 3 4 5
Sample Output 4
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は 2 次元コード TaK Code を考案しました。以下の条件を全て満たすものが TaK Code です。
- 縦 9 マス 横 9 マスの領域である
- 左上及び右下の縦 3 マス 横 3 マスの領域に含まれる計 18 マスは全て黒である
- 左上及び右下の縦 3 マス 横 3 マスの領域に八方位で隣接する計 14 マスは全て白である
TaK Code を回転させることはできません。
縦 N マス、横 M マスのグリッドがあります。
グリッドの状態は N 個の長さ M の文字列 S_1,\ldots,S_N で与えられ、上から i 行目左から j 列目のマスは S_i の j 文字目が # のとき黒、. のとき白です。
グリッドに完全に含まれる縦 9 マス横 9 マスの領域で、TaK Code の条件を満たす箇所を全て求めてください。
制約
- 9 \leq N,M \leq 100
- N,M は整数である
- S_i は
.および#のみからなる長さ M の文字列である
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 \vdots S_N
出力
上から i 行目左から j 列目のマスを左上とする縦 9 マス 横 9 マスの領域が TaK Code の条件を満たす (i,j) の組全てを辞書順の昇順で 1 行に 1 組ずつ、i,j をこの順に空白区切りで出力せよ。
(i,j) の組の辞書順の昇順とは、i の昇順、i が等しい組は j の昇順にすることである。
入力例 1
19 18 ###......###...... ###......###...... ###..#...###..#... ..............#... .................. .................. ......###......### ......###......### ......###......### .###.............. .###......##...... .###.............. ............###... ...##.......###... ...##.......###... .......###........ .......###........ .......###........ ........#.........
出力例 1
1 1 1 10 7 7 10 2
TaK Code は以下のものです。# が黒マス、. が白マス、? が白黒どちらでもよいマスを表します。
###.????? ###.????? ###.????? ....????? ????????? ?????.... ?????.### ?????.### ?????.###
入力で与えられたグリッドの上から 10 マス目左から 2 列目のマスを左上とする縦 9 マス 横 9 マスの領域は以下のようになっており、TaK Code の条件を満たします。
###...... ###...... ###...... ......... ..##..... ..##..... ......### ......### ......###
入力例 2
9 21 ###.#...........#.### ###.#...........#.### ###.#...........#.### ....#...........#.... #########...######### ....#...........#.... ....#.###...###.#.... ....#.###...###.#.... ....#.###...###.#....
出力例 2
1 1
入力例 3
18 18 ######............ ######............ ######............ ######............ ######............ ######............ .................. .................. .................. .................. .................. .................. ............###### ............###### ............###### ............###### ............###### ............######
出力例 3
TaK Code の条件を満たす箇所が 1 つもないこともあります。
Score : 200 points
Problem Statement
Takahashi invented Tak Code, a two-dimensional code. A TaK Code satisfies all of the following conditions:
- It is a region consisting of nine horizontal rows and nine vertical columns.
- All the 18 cells in the top-left and bottom-right three-by-three regions are black.
- All the 14 cells that are adjacent (horizontally, vertically, or diagonally) to the top-left or bottom-right three-by-three region are white.
It is not allowed to rotate a TaK Code.
You are given a grid with N horizontal rows and M vertical columns.
The state of the grid is described by N strings, S_1,\ldots, and S_N, each of length M. The cell at the i-th row from the top and j-th column from the left is black if the j-th character of S_i is #, and white if it is ..
Find all the nine-by-nine regions, completely contained in the grid, that satisfy the conditions of a TaK Code.
Constraints
- 9 \leq N,M \leq 100
- N and M are integers.
- S_i is a string of length M consisting of
.and#.
Input
The input is given from Standard Input in the following format:
N M S_1 \vdots S_N
Output
For all pairs (i,j) such that the nine-by-nine region, whose top-left cell is at the i-th row from the top and j-th columns from the left, satisfies the conditions of a TaK Code, print a line containing i, a space, and j in this order.
The pairs must be sorted in lexicographical ascending order; that is, i must be in ascending order, and within the same i, j must be in ascending order.
Sample Input 1
19 18 ###......###...... ###......###...... ###..#...###..#... ..............#... .................. .................. ......###......### ......###......### ......###......### .###.............. .###......##...... .###.............. ............###... ...##.......###... ...##.......###... .......###........ .......###........ .......###........ ........#.........
Sample Output 1
1 1 1 10 7 7 10 2
A TaK Code looks like the following, where # is a black cell, . is a white cell, and ? can be either black or white.
###.????? ###.????? ###.????? ....????? ????????? ?????.... ?????.### ?????.### ?????.###
In the grid given by the input, the nine-by-nine region, whose top-left cell is at the 10-th row from the top and 2-nd column from the left, satisfies the conditions of a TaK Code, as shown below.
###...... ###...... ###...... ......... ..##..... ..##..... ......### ......### ......###
Sample Input 2
9 21 ###.#...........#.### ###.#...........#.### ###.#...........#.### ....#...........#.... #########...######### ....#...........#.... ....#.###...###.#.... ....#.###...###.#.... ....#.###...###.#....
Sample Output 2
1 1
Sample Input 3
18 18 ######............ ######............ ######............ ######............ ######............ ######............ .................. .................. .................. .................. .................. .................. ............###### ............###### ............###### ............###### ............###### ............######
Sample Output 3
There may be no region that satisfies the conditions of TaK Code.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は chess960 と呼ばれるゲームで遊んでいます。 高橋君はランダムに決めた初期配置が chess960 の条件を満たすか確認するプログラムを書くことにしました。
長さ 8 の文字列 S が与えられます。S には K, Q がちょうど 1 文字ずつ、R, B, N がちょうど 2 文字ずつ含まれます。 S が以下の条件を全て満たしているか判定してください。
-
S において左から x,y\ (x < y) 文字目が
Bであるとする。このとき x と y の偶奇が異なる。 -
Kは 2 つのRの間にある。より形式的には、S において左から x,y\ (x < y) 文字目がRであり、 z 文字目がKであるとする。このとき、 x< z < y が成り立つ。
制約
- S は 長さ 8 の文字列であり、
K,Qがちょうど 1 文字ずつ、R,B,Nがちょうど 2 文字ずつ含まれる。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が条件を満たす場合 Yes を、そうでない場合 No を出力せよ。
入力例 1
RNBQKBNR
出力例 1
Yes
B は左から 3 番目、6 番目にあり、3 と 6 は偶奇が異なります。
また、K は 2 つの R の間にあります。よって条件を満たします。
入力例 2
KRRBBNNQ
出力例 2
No
K は 2 つの R の間にありません。
入力例 3
BRKRBQNN
出力例 3
No
Score : 200 points
Problem Statement
Takahashi is playing a game called Chess960. He has decided to write a code that determines if a random initial state satisfies the conditions of Chess960.
You are given a string S of length eight. S has exactly one K and Q, and exactly two R's, B's , and N's. Determine if S satisfies all of the following conditions.
-
Suppose that the x-th and y-th (x < y) characters from the left of S are
B; then, x and y have different parities. -
Kis between twoR's. More formally, suppose that the x-th and y-th (x < y) characters from the left of S areRand the z-th isK; then x< z < y.
Constraints
- S is a string of length 8 that contains exactly one
KandQ, and exactly twoR's,B's , andN's.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes if S satisfies the conditions; print No otherwise.
Sample Input 1
RNBQKBNR
Sample Output 1
Yes
The 3-rd and 6-th characters are B, and 3 and 6 have different parities.
Also, K is between the two R's. Thus, the conditions are fulfilled.
Sample Input 2
KRRBBNNQ
Sample Output 2
No
K is not between the two R's.
Sample Input 3
BRKRBQNN
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
xy 座標平面上に N 人の人がいます。人 i は (X_i, Y_i) にいます。すべての人は異なる地点にいます。
L, R からなる長さ N の文字列 S があります。
人 i は S_i = R ならば右向きに、S_i = L ならば左向きに、一斉に同じ速度で歩き始めます。ここで、右は x 軸の正の向き、左は x 軸の負の向きです。
たとえば (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL の場合は次の図のように動きます。

反対の向きに歩いている人同士が同じ地点に来ることを「衝突」と呼びます。すべての人が歩き続けたとき、衝突は発生しますか?
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq X_i \leq 10^9
- 0 \leq Y_i \leq 10^9
- i \neq j ならば (X_i, Y_i) \neq (X_j, Y_j) である。
- X_i, Y_i はすべて整数である。
- S は
LおよびRからなる長さ N の文字列である。
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N S
出力
衝突が発生するならば Yes を、発生しないならば No を出力せよ。
入力例 1
3 2 3 1 1 4 1 RRL
出力例 1
Yes
この入力は問題文にある例と同じケースです。
すべての人が歩き続けると人 2 と人 3 が衝突します。よって Yes を出力します。
入力例 2
2 1 1 2 1 RR
出力例 2
No
人 1 と人 2 は同じ向きに歩いているので衝突することはありません。
入力例 3
10 1 3 1 4 0 0 0 2 0 4 3 1 2 4 4 2 4 4 3 3 RLRRRLRLRR
出力例 3
Yes
Score : 300 points
Problem Statement
There are N people in an xy-plane. Person i is at (X_i, Y_i). The positions of all people are different.
We have a string S of length N consisting of L and R.
If S_i = R, Person i is facing right; if S_i = L, Person i is facing left. All people simultaneously start walking in the direction they are facing. Here, right and left correspond to the positive and negative x-direction, respectively.
For example, the figure below shows the movement of people when (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL.

We say that there is a collision when two people walking in opposite directions come to the same position. Will there be a collision if all people continue walking indefinitely?
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq X_i \leq 10^9
- 0 \leq Y_i \leq 10^9
- (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
- All X_i and Y_i are integers.
- S is a string of length N consisting of
LandR.
Input
Input is given from Standard Input in the following format:
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N S
Output
If there will be a collision, print Yes; otherwise, print No.
Sample Input 1
3 2 3 1 1 4 1 RRL
Sample Output 1
Yes
This input corresponds to the example in the Problem Statement.
If all people continue walking, Person 2 and Person 3 will collide. Thus, Yes should be printed.
Sample Input 2
2 1 1 2 1 RR
Sample Output 2
No
Since Person 1 and Person 2 walk in the same direction, they never collide.
Sample Input 3
10 1 3 1 4 0 0 0 2 0 4 3 1 2 4 4 2 4 4 3 3 RLRRRLRLRR
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
2 つの文字列 A,B に対して、A の末尾に B を連結した文字列を A+B と表します。
N 個の文字列 S_1,\ldots,S_N が与えられます。i=1,\ldots,N の順に、次の指示に従って加工して出力してください。
- S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が存在しないならば、S_i を出力する。
- S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が X 個 (X>0) 存在するならば、X を文字列として扱って S_i+
(+X+)を出力する。
制約
- 1 \leq N \leq 2\times 10^5
- S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
問題文中の指示にしたがって、N 行出力せよ。
入力例 1
5 newfile newfile newfolder newfile newfolder
出力例 1
newfile newfile(1) newfolder newfile(2) newfolder(1)
入力例 2
11 a a a a a a a a a a a
出力例 2
a a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
Score : 300 points
Problem Statement
For two strings A and B, let A+B denote the concatenation of A and B in this order.
You are given N strings S_1,\ldots,S_N. Modify and print them as follows, in the order i=1, \ldots, N:
- if none of S_1,\ldots,S_{i-1} is equal to S_i, print S_i;
- if X (X>0) of S_1,\ldots,S_{i-1} are equal to S_i, print S_i+
(+X+), treating X as a string.
Constraints
- 1 \leq N \leq 2\times 10^5
- S_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print N lines as specified in the Problem Statement.
Sample Input 1
5 newfile newfile newfolder newfile newfolder
Sample Output 1
newfile newfile(1) newfolder newfile(2) newfolder(1)
Sample Input 2
11 a a a a a a a a a a a
Sample Output 2
a a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
英大小文字からなる文字列 S が与えられます。
S に以下の操作を 10^{100} 回繰り返します。
- まず、 S の大文字を小文字に、小文字を大文字に書き換えた文字列を T とする。
- その後、 S と T とをこの順に連結した文字列を新たな S とする。
Q 個の質問に答えて下さい。 そのうち i 個目は次の通りです。
- 全ての操作を終えた後の S の先頭から K_i 文字目を求めよ。
制約
- S は英大小文字からなる長さ 1 以上 2 \times 10^5 以下の文字列
- Q,K_i は整数
- 1 \le Q \le 2 \times 10^5
- 1 \le K_i \le 10^{18}
入力
入力は以下の形式で標準入力から与えられる。
S Q K_1 K_2 \dots K_Q
出力
i 個目の質問の答えを C_i とする時、以下の形式で 1 行に空白区切りで出力せよ。
C_1 C_2 \dots C_Q
入力例 1
aB 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
出力例 1
a B A b A b a B A b a B a B A b
操作前の S = aB です。
aBに 1 回操作を行うとaBAbとなります。aBに 2 回操作を行うとaBAbAbaBとなります。- \dots
10^{100} 回の操作を終えた後の S = aBAbAbaBAbaBaBAb... です。
入力例 2
qWeRtYuIoP 8 1 1 2 3 5 8 13 21
出力例 2
q q W e t I E Q
入力例 3
AnUoHrjhgfLMcDIpzxXmEWPwBZvbKqQuiJTtFSlkNGVReOYCdsay 5 1000000000000000000 123456789 1 987654321 999999999999999999
出力例 3
K a A Z L
Score : 350 points
Problem Statement
You are given a string S consisting of uppercase and lowercase English letters.
We perform the following operation on S 10^{100} times:
- First, create a string T by changing uppercase letters in S to lowercase, and lowercase letters to uppercase.
- Then, concatenate S and T in this order to form a new S.
Answer Q queries. The i-th query is as follows:
- Find the K_i-th character from the beginning of S after all operations are completed.
Constraints
- S is a string consisting of uppercase and lowercase English letters, with length between 1 and 2 \times 10^5, inclusive.
- Q and K_i are integers.
- 1 \le Q \le 2 \times 10^5
- 1 \le K_i \le 10^{18}
Input
The input is given from Standard Input in the following format:
S Q K_1 K_2 \dots K_Q
Output
Let C_i be the answer to the i-th query. Print them in a single line, separated by spaces, in the following format:
C_1 C_2 \dots C_Q
Sample Input 1
aB 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Sample Output 1
a B A b A b a B A b a B a B A b
Before the operations, S = aB.
- After performing the operation once on
aB, it becomesaBAb. - After performing the operation twice on
aB, it becomesaBAbAbaB. - \dots
After performing the operation 10^{100} times, S = aBAbAbaBAbaBaBAb...
Sample Input 2
qWeRtYuIoP 8 1 1 2 3 5 8 13 21
Sample Output 2
q q W e t I E Q
Sample Input 3
AnUoHrjhgfLMcDIpzxXmEWPwBZvbKqQuiJTtFSlkNGVReOYCdsay 5 1000000000000000000 123456789 1 987654321 999999999999999999
Sample Output 3
K a A Z L
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
この問題は インタラクティブな問題 (あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。
1 から N の番号がついた N 頂点からなる木 G が与えられます。i 番目の辺は頂点 U_i と頂点 V_i を結んでいます。
この木 G を使って、あなたと高橋君がゲームをします。まず、あなたは先手後手を決めます。その後、先手から順に交互に以下の操作を行います。
- 1 \leq i < j \leq N を満たす整数の組 (i,j) であって、以下の条件をともに満たすものを選び、頂点 i と頂点 j を結ぶ辺を G に追加する。
- G は頂点 i と頂点 j を結ぶ辺を持たない
- 頂点 i と頂点 j を結ぶ辺を G に追加しても、奇閉路ができない
操作を行えなくなった方が負けであり、負けでない方が勝ちです。実際に高橋君とゲームをして勝ってください。
奇閉路とは?
G の頂点の列 (v_0,v_1,\ldots,v_k) が以下の条件を全て満たすとき、かつ、そのときに限りこの列を G の奇閉路といいます。
- k は奇数である。
- v_0=v_k である。
- 全ての 1\leq i \leq k に対して、v_{i-1} と v_{i} を結ぶ辺が存在する。
制約
- 2 \leq N \leq 100
- 1 \leq U_i < V_i \leq N
- 与えられるグラフは木である
- 入力は全て整数である
入出力
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。
最初に、N および G の情報を標準入力から受け取ってください。以下の形式で与えられます。
N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
次に、あなたが先手と後手のどちらを選ぶか決めてください。先手を選ぶ場合 First、後手を選ぶ場合 Second と標準出力に出力してください。
その後ゲームが始まります。
あなたの手番では、操作のために選んだ整数の組 (i,j) を、i,j の順に空白区切りで標準出力に出力してください。
i j
高橋君の手番では、2 つの整数 i,j が順に空白区切りで標準入力に与えられます。
i j
(i,j)=(-1,-1) のとき、あなたがゲームに勝利しゲームが終了したことを意味します。この場合、直ちにプログラムを終了してください。
それ以外のとき、(i,j) は高橋君が操作のために選んだ整数の組を表します。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 対話の途中で誤った出力形式による出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
- ゲームが終了したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
入出力例
| 入力 | 出力 | 説明 |
|---|---|---|
41 22 33 4 | まず整数 N および G の情報が与えられます。 | |
First | あなたは先手を選びました。 | |
1 4 | あなたは (1,4) に対して操作を行いました。 | |
-1 -1 | 高橋君が操作を行えなくなったためゲームを終了します。ジャッジ結果は になります。 |
Score : 425 points
Problem Statement
This problem is an interactive problem (in which your program and the judge system communicate via input and output).
You are given a tree G with N vertices numbered 1 to N. The i-th edge connects vertices U_i and V_i.
You will play a game with Takahashi using this tree G. First, you decide who is first and who is second. Then, starting from the first player, take turns performing the following operation:
- Choose a pair of integers (i,j) with 1 \leq i < j \leq N that satisfies both of the following conditions, then add an edge connecting vertices i and j to G.
- G does not already have an edge connecting vertices i and j.
- Adding an edge connecting vertices i and j does not create an odd cycle.
A player who cannot perform this operation loses, and the other player wins. Play this game against Takahashi and win.
What is an odd cycle?
A sequence of vertices (v_0,v_1,\ldots,v_k) of G is called an odd cycle if and only if all of the following conditions are satisfied:
- k is odd.
- v_0=v_k.
- For every 1\leq i \leq k, there is an edge connecting v_{i-1} and v_{i}.
Constraints
- 2 \leq N \leq 100
- 1 \leq U_i < V_i \leq N
- The given graph is a tree.
- All input values are integers.
Interaction
This is an interactive problem (in which your program and the judge system communicate via input and output).
First, read N and the edges of G from Standard Input, given in the following format:
N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Then, decide whether you go first or second. If you choose first, print First; if second, print Second.
Then, the game begins.
On your turn, output the chosen pair (i,j) by printing two integers i and j in this order, separated by a space:
i j
On Takahashi's turn, two integers i,j will be given in order, separated by a space, via Standard Input:
i j
If (i,j)=(-1,-1), it means he can no longer make a move and you win. Immediately terminate your program in this case.
Otherwise, (i,j) is the pair of integers he has chosen.
Notes
- After each output, be sure to print a newline and flush Standard Output. Otherwise, you may get TLE.
- If you produce output in an incorrect format or your program ends prematurely, the judge result is indeterminate.
- Once the game finishes, terminate your program immediately. Otherwise, the judge result is indeterminate.
Sample Interaction
| Input | Output | Explanation |
|---|---|---|
41 22 33 4 |
First, you receive N and the edges of G. | |
First |
You choose to go first. | |
1 4 |
You add an edge between vertices 1 and 4. | |
-1 -1 |
Takahashi can no longer move, so you win. The judge result will be . |
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
xy 平面上にある AtCoder 王国の道路は、全ての整数 n に対する直線 x=n および直線 y=n からなります。 そのうち、全ての整数 n に対する直線 x=Bn および直線 y=Bn は大通りです。
高橋君は (x,y) にいるときに、(x,y-1),(x,y+1),(x+1,y),(x-1,y) のいずれかに移動することができます。 また、1 回の移動につき、大通りに沿って移動する場合は 1 秒、それ以外の場合は K 秒かかります。
(S_x,S_y) にいる高橋君が (G_x,G_y) に移動するのに最短で何秒かかるかを求めてください。
この問題は T ケース与えられます。
制約
- 1 \le T \le 2 \times 10^5
- 1 \le B,K \le 10^9
- 0 \le S_x,S_y,G_x,G_y \le 10^9
- 入力はすべて整数。
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{testcase}_1
\mathrm{testcase}_2
\vdots
\mathrm{testcase}_T
それぞれのテストケースは以下の形式で与えられる。
B K S_x S_y G_x G_y
出力
T 行出力せよ。i 行目には i 個目のテストケースの解を出力せよ。
入力例 1
4 3 4 2 2 4 4 5 6 2 3 2 3 1 1000000000 0 0 1000000000 1000000000 1000000000 1000000000 500000000 500000000 1000000000 1000000000
出力例 1
10 0 2000000000 500000000500000000
1 個目のテストケースについて、(2,2) から (2,3) に 4 秒かけて移動し、(2,3) から (4,3) に 2 秒かけて移動し、(4,3) から (4,4) に 4 秒かけて移動することで 10 秒で (2,2) から (4,4) に移動することができます。10 秒より早く移動することはできないため、解は 10 です。
2 個目のテストケースについて、初めから (G_x,G_y) にいるため解は 0 です。
入力例 2
10 928184439 674654465 203937094 186855052 851783856 805293696 55480262 448852233 823161539 786348805 550018803 322680316 891870741 235679524 32164572 497841190 620600021 96487871 321502816 428964257 499656016 521484999 717623189 824784374 144040837 680268887 76238777 371138006 350230937 78690135 768922620 799628518 403830696 60449731 218880692 88319939 482031503 121412614 472330444 284479575 949635609 427232765 389524418 132987043 656496997 678732442 23028233 488463974 857778764 629964237 714551548 739330018 579247790 874251485 461612428 535402609 555160129 833592114 44418273 287363785
出力例 2
177606591118701316 6205925075792263 30320747646118343 84136273267803188 83764071874751489 118960470930399064 2929499649126153 16403238161749820 84995699148879437 71771264361119335
Score : 500 points
Problem Statement
The roads in the Kingdom of AtCoder, which lies on the xy-plane, are the lines x=n and y=n for all integers n. Among them, the lines x=Bn and y=Bn for all integers n are main roads.
When Takahashi is at (x,y), he can move to (x,y-1), (x,y+1), (x+1,y), or (x-1,y). Each move takes 1 second along a main road and K seconds otherwise.
Find the minimum number of seconds Takahashi needs to get from (S_x, S_y) to (G_x, G_y).
You will have T test cases to solve.
Constraints
- 1 \le T \le 2 \times 10^5
- 1 \le B,K \le 10^9
- 0 \le S_x,S_y,G_x,G_y \le 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
T
\mathrm{testcase}_1
\mathrm{testcase}_2
\vdots
\mathrm{testcase}_T
Each test case is in the following format:
B K S_x S_y G_x G_y
Output
Print T lines. The i-th line should contain the answer to the i-th test case.
Sample Input 1
4 3 4 2 2 4 4 5 6 2 3 2 3 1 1000000000 0 0 1000000000 1000000000 1000000000 1000000000 500000000 500000000 1000000000 1000000000
Sample Output 1
10 0 2000000000 500000000500000000
For the 1-st test case, he can go from (2,2) to (2,3) in 4 seconds, from (2,3) to (4,3) in 2 seconds, and from (4,3) to (4,4) in 4 seconds to get from (2,2) to (4,4) in 10 seconds. It is impossible to get there in less than 10 seconds, so the answer is 10.
For the 2-nd test case, he is already at (G_x, G_y) in the beginning, so the answer is 0.
Sample Input 2
10 928184439 674654465 203937094 186855052 851783856 805293696 55480262 448852233 823161539 786348805 550018803 322680316 891870741 235679524 32164572 497841190 620600021 96487871 321502816 428964257 499656016 521484999 717623189 824784374 144040837 680268887 76238777 371138006 350230937 78690135 768922620 799628518 403830696 60449731 218880692 88319939 482031503 121412614 472330444 284479575 949635609 427232765 389524418 132987043 656496997 678732442 23028233 488463974 857778764 629964237 714551548 739330018 579247790 874251485 461612428 535402609 555160129 833592114 44418273 287363785
Sample Output 2
177606591118701316 6205925075792263 30320747646118343 84136273267803188 83764071874751489 118960470930399064 2929499649126153 16403238161749820 84995699148879437 71771264361119335