実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
1 以上 9 以下の整数 N が入力として与えられます。
数字 N を N 個繋げて得られる文字列を出力してください。
制約
- N は 1 以上 9 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
3
出力例 1
333
3 を 3 個繋げて得られる文字列は 333 です。
入力例 2
9
出力例 2
999999999
Score : 100 points
Problem Statement
You are given an integer N between 1 and 9, inclusive, as input.
Concatenate N copies of the digit N and print the resulting string.
Constraints
- N is an integer between 1 and 9, inclusive.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
3
Sample Output 1
333
Concatenate three copies of the digit 3 to yield the string 333.
Sample Input 2
9
Sample Output 2
999999999
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
高橋君は、時刻 0 にパソコンの電源をつけ、それからマウスを N 回クリックしました。i(1 \le i \le N) 回目のクリックは時刻 T_i に行われました。
高橋君が時刻 x_1 と時刻 x_2 (ただし x_1 < x_2)にマウスを連続してクリックしたとき、x_2 - x_1 \le D であれば時刻 x_2 にダブルクリックが成立したと言います。
高橋君が最初にダブルクリックを成立させた時刻を求めてください。ただし、高橋君が 1 回もダブルクリックを成立させていないならば -1 を出力してください。
制約
- 1 \le N \le 100
- 1 \le D \le 10^9
- 1 \le T_i \le 10^9(1 \le i \le N)
- T_i < T_{i+1}(1 \le i \le N-1)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D T_1 T_2 \dots T_N
出力
高橋君が 1 回でもダブルクリックを成立させたならば最初にダブルクリックが成立した時刻を、そうでないならば -1 を出力せよ。
入力例 1
4 500 300 900 1300 1700
出力例 1
1300
高橋君は時刻 900,1300 にマウスをクリックしていて、1300 - 900 \le 500 であるため時刻 1300 にダブルクリックが成立しています。
時刻 1300 より前にダブルクリックは成立していないため、1300 を出力してください。
入力例 2
5 99 100 200 300 400 500
出力例 2
-1
高橋君は 1 回もダブルクリックを成立させていません。よって、-1 を出力してください。
入力例 3
4 500 100 600 1100 1600
出力例 3
600
高橋君が複数回ダブルクリックを成立させていても、そのうち最初の時刻のみを出力することに注意してください。
Score : 100 points
Problem Statement
Takahashi turned on a computer at time 0 and clicked the mouse N times. The i-th (1 \le i \le N) click was at time T_i.
If he consecutively clicked the mouse at time x_1 and time x_2 (where x_1 < x_2), a double click is said to be fired at time x_2 if and only if x_2 - x_1 \le D.
What time was a double click fired for the first time? If no double click was fired, print -1 instead.
Constraints
- 1 \le N \le 100
- 1 \le D \le 10^9
- 1 \le T_i \le 10^9(1 \le i \le N)
- T_i < T_{i+1}(1 \le i \le N-1)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N D T_1 T_2 \dots T_N
Output
If at least one double click was fired, print the time of the first such event; otherwise, print -1.
Sample Input 1
4 500 300 900 1300 1700
Sample Output 1
1300
Takahashi clicked the mouse at time 900 and 1300. Since 1300 - 900 \le 500, a double click was fired at time 1300.
A double click had not been fired before time 1300, so 1300 should be printed.
Sample Input 2
5 99 100 200 300 400 500
Sample Output 2
-1
No double click was fired, so print -1.
Sample Input 3
4 500 100 600 1100 1600
Sample Output 3
600
If multiple double clicks were fired, be sure to print only the first such event.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
正整数 N が与えられます。
N 行 N 列のマス目があり、上から i 行目、左から j 列目のマスには数字 A_{i,j} が書かれています。
このマス目は上下および左右がつながっているものとします。つまり以下が全て成り立ちます。
- (1,i) の上のマスは (N,i) であり、(N,i) の下のマスは (1,i) である。(1\le i\le N)
- (i,1) の左のマスは (i,N) であり、(i,N) の右のマスは (i,1) である。(1\le i\le N)
高橋君は、上下左右および斜めの 8 方向のうちいずれかを初めに選びます。そして、好きなマスから決めた方向に 1 マス移動することを N-1 回繰り返します。
高橋君は N 個のマス上を移動することになりますが、高橋君が通ったマスに書かれている数字を左から通った順番に並べた整数としてあり得る最大のものを求めてください。
制約
- 1 \le N \le 10
- 1 \le A_{i,j} \le 9
- 入力はすべて整数。
入力
入力は以下の形式で標準入力から与えられる。
N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
出力
答えを出力せよ。
入力例 1
4 1161 1119 7111 1811
出力例 1
9786
高橋君が上から 2 行目、左から 4 列目のマスから出発し、右下に進むことで、通ったマスに書かれた数字を並べ 9786 を作ることができます。 9786 より大きい値を作ることはできないため、9786 が解です。
入力例 2
10 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111
出力例 2
1111111111
32bit整数型に答えが収まるとは限らないことに注意してください。
Score : 200 points
Problem Statement
You are given a positive integer N.
We have a grid with N rows and N columns, where the square at the i-th row from the top and j-th column from the left has a digit A_{i,j} written on it.
Assume that the upper and lower edges of this grid are connected, as well as the left and right edges. In other words, all of the following holds.
- (N,i) is just above (1,i), and (1,i) is just below (N,i). (1\le i\le N).
- (i,N) is just to the left of (i,1), and (i,1) is just to the right of (i,N). (1\le i\le N).
Takahashi will first choose one of the following eight directions: up, down, left, right, and the four diagonal directions. Then, he will start on a square of his choice and repeat moving one square in the chosen direction N-1 times.
In this process, Takahashi visits N squares. Find the greatest possible value of the integer that is obtained by arranging the digits written on the squares visited by Takahashi from left to right in the order visited by him.
Constraints
- 1 \le N \le 10
- 1 \le A_{i,j} \le 9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
Output
Print the answer.
Sample Input 1
4 1161 1119 7111 1811
Sample Output 1
9786
If Takahashi starts on the square at the 2-nd row from the top and 4-th column from the left and goes down and to the right, the integer obtained by arranging the digits written on the visited squares will be 9786. It is impossible to make a value greater than 9786, so the answer is 9786.
Sample Input 2
10 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111 1111111111
Sample Output 2
1111111111
Note that the answer may not fit into a 32-bit integer.
実行時間制限: 2 sec / メモリ制限: 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.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
xy 座標平面上の 2 つの格子点 (x_1, y_1), (x_2, y_2) からの距離がともに \sqrt{5} である格子点は存在しますか?
注記
xy 座標平面上にある点のうち、x 座標と y 座標がともに整数である点を格子点と呼びます。
また、xy 平面上の 2 点 (a, b), (c, d) の距離をユークリッド距離 \sqrt{(a - c)^2 + (b-d)^2} として定義します。
参考として、xy 平面の (0, 0) の上に黒丸を、(0, 0) からの距離が \sqrt{5} となる格子点の上に白丸を書いた図は以下のようになります。(x,y いずれかが整数となる部分に目盛り線を引いています。)

制約
- -10^9 \leq x_1 \leq 10^9
- -10^9 \leq y_1 \leq 10^9
- -10^9 \leq x_2 \leq 10^9
- -10^9 \leq y_2 \leq 10^9
- (x_1, y_1) \neq (x_2, y_2)
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
x_1 y_1 x_2 y_2
出力
条件を満たす格子点が存在する場合は Yes を、存在しない場合は No を出力せよ。
入力例 1
0 0 3 3
出力例 1
Yes
- 点 (2,1) と (x_1, y_1) の距離は \sqrt{(0-2)^2 + (0-1)^2} = \sqrt{5}
- 点 (2,1) と (x_2, y_2) の距離は \sqrt{(3-2)^2 + (3-1)^2} = \sqrt{5}
- 点 (2, 1) は格子点
なので点 (2, 1) は条件を満たします。よって Yes を出力します。
なお、点 (1, 2) も条件を満たすことが同様にして確認できます。
入力例 2
0 1 2 3
出力例 2
No
条件を満たす格子点は存在しません。よって No を出力します。
入力例 3
1000000000 1000000000 999999999 999999999
出力例 3
Yes
点 (10^9 + 1, 10^9 - 2) および点 (10^9 - 2, 10^9 + 1)が条件を満たします。
Score : 300 points
Problem Statement
On an xy-coordinate plane, is there a lattice point whose distances from two lattice points (x_1, y_1) and (x_2, y_2) are both \sqrt{5}?
Notes
A point on an xy-coordinate plane whose x and y coordinates are both integers is called a lattice point.
The distance between two points (a, b) and (c, d) is defined to be the Euclidean distance between them, \sqrt{(a - c)^2 + (b-d)^2}.
The following figure illustrates an xy-plane with a black circle at (0, 0) and white circles at the lattice points whose distances from (0, 0) are \sqrt{5}. (The grid shows where either x or y is an integer.)

Constraints
- -10^9 \leq x_1 \leq 10^9
- -10^9 \leq y_1 \leq 10^9
- -10^9 \leq x_2 \leq 10^9
- -10^9 \leq y_2 \leq 10^9
- (x_1, y_1) \neq (x_2, y_2)
- 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
Output
If there is a lattice point satisfying the condition, print Yes; otherwise, print No.
Sample Input 1
0 0 3 3
Sample Output 1
Yes
- The distance between points (2,1) and (x_1, y_1) is \sqrt{(0-2)^2 + (0-1)^2} = \sqrt{5};
- the distance between points (2,1) and (x_2, y_2) is \sqrt{(3-2)^2 + (3-1)^2} = \sqrt{5};
- point (2, 1) is a lattice point,
so point (2, 1) satisfies the condition. Thus, Yes should be printed.
One can also assert in the same way that (1, 2) also satisfies the condition.
Sample Input 2
0 1 2 3
Sample Output 2
No
No lattice point satisfies the condition, so No should be printed.
Sample Input 3
1000000000 1000000000 999999999 999999999
Sample Output 3
Yes
Point (10^9 + 1, 10^9 - 2) and point (10^9 - 2, 10^9 + 1) satisfy the condition.
実行時間制限: 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
配点 : 350 点
問題文
数直線上に 1 から N の番号がつけられた N 匹の蟻がいます。 蟻 i (1 \leq i \leq N) ははじめ座標 X_i にいて、正負どちらかの方向を向いています。はじめに全ての蟻は相異なる座標にいます。各蟻が向いている方向は長さ N の 01 文字列 S で表され、S_i が 0 のとき蟻 i は負の方向を向いており、 1 のとき蟻 i は正の方向を向いています。
現在を時刻 0 とし、時刻 (T+0.1) までの (T+0.1) 単位時間にわたって、N 匹の蟻がそれぞれの向いている方向に向かって単位時間あたり 1 の速さで移動します。 複数の蟻が同じ座標に到達すると、それらの蟻はすれ違い、方向や速度を変えずに通り過ぎます。 (T+0.1) 単位時間が経過したとき、すべての蟻は停止します。
1 \leq i < j \leq N を満たし、今から時刻 (T+0.1) までに蟻 i と蟻 j がすれ違う整数の組 (i,j) の個数を求めてください。
制約
- 2 \leq N \leq 2 \times 10^{5}
- 1 \leq T \leq 10^{9}
- S は
0と1からなる長さ N の文字列 - -10^{9} \leq X_i \leq 10^{9} (1 \leq i \leq N)
- X_i \neq X_j (1 \leq i < j \leq N)
- N,T,X_i (1 \leq i \leq N) は整数
入力
入力は以下の形式で標準入力から与えられる。
N T S X_1 X_2 ... X_N
出力
答えを出力せよ。
入力例 1
6 3 101010 -5 -1 0 1 2 4
出力例 1
5
以下の 5 つの蟻の組み合わせがすれ違います。
- 蟻 3 と蟻 4 が時刻 0.5 にすれ違う。
- 蟻 5 と蟻 6 が時刻 1 にすれ違う。
- 蟻 1 と蟻 2 が時刻 2 にすれ違う。
- 蟻 3 と蟻 6 が時刻 2 にすれ違う。
- 蟻 1 と蟻 4 が時刻 3 にすれ違う。
これ以外の蟻の組み合わせはすれ違うことはないため、5 を出力します。
入力例 2
13 656320850 0100110011101 -900549713 -713494784 -713078652 -687818593 -517374932 -498415009 -472742091 -390030458 -379340552 -237481538 -44636942 352721061 695864366
出力例 2
14
Score : 350 points
Problem Statement
There are N ants on a number line, labeled 1 to N. Ant i (1 \leq i \leq N) starts at coordinate X_i and faces either a positive or negative direction. Initially, all ants are at distinct coordinates. The direction each ant is facing is represented by a binary string S of length N, where ant i is facing the negative direction if S_i is 0 and the positive direction if S_i is 1.
Let the current time be 0, and the ants move in their respective directions at a speed of 1 unit per unit time for (T+0.1) units of time until time (T+0.1). If multiple ants reach the same coordinate, they pass through each other without changing direction or speed. After (T+0.1) units of time, all ants stop.
Find the number of pairs (i, j) such that 1 \leq i < j \leq N and ants i and j pass each other from now before time (T+0.1).
Constraints
- 2 \leq N \leq 2 \times 10^{5}
- 1 \leq T \leq 10^{9}
- S is a string of length N consisting of
0and1. - -10^{9} \leq X_i \leq 10^{9} (1 \leq i \leq N)
- X_i \neq X_j (1 \leq i < j \leq N)
- N, T, and X_i (1 \leq i \leq N) are integers.
Input
The input is given from Standard Input in the following format:
N T S X_1 X_2 ... X_N
Output
Print the answer.
Sample Input 1
6 3 101010 -5 -1 0 1 2 4
Sample Output 1
5
The following five pairs of ants pass each other:
- Ant 3 and ant 4 pass each other at time 0.5.
- Ant 5 and ant 6 pass each other at time 1.
- Ant 1 and ant 2 pass each other at time 2.
- Ant 3 and ant 6 pass each other at time 2.
- Ant 1 and ant 4 pass each other at time 3.
No other pairs of ants pass each other, so print 5.
Sample Input 2
13 656320850 0100110011101 -900549713 -713494784 -713078652 -687818593 -517374932 -498415009 -472742091 -390030458 -379340552 -237481538 -44636942 352721061 695864366
Sample Output 2
14
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
AtCoder国には N 個の都市があり、都市 1 , 都市 2 , \ldots , 都市 N と番号付けられています。 最初、どの 2 つの相異なる都市の間も双方向に通れる道で結ばれていましたが、老朽化が進み、これらのうち M 本の道が使えなくなってしまいました。具体的には 1\leq i \leq M について都市 U_i と都市 V_i を結ぶ道が使えなくなってしまいました。
いま、高橋君は都市 1 で始まり、都市 1 で終わる K 日間の旅をしようと考えました。都市 1 で始まり、都市 1 で終わる K 日間の旅とは、 K+1 個の都市の列 (A_0, A_1, \ldots, A_K) であって、A_0=A_K=1 をみたし、 0\leq i\leq K-1 について A_i と A_{i+1} が相異なり、かつ都市 A_i と都市 A_{i+1} が現在も使用可能な道で結ばれているものを指します。
都市 1 で始まり、都市 1 で終わる K 日間の相異なる旅の数を 998244353 で割った余りを出力してください。ただし、 2 つの K 日間の旅 (A_0, A_1, \ldots, A_K) と (B_0, B_1, \ldots, B_K) が相異なるとは、ある i が存在して A_i\neq B_i となることを言います。
制約
- 2 \leq N \leq 5000
- 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
- 2 \leq K \leq 5000
- 1 \leq U_i<V_i \leq N
- (U_i, V_i) は全て互いに相異なる。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M K U_1 V_1 : U_M V_M
出力
答えを出力せよ。
入力例 1
3 1 4 2 3
出力例 1
4
次のような 4 種類の旅が存在します。
- (1,2,1,2,1)
- (1,2,1,3,1)
- (1,3,1,2,1)
- (1,3,1,3,1)
これ以外に条件をみたすようなものは無いため、 4 を出力します。
入力例 2
3 3 3 1 2 1 3 2 3
出力例 2
0
使える道が 1 本も残っておらず、条件をみたすような旅は存在しません。
入力例 3
5 3 100 1 2 4 5 2 3
出力例 3
428417047
Score : 500 points
Problem Statement
The Republic of AtCoder has N cities, called City 1, City 2, \ldots, City N. Initially, there was a bidirectional road between every pair of different cities, but M of these roads have become unusable due to deterioration over time. More specifically, for each 1\leq i \leq M, the road connecting City U_i and City V_i has become unusable.
Takahashi will go for a K-day trip that starts and ends in City 1. Formally speaking, a K-day trip that starts and ends in City 1 is a sequence of K+1 cities (A_0, A_1, \ldots, A_K) such that A_0=A_K=1 holds and for each 0\leq i\leq K-1, A_i and A_{i+1} are different and there is still a usable road connecting City A_i and City A_{i+1}.
Print the number of different K-day trips that start and end in City 1, modulo 998244353. Here, two K-day trips (A_0, A_1, \ldots, A_K) and (B_0, B_1, \ldots, B_K) are said to be different when there exists an i such that A_i\neq B_i.
Constraints
- 2 \leq N \leq 5000
- 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
- 2 \leq K \leq 5000
- 1 \leq U_i<V_i \leq N
- All pairs (U_i, V_i) are pairwise distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M K U_1 V_1 : U_M V_M
Output
Print the answer.
Sample Input 1
3 1 4 2 3
Sample Output 1
4
There are four different trips as follows.
- (1,2,1,2,1)
- (1,2,1,3,1)
- (1,3,1,2,1)
- (1,3,1,3,1)
No other trip is valid, so we should print 4.
Sample Input 2
3 3 3 1 2 1 3 2 3
Sample Output 2
0
No road remains usable, so there is no valid trip.
Sample Input 3
5 3 100 1 2 4 5 2 3
Sample Output 3
428417047
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
頂点に 1 から N の、辺に 1 から M の番号がついた N 頂点 M 辺の単純グラフがあります。辺 i は頂点 u_i と頂点 v_i を結んでいます。
各頂点にはランプが 1 個ずつ載っています。はじめ、全てのランプは消えています。
以下の操作を 0 回以上 M 回以下行うことで、ランプがちょうど K 個ついた状態にできるかどうかを判定してください。
- 辺を 1 本選ぶ。辺の両端点を u, v とする。u, v に載っているランプの状態を反転させる。つまり、ランプがついていたら消して、消えていたらつける。
また、ちょうど K 個のランプがついた状態にすることが可能な場合は、そのような操作の手順を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min\left( 2 \times 10^5, \frac{N(N-1)}{2} \right)
- 0 \leq K \leq N
- 1 \leq u_i \lt v_i \leq N
- 入力で与えられるグラフは単純
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
ちょうど K 個のランプがついた状態にすることが不可能な場合は No を出力せよ。
可能な場合はまず Yes を出力して、その後に操作の手順を以下の形式で出力せよ。
X e_1 e_2 \dots e_X
ここで、X は操作回数を、e_i は i 番目の操作で選ぶ辺の番号を意味する。これらは次を満たす必要がある。
- 0 \leq X \leq M
- 1 \leq e_i \leq M
条件を満たす操作の手順が複数ある場合は、どれを出力しても正解とみなされる。
入力例 1
5 5 4 1 2 1 3 2 4 3 5 1 5
出力例 1
Yes 3 3 4 5
出力例に従って操作を行うと次のようになります。
- 辺 3 を選ぶ。頂点 2 と頂点 4 に載っているランプをつける。
- 辺 4 を選ぶ。頂点 3 と頂点 5 に載っているランプをつける。
- 辺 5 を選ぶ。頂点 1 に載っているランプをつけて、頂点 5 に載っているランプを消す。
操作を全て終了した時点で頂点 1,2,3,4 に載っているランプがついています。よってこの操作の手順は条件を満たしています。
条件を満たす操作の手順としては他に X = 4, (e_1,e_2,e_3,e_4) = (3,4,3,1) などが挙げられます。(同じ辺を 2 回以上選んでもよいです。)
入力例 2
5 5 5 1 2 1 3 2 4 3 5 1 5
出力例 2
No
入力例 3
10 10 6 2 5 2 6 3 5 3 8 4 6 4 8 5 9 6 7 6 10 7 9
出力例 3
Yes 3 10 9 6
Score: 550 points
Problem Statement
There is a simple graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i connects vertices u_i and v_i.
Each vertex has one lamp on it. Initially, all the lamps are off.
Determine whether it is possible to turn exactly K lamps on by performing the following operation between 0 and M times, inclusive.
- Choose one edge. Let u and v be the endpoints of the edge. Toggle the states of the lamps on u and v. That is, if the lamp is on, turn it off, and vice versa.
If it is possible to turn exactly K lamps on, print a sequence of operations that achieves this state.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min\left( 2 \times 10^5, \frac{N(N-1)}{2} \right)
- 0 \leq K \leq N
- 1 \leq u_i < v_i \leq N
- The given graph is simple.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M K u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
If it is impossible to turn exactly K lamps on, print No.
Otherwise, first print Yes, and then print a sequence of operations in the following format:
X e_1 e_2 \dots e_X
Here, X is the number of operations, and e_i is the number of the edge chosen in the i-th operation. These must satisfy the following:
- 0 \leq X \leq M
- 1 \leq e_i \leq M
If multiple sequences of operations satisfy the conditions, any of them will be considered correct.
Sample Input 1
5 5 4 1 2 1 3 2 4 3 5 1 5
Sample Output 1
Yes 3 3 4 5
If we operate according to the sample output, it will go as follows:
- Choose edge 3. Turn on the lamps on vertex 2 and vertex 4.
- Choose edge 4. Turn on the lamps on vertex 3 and vertex 5.
- Choose edge 5. Turn on the lamp on vertex 1 and turn off the lamp on vertex 5.
After completing all operations, the lamps on vertices 1, 2, 3, and 4 are on. Therefore, this sequence of operations satisfies the conditions.
Other possible sequences of operations that satisfy the conditions include X = 4, (e_1,e_2,e_3,e_4) = (3,4,3,1). (It is allowed to choose the same edge more than once.)
Sample Input 2
5 5 5 1 2 1 3 2 4 3 5 1 5
Sample Output 2
No
Sample Input 3
10 10 6 2 5 2 6 3 5 3 8 4 6 4 8 5 9 6 7 6 10 7 9
Sample Output 3
Yes 3 10 9 6