message stringlengths 2 19.9k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 322 108k | cluster float64 15 15 | __index_level_0__ int64 644 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Cirno gave AquaMoon a chessboard of size 1 × n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.
In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if possible):
* Move pawn from it to the (i+2)-th cell, if i+2 ≤ n and the (i+1)-th cell is occupied and the (i+2)-th cell is unoccupied.
* Move pawn from it to the (i-2)-th cell, if i-2 ≥ 1 and the (i-1)-th cell is occupied and the (i-2)-th cell is unoccupied.
You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo 998 244 353.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10 000) — the number of test cases.
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the size of the chessboard.
The second line contains a string of n characters, consists of characters "0" and "1". If the i-th character is "1", the i-th cell is initially occupied; otherwise, the i-th cell is initially unoccupied.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo 998 244 353.
Example
Input
6
4
0110
6
011011
5
01010
20
10001111110110111000
20
00110110100110111101
20
11101111011000100010
Output
3
6
1
1287
1287
715
Note
In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations.
Submitted Solution:
```
import math
t=int(input())
fin=0
for i in range(t):
n=int(input())
zoz=input()
z=zoz.count("0")
k=0
j=1
while j<n:
if zoz[j]=="1" and zoz[j-1]=="1":
k+=1
j+=2
else:
j+=1
print(int(math.factorial(k+z)/(math.factorial(k)*math.factorial(z))%998244353))
``` | instruction | 0 | 17,930 | 15 | 35,860 |
No | output | 1 | 17,930 | 15 | 35,861 |
Provide a correct Python 3 solution for this coding contest problem.
The full exploration sister is a very talented woman. Your sister can easily count the number of routes in a grid pattern if it is in the thousands. You and your exploration sister are now in a room lined with hexagonal tiles. The older sister seems to be very excited about the hexagon she sees for the first time. The older sister, who is unfamiliar with expressing the arrangement of hexagons in coordinates, represented the room in the coordinate system shown in Fig. 1.
<image> Figure 1
You want to move from one point on this coordinate system to another. But every minute your sister tells you the direction you want to move. Your usual sister will instruct you to move so that you don't go through locations with the same coordinates. However, an older sister who is unfamiliar with this coordinate system corresponds to the remainder of | x × y × t | (x: x coordinate, y: y coordinate, t: elapsed time [minutes] from the first instruction) divided by 6. Simply indicate the direction (corresponding to the number shown in the figure) and it will guide you in a completely random direction.
<image> Figure 2
If you don't want to hurt your sister, you want to reach your destination while observing your sister's instructions as much as possible. You are allowed to do the following seven actions.
* Move 1 tile to direction 0
* Move 1 tile to direction 1
* Move 1 tile in direction 2
* Move 1 tile in direction 3
* Move 1 tile in direction 4
* Move 1 tile in direction 5
* Stay on the spot
You always do one of these actions immediately after your sister gives instructions. The room is furnished and cannot be moved into the tiles where the furniture is placed. Also, movements where the absolute value of the y coordinate exceeds ly or the absolute value of the x coordinate exceeds lx are not allowed. However, the older sister may instruct such a move. Ignoring instructions means moving in a different direction than your sister indicated, or staying there. Output the minimum number of times you should ignore the instructions to get to your destination. Output -1 if it is not possible to reach your destination.
Constraints
* All inputs are integers
* -lx ≤ sx, gx ≤ lx
* -ly ≤ sy, gy ≤ ly
* (sx, sy) ≠ (gx, gy)
* (xi, yi) ≠ (sx, sy) (1 ≤ i ≤ n)
* (xi, yi) ≠ (gx, gy) (1 ≤ i ≤ n)
* (xi, yi) ≠ (xj, yj) (i ≠ j)
* 0 ≤ n ≤ 1000
* -lx ≤ xi ≤ lx (1 ≤ i ≤ n)
* -ly ≤ yi ≤ ly (1 ≤ i ≤ n)
* 0 <lx, ly ≤ 100
Input
The input is given in the following format.
> sx sy gx gy
> n
> x1 y1
> ...
> xi yi
> ...
> xn yn
> lx ly
>
here,
* sx, sy are the coordinates of the starting point
* gx, gy are the coordinates of the destination
* n is the number of furniture placed in the room
* xi, yi are the coordinates of the tile with furniture
* lx, ly are the upper limits of the absolute values of the movable X and Y coordinates.
Output
Output in one line containing one integer. If you can reach your destination, print the number of times you ignore the minimum instructions. Output -1 if it is not possible to reach your destination.
Examples
Input
0 0 0 2
0
2 2
Output
0
Input
0 0 0 2
6
0 1
1 0
1 -1
0 -1
-1 -1
-1 0
2 2
Output
-1
Input
0 0 0 2
1
0 1
2 2
Output
1 | instruction | 0 | 19,362 | 15 | 38,724 |
"Correct Solution:
```
from heapq import heappush, heappop
sx, sy, gx, gy = map(int, input().split())
n = int(input())
outs = {tuple(map(int, input().split())) for _ in range(n)}
lx, ly = map(int, input().split())
for x in range(-lx - 1, lx + 2):
outs.add((x, -ly - 1))
outs.add((x, ly + 1))
for y in range(-ly - 1, ly + 2):
outs.add((-lx - 1, y))
outs.add((lx + 1, y))
dic = {}
dic[(0, sx, sy)] = 0
que = []
heappush(que, (0, 0, sx, sy))
vec1 = ((0, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0))
vec2 = ((0, 1), (1, 1), (1, 0), (0, -1), (-1, 0), (-1, 1))
while que:
score, time, x, y = heappop(que)
if (x, y) == (gx, gy):
print(score)
break
vec = vec2 if x % 2 else vec1
inst = vec[abs(x * y * time) % 6]
new_time = (time + 1) % 6
for dx, dy in vec:
nx, ny = x + dx, y + dy
if (nx, ny) in outs: continue
new_score = score if (dx, dy) == inst else score + 1
if (new_time, nx, ny) not in dic or dic[(new_time, nx, ny)] > new_score:
dic[(new_time, nx, ny)] = new_score
heappush(que, (new_score, new_time, nx, ny))
if (new_time, x, y) not in dic or dic[(new_time, x, y)] > score + 1:
dic[(new_time, x, y)] = score + 1
heappush(que, (score + 1, new_time, x, y))
else:
print(-1)
``` | output | 1 | 19,362 | 15 | 38,725 |
Provide a correct Python 3 solution for this coding contest problem.
The full exploration sister is a very talented woman. Your sister can easily count the number of routes in a grid pattern if it is in the thousands. You and your exploration sister are now in a room lined with hexagonal tiles. The older sister seems to be very excited about the hexagon she sees for the first time. The older sister, who is unfamiliar with expressing the arrangement of hexagons in coordinates, represented the room in the coordinate system shown in Fig. 1.
<image> Figure 1
You want to move from one point on this coordinate system to another. But every minute your sister tells you the direction you want to move. Your usual sister will instruct you to move so that you don't go through locations with the same coordinates. However, an older sister who is unfamiliar with this coordinate system corresponds to the remainder of | x × y × t | (x: x coordinate, y: y coordinate, t: elapsed time [minutes] from the first instruction) divided by 6. Simply indicate the direction (corresponding to the number shown in the figure) and it will guide you in a completely random direction.
<image> Figure 2
If you don't want to hurt your sister, you want to reach your destination while observing your sister's instructions as much as possible. You are allowed to do the following seven actions.
* Move 1 tile to direction 0
* Move 1 tile to direction 1
* Move 1 tile in direction 2
* Move 1 tile in direction 3
* Move 1 tile in direction 4
* Move 1 tile in direction 5
* Stay on the spot
You always do one of these actions immediately after your sister gives instructions. The room is furnished and cannot be moved into the tiles where the furniture is placed. Also, movements where the absolute value of the y coordinate exceeds ly or the absolute value of the x coordinate exceeds lx are not allowed. However, the older sister may instruct such a move. Ignoring instructions means moving in a different direction than your sister indicated, or staying there. Output the minimum number of times you should ignore the instructions to get to your destination. Output -1 if it is not possible to reach your destination.
Constraints
* All inputs are integers
* -lx ≤ sx, gx ≤ lx
* -ly ≤ sy, gy ≤ ly
* (sx, sy) ≠ (gx, gy)
* (xi, yi) ≠ (sx, sy) (1 ≤ i ≤ n)
* (xi, yi) ≠ (gx, gy) (1 ≤ i ≤ n)
* (xi, yi) ≠ (xj, yj) (i ≠ j)
* 0 ≤ n ≤ 1000
* -lx ≤ xi ≤ lx (1 ≤ i ≤ n)
* -ly ≤ yi ≤ ly (1 ≤ i ≤ n)
* 0 <lx, ly ≤ 100
Input
The input is given in the following format.
> sx sy gx gy
> n
> x1 y1
> ...
> xi yi
> ...
> xn yn
> lx ly
>
here,
* sx, sy are the coordinates of the starting point
* gx, gy are the coordinates of the destination
* n is the number of furniture placed in the room
* xi, yi are the coordinates of the tile with furniture
* lx, ly are the upper limits of the absolute values of the movable X and Y coordinates.
Output
Output in one line containing one integer. If you can reach your destination, print the number of times you ignore the minimum instructions. Output -1 if it is not possible to reach your destination.
Examples
Input
0 0 0 2
0
2 2
Output
0
Input
0 0 0 2
6
0 1
1 0
1 -1
0 -1
-1 -1
-1 0
2 2
Output
-1
Input
0 0 0 2
1
0 1
2 2
Output
1 | instruction | 0 | 19,363 | 15 | 38,726 |
"Correct Solution:
```
from collections import deque
import sys
sys.setrecursionlimit(10000000)
input=lambda : sys.stdin.readline().rstrip()
sx,sy,gx,gy=map(int,input().split())
n=int(input())
que=deque()
field=[[[0 for k in range(6)] for j in range(201)]for i in range(201)]
for i in range(n):
x,y=map(int,input().split())
for j in range(6):
field[y+100][x+100][j]+=1
lx,ly=map(int,input().split())
field[sy+100][sx+100][0]=0
que.append((sy+100,sx+100,0,0))
r = [[(1,0),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1)],[(1,0),(1,1),(0,1),(-1,0),(0,-1),(1,-1)]]
while que:
y,x,t,c=que.popleft()
if field[y][x][t%6]:
continue
else:
field[y][x][t%6]=1
if (y,x)==(gy+100,gx+100):
print(c)
break
for i in range(6):
if abs(y-100+r[x%2][i][0])<=ly and abs(x-100+r[x%2][i][1])<=lx and not field[y+r[x%2][i][0]][x+r[x%2][i][1]][(t+1)%6]:
if i==abs((y-100)*(x-100)*(t))%6:
que.appendleft((y+r[x%2][i][0],x+r[x%2][i][1],t+1,c))
else:
que.append((y+r[x%2][i][0],x+r[x%2][i][1],t+1,c+1))
if not field[y][x][(t+1)%6]:
que.append((y,x,t+1,c+1))
else:
print(-1)
``` | output | 1 | 19,363 | 15 | 38,727 |
Provide a correct Python 3 solution for this coding contest problem.
The full exploration sister is a very talented woman. Your sister can easily count the number of routes in a grid pattern if it is in the thousands. You and your exploration sister are now in a room lined with hexagonal tiles. The older sister seems to be very excited about the hexagon she sees for the first time. The older sister, who is unfamiliar with expressing the arrangement of hexagons in coordinates, represented the room in the coordinate system shown in Fig. 1.
<image> Figure 1
You want to move from one point on this coordinate system to another. But every minute your sister tells you the direction you want to move. Your usual sister will instruct you to move so that you don't go through locations with the same coordinates. However, an older sister who is unfamiliar with this coordinate system corresponds to the remainder of | x × y × t | (x: x coordinate, y: y coordinate, t: elapsed time [minutes] from the first instruction) divided by 6. Simply indicate the direction (corresponding to the number shown in the figure) and it will guide you in a completely random direction.
<image> Figure 2
If you don't want to hurt your sister, you want to reach your destination while observing your sister's instructions as much as possible. You are allowed to do the following seven actions.
* Move 1 tile to direction 0
* Move 1 tile to direction 1
* Move 1 tile in direction 2
* Move 1 tile in direction 3
* Move 1 tile in direction 4
* Move 1 tile in direction 5
* Stay on the spot
You always do one of these actions immediately after your sister gives instructions. The room is furnished and cannot be moved into the tiles where the furniture is placed. Also, movements where the absolute value of the y coordinate exceeds ly or the absolute value of the x coordinate exceeds lx are not allowed. However, the older sister may instruct such a move. Ignoring instructions means moving in a different direction than your sister indicated, or staying there. Output the minimum number of times you should ignore the instructions to get to your destination. Output -1 if it is not possible to reach your destination.
Constraints
* All inputs are integers
* -lx ≤ sx, gx ≤ lx
* -ly ≤ sy, gy ≤ ly
* (sx, sy) ≠ (gx, gy)
* (xi, yi) ≠ (sx, sy) (1 ≤ i ≤ n)
* (xi, yi) ≠ (gx, gy) (1 ≤ i ≤ n)
* (xi, yi) ≠ (xj, yj) (i ≠ j)
* 0 ≤ n ≤ 1000
* -lx ≤ xi ≤ lx (1 ≤ i ≤ n)
* -ly ≤ yi ≤ ly (1 ≤ i ≤ n)
* 0 <lx, ly ≤ 100
Input
The input is given in the following format.
> sx sy gx gy
> n
> x1 y1
> ...
> xi yi
> ...
> xn yn
> lx ly
>
here,
* sx, sy are the coordinates of the starting point
* gx, gy are the coordinates of the destination
* n is the number of furniture placed in the room
* xi, yi are the coordinates of the tile with furniture
* lx, ly are the upper limits of the absolute values of the movable X and Y coordinates.
Output
Output in one line containing one integer. If you can reach your destination, print the number of times you ignore the minimum instructions. Output -1 if it is not possible to reach your destination.
Examples
Input
0 0 0 2
0
2 2
Output
0
Input
0 0 0 2
6
0 1
1 0
1 -1
0 -1
-1 -1
-1 0
2 2
Output
-1
Input
0 0 0 2
1
0 1
2 2
Output
1 | instruction | 0 | 19,364 | 15 | 38,728 |
"Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
rr = []
dd0 = [(0,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(0,0)]
dd1 = [(0,1),(1,1),(1,0),(0,-1),(-1,0),(-1,1),(0,0)]
dd = [dd0, dd1]
def f(n):
sx,sy,gx,gy = LI()
n = I()
fs = set([tuple(LI()) for _ in range(n)])
lx,ly = LI()
def search(s, g):
d = collections.defaultdict(lambda: inf)
s = tuple(list(s) + [0])
d[s] = 0
q = []
heapq.heappush(q, (0, s))
v = collections.defaultdict(bool)
while len(q):
k, u = heapq.heappop(q)
if v[u]:
continue
if (u[0],u[1]) == g:
return k
v[u] = True
ddi = 0 if u[0] % 2 == 0 else 1
di,dj = dd[ddi][abs(u[0]*u[1]*u[2])%6]
nu = (u[2] + 1) % 6
uv = (u[0]+di, u[1]+dj, nu)
if d[uv] > k and abs(uv[0]) <= lx and abs(uv[1]) <= ly and not ((uv[0],uv[1]) in fs):
d[uv] = k
heapq.heappush(q, (k, uv))
vd = k + 1
for di,dj in dd[ddi]:
uv = (u[0]+di, u[1]+dj, nu)
if v[uv] or abs(uv[0]) > lx or abs(uv[1]) > ly or (uv[0],uv[1]) in fs:
continue
if d[uv] > vd:
d[uv] = vd
heapq.heappush(q, (vd, uv))
return None
r = search((sx,sy),(gx,gy))
if r is None:
return -1
return r
while 1:
n = 1
if n == 0:
break
rr.append(f(n))
break
# print('rr', rr[-1])
return '\n'.join(map(str,rr))
print(main())
``` | output | 1 | 19,364 | 15 | 38,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Now Serval is a senior high school student in Japari Middle School. However, on the way to the school, he must go across a pond, in which there is a dangerous snake. The pond can be represented as a n × n grid. The snake has a head and a tail in different cells, and its body is a series of adjacent cells connecting the head and the tail without self-intersecting. If Serval hits its head or tail, the snake will bite him and he will die.
Luckily, he has a special device which can answer the following question: you can pick a rectangle, it will tell you the number of times one needs to cross the border of the rectangle walking cell by cell along the snake from the head to the tail. The pictures below show a possible snake and a possible query to it, which will get an answer of 4.
<image> <image>
Today Serval got up too late and only have time to make 2019 queries. As his best friend, can you help him find the positions of the head and the tail?
Note that two cells are adjacent if and only if they have a common edge in the grid, and a snake can have a body of length 0, that means it only has adjacent head and tail.
Also note that the snake is sleeping, so it won't move while Serval using his device. And what's obvious is that the snake position does not depend on your queries.
Input
The first line contains a single integer n (2≤ n ≤ 1000) — the size of the grid.
Output
When you are ready to answer, you should print ! x1 y1 x2 y2, where (x_1, y_1) represents the position of the head and (x_2,y_2) represents the position of the tail. You can print head and tail in any order.
Interaction
To make a query, you should print ? x1 y1 x2 y2 (1 ≤ x_1 ≤ x_2 ≤ n, 1≤ y_1 ≤ y_2 ≤ n), representing a rectangle consisting of all cells (x,y) such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2. You will get a single integer as the answer.
After printing a query, do not forget to output the end of line and flush the output, otherwise you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Answer -1 instead of a valid answer means that you made an invalid query or exceeded the maximum number of queries. Exit immediately after receiving -1 and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
If your program cannot find out the head and tail of the snake correctly, you will also get a Wrong Answer verdict.
Hacks
To make a hack, print a single integer n (2 ≤ n ≤ 1000) in the first line, indicating the size of the grid.
Then print an integer k (2 ≤ k ≤ n^2) in the second line, indicating the length of the snake.
In the next k lines, print k pairs of integers x_i, y_i (1 ≤ x_i, y_i ≤ n), each pair in a single line, indicating the i-th cell of snake, such that the adjacent pairs are adjacent, and all k pairs are distinct.
Examples
Input
2
1
0
0
Output
? 1 1 1 1
? 1 2 1 2
? 2 2 2 2
! 1 1 2 1
Input
3
2
0
Output
? 2 2 2 2
? 2 1 2 3
! 2 1 2 3
Note
<image> <image> <image> <image>
The pictures above show our queries and the answers in the first example. We first made a query for (1,1) and got an answer 1, then found that it must be connected to exactly one other cell. Then we made a query for (1,2) and got an answer of 0, then knew that the snake never entered it. So the cell connected to (1,1) must be (2,1). Then we made a query for (2,2) and got an answer 0, then knew that it never entered (2,2) as well. So the snake cannot leave (2,1), which implies that the answer is (1,1) and (2,1).
<image> <image> <image>
The pictures above show our queries and the answers in the second example. By making query to (2,2) and receiving 2, we found that the snake occupies (2,2). And by making query to rectangle from (2,1) to (2,3) and receiving answer 0, we knew that it never goes out of the rectangle from (2,1) to (2,3). Since the first answer is 2, both (2,1) and (2,3) must be occupied but none of others, so the answer is (2,1) and (2,3). | instruction | 0 | 19,501 | 15 | 39,002 |
Tags: binary search, brute force, interactive
Correct Solution:
```
# AC
import sys
class Main:
def __init__(self):
self.buff = None
self.index = 0
def next(self):
if self.buff is None or self.index == len(self.buff):
self.buff = sys.stdin.readline().split()
self.index = 0
val = self.buff[self.index]
self.index += 1
return val
def next_int(self):
return int(self.next())
def cal(self, s):
if len(s) == 1:
return s[0]
if s[0] == 0:
return self.cal(s[1:])
v = 1
for c in s:
v *= c
return v
def q(self, x1, y1, x2, y2):
print('? {} {} {} {}'.format(x1, y1, x2, y2))
sys.stdout.flush()
return self.next_int()
def ans(self, rs):
print('! {} {} {} {}'.format(rs[0][0], rs[0][1], rs[1][0], rs[1][1]))
sys.stdout.flush()
def f(self, c, n):
low = 1
high = n
while high > low:
mid = (high + low) // 2
if self.q(c, low, c, mid) % 2 == 1:
high = mid
else:
low = mid + 1
return c, low
def g(self, c, n):
low = 1
high = n
while high > low:
mid = (high + low) // 2
if self.q(low, c, mid, c) % 2 == 1:
high = mid
else:
low = mid + 1
return low, c
def solve(self):
n = self.next_int()
rs = []
for i in range(0, n):
if self.q(i + 1, 1, i + 1, n) % 2 == 1:
rs.append(self.f(i + 1, n))
if len(rs) == 2:
self.ans(rs)
return
for i in range(0, n - 1):
if self.q(1, i + 1, n, i + 1) % 2 == 1:
rs.append(self.g(i + 1, n))
if len(rs) == 2:
self.ans(rs)
return
rs.append(self.g(n, n))
if len(rs) == 2:
self.ans(rs)
if __name__ == '__main__':
Main().solve()
``` | output | 1 | 19,501 | 15 | 39,003 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Now Serval is a senior high school student in Japari Middle School. However, on the way to the school, he must go across a pond, in which there is a dangerous snake. The pond can be represented as a n × n grid. The snake has a head and a tail in different cells, and its body is a series of adjacent cells connecting the head and the tail without self-intersecting. If Serval hits its head or tail, the snake will bite him and he will die.
Luckily, he has a special device which can answer the following question: you can pick a rectangle, it will tell you the number of times one needs to cross the border of the rectangle walking cell by cell along the snake from the head to the tail. The pictures below show a possible snake and a possible query to it, which will get an answer of 4.
<image> <image>
Today Serval got up too late and only have time to make 2019 queries. As his best friend, can you help him find the positions of the head and the tail?
Note that two cells are adjacent if and only if they have a common edge in the grid, and a snake can have a body of length 0, that means it only has adjacent head and tail.
Also note that the snake is sleeping, so it won't move while Serval using his device. And what's obvious is that the snake position does not depend on your queries.
Input
The first line contains a single integer n (2≤ n ≤ 1000) — the size of the grid.
Output
When you are ready to answer, you should print ! x1 y1 x2 y2, where (x_1, y_1) represents the position of the head and (x_2,y_2) represents the position of the tail. You can print head and tail in any order.
Interaction
To make a query, you should print ? x1 y1 x2 y2 (1 ≤ x_1 ≤ x_2 ≤ n, 1≤ y_1 ≤ y_2 ≤ n), representing a rectangle consisting of all cells (x,y) such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2. You will get a single integer as the answer.
After printing a query, do not forget to output the end of line and flush the output, otherwise you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Answer -1 instead of a valid answer means that you made an invalid query or exceeded the maximum number of queries. Exit immediately after receiving -1 and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
If your program cannot find out the head and tail of the snake correctly, you will also get a Wrong Answer verdict.
Hacks
To make a hack, print a single integer n (2 ≤ n ≤ 1000) in the first line, indicating the size of the grid.
Then print an integer k (2 ≤ k ≤ n^2) in the second line, indicating the length of the snake.
In the next k lines, print k pairs of integers x_i, y_i (1 ≤ x_i, y_i ≤ n), each pair in a single line, indicating the i-th cell of snake, such that the adjacent pairs are adjacent, and all k pairs are distinct.
Examples
Input
2
1
0
0
Output
? 1 1 1 1
? 1 2 1 2
? 2 2 2 2
! 1 1 2 1
Input
3
2
0
Output
? 2 2 2 2
? 2 1 2 3
! 2 1 2 3
Note
<image> <image> <image> <image>
The pictures above show our queries and the answers in the first example. We first made a query for (1,1) and got an answer 1, then found that it must be connected to exactly one other cell. Then we made a query for (1,2) and got an answer of 0, then knew that the snake never entered it. So the cell connected to (1,1) must be (2,1). Then we made a query for (2,2) and got an answer 0, then knew that it never entered (2,2) as well. So the snake cannot leave (2,1), which implies that the answer is (1,1) and (2,1).
<image> <image> <image>
The pictures above show our queries and the answers in the second example. By making query to (2,2) and receiving 2, we found that the snake occupies (2,2). And by making query to rectangle from (2,1) to (2,3) and receiving answer 0, we knew that it never goes out of the rectangle from (2,1) to (2,3). Since the first answer is 2, both (2,1) and (2,3) must be occupied but none of others, so the answer is (2,1) and (2,3).
Submitted Solution:
```
from sys import stdout
n = int(input())
def ask(x1, y1, x2, y2):
print('?', x1, y1, x2, y2)
r = int(input()) % 2
stdout.flush()
return r
row, col = [], []
for i in range(1, n + 1):
if ask(i, 1, i, n) == 1:
row.append(i)
if ask(1, i, n, i) == 1:
col.append(i)
def get(x, c):
l, r = 1, n
while l < r:
m = (l + r + 1) // 2
if (c and ask(x, m, x, n)) or (not c and ask(m, x, n, x)):
l = m
else:
r = m - 1
if c:
row.append(l)
row.append(l)
else:
col.append(l)
col.append(l)
if len(row):
get(row[0], False)
else:
get(col[0], True)
if ask(row[0], col[0], row[0], col[0]):
print('!', row[0], col[0], row[1], col[1])
else:
print('!', row[0], col[1], row[1], col[0])
``` | instruction | 0 | 19,502 | 15 | 39,004 |
No | output | 1 | 19,502 | 15 | 39,005 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Now Serval is a senior high school student in Japari Middle School. However, on the way to the school, he must go across a pond, in which there is a dangerous snake. The pond can be represented as a n × n grid. The snake has a head and a tail in different cells, and its body is a series of adjacent cells connecting the head and the tail without self-intersecting. If Serval hits its head or tail, the snake will bite him and he will die.
Luckily, he has a special device which can answer the following question: you can pick a rectangle, it will tell you the number of times one needs to cross the border of the rectangle walking cell by cell along the snake from the head to the tail. The pictures below show a possible snake and a possible query to it, which will get an answer of 4.
<image> <image>
Today Serval got up too late and only have time to make 2019 queries. As his best friend, can you help him find the positions of the head and the tail?
Note that two cells are adjacent if and only if they have a common edge in the grid, and a snake can have a body of length 0, that means it only has adjacent head and tail.
Also note that the snake is sleeping, so it won't move while Serval using his device. And what's obvious is that the snake position does not depend on your queries.
Input
The first line contains a single integer n (2≤ n ≤ 1000) — the size of the grid.
Output
When you are ready to answer, you should print ! x1 y1 x2 y2, where (x_1, y_1) represents the position of the head and (x_2,y_2) represents the position of the tail. You can print head and tail in any order.
Interaction
To make a query, you should print ? x1 y1 x2 y2 (1 ≤ x_1 ≤ x_2 ≤ n, 1≤ y_1 ≤ y_2 ≤ n), representing a rectangle consisting of all cells (x,y) such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2. You will get a single integer as the answer.
After printing a query, do not forget to output the end of line and flush the output, otherwise you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Answer -1 instead of a valid answer means that you made an invalid query or exceeded the maximum number of queries. Exit immediately after receiving -1 and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
If your program cannot find out the head and tail of the snake correctly, you will also get a Wrong Answer verdict.
Hacks
To make a hack, print a single integer n (2 ≤ n ≤ 1000) in the first line, indicating the size of the grid.
Then print an integer k (2 ≤ k ≤ n^2) in the second line, indicating the length of the snake.
In the next k lines, print k pairs of integers x_i, y_i (1 ≤ x_i, y_i ≤ n), each pair in a single line, indicating the i-th cell of snake, such that the adjacent pairs are adjacent, and all k pairs are distinct.
Examples
Input
2
1
0
0
Output
? 1 1 1 1
? 1 2 1 2
? 2 2 2 2
! 1 1 2 1
Input
3
2
0
Output
? 2 2 2 2
? 2 1 2 3
! 2 1 2 3
Note
<image> <image> <image> <image>
The pictures above show our queries and the answers in the first example. We first made a query for (1,1) and got an answer 1, then found that it must be connected to exactly one other cell. Then we made a query for (1,2) and got an answer of 0, then knew that the snake never entered it. So the cell connected to (1,1) must be (2,1). Then we made a query for (2,2) and got an answer 0, then knew that it never entered (2,2) as well. So the snake cannot leave (2,1), which implies that the answer is (1,1) and (2,1).
<image> <image> <image>
The pictures above show our queries and the answers in the second example. By making query to (2,2) and receiving 2, we found that the snake occupies (2,2). And by making query to rectangle from (2,1) to (2,3) and receiving answer 0, we knew that it never goes out of the rectangle from (2,1) to (2,3). Since the first answer is 2, both (2,1) and (2,3) must be occupied but none of others, so the answer is (2,1) and (2,3).
Submitted Solution:
```
import sys
def query(x,y,xx,yy):
print('?',x+1,y+1,xx,yy,flush=True)
sys.stdout.flush()
r = int(input())
if r == -1:
sys.exit()
return r
n = int(input())
C = []
R = []
for i in range(n):
C.append(query(0,i,n,i+1))
R.append(query(i,0,i+1,n))
oddc = [i for i in range(n) if C[i]%2]
oddr = [i for i in range(n) if R[i]%2]
#print(oddc)
#print(oddr)
if len(oddc) == 2:
x = oddc[1]
l,r = 0,n
while r-l > 1:
mid = (l+r)//2
if query(0,l,x,mid)%2:
r = mid
else:
l = mid
ans1 = oddc[0]+1,l+1
x = oddc[0]+1
l,r = 0,n
while r-l > 1:
mid = (l+r)//2
if query(x,l,n,mid)%2:
r = mid
else:
l = mid
ans2 = oddc[1]+1,l+1
print('!',*ans1,*ans2,flush=True)
sys.stdout.flush()
else:
x = oddr[1]
l,r = 0,n
while r-l > 1:
mid = (l+r)//2
if query(l,0,mid,x)%2:
r = mid
else:
l = mid
ans1 = l+1,oddr[0]+1
x = oddr[0]+1
l,r = 0,n
while r-l > 1:
mid = (l+r)//2
if query(l,x,mid,n)%2:
r = mid
else:
l = mid
ans2 = l+1,oddr[1]+1
print('!',*ans1,*ans2,flush=True)
sys.stdout.flush()
``` | instruction | 0 | 19,503 | 15 | 39,006 |
No | output | 1 | 19,503 | 15 | 39,007 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Now Serval is a senior high school student in Japari Middle School. However, on the way to the school, he must go across a pond, in which there is a dangerous snake. The pond can be represented as a n × n grid. The snake has a head and a tail in different cells, and its body is a series of adjacent cells connecting the head and the tail without self-intersecting. If Serval hits its head or tail, the snake will bite him and he will die.
Luckily, he has a special device which can answer the following question: you can pick a rectangle, it will tell you the number of times one needs to cross the border of the rectangle walking cell by cell along the snake from the head to the tail. The pictures below show a possible snake and a possible query to it, which will get an answer of 4.
<image> <image>
Today Serval got up too late and only have time to make 2019 queries. As his best friend, can you help him find the positions of the head and the tail?
Note that two cells are adjacent if and only if they have a common edge in the grid, and a snake can have a body of length 0, that means it only has adjacent head and tail.
Also note that the snake is sleeping, so it won't move while Serval using his device. And what's obvious is that the snake position does not depend on your queries.
Input
The first line contains a single integer n (2≤ n ≤ 1000) — the size of the grid.
Output
When you are ready to answer, you should print ! x1 y1 x2 y2, where (x_1, y_1) represents the position of the head and (x_2,y_2) represents the position of the tail. You can print head and tail in any order.
Interaction
To make a query, you should print ? x1 y1 x2 y2 (1 ≤ x_1 ≤ x_2 ≤ n, 1≤ y_1 ≤ y_2 ≤ n), representing a rectangle consisting of all cells (x,y) such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2. You will get a single integer as the answer.
After printing a query, do not forget to output the end of line and flush the output, otherwise you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Answer -1 instead of a valid answer means that you made an invalid query or exceeded the maximum number of queries. Exit immediately after receiving -1 and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
If your program cannot find out the head and tail of the snake correctly, you will also get a Wrong Answer verdict.
Hacks
To make a hack, print a single integer n (2 ≤ n ≤ 1000) in the first line, indicating the size of the grid.
Then print an integer k (2 ≤ k ≤ n^2) in the second line, indicating the length of the snake.
In the next k lines, print k pairs of integers x_i, y_i (1 ≤ x_i, y_i ≤ n), each pair in a single line, indicating the i-th cell of snake, such that the adjacent pairs are adjacent, and all k pairs are distinct.
Examples
Input
2
1
0
0
Output
? 1 1 1 1
? 1 2 1 2
? 2 2 2 2
! 1 1 2 1
Input
3
2
0
Output
? 2 2 2 2
? 2 1 2 3
! 2 1 2 3
Note
<image> <image> <image> <image>
The pictures above show our queries and the answers in the first example. We first made a query for (1,1) and got an answer 1, then found that it must be connected to exactly one other cell. Then we made a query for (1,2) and got an answer of 0, then knew that the snake never entered it. So the cell connected to (1,1) must be (2,1). Then we made a query for (2,2) and got an answer 0, then knew that it never entered (2,2) as well. So the snake cannot leave (2,1), which implies that the answer is (1,1) and (2,1).
<image> <image> <image>
The pictures above show our queries and the answers in the second example. By making query to (2,2) and receiving 2, we found that the snake occupies (2,2). And by making query to rectangle from (2,1) to (2,3) and receiving answer 0, we knew that it never goes out of the rectangle from (2,1) to (2,3). Since the first answer is 2, both (2,1) and (2,3) must be occupied but none of others, so the answer is (2,1) and (2,3).
Submitted Solution:
```
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n = int(input())
dp = [[0]*n for i in range(n)]
for i in range(n):
for j in range(n):
print("?",i+1,j+1,i+1,j+1)
sys.stdout.flush()
dp[i][j] = int(input())
flag = 0
for j in range(n):
for i in range(n):
if dp[i][j] ==1:
hi = j+1
flag = 1
break
if flag == 1:
break
flag = 0
for j in range(n-1,-1,-1):
for i in range(n-1,-1,-1):
if dp[i][j] ==1:
mi = j+1
flag = 1
break
if flag == 1:
break
flag = 0
for i in range(n-1,-1,-1):
for j in range(n-1,-1,-1):
if dp[i][j] ==1:
sita = i+1
flag = 1
break
if flag == 1:
break
flag = 0
for i in range(n):
for j in range(n):
if dp[i][j] ==1:
ue = i+1
flag = 1
break
if flag == 1:
break
print("!",ue,hi,sita,mi)
``` | instruction | 0 | 19,504 | 15 | 39,008 |
No | output | 1 | 19,504 | 15 | 39,009 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Now Serval is a senior high school student in Japari Middle School. However, on the way to the school, he must go across a pond, in which there is a dangerous snake. The pond can be represented as a n × n grid. The snake has a head and a tail in different cells, and its body is a series of adjacent cells connecting the head and the tail without self-intersecting. If Serval hits its head or tail, the snake will bite him and he will die.
Luckily, he has a special device which can answer the following question: you can pick a rectangle, it will tell you the number of times one needs to cross the border of the rectangle walking cell by cell along the snake from the head to the tail. The pictures below show a possible snake and a possible query to it, which will get an answer of 4.
<image> <image>
Today Serval got up too late and only have time to make 2019 queries. As his best friend, can you help him find the positions of the head and the tail?
Note that two cells are adjacent if and only if they have a common edge in the grid, and a snake can have a body of length 0, that means it only has adjacent head and tail.
Also note that the snake is sleeping, so it won't move while Serval using his device. And what's obvious is that the snake position does not depend on your queries.
Input
The first line contains a single integer n (2≤ n ≤ 1000) — the size of the grid.
Output
When you are ready to answer, you should print ! x1 y1 x2 y2, where (x_1, y_1) represents the position of the head and (x_2,y_2) represents the position of the tail. You can print head and tail in any order.
Interaction
To make a query, you should print ? x1 y1 x2 y2 (1 ≤ x_1 ≤ x_2 ≤ n, 1≤ y_1 ≤ y_2 ≤ n), representing a rectangle consisting of all cells (x,y) such that x_1 ≤ x ≤ x_2 and y_1 ≤ y ≤ y_2. You will get a single integer as the answer.
After printing a query, do not forget to output the end of line and flush the output, otherwise you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Answer -1 instead of a valid answer means that you made an invalid query or exceeded the maximum number of queries. Exit immediately after receiving -1 and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
If your program cannot find out the head and tail of the snake correctly, you will also get a Wrong Answer verdict.
Hacks
To make a hack, print a single integer n (2 ≤ n ≤ 1000) in the first line, indicating the size of the grid.
Then print an integer k (2 ≤ k ≤ n^2) in the second line, indicating the length of the snake.
In the next k lines, print k pairs of integers x_i, y_i (1 ≤ x_i, y_i ≤ n), each pair in a single line, indicating the i-th cell of snake, such that the adjacent pairs are adjacent, and all k pairs are distinct.
Examples
Input
2
1
0
0
Output
? 1 1 1 1
? 1 2 1 2
? 2 2 2 2
! 1 1 2 1
Input
3
2
0
Output
? 2 2 2 2
? 2 1 2 3
! 2 1 2 3
Note
<image> <image> <image> <image>
The pictures above show our queries and the answers in the first example. We first made a query for (1,1) and got an answer 1, then found that it must be connected to exactly one other cell. Then we made a query for (1,2) and got an answer of 0, then knew that the snake never entered it. So the cell connected to (1,1) must be (2,1). Then we made a query for (2,2) and got an answer 0, then knew that it never entered (2,2) as well. So the snake cannot leave (2,1), which implies that the answer is (1,1) and (2,1).
<image> <image> <image>
The pictures above show our queries and the answers in the second example. By making query to (2,2) and receiving 2, we found that the snake occupies (2,2). And by making query to rectangle from (2,1) to (2,3) and receiving answer 0, we knew that it never goes out of the rectangle from (2,1) to (2,3). Since the first answer is 2, both (2,1) and (2,3) must be occupied but none of others, so the answer is (2,1) and (2,3).
Submitted Solution:
```
# AC
import sys
class Main:
def __init__(self):
self.buff = None
self.index = 0
def next(self):
if self.buff is None or self.index == len(self.buff):
self.buff = sys.stdin.readline().split()
self.index = 0
val = self.buff[self.index]
self.index += 1
return val
def next_int(self):
return int(self.next())
def cal(self, s):
if len(s) == 1:
return s[0]
if s[0] == 0:
return self.cal(s[1:])
v = 1
for c in s:
v *= c
return v
def q(self, x1, y1, x2, y2):
print('? {} {} {} {}'.format(x1, y1, x2, y2))
sys.stdout.flush()
return self.next_int()
def ans(self, rs):
print('! {} {} {} {}'.format(rs[0][0], rs[0][1], rs[1][0], rs[1][1]))
sys.stdout.flush()
def f(self, c, n):
low = 1
high = n
while high > low:
mid = (high + low) // 2
if self.q(c, low, c, mid) % 2 == 1:
high = mid
else:
low = mid + 1
return c, low
def g(self, c, n):
low = 1
high = n
while high > low:
mid = (high + low) // 2
if self.q(low, c, mid, c) % 2 == 1:
high = mid
else:
low = mid + 1
return low, c
def solve(self):
n = self.next_int()
rs = []
for i in range(0, n):
if self.q(i + 1, 1, i + 1, n) % 2 == 1:
rs.append(self.f(i + 1, n))
if len(rs) == 2:
self.ans(rs)
return
for i in range(0, n):
if self.q(1, i + 1, n, i + 1) % 2 == 1:
rs.append(self.g(i + 1, n))
if len(rs) == 2:
self.ans(rs)
return
if __name__ == '__main__':
Main().solve()
``` | instruction | 0 | 19,505 | 15 | 39,010 |
No | output | 1 | 19,505 | 15 | 39,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3). | instruction | 0 | 19,673 | 15 | 39,346 |
Tags: constructive algorithms, graphs
Correct Solution:
```
# Problem 1495C - Garden of the Sun
def main():
T = int(input())
for c in range(T):
inp = input().rstrip().split(" ")
n, m = int(inp[0]), int(inp[1])
mat = []
for _ in range(n):
mat.append(list(input().rstrip()))
if n == 1 or n == 2:
for j in range(m):
mat[0][j] = 'X'
else:
i=0
pending = False
while i < n:
if i == 0:
pass
elif i % 3 == 1:
for j in range(m):
mat[i][j] = 'X'
pending = True
elif i % 3 == 2:
for j in range(m):
if pending and mat[i][j] == 'X' and i+1 < n:
mat[i+1][j] = 'X'
pending = False
else:
for j in range(m):
if pending and mat[i][j] == 'X':
mat[i-1][j] = 'X'
pending = False
if pending:
mat[i-1][0] = 'X'
mat[i][0] = 'X'
pending = False
i += 1
if n % 3 == 1:
for j in range(m):
if mat[-1][j] == 'X':
mat[-2][j] = 'X'
for row in mat:
print("".join(row))
main()
``` | output | 1 | 19,673 | 15 | 39,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3). | instruction | 0 | 19,674 | 15 | 39,348 |
Tags: constructive algorithms, graphs
Correct Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n,m = map(int,input().split())
board = []
newBoard = []
for _ in range(n):
board.append(input())
newBoard.append([])
for elem in board[_]:
newBoard[_].append("X" if elem == "X" else ".")
if n % 3:
for i in range(n // 3 + 1):
for j in range(m):
newBoard[3 * i][j] = "X"
if i < n // 3:
flag = False
for j in range(m):
if newBoard[3 * i + 1][j] == "X" or newBoard[3 * i + 2][j] == "X":
flag = True
newBoard[3 * i + 1][j] = "X"
newBoard[3 * i + 2][j] = "X"
break
if not flag:
newBoard[3 * i + 1][0] = "X"
newBoard[3 * i + 2][0] = "X"
else:
for i in range(n // 3):
for j in range(m):
newBoard[3 * i + 1][j] = "X"
if i < n // 3 - 1:
flag = False
for j in range(m):
if newBoard[3 * i + 2][j] == "X" or newBoard[3 * i + 3][j] == "X":
flag = True
newBoard[3 * i + 2][j] = "X"
newBoard[3 * i + 3][j] = "X"
break
if not flag:
newBoard[3 * i + 2][0] = "X"
newBoard[3 * i + 3][0] = "X"
for row in newBoard:
print("".join(row))
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | output | 1 | 19,674 | 15 | 39,349 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3). | instruction | 0 | 19,675 | 15 | 39,350 |
Tags: constructive algorithms, graphs
Correct Solution:
```
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.readline
def print_(grid):
for line in grid:
print("".join(line))
for _ in range(int(input())):
H, W = map(int, input().split())
grid = [["."] * W for _ in range(H)]
for h in range(H):
line = input().rstrip('\n')
for w in range(W):
grid[h][w] = line[w]
if H == 1:
print("X" * W)
continue
elif H == 2:
for w in range(W):
grid[0][w] = "X"
print_(grid)
continue
elif H == 3:
for w in range(W):
grid[1][w] = "X"
print_(grid)
continue
if W == 1:
for h in range(H):
grid[h][0] = "X"
print_(grid)
continue
elif W == 2:
for h in range(H):
grid[h][0] = "X"
print_(grid)
continue
elif W == 3:
for h in range(H):
grid[h][1] = "X"
print_(grid)
continue
for h in range(H):
if h%3 == 0:
for w in range(W):
grid[h][w] = "X"
for h in range(H):
if h%3 == 2:
flg = 0
for w in range(W):
if grid[h][w] == "X" or grid[h-1][w] == "X":
flg = 1
grid[h][w] = "X"
grid[h-1][w] = "X"
break
if not flg:
grid[h][0] = "X"
grid[h-1][0] = "X"
if H%3 == 0:
for w in range(W):
if grid[H-1][w] == "X" or grid[H-2][w] == "X":
grid[H-1][w] = "X"
grid[H-2][w] = "X"
print_(grid)
if __name__ == '__main__':
main()
``` | output | 1 | 19,675 | 15 | 39,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3). | instruction | 0 | 19,676 | 15 | 39,352 |
Tags: constructive algorithms, graphs
Correct Solution:
```
for _ in range(int(input())):
n, m = map(int, input().split())
grid = []
for _ in range(n):
grid.append(list(input()))
if n % 3 != 0:
for i in range(0, n, 3):
for j in range(m):
grid[i][j] = "X"
for i in range(0, n - 3, 3):
if m == 1 or (grid[i + 1][1] != "X" and grid[i + 2][1] != "X"):
grid[i + 1][0] = "X"
grid[i + 2][0] = "X"
else:
grid[i + 1][1] = "X"
grid[i + 2][1] = "X"
else:
for i in range(0, n, 3):
for j in range(m):
grid[i+1][j] = "X"
for i in range(1, n - 3, 3):
if m == 1 or (grid[i + 1][1] != "X" and grid[i + 2][1] != "X"):
grid[i + 1][0] = "X"
grid[i + 2][0] = "X"
else:
grid[i + 1][1] = "X"
grid[i + 2][1] = "X"
for i in range(n):
print("".join(grid[i]))
``` | output | 1 | 19,676 | 15 | 39,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3). | instruction | 0 | 19,677 | 15 | 39,354 |
Tags: constructive algorithms, graphs
Correct Solution:
```
import sys,io,os
try:Z=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
except:Z=lambda:sys.stdin.readline().encode()
O=[]
for _ in range(int(Z())):
N,M=map(int,Z().split());b=[];w=[['']*M for i in range(N)];s=M%3==0;t=2-s-s
for i in range(N):b.append([*Z().strip()])
if M<2:O.append('\n'.join(['X']*N));continue
if N<2:O.append('X'*M);continue
for m in range(M):
v=m%3
if v==s:
for n in range(N):w[n][m]='X'
elif m and v==t:
for n in range(N):w[n][m]=chr(b[n][m])
if b[1][m-1]==88 or b[1][m]==88:w[1][m-1]='X';w[1][m]='X'
else:w[0][m-1]='X';w[0][m]='X'
else:
for n in range(N):w[n][m]=chr(b[n][m])
O.append('\n'.join(''.join(i)for i in w))
print('\n'.join(O))
``` | output | 1 | 19,677 | 15 | 39,355 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3). | instruction | 0 | 19,678 | 15 | 39,356 |
Tags: constructive algorithms, graphs
Correct Solution:
```
import sys
input = sys.stdin.readline
t=int(input())
for tests in range(t):
n,m=map(int,input().split())
MAP=[list(input().strip()) for i in range(n)]
if m<=1:
for i in range(n):
MAP[i][0]="X"
for j in range(1,m,3):
for i in range(n):
MAP[i][j]="X"
for j in range(2,m,3):
if j+1<m:
for i in range(n):
if MAP[i][j]=="X":
MAP[i][j+1]="X"
break
else:
for i in range(n):
if MAP[i][j+1]=="X":
MAP[i][j]="X"
break
else:
MAP[0][j]=MAP[0][j+1]="X"
if m%3==1:
for i in range(n):
if MAP[i][m-1]=="X":
MAP[i][m-2]="X"
for i in range(n):
print("".join(MAP[i]))
#print()
``` | output | 1 | 19,678 | 15 | 39,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3). | instruction | 0 | 19,679 | 15 | 39,358 |
Tags: constructive algorithms, graphs
Correct Solution:
```
for _ in range(int(input())):
m,n = map(int,input().split())
s = [['' for _ in range(n)] for _ in range(m)]
t = ["" for _ in range(m)]
for i in range(m):
t[i] = input()
for j in range(n):
s[i][j] = t[i][j]
if m % 3 == 1:
for i in range(0,m,3):
for j in range(n):
s[i][j] = 'X'
for i in range(1,m - 1,3):
if t[i].find('X') == -1 and t[i + 1].find('X') == -1:
s[i][0] = s[i + 1][0] = 'X'
elif t[i].find('X') == -1:
s[i][t[i + 1].find('X')] = 'X'
else:
s[i + 1][t[i].find('X')] = 'X'
else:
for i in range(1,m,3):
for j in range(n):
s[i][j] = 'X'
for i in range(2,m - 1,3):
if t[i].find('X') == -1 and t[i + 1].find('X') == -1:
s[i][0] = s[i + 1][0] = 'X'
elif t[i].find('X') == -1:
s[i][t[i + 1].find('X')] = 'X'
else:
s[i + 1][t[i].find('X')] = 'X'
for i in range(m):
for j in range(n):
print(s[i][j], end = '')
print()
``` | output | 1 | 19,679 | 15 | 39,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3). | instruction | 0 | 19,680 | 15 | 39,360 |
Tags: constructive algorithms, graphs
Correct Solution:
```
#!/usr/bin/env python3
import sys, getpass
import math, random
import functools, itertools, collections, heapq, bisect
from collections import Counter, defaultdict, deque
input = sys.stdin.readline # to read input quickly
# available on Google, AtCoder Python3, not available on Codeforces
# import numpy as np
# import scipy
M9 = 10**9 + 7 # 998244353
# d4 = [(1,0),(0,1),(-1,0),(0,-1)]
# d8 = [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]
# d6 = [(2,0),(1,1),(-1,1),(-2,0),(-1,-1),(1,-1)] # hexagonal layout
MAXINT = sys.maxsize
# if testing locally, print to terminal with a different color
OFFLINE_TEST = getpass.getuser() == "hkmac"
# OFFLINE_TEST = False # codechef does not allow getpass
def log(*args):
if OFFLINE_TEST:
print('\033[36m', *args, '\033[0m', file=sys.stderr)
def solve(*args):
# screen input
if OFFLINE_TEST:
log("----- solving ------")
log(*args)
log("----- ------- ------")
return solve_(*args)
def read_matrix(rows):
return [list(map(int,input().split())) for _ in range(rows)]
def read_strings(rows):
return [input().strip() for _ in range(rows)]
# ---------------------------- template ends here ----------------------------
def solve_(mrr):
if OFFLINE_TEST:
for r in mrr:
log(r)
# every thrid row, starting from
# the second row
# the first row if if col%3 == 1
# connect the columns
row_to_list = defaultdict(list)
for i,row in enumerate(mrr):
for j,cell in enumerate(row):
if cell == "X":
row_to_list[i].append(j)
xrr = [list(row) for row in mrr]
if len(xrr[0]) <= 2 and len(xrr) <= 2:
return xrr
offset = 1
if len(xrr)%3 == 1:
offset = 0
allpillars = []
for i,row in enumerate(xrr):
if i%3 == offset:
xrr[i] = ["X"]*len(xrr[0])
allpillars.append(i)
else:
if i == 0 or i == len(xrr)-1:
continue
log(allpillars)
for a,b in zip(allpillars, allpillars[1:]):
if row_to_list[a+1]:
xrr[a+2][row_to_list[a+1][0]] = "X"
elif row_to_list[b-1]:
xrr[b-2][row_to_list[b-1][0]] = "X"
else:
xrr[a+1][0] = "X"
xrr[b-1][0] = "X"
return xrr
# for case_num in [0]: # no loop over test case
# for case_num in range(100): # if the number of test cases is specified
for case_num in range(int(input())):
# read line as an integer
# k = int(input())
# read line as a string
# srr = input().strip()
# read one line and parse each word as a string
# lst = input().split()
# read one line and parse each word as an integer
k,_ = list(map(int,input().split()))
# lst = list(map(int,input().split()))
# read multiple rows
# mrr = read_matrix(k) # and return as a list of list of int
arr = read_strings(k) # and return as a list of str
res = solve(arr) # include input here
# print result
# Google and Facebook - case number required
# print("Case #{}: {}".format(case_num+1, res))
res = "\n".join(["".join(r) for r in res])
print(res)
# Other platforms - no case number required
# print(res)
# print(len(res))
# print(*res) # print a list with elements
# for r in res: # print each list in a different line
# print(res)
# print("".join(r))
``` | output | 1 | 19,680 | 15 | 39,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3).
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def solve(n, m):
empty = "X"
emptyall = [empty] * m
grid = []
for i in range(n):
grid.append(list(input()))
if n % 3 == 1:
for i in range(n):
if i % 3 == 0:
grid[i] = emptyall
elif i % 3 == 2:
if empty in grid[i-1] or empty in grid[i]:
for j in range(m):
if grid[i][j] == empty:
grid[i-1][j] = empty
break
elif grid[i-1][j] == empty:
grid[i][j] = empty
break
else:
grid[i-1][0] = empty
grid[i][0] = empty
else:
for i in range(1, n):
if i % 3 == 1:
grid[i] = emptyall
elif i % 3 == 0:
if empty in grid[i - 1] or empty in grid[i]:
for j in range(m):
if grid[i][j] == empty:
grid[i - 1][j] = empty
break
elif grid[i - 1][j] == empty:
grid[i][j] = empty
break
else:
grid[i-1][0] = empty
grid[i][0] = empty
for i in range(n):
print("".join(grid[i]))
t = int(input())
for i in range(t):
n, m = map(int, input().split())
solve(n, m)
``` | instruction | 0 | 19,681 | 15 | 39,362 |
Yes | output | 1 | 19,681 | 15 | 39,363 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3).
Submitted Solution:
```
import io
import os
def solve(R, C, grid):
grid = [list(row) for row in grid]
# Caterpillar across every row 0 mod 3
for r in range(0, R, 3):
for c in range(C):
grid[r][c] = "X"
# Connect the caterpillars
for r in range(0, R, 3):
if r + 3 < R:
for c in range(C):
if grid[r + 1][c] == "X" or grid[r + 2][c] == "X":
assert (grid[r + 1][c] == "X") ^ (grid[r + 2][c] == "X")
grid[r + 1][c] = "X"
grid[r + 2][c] = "X"
break
else:
assert grid[r + 1][0] != "X"
assert grid[r + 2][0] != "X"
grid[r + 1][0] = "X"
grid[r + 2][0] = "X"
# Unconnected last row
if R % 3 == 0:
for c in range(C):
if grid[-1][c] == "X":
assert grid[-3][c] == "X"
assert grid[-2][c] != "X"
grid[-2][c] = "X"
return "\n".join("".join(row) for row in grid)
if __name__ == "__main__":
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
N, M = [int(x) for x in input().split()]
grid = [input().decode().rstrip() for i in range(N)]
ans = solve(N, M, grid)
print(ans)
``` | instruction | 0 | 19,682 | 15 | 39,364 |
Yes | output | 1 | 19,682 | 15 | 39,365 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3).
Submitted Solution:
```
import sys, os
if os.environ['USERNAME']=='kissz':
inp=open('in.txt','r').readline
def debug(*args):
print(*args,file=sys.stderr)
else:
inp=sys.stdin.readline
def debug(*args):
pass
# SCRIPT STARTS HERE
for _ in range(int(inp())):
n,m=map(int,inp().split())
R=[]
for i in range(n):
R+=[list(inp().strip())]
n_=((n-1)//3)*3+1
n__=n-n_
for i in range(n_):
if i%3==0:
R[i]=['X']*m
elif i%3==1:
if 'X' in R[i]:
j=R[i].index('X')
R[i+1][j]='X'
elif 'X' in R[i+1]:
j=R[i+1].index('X')
R[i][j]='X'
else:
R[i][0]='X'
R[i+1][0]='X'
if n__==2:
for j in range(m):
if R[-1][j]=='X': R[-2][j]='X'
for i in range(n):
print(''.join(R[i]))
``` | instruction | 0 | 19,683 | 15 | 39,366 |
Yes | output | 1 | 19,683 | 15 | 39,367 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3).
Submitted Solution:
```
def solve(n, m):
empty = "X"
emptyall = [empty] * m
grid = []
for i in range(n):
grid.append(list(input()))
if n % 3 == 1:
for i in range(n):
if i % 3 == 0:
grid[i] = emptyall
elif i % 3 == 2:
if empty in grid[i-1] or empty in grid[i]:
for j in range(m):
if grid[i][j] == empty:
grid[i-1][j] = empty
break
elif grid[i-1][j] == empty:
grid[i][j] = empty
break
else:
grid[i-1][0] = empty
grid[i][0] = empty
else:
for i in range(1, n):
if i % 3 == 1:
grid[i] = emptyall
elif i % 3 == 0:
if empty in grid[i - 1] or empty in grid[i]:
for j in range(m):
if grid[i][j] == empty:
grid[i - 1][j] = empty
break
elif grid[i - 1][j] == empty:
grid[i][j] = empty
break
else:
grid[i-1][0] = empty
grid[i][0] = empty
for i in range(n):
print("".join(grid[i]))
t = int(input())
for i in range(t):
n, m = map(int, input().split())
solve(n, m)
``` | instruction | 0 | 19,684 | 15 | 39,368 |
Yes | output | 1 | 19,684 | 15 | 39,369 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3).
Submitted Solution:
```
from sys import stdin,stderr
def rl():
return [int(w) for w in stdin.readline().split()]
t, = rl()
for _ in range(t):
n,m = rl()
a = [[c for c in stdin.readline().rstrip()] for _ in range(n)]
if m > 3:
base = 0 if n % 3 == 1 else 1
for y in range(n):
if y % 3 == base:
a[y] = ['X'] * m
elif y % 6 in (base + 1, base + 2):
a[y][0] = 'X'
else:
a[y][m-1] = 'X'
for y in range(base, n, 6):
if y > 0 and a[y-1][m-2] == 'X':
a[y][m-1] = '.'
if y < n-1 and a[y+1][m-1] == 'X':
a[y+1][m-2] = 'X'
if a[y+1][m-3] == 'X':
a[y][m-2] = '.'
a[y-1][m-3] = 'X'
if y < n-1 and a[y+1][1] == 'X':
a[y][0] = '.'
if y > 0 and a[y-1][0] == 'X':
a[y-1][1] = 'X'
if a[y-1][2] == 'X':
a[y][1] = '.'
a[y+1][2] = 'X'
for y in range(base + 3, n, 6):
if y < n-1 and a[y+1][m-2] == 'X':
a[y][m-1] = '.'
if y > 0 and a[y-1][m-1] == 'X':
a[y-1][m-2] = 'X'
if a[y-1][m-3] == 'X':
a[y][m-2] = '.'
a[y+1][m-3] ='X'
if a[y-1][1] == 'X':
a[y][0] = '.'
if y < n-1 and a[y+1][0] == 'X':
a[y+1][1] = 'X'
if a[y+1][2] == 'X':
a[y][1] = '.'
a[y-1][2] = 'X'
elif m == 3:
for y in range(n):
a[y][1] = 'X'
else:
for y in range(n):
a[y][0] = 'X'
for line in a:
print(''.join(line))
``` | instruction | 0 | 19,685 | 15 | 39,370 |
No | output | 1 | 19,685 | 15 | 39,371 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3).
Submitted Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n,m = map(int,input().split())
board = []
newBoard = []
for _ in range(n):
board.append(input())
newBoard.append([])
for elem in board[_]:
newBoard[_].append("X" if elem == "X" else ".")
if n % 3:
for i in range(n // 3 + 1):
for j in range(m):
newBoard[3 * i][j] = "X"
if i < n // 3:
flag = False
for j in range(m):
if newBoard[3 * i + 1][j] == "X" or newBoard[3 * i + 2][j] == "X":
flag = True
newBoard[3 * i + 1][j] = "X"
newBoard[3 * i + 2][j] = "X"
if not flag:
newBoard[3 * i + 1][0] = "X"
newBoard[3 * i + 2][0] = "X"
else:
for i in range(n // 3):
for j in range(m):
newBoard[3 * i + 1][j] = "X"
if i < n // 3 - 1:
flag = False
for j in range(m):
if newBoard[3 * i + 2][j] == "X" or newBoard[3 * i + 3][j] == "X":
flag = True
newBoard[3 * i + 2][j] = "X"
newBoard[3 * i + 3][j] = "X"
if not flag:
newBoard[3 * i + 2][0] = "X"
newBoard[3 * i + 3][0] = "X"
for row in newBoard:
print("".join(row))
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | instruction | 0 | 19,686 | 15 | 39,372 |
No | output | 1 | 19,686 | 15 | 39,373 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3).
Submitted Solution:
```
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
s = [list(input()[:-1]) for _ in range(n)]
for i in range(0, n, 2):
for j in range(m):
s[i][j] = 'X'
for i in range(1, n, 2):
cnt = 0
for j in range(1, m):
if s[i][j] == 'X':
if cnt >= 1 and i < n - 1:
s[i + 1][j - 1] = '.'
cnt += 1
if cnt == 0:
s[i][0] = 'X'
for row in s:
print(''.join(row))
``` | instruction | 0 | 19,687 | 15 | 39,374 |
No | output | 1 | 19,687 | 15 | 39,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are many sunflowers in the Garden of the Sun.
Garden of the Sun is a rectangular table with n rows and m columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).
Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:
* When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.
* There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells.
You can walk from an empty cell to another if they share a common edge.
Could you please give the owner a solution that meets all her requirements?
Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤ t≤ 10^4) — the number of test cases. The description of the test cases follows.
The first line contains two integers n, m (1 ≤ n,m ≤ 500) — the number of rows and columns.
Each of the next n lines contains m characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively.
It is guaranteed that the sum of n ⋅ m for all test cases does not exceed 250 000.
Output
For each test case, print n lines. Each should contain m characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively.
If there are multiple answers, you can print any. It can be shown that the answer always exists.
Example
Input
5
3 3
X.X
...
X.X
4 4
....
.X.X
....
.X.X
5 5
.X...
....X
.X...
.....
X.X.X
1 10
....X.X.X.
2 2
..
..
Output
XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..
Note
Let's use (x,y) to describe the cell on x-th row and y-th column.
In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.
<image>
In the first test case, one possible solution is to remove sunflowers on (1,2), (2,3) and (3 ,2).
<image>
Another acceptable solution is to remove sunflowers on (1,2), (2,2) and (3,2).
<image>
This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between (1,1) and (3,3).
1. (1,1)→ (1,2)→ (1,3)→ (2,3)→ (3,3)
2. (1,1)→ (2,1)→ (3,1)→ (3,2)→ (3,3)
<image>
This output is considered wrong because you can't walk from (1,1) to (3,3).
Submitted Solution:
```
import sys
input=sys.stdin.readline
t=int(input())
for _ in range(t):
n,m=map(int,input().split())
grid=[]
for i in range(n):
grid.append(input())
grid2=[]
for i in range(n):
grid2.append([0]*m)
for i in range(n):
for j in range(m):
if grid[i][j]=='X':
grid2[i][j]='X'
else:
grid2[i][j]='.'
for i in range(0,n,2):
for j in range(m):
grid2[i][j]='X'
for i in range(2,n,2):
count=0
for j in range(m):
if grid2[i-1][j]=='X':
count+=1
if count>=2 and grid2[i-2][j]=='X':
grid2[i][j-1]='.'
if count==0:
for j in range(m):
if grid2[i-2][j]=='X':
grid2[i-1][j]='X'
break
for i in range(n):
print(''.join(grid2[i]))
``` | instruction | 0 | 19,688 | 15 | 39,376 |
No | output | 1 | 19,688 | 15 | 39,377 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 ≤ x1, y1 ≤ 109) — the start position of the robot.
The second line contains two integers x2, y2 ( - 109 ≤ x2, y2 ≤ 109) — the finish position of the robot.
Output
Print the only integer d — the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times. | instruction | 0 | 19,856 | 15 | 39,712 |
Tags: implementation, math
Correct Solution:
```
a,b=map(int,input().split())
c,d=map(int,input().split())
t1=abs(a-c)
t2=abs(b-d)
print(max(t1,t2))
``` | output | 1 | 19,856 | 15 | 39,713 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 ≤ x1, y1 ≤ 109) — the start position of the robot.
The second line contains two integers x2, y2 ( - 109 ≤ x2, y2 ≤ 109) — the finish position of the robot.
Output
Print the only integer d — the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times. | instruction | 0 | 19,857 | 15 | 39,714 |
Tags: implementation, math
Correct Solution:
```
x1,y1 = map(int,input().split())
x2,y2 = map(int,input().split())
print(int(max(abs(x1-x2),abs(y1-y2))))
``` | output | 1 | 19,857 | 15 | 39,715 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 ≤ x1, y1 ≤ 109) — the start position of the robot.
The second line contains two integers x2, y2 ( - 109 ≤ x2, y2 ≤ 109) — the finish position of the robot.
Output
Print the only integer d — the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times. | instruction | 0 | 19,858 | 15 | 39,716 |
Tags: implementation, math
Correct Solution:
```
x1,y1=map(int,(input().split()))
x2,y2=map(int,(input().split()))
b=abs(x2-x1)
c=abs(y2-y1)
a=min(abs(x1-x2),abs(y2-y1))
print(a+abs(a-b)+abs(a-c))
``` | output | 1 | 19,858 | 15 | 39,717 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 ≤ x1, y1 ≤ 109) — the start position of the robot.
The second line contains two integers x2, y2 ( - 109 ≤ x2, y2 ≤ 109) — the finish position of the robot.
Output
Print the only integer d — the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times. | instruction | 0 | 19,859 | 15 | 39,718 |
Tags: implementation, math
Correct Solution:
```
[x_1,y_1] = [int(i) for i in input().split()]
[x_2,y_2] = [int(i) for i in input().split()]
print(max(abs(x_1-x_2),abs(y_1-y_2)))
``` | output | 1 | 19,859 | 15 | 39,719 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 ≤ x1, y1 ≤ 109) — the start position of the robot.
The second line contains two integers x2, y2 ( - 109 ≤ x2, y2 ≤ 109) — the finish position of the robot.
Output
Print the only integer d — the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times. | instruction | 0 | 19,861 | 15 | 39,722 |
Tags: implementation, math
Correct Solution:
```
#!/usr/bin/env python3
x1, y1 = list([int(x) for x in input().split()])
x2, y2 = list([int(x) for x in input().split()])
x = abs(x2 - x1)
y = abs(y2 - y1)
print(max(x,y))
``` | output | 1 | 19,861 | 15 | 39,723 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 ≤ x1, y1 ≤ 109) — the start position of the robot.
The second line contains two integers x2, y2 ( - 109 ≤ x2, y2 ≤ 109) — the finish position of the robot.
Output
Print the only integer d — the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times. | instruction | 0 | 19,862 | 15 | 39,724 |
Tags: implementation, math
Correct Solution:
```
def robot_move(x, y, a, b):
return max(abs(x - a), abs(y - b))
x, y = map(int, input().split())
a, b = map(int, input().split())
print(robot_move(x, y, a, b))
``` | output | 1 | 19,862 | 15 | 39,725 |
Provide a correct Python 3 solution for this coding contest problem.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622 | instruction | 0 | 20,082 | 15 | 40,164 |
"Correct Solution:
```
# coding: utf-8
# Your code here!
SIZE=300000; MOD=10**9+7 #ここを変更する
inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD
fac = [0]*(SIZE+1)# fac[j] = j! mod MOD
finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE+1):
inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD
fac[i] = fac[i-1]*i%MOD
finv[i]= finv[i-1]*inv[i]%MOD
def choose(n,r): # nCk mod MOD の計算
if 0 <= r <= n:
return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
else:
return 0
h,w,n = [int(i) for i in input().split()]
xy = [[1,1]]+[[int(i) for i in input().split()] for _ in range(n)]
dp = [0]*(n+1)
dp[0] = 1
xy.sort(key=lambda x: x[1])
xy.sort(key=lambda x: x[0])
#print(xy)
for i in range(1,n+1):
x,y = xy[i]
for j in range(i):
xj,yj = xy[j]
dp[i] -= choose(x - xj + y-yj, y-yj)*dp[j]
dp[i] %= MOD
ans = 0
#print(dp)
for i,dpi in enumerate(dp):
x,y=xy[i]
ans += choose(h-x + w-y, w-y) * dpi
ans %= MOD
print(ans)
``` | output | 1 | 20,082 | 15 | 40,165 |
Provide a correct Python 3 solution for this coding contest problem.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622 | instruction | 0 | 20,083 | 15 | 40,166 |
"Correct Solution:
```
def main():
import sys
from operator import itemgetter
input = sys.stdin.readline
mod = 1000000007
# comb init
nmax = 3 * 10 ** 5 + 1 # change here
fac = [0] * (nmax + 1)
finv = [0] * (nmax + 1)
inv = [0] * (nmax + 1)
fac[0] = 1
fac[1] = 1
finv[0] = 1
finv[1] = 1
inv[1] = 1
for i in range(2, nmax):
fac[i] = fac[i - 1] * i % mod
inv[i] = mod - inv[mod % i] * (mod // i) % mod
finv[i] = finv[i - 1] * inv[i] % mod
def comb(n, r):
if n < r:
return 0
else:
return (fac[n] * ((finv[r] * finv[n - r]) % mod)) % mod
H, W, N = map(int, input().split())
block = []
for _ in range(N):
r, c = map(int, input().split())
block.append((r, c))
block.sort(key=itemgetter(1))
block.sort(key=itemgetter(0))
block.insert(0, (1, 1))
dp_p = [0] * (N+1)
dp_n = [0] * (N+1)
dp_p[0] = 1
for i in range(1, N+1):
ri, ci = block[i]
for j in range(i):
rj, cj = block[j]
if ri >= rj and ci >= cj:
dp_p[i] = (dp_p[i] + (dp_n[j] * comb(ri - rj + ci - cj, ri - rj))%mod)%mod
dp_n[i] = (dp_n[i] + (dp_p[j] * comb(ri - rj + ci - cj, ri - rj))%mod)%mod
ans = 0
for i in range(N+1):
ans = (ans + ((dp_p[i] - dp_n[i])%mod * comb(H - block[i][0] + W - block[i][1], H - block[i][0]))%mod)%mod
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 20,083 | 15 | 40,167 |
Provide a correct Python 3 solution for this coding contest problem.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622 | instruction | 0 | 20,084 | 15 | 40,168 |
"Correct Solution:
```
class Combination:
def __init__(self, n, MOD):
self.fact = [1] * (n + 1)
for i in range(1, n + 1):
self.fact[i] = self.fact[i-1] * i % MOD
self.inv_fact = [pow(self.fact[i], MOD - 2, MOD) for i in range(n + 1)]
self.MOD = MOD
def factorial(self, k):
"""k!を求める O(1)"""
return self.fact[k]
def inverse_factorial(self, k):
"""k!の逆元を求める O(1)"""
return self.inv_fact[k]
def permutation(self, k, r):
"""kPrを求める O(1)"""
if k < r:
return 0
return (self.fact[k] * self.inv_fact[r]) % self.MOD
def combination(self, k, r):
"""kCrを求める O(1)"""
if k < r:
return 0
return (self.fact[k] * self.inv_fact[k - r] * self.inv_fact[r]) % self.MOD
h, w, n = map(int, input().split())
info = [(1, 1)] + [tuple(map(int, input().split())) for i in range(n)] + [(h, w)]
info = sorted(info)
n += 2
MOD = 10**9 + 7
comb = Combination(w+h+1, MOD)
dp = {}
dp[0] = (0, 1)
def solve(ind):
if ind in dp:
return dp[ind]
i, j = info[ind]
dp0 = 0
dp1 = 0
for ind2 in range(n):
if ind2 == ind:
continue
pi, pj = info[ind2]
if pi <= i and pj <= j:
tmp = comb.combination((i+j)-(pi+pj), i-pi)
dp0 += tmp * solve(ind2)[1] % MOD
dp1 += tmp * solve(ind2)[0] % MOD
dp[ind] = (dp0, dp1)
return dp[ind]
for i in range(n):
solve(i)
print((dp[n-1][0] - dp[n-1][1]) % MOD)
``` | output | 1 | 20,084 | 15 | 40,169 |
Provide a correct Python 3 solution for this coding contest problem.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622 | instruction | 0 | 20,085 | 15 | 40,170 |
"Correct Solution:
```
mod = 10**9+7
frac = [1]*200001
for i in range(2,200001):
frac[i] = i * frac[i-1]%mod
fraci = [None]*200001
fraci[-1] = pow(frac[-1], mod -2, mod)
for i in range(-2, -200001-1, -1):
fraci[i] = fraci[i+1] * (200001 + i + 1) % mod
def comb(a, b):
if not a >= b >= 0:
return 0
return frac[a]*fraci[b]*fraci[a-b]%mod
def count(a, b):
(r1, c1), (r2, c2) = RC[a], RC[b]
if c2 < c1:
return 0
return comb(r2-r1 + c2-c1, r2-r1)
H, W, N = map(int, input().split())
RC = [list(map(int, input().split())) for _ in range(N)] + [[1, 1], [H, W]]
RC.sort()
dp = [0]*(N+2)
dp[0] += 1
for i in range(1, N+2):
res = count(0, i)
for k in range(1, i):
res = (res - count(k, i)*dp[k]) % mod
dp[i] = res
print(dp[-1])
``` | output | 1 | 20,085 | 15 | 40,171 |
Provide a correct Python 3 solution for this coding contest problem.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622 | instruction | 0 | 20,086 | 15 | 40,172 |
"Correct Solution:
```
import sys
def prepare(n):
fact = [1] * (n + 1)
for i in range(1, n + 1):
fact[i] = fact[i - 1] * i % MOD
inv = [1] * (n + 1)
inv[n] = pow(fact[n], MOD - 2, MOD)
for i in range(n - 1, 0, -1):
inv[i] = inv[i + 1] * (i + 1) % MOD
return fact, inv
MOD = 10 ** 9 + 7
h, w, n = map(int, input().split())
walls = [tuple(map(int, line.split())) for line in sys.stdin.readlines()]
walls.append((1, 1))
walls.append((h, w))
walls.sort()
fact, inv = prepare(h + w)
dp = [1]
for i, (cy, cx) in enumerate(walls[1:], start=1):
res = 0
for j, (py, px) in enumerate(walls[:i]):
if cx < px:
continue
dy, dx = cy - py, cx - px
res -= dp[j] * fact[dy + dx] % MOD * inv[dy] % MOD * inv[dx] % MOD
res %= MOD
dp.append(res)
print(-dp[-1] % MOD)
``` | output | 1 | 20,086 | 15 | 40,173 |
Provide a correct Python 3 solution for this coding contest problem.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622 | instruction | 0 | 20,087 | 15 | 40,174 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
mod=10**9+7
H,W,N=map(int,input().split())
B=[tuple(map(int,input().split())) for i in range(N)]
B.append((H,W))
B.sort()
FACT=[1]
INVFACT=[1]
for i in range(1,H+W):
FACT.append(FACT[-1]*i%mod)
INVFACT.append(INVFACT[-1]*pow(i,mod-2,mod)%mod)
def Combi(n,r):
return FACT[n]*INVFACT[n-r]*INVFACT[r]%mod
DP=[0]*(N+1)#i-1番目までに触れず、i番目のブロックに触れる経路数
for i in range(N+1):
h,w=B[i]
ANS=Combi(h+w-2,h-1)
for j in range(i):
if B[j][0]>h or B[j][1]>w:
continue
ANS=(ANS-DP[j]*Combi(h+w-B[j][0]-B[j][1],h-B[j][0]))%mod
DP[i]=ANS
print(DP[-1])
``` | output | 1 | 20,087 | 15 | 40,175 |
Provide a correct Python 3 solution for this coding contest problem.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622 | instruction | 0 | 20,088 | 15 | 40,176 |
"Correct Solution:
```
H, W, N = map(int, input().split())
P = [list(map(int, input().split())) for i in range(N)]
P.sort(reverse=1)
MOD = 10**9 + 7
fact = [1]*(H+W+1)
rfact = [1]*(H+W+1)
for i in range(H+W):
fact[i+1] = r = fact[i] * (i+1) % MOD
rfact[i+1] = pow(r, MOD-2, MOD)
def comb(n, k):
return fact[n] * rfact[k] * rfact[n-k] % MOD
dp = [0]*N
for i in range(N):
r, c = P[i]
res = comb(H - r + W - c, H - r)
for j in range(i):
r0, c0 = P[j]
if not r <= r0 or not c <= c0:
continue
res -= comb(r0-r + c0-c, r0-r) * dp[j] % MOD
dp[i] = res % MOD
ans = comb(H+W-2, H-1)
for i in range(N):
r0, c0 = P[i]
ans -= comb(r0+c0-2, r0-1) * dp[i] % MOD
print(ans % MOD)
``` | output | 1 | 20,088 | 15 | 40,177 |
Provide a correct Python 3 solution for this coding contest problem.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622 | instruction | 0 | 20,089 | 15 | 40,178 |
"Correct Solution:
```
mod = 10**9+7
frac = [1]*200001
for i in range(2,200001):
frac[i] = i * frac[i-1]%mod
fraci = [None]*200001
fraci[-1] = pow(frac[-1], mod -2, mod)
for i in range(-2, -200001-1, -1):
fraci[i] = fraci[i+1] * (200001 + i + 1) % mod
def comb(a, b):
if not a >= b >= 0:
return 0
return frac[a]*fraci[b]*fraci[a-b]%mod
H, W, N = map(int, input().split())
RC = [tuple(map(int, input().split())) for _ in range(N)] + [(1, 1), (H, W)]
RC.sort()
R, C = map(tuple, zip(*RC))
dp = [0]*(N+2)
for i in range(1, N+2):
r, c = R[i], C[i]
res = comb(r+c-2, r-1)
for k in range(1, i):
rk, ck = R[k], C[k]
if ck > c:
continue
res = (res - comb(r-rk + c-ck, r-rk)*dp[k]%mod) % mod
dp[i] = res
print(dp[-1])
``` | output | 1 | 20,089 | 15 | 40,179 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622
Submitted Solution:
```
def f():
h, w, n = map(int, input().split())
md = 10 ** 9 + 7
k = [list(map(int, input().split())) for _ in range(n)] + [[h, w]]
# 壁をマンハッタン距離順に
k.sort(key=lambda x: x[0] + x[1])
# 階乗と逆元とコンビネーションを事前準備
fct = [1] * (h + w + 1)
re_fct = [1] * (h + w + 1)
fct_i = 1
for i in range(1, h + w + 1):
fct_i = fct_i * i % md
fct[i] = fct_i
# fct_i**(md-2)が逆元re_i
re_i = pow(fct_i, md - 2, md)
for i in range(h + w, 1, -1):
re_fct[i] = re_i
re_i = re_i * i % md
cmb = lambda a, b: fct[a] * re_fct[b] * re_fct[a - b] % md
# こっから本体
dp = [0] * (n + 1)
for i, [r, c] in enumerate(k):
dpi = cmb(r + c - 2, r - 1) % md
for j in range(i):
dr, dc = r - k[j][0], c - k[j][1]
if dr >= 0 and dc >= 0:
dpi = (dpi - dp[j] * cmb(dr + dc, dr)) % md
dp[i] = dpi
print(dp[-1])
f()
``` | instruction | 0 | 20,090 | 15 | 40,180 |
Yes | output | 1 | 20,090 | 15 | 40,181 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622
Submitted Solution:
```
import sys
input = lambda : sys.stdin.readline().strip()
class PERM_COMB_MOD():
# http://drken1215.hatenablog.com/entry/2018/06/08/210000
def __init__(self, max_n=510000, mod=10**9+7):
self.fac = [0]*max_n
self.finv = [0]*max_n
self.inv = [0]*max_n
self.fac[0] = self.fac[1] = 1
self.finv[0] = self.finv[1] = 1
self.inv[1] = 1
self.max = max_n
self.mod = mod
self._maesyori()
def _maesyori(self):
for i in range(2,self.max):
self.fac[i] = self.fac[i-1] * i % self.mod
self.inv[i] = self.mod - self.inv[self.mod % i] * (self.mod // i) % self.mod
self.finv[i] = self.finv[i-1] * self.inv[i] % self.mod
def comb(self, n, k):
if n < k : return 0
if n < 0 or k < 0:return 0
return self.fac[n] * (self.finv[k] * self.finv[n-k] % self.mod) % self.mod
mod = 10**9+7
PCM = PERM_COMB_MOD(3*10**5, mod=mod)
h,w,n = map(int, input().split())
rc = [list(map(int, input().split())) for i in range(n)]
rc.append([h,w])
n += 1
rc.sort()
DP = [0]*(n+1)
# start地点
DP[0] = 1
for i in range(n):
r,c = rc[i]
DP[i+1] = PCM.comb(r-1+c-1, r-1)
for j in range(i):
rj, cj = rc[j]
if rj <= r and cj <= c:
DP[i+1] -= DP[j+1]*PCM.comb(r-rj+c-cj, r-rj)
DP[i+1] %= mod
print(DP[-1])
``` | instruction | 0 | 20,091 | 15 | 40,182 |
Yes | output | 1 | 20,091 | 15 | 40,183 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622
Submitted Solution:
```
def f():
h, w, n = map(int, input().split())
md = 10 ** 9 + 7
k = [list(map(int, input().split())) for _ in range(n)] + [[h, w]]
# 壁をマンハッタン距離順に
k.sort(key=lambda x: x[0] + x[1])
# 階乗と逆元とコンビネーションを事前準備
fct = [1] * (h + w + 1)
re_fct = [1] * (h + w + 1)
fct_i = 1
for i in range(1, h + w + 1):
fct_i = fct_i * i % md
fct[i] = fct_i
# fct_i**(md-2)が逆元re_i
re_i = 1
p = md - 2
while p:
if p & 1:
re_i = re_i * fct_i % md
p >>= 1
fct_i = fct_i ** 2 % md
for i in range(h + w, 1, -1):
re_fct[i] = re_i
re_i = re_i * i % md
cmb = lambda a, b: fct[a] * re_fct[b] * re_fct[a - b] % md
# こっから本体
dp = [0] * (n + 1)
for i, [r, c] in enumerate(k):
dpi = cmb(r + c - 2, r - 1) % md
for j in range(i):
dr, dc = r - k[j][0], c - k[j][1]
if dr >= 0 and dc >= 0:
dpi = (dpi - dp[j] * cmb(dr + dc, dr)) % md
dp[i] = dpi
print(dp[-1])
f()
``` | instruction | 0 | 20,092 | 15 | 40,184 |
Yes | output | 1 | 20,092 | 15 | 40,185 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622
Submitted Solution:
```
def f():
h, w, n = map(int, input().split())
md = 10 ** 9 + 7
k = [list(map(int, input().split())) for _ in range(n)] + [[h, w]]
# 壁をマンハッタン距離順に
k.sort(key=lambda x: x[0] + x[1])
# 階乗と逆元とコンビネーションを事前準備
fct = [1]
re_fct = []
fct_i = 1
for i in range(1, h + w + 1):
fct_i = fct_i * i % md
fct += [fct_i]
# fct_i**(md-2)が逆元re_i
re_i = 1
p = md - 2
while p:
if p & 1:
re_i = re_i * fct_i % md
p >>= 1
fct_i = fct_i ** 2 % md
for i in range(h + w, -1, -1):
re_fct += [re_i]
re_i = re_i * i % md
re_fct = re_fct[::-1]
cmb = lambda a, b: fct[a] * re_fct[b] * re_fct[a - b] % md
# こっから本体
dp = [0] * (n + 1)
for i, [r, c] in enumerate(k):
dpi = cmb(r + c - 2, r - 1) % md
for j in range(i):
dr, dc = r - k[j][0], c - k[j][1]
if dr >= 0 and dc >= 0:
dpi = (dpi - dp[j] * cmb(dr + dc, dr)) % md
dp[i] = dpi
print(dp[-1])
f()
``` | instruction | 0 | 20,093 | 15 | 40,186 |
Yes | output | 1 | 20,093 | 15 | 40,187 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622
Submitted Solution:
```
import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def com(com_n, com_r):
return fac[com_n] * inv[com_r] * inv[com_n - com_r] % md
def way(h, w):
if h < 0 or w < 0:
return 0
else:
return com(h + w, h)
# combinationの準備
# md>n_maxの条件
md = 10 ** 9 + 7
n_max = 100
fac = [1]
inv = [1] * (n_max + 1)
k_fac_inv = 1
for i in range(1, n_max + 1):
k_fac_inv = k_fac_inv * i % md
fac.append(k_fac_inv)
k_fac_inv = pow(k_fac_inv, md - 2, md)
for i in range(n_max, 1, -1):
inv[i] = k_fac_inv
k_fac_inv = k_fac_inv * i % md
def main():
h, w, n = MI()
rc = [LI1() for _ in range(n)]
# i番目の壁まで無傷で来る方法の数
# (全ルート)-(i-1までの壁にぶつかる分)
rc.sort(key=lambda x: x[0] + x[1])
dp = [0] * n
for i, (r, c) in enumerate(rc):
dp[i] = way(r, c) - sum(v * way(r - pr, c - pc) % md for v, (pr, pc) in zip(dp[:i], rc[:i]))
dp[i] %= md
h, w = h - 1, w - 1
ans = way(h, w) - sum(v * way(h - r, w - c) % md for v, (r, c) in zip(dp, rc))
print(ans % md)
main()
``` | instruction | 0 | 20,094 | 15 | 40,188 |
No | output | 1 | 20,094 | 15 | 40,189 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622
Submitted Solution:
```
#-----conbination計算部分------
def cmb(n, r):
#if ( r<0 or r>n ):
# return 0
r = min(r, n-r)
return g1[n] * g2[r] * g2[n-r] % mod
mod = 10**9+7 #出力の制限
N = 10**5*2
g1 = [1, 1] # 元テーブル
g2 = [1, 1] # 逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル
for i in range( 2, N + 1 ):
g1.append( ( g1[-1] * i ) % mod )
inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
g2.append( (g2[-1] * inverse[-1]) % mod )
#入力
H,W,N = map(int, input().split())
#迷路の初期化
maplst = []
for i in range(N):
x,y = map(int, input().split())
maplst.append([x,y])
maplst.sort()
maplst.append([H,W])
dp = []
for x,y in maplst:
dp.append(cmb(x+y-2,y-1))
#壁の分を減算
for i in range(N):
x1,y1 = maplst[i]
for j in range(i+1,N+1):
x2,y2 = maplst[j]
if y1 <= y2:
#dx = x2 - x1 + 1
#dy = y2 - y1 + 1
#dv = v1 * cmb(dx+dy-2,dy-1)
#maplst[j][2] = v2-dv
dp[j] = dp[j] - dp[i] * cmb(x2-x1+y2-y1,y2-y1)
print(dp[-1] % mod)
``` | instruction | 0 | 20,095 | 15 | 40,190 |
No | output | 1 | 20,095 | 15 | 40,191 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 13 19:37:57 2020
@author: matsui
"""
H,W,N=map(int,input().split())
H-=1
W-=1
mod=1000000007
K=[1,1]
Kinv=[1,1]
inv=[1,1]
for i in range(2,H+W+1):
K.append(K[i-1]*(i)%mod)
inv.append(mod-inv[int(mod%i)]*(mod//i)%mod)
Kinv.append(Kinv[-1]*inv[-1]%mod)
def co(r1,c1,r2,c2):
return K[r2+c2-(r1+c1)]*(Kinv[r2-r1]*Kinv[c2-c1]%mod)%mod
ans=co(0,0,H,W)
#ans=K[H+W]*(Kinv[H]*Kinv[W]%mod)%mod
R,C=[],[]
for i in range(N):
r,c=map(int,input().split())
r-=1
c-=1
B=co(0,0,r,c)
A=co(r,c,H,W)
ans-=B*A%mod
if ans<0: ans+=mod
for ri,ci in zip(R,C):
if r>=ri and c>=ci:
ans+=co(0,0,ri,ci)*co(ri,ci,r,c)*co(r,c,H,W)
#ans+=K[r+c-(ri+ci)]*(Kinv[r-ri]*Kinv[c-ci]%mod)%mod
if r<=ri and c<=ci:
ans+=co(0,0,r,c)*co(r,c,ri,ci)*co(ri,ci,H,W)
#ans+=K[ri+ci-(r+c)]*(Kinv[ri-r]*Kinv[ci-c]%mod)%mod
R.append(r)
C.append(c)
ans%=mod
#print(B,A,ans)
print(ans)
``` | instruction | 0 | 20,096 | 15 | 40,192 |
No | output | 1 | 20,096 | 15 | 40,193 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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 the j-th column from the left.
In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.
Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 2 \leq H, W \leq 10^5
* 1 \leq N \leq 3000
* 1 \leq r_i \leq H
* 1 \leq c_i \leq W
* Squares (r_i, c_i) are all distinct.
* Squares (1, 1) and (H, W) are empty squares.
Input
Input is given from Standard Input in the following format:
H W N
r_1 c_1
r_2 c_2
:
r_N c_N
Output
Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.
Examples
Input
3 4 2
2 2
1 4
Output
3
Input
5 2 2
2 1
4 2
Output
0
Input
5 5 4
3 1
3 5
1 3
5 3
Output
24
Input
100000 100000 1
50000 50000
Output
123445622
Submitted Solution:
```
#-----conbination計算部分------
def cmb(n, r):
#if ( r<0 or r>n ):
# return 0
r = min(r, n-r)
return g1[n] * g2[r] * g2[n-r] % mod
mod = 10**9+7 #出力の制限
N = 10**5*2
g1 = [1, 1] # 元テーブル
g2 = [1, 1] # 逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル
for i in range( 2, N + 1 ):
g1.append( ( g1[-1] * i ) % mod )
inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
g2.append( (g2[-1] * inverse[-1]) % mod )
#入力
H,W,N = map(int, input().split())
#迷路の初期化
maplst = []
for i in range(N):
x,y = map(int, input().split())
maplst.append([x,y])
maplst.sort()
maplst.append([H,W])
dp = [0] * len(maplst)
for i in range(len(maplst)):
x,y = maplst[i]
dp[i] = cmb(x+y-2,y-1)
#壁の分を減算
for i in range(N):
x1,y1 = maplst[i]
for j in range(i+1,N+1):
x2,y2 = maplst[j]
if y1 <= y2:
#dx = x2 - x1 + 1
#dy = y2 - y1 + 1
#dv = v1 * cmb(dx+dy-2,dy-1)
#maplst[j][2] = v2-dv
dp[j] = dp[j] - dp[i] * cmb(x2-x1+y2-y1,y2-y1)
print(dp[-1] % mod)
``` | instruction | 0 | 20,097 | 15 | 40,194 |
No | output | 1 | 20,097 | 15 | 40,195 |
Provide a correct Python 3 solution for this coding contest problem.
A robot is put at the origin in a two-dimensional plane. Initially, the robot is facing in the positive x-axis direction.
This robot will be given an instruction sequence s. s consists of the following two kinds of letters, and will be executed in order from front to back.
* `F` : Move in the current direction by distance 1.
* `T` : Turn 90 degrees, either clockwise or counterclockwise.
The objective of the robot is to be at coordinates (x, y) after all the instructions are executed. Determine whether this objective is achievable.
Constraints
* s consists of `F` and `T`.
* 1 \leq |s| \leq 8 000
* x and y are integers.
* |x|, |y| \leq |s|
Input
Input is given from Standard Input in the following format:
s
x y
Output
If the objective is achievable, print `Yes`; if it is not, print `No`.
Examples
Input
FTFFTFFF
4 2
Output
Yes
Input
FTFFTFFF
-2 -2
Output
Yes
Input
FF
1 0
Output
No
Input
TF
1 0
Output
No
Input
FFTTFF
0 0
Output
Yes
Input
TTTT
1 0
Output
No | instruction | 0 | 20,114 | 15 | 40,228 |
"Correct Solution:
```
def f(m,l):
tmpm=set()
for x in m:
tmpm.add(x+l);tmpm.add(x-l)
return tmpm
s=input()+"T"
x,y=map(int,input().split())
while s and s[0]=="F":
x-=1
s=s[1:]
ss=[{0},{0}]
mode=0
l=0
for i in range(len(s)):
if s[i]=="F": l+=1
else:
ss[mode]=f(ss[mode],l)
mode^=1
l=0
print("Yes" if x in ss[0] and y in ss[1] else "No")
``` | output | 1 | 20,114 | 15 | 40,229 |
Provide a correct Python 3 solution for this coding contest problem.
A robot is put at the origin in a two-dimensional plane. Initially, the robot is facing in the positive x-axis direction.
This robot will be given an instruction sequence s. s consists of the following two kinds of letters, and will be executed in order from front to back.
* `F` : Move in the current direction by distance 1.
* `T` : Turn 90 degrees, either clockwise or counterclockwise.
The objective of the robot is to be at coordinates (x, y) after all the instructions are executed. Determine whether this objective is achievable.
Constraints
* s consists of `F` and `T`.
* 1 \leq |s| \leq 8 000
* x and y are integers.
* |x|, |y| \leq |s|
Input
Input is given from Standard Input in the following format:
s
x y
Output
If the objective is achievable, print `Yes`; if it is not, print `No`.
Examples
Input
FTFFTFFF
4 2
Output
Yes
Input
FTFFTFFF
-2 -2
Output
Yes
Input
FF
1 0
Output
No
Input
TF
1 0
Output
No
Input
FFTTFF
0 0
Output
Yes
Input
TTTT
1 0
Output
No | instruction | 0 | 20,116 | 15 | 40,232 |
"Correct Solution:
```
S=str(input())
s=list(S)
x,y=map(int,input().split())
fora=0
side=[]
updw=[]
while s[0]=="F":
fora+=1
s.pop(0)
if len(s)==0:break
flag=1#1のとき横向き、-1のときたて向き
step=0
while s:
if s[0]=="T":
s.pop(0)
if step:
if flag==-1: updw+=[step]
else: side+=[step]
flag=flag*(-1)
step=0
else:
s.pop(0)
step+=1
if len(s)==0:
if flag==-1: updw+=[step]
else: side+=[step]
ypossi=len(side)
tpossi=len(updw)
X=sum(side)+fora+abs(x)
Y=sum(updw)+abs(y)
xdp=[[0 for i in range(2*X+1)] for j in range(ypossi+1)]
#dp[i][j] i個文字使ったときにj+len(s)は達成できるか
xdp[0][fora+X]=1
for i in range(ypossi):
for j in range(2*X+1):
if j-side[i]>=0:
if xdp[i][j-side[i]]==1:xdp[i+1][j]=1
if j+side[i]<2*X+1:
if xdp[i][j+side[i]]==1:xdp[i+1][j]=1
ydp=[[0 for i in range(2*Y+1)] for j in range(tpossi+1)]
#dp[i][j] i個文字使ったときにj+len(s)は達成できるか
ydp[0][Y]=1
for i in range(tpossi):
for j in range(2*Y+1):
if j-updw[i]>=0:
if ydp[i][j-updw[i]]==1:ydp[i+1][j]=1
if j+updw[i]<2*Y+1:
if ydp[i][j+updw[i]]==1:ydp[i+1][j]=1
if xdp[ypossi][x+X]==1 and ydp[tpossi][y+Y]==1:
print("Yes")
else: print("No")
``` | output | 1 | 20,116 | 15 | 40,233 |
Provide a correct Python 3 solution for this coding contest problem.
A robot is put at the origin in a two-dimensional plane. Initially, the robot is facing in the positive x-axis direction.
This robot will be given an instruction sequence s. s consists of the following two kinds of letters, and will be executed in order from front to back.
* `F` : Move in the current direction by distance 1.
* `T` : Turn 90 degrees, either clockwise or counterclockwise.
The objective of the robot is to be at coordinates (x, y) after all the instructions are executed. Determine whether this objective is achievable.
Constraints
* s consists of `F` and `T`.
* 1 \leq |s| \leq 8 000
* x and y are integers.
* |x|, |y| \leq |s|
Input
Input is given from Standard Input in the following format:
s
x y
Output
If the objective is achievable, print `Yes`; if it is not, print `No`.
Examples
Input
FTFFTFFF
4 2
Output
Yes
Input
FTFFTFFF
-2 -2
Output
Yes
Input
FF
1 0
Output
No
Input
TF
1 0
Output
No
Input
FFTTFF
0 0
Output
Yes
Input
TTTT
1 0
Output
No | instruction | 0 | 20,117 | 15 | 40,234 |
"Correct Solution:
```
import sys
s=input()
A,B =map(int,input().split())
table=[[] for i in range(2)]#0が横1が縦
t = 0
k=0
for i in range(len(s)):
if s[i]=='F':
k+=1
else:
table[t].append(k)
t=(t+1)%2
k=0
table[t].append(k)
L=len(s)
Lx=[set() for i in range(len(table[0]) +1)]
Ly=[set() for i in range(len(table[1]) +1)]
Lx[0].add(0)
Ly[0].add(0)
for i in range(len(table[0])):
x = table[0][i]
if i==0:
Lx[1].add(x)
continue
for a in Lx[i]:
if -L<=a+x<=L:
Lx[i+1].add(a+x)
if -L<=a-x<=L:
Lx[i+1].add(a-x)
flag=True
for a in Lx[len(table[0])]:
if a==A:
flag =False
if flag:
print('No')
sys.exit()
for i in range(len(table[1])):
x = table[1][i]
for a in Ly[i]:
if -L<=a+x<=L:
Ly[i+1].add(a+x)
if -L<=a-x<=L:
Ly[i+1].add(a-x)
flag=True
for a in Ly[len(table[1])]:
if a==B:
flag =False
if flag:
print('No')
sys.exit()
print('Yes')
``` | output | 1 | 20,117 | 15 | 40,235 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.