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
Provide tags and a correct Python 3 solution for this coding contest problem. On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column. The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)... Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules: As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time. The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win. Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a + 1,b), (a - 1,b), (a,b + 1), (a,b - 1), (a + 1,b - 1), (a + 1,b + 1), (a - 1,b - 1), or (a - 1,b + 1). Going outside of the field is prohibited. Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first. Input The first line contains a single integer n (2 ≀ n ≀ 10^{18}) β€” the length of the side of the chess field. The second line contains two integers x and y (1 ≀ x,y ≀ n) β€” coordinates of the cell, where the coin fell. Output In a single line print the answer "White" (without quotes), if the white king will win, or "Black" (without quotes), if the black king will win. You can print each letter in any case (upper or lower). Examples Input 4 2 3 Output White Input 5 3 5 Output Black Input 2 2 2 Output Black Note An example of the race from the first sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (4,4) into the cell (3,3). 3. The white king moves from the cell (2,2) into the cell (2,3). This is cell containing the coin, so the white king wins. <image> An example of the race from the second sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (5,5) into the cell (4,4). 3. The white king moves from the cell (2,2) into the cell (3,3). 4. The black king moves from the cell (4,4) into the cell (3,5). This is the cell, where the coin fell, so the black king wins. <image> In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. <image>
instruction
0
10,820
15
21,640
Tags: implementation, math Correct Solution: ``` n = int(input()) x, y = list(map(int, input().split(" "))) n1 = abs(x-1) + abs(y-1) n2 = abs(x-n) + abs(y-n) if n1 > n2: print("Black") else: print("White") ```
output
1
10,820
15
21,641
Provide tags and a correct Python 3 solution for this coding contest problem. On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column. The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)... Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules: As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time. The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win. Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a + 1,b), (a - 1,b), (a,b + 1), (a,b - 1), (a + 1,b - 1), (a + 1,b + 1), (a - 1,b - 1), or (a - 1,b + 1). Going outside of the field is prohibited. Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first. Input The first line contains a single integer n (2 ≀ n ≀ 10^{18}) β€” the length of the side of the chess field. The second line contains two integers x and y (1 ≀ x,y ≀ n) β€” coordinates of the cell, where the coin fell. Output In a single line print the answer "White" (without quotes), if the white king will win, or "Black" (without quotes), if the black king will win. You can print each letter in any case (upper or lower). Examples Input 4 2 3 Output White Input 5 3 5 Output Black Input 2 2 2 Output Black Note An example of the race from the first sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (4,4) into the cell (3,3). 3. The white king moves from the cell (2,2) into the cell (2,3). This is cell containing the coin, so the white king wins. <image> An example of the race from the second sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (5,5) into the cell (4,4). 3. The white king moves from the cell (2,2) into the cell (3,3). 4. The black king moves from the cell (4,4) into the cell (3,5). This is the cell, where the coin fell, so the black king wins. <image> In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. <image>
instruction
0
10,821
15
21,642
Tags: implementation, math Correct Solution: ``` n=int(input()) (x,y)=map(int,input().split()) if ((x-1)+(y-1))>((n-x)+(n-y)): print("Black") else: print("White") ```
output
1
10,821
15
21,643
Provide tags and a correct Python 3 solution for this coding contest problem. On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column. The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)... Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules: As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time. The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win. Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a + 1,b), (a - 1,b), (a,b + 1), (a,b - 1), (a + 1,b - 1), (a + 1,b + 1), (a - 1,b - 1), or (a - 1,b + 1). Going outside of the field is prohibited. Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first. Input The first line contains a single integer n (2 ≀ n ≀ 10^{18}) β€” the length of the side of the chess field. The second line contains two integers x and y (1 ≀ x,y ≀ n) β€” coordinates of the cell, where the coin fell. Output In a single line print the answer "White" (without quotes), if the white king will win, or "Black" (without quotes), if the black king will win. You can print each letter in any case (upper or lower). Examples Input 4 2 3 Output White Input 5 3 5 Output Black Input 2 2 2 Output Black Note An example of the race from the first sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (4,4) into the cell (3,3). 3. The white king moves from the cell (2,2) into the cell (2,3). This is cell containing the coin, so the white king wins. <image> An example of the race from the second sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (5,5) into the cell (4,4). 3. The white king moves from the cell (2,2) into the cell (3,3). 4. The black king moves from the cell (4,4) into the cell (3,5). This is the cell, where the coin fell, so the black king wins. <image> In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. <image>
instruction
0
10,822
15
21,644
Tags: implementation, math Correct Solution: ``` n = int(input()) x, y = map(int, input().split()) w = max(abs(x-1), abs(y-1)) b = max(abs(x-n), abs(y-n)) if b < w: print("Black") else: print("White") ```
output
1
10,822
15
21,645
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column. The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)... Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules: As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time. The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win. Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a + 1,b), (a - 1,b), (a,b + 1), (a,b - 1), (a + 1,b - 1), (a + 1,b + 1), (a - 1,b - 1), or (a - 1,b + 1). Going outside of the field is prohibited. Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first. Input The first line contains a single integer n (2 ≀ n ≀ 10^{18}) β€” the length of the side of the chess field. The second line contains two integers x and y (1 ≀ x,y ≀ n) β€” coordinates of the cell, where the coin fell. Output In a single line print the answer "White" (without quotes), if the white king will win, or "Black" (without quotes), if the black king will win. You can print each letter in any case (upper or lower). Examples Input 4 2 3 Output White Input 5 3 5 Output Black Input 2 2 2 Output Black Note An example of the race from the first sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (4,4) into the cell (3,3). 3. The white king moves from the cell (2,2) into the cell (2,3). This is cell containing the coin, so the white king wins. <image> An example of the race from the second sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (5,5) into the cell (4,4). 3. The white king moves from the cell (2,2) into the cell (3,3). 4. The black king moves from the cell (4,4) into the cell (3,5). This is the cell, where the coin fell, so the black king wins. <image> In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. <image> Submitted Solution: ``` a=int(input()) n, m=map(int, input().split()) mh=min(abs(1-n),abs(1-m)) mb=min(abs(a-n),abs(a-m)) if mh<=mb: print("White") else: print("Black") ```
instruction
0
10,823
15
21,646
Yes
output
1
10,823
15
21,647
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column. The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)... Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules: As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time. The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win. Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a + 1,b), (a - 1,b), (a,b + 1), (a,b - 1), (a + 1,b - 1), (a + 1,b + 1), (a - 1,b - 1), or (a - 1,b + 1). Going outside of the field is prohibited. Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first. Input The first line contains a single integer n (2 ≀ n ≀ 10^{18}) β€” the length of the side of the chess field. The second line contains two integers x and y (1 ≀ x,y ≀ n) β€” coordinates of the cell, where the coin fell. Output In a single line print the answer "White" (without quotes), if the white king will win, or "Black" (without quotes), if the black king will win. You can print each letter in any case (upper or lower). Examples Input 4 2 3 Output White Input 5 3 5 Output Black Input 2 2 2 Output Black Note An example of the race from the first sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (4,4) into the cell (3,3). 3. The white king moves from the cell (2,2) into the cell (2,3). This is cell containing the coin, so the white king wins. <image> An example of the race from the second sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (5,5) into the cell (4,4). 3. The white king moves from the cell (2,2) into the cell (3,3). 4. The black king moves from the cell (4,4) into the cell (3,5). This is the cell, where the coin fell, so the black king wins. <image> In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. <image> Submitted Solution: ``` n = int(input()) x, y = map(int, input().split()) #w = ((x-1) ** 2 + (y-1) ** 2) ** 0.5 #b = ((n-x) ** 2 + (n-y) ** 2) ** 0.5 w = max((x-1), (y-1)) b = max((n-x), (n-y)) if (w <= b): print("White") else: print("Black") ```
instruction
0
10,824
15
21,648
Yes
output
1
10,824
15
21,649
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column. The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)... Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules: As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time. The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win. Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a + 1,b), (a - 1,b), (a,b + 1), (a,b - 1), (a + 1,b - 1), (a + 1,b + 1), (a - 1,b - 1), or (a - 1,b + 1). Going outside of the field is prohibited. Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first. Input The first line contains a single integer n (2 ≀ n ≀ 10^{18}) β€” the length of the side of the chess field. The second line contains two integers x and y (1 ≀ x,y ≀ n) β€” coordinates of the cell, where the coin fell. Output In a single line print the answer "White" (without quotes), if the white king will win, or "Black" (without quotes), if the black king will win. You can print each letter in any case (upper or lower). Examples Input 4 2 3 Output White Input 5 3 5 Output Black Input 2 2 2 Output Black Note An example of the race from the first sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (4,4) into the cell (3,3). 3. The white king moves from the cell (2,2) into the cell (2,3). This is cell containing the coin, so the white king wins. <image> An example of the race from the second sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (5,5) into the cell (4,4). 3. The white king moves from the cell (2,2) into the cell (3,3). 4. The black king moves from the cell (4,4) into the cell (3,5). This is the cell, where the coin fell, so the black king wins. <image> In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. <image> Submitted Solution: ``` n=int(input()) x,y=input().split() x=int(x) y=int(y) s=x*y a=n-x+1 b=n-y+1 c=a*b if s<=c: print("white") elif c<s: print("black") ```
instruction
0
10,825
15
21,650
Yes
output
1
10,825
15
21,651
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column. The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)... Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules: As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time. The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win. Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a + 1,b), (a - 1,b), (a,b + 1), (a,b - 1), (a + 1,b - 1), (a + 1,b + 1), (a - 1,b - 1), or (a - 1,b + 1). Going outside of the field is prohibited. Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first. Input The first line contains a single integer n (2 ≀ n ≀ 10^{18}) β€” the length of the side of the chess field. The second line contains two integers x and y (1 ≀ x,y ≀ n) β€” coordinates of the cell, where the coin fell. Output In a single line print the answer "White" (without quotes), if the white king will win, or "Black" (without quotes), if the black king will win. You can print each letter in any case (upper or lower). Examples Input 4 2 3 Output White Input 5 3 5 Output Black Input 2 2 2 Output Black Note An example of the race from the first sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (4,4) into the cell (3,3). 3. The white king moves from the cell (2,2) into the cell (2,3). This is cell containing the coin, so the white king wins. <image> An example of the race from the second sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (5,5) into the cell (4,4). 3. The white king moves from the cell (2,2) into the cell (3,3). 4. The black king moves from the cell (4,4) into the cell (3,5). This is the cell, where the coin fell, so the black king wins. <image> In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. <image> Submitted Solution: ``` import math n = int(input()) s = input() a = s.split(" ") x, y = int(a[0]), int(a[1]) if x == y and x*2 > n and n % 2 == 0: print("Black") elif x < y and x*2 > n: print("Black") else: dis1 = math.sqrt(pow(x - 1, 2) + pow(y - 1, 2)) dis2 = math.sqrt(pow(x - n, 2) + pow(y - n, 2)) if dis1 <= dis2: print("White") else: print("Black") ```
instruction
0
10,826
15
21,652
Yes
output
1
10,826
15
21,653
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column. The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)... Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules: As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time. The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win. Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a + 1,b), (a - 1,b), (a,b + 1), (a,b - 1), (a + 1,b - 1), (a + 1,b + 1), (a - 1,b - 1), or (a - 1,b + 1). Going outside of the field is prohibited. Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first. Input The first line contains a single integer n (2 ≀ n ≀ 10^{18}) β€” the length of the side of the chess field. The second line contains two integers x and y (1 ≀ x,y ≀ n) β€” coordinates of the cell, where the coin fell. Output In a single line print the answer "White" (without quotes), if the white king will win, or "Black" (without quotes), if the black king will win. You can print each letter in any case (upper or lower). Examples Input 4 2 3 Output White Input 5 3 5 Output Black Input 2 2 2 Output Black Note An example of the race from the first sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (4,4) into the cell (3,3). 3. The white king moves from the cell (2,2) into the cell (2,3). This is cell containing the coin, so the white king wins. <image> An example of the race from the second sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (5,5) into the cell (4,4). 3. The white king moves from the cell (2,2) into the cell (3,3). 4. The black king moves from the cell (4,4) into the cell (3,5). This is the cell, where the coin fell, so the black king wins. <image> In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. <image> Submitted Solution: ``` white = [1,1] black = [0, 0] target = ["x", "y"] black[0] = black[1] = int(input()) target[0], target[1] = map(int, input().split()) white_move = max(target[0]-white[0], target[1]-white[1]) black_move = max(black[0]-target[0], black[1]-target[1]) print(white_move) print(black_move) if(black_move >=white_move): print("White") else: print("Black") ```
instruction
0
10,827
15
21,654
No
output
1
10,827
15
21,655
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column. The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)... Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules: As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time. The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win. Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a + 1,b), (a - 1,b), (a,b + 1), (a,b - 1), (a + 1,b - 1), (a + 1,b + 1), (a - 1,b - 1), or (a - 1,b + 1). Going outside of the field is prohibited. Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first. Input The first line contains a single integer n (2 ≀ n ≀ 10^{18}) β€” the length of the side of the chess field. The second line contains two integers x and y (1 ≀ x,y ≀ n) β€” coordinates of the cell, where the coin fell. Output In a single line print the answer "White" (without quotes), if the white king will win, or "Black" (without quotes), if the black king will win. You can print each letter in any case (upper or lower). Examples Input 4 2 3 Output White Input 5 3 5 Output Black Input 2 2 2 Output Black Note An example of the race from the first sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (4,4) into the cell (3,3). 3. The white king moves from the cell (2,2) into the cell (2,3). This is cell containing the coin, so the white king wins. <image> An example of the race from the second sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (5,5) into the cell (4,4). 3. The white king moves from the cell (2,2) into the cell (3,3). 4. The black king moves from the cell (4,4) into the cell (3,5). This is the cell, where the coin fell, so the black king wins. <image> In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. <image> Submitted Solution: ``` import math n = int(input()) x, y = map(int, input().split(" ")) white = math.sqrt((x - 1) * (x - 1) + (y - 1) * (y - 1)) black = math.sqrt((x - n) * (x - n) + (y - n) * (y - n)) if n % 2 == 0: if white == black: if x > n / 2 and y > n / 2: print("Black") else: print("White") else: if white == black: print("White") elif white < black: print("White") elif x > n / 2 and y > n / 2: print("Black") else: print("Black") ```
instruction
0
10,828
15
21,656
No
output
1
10,828
15
21,657
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column. The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)... Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules: As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time. The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win. Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a + 1,b), (a - 1,b), (a,b + 1), (a,b - 1), (a + 1,b - 1), (a + 1,b + 1), (a - 1,b - 1), or (a - 1,b + 1). Going outside of the field is prohibited. Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first. Input The first line contains a single integer n (2 ≀ n ≀ 10^{18}) β€” the length of the side of the chess field. The second line contains two integers x and y (1 ≀ x,y ≀ n) β€” coordinates of the cell, where the coin fell. Output In a single line print the answer "White" (without quotes), if the white king will win, or "Black" (without quotes), if the black king will win. You can print each letter in any case (upper or lower). Examples Input 4 2 3 Output White Input 5 3 5 Output Black Input 2 2 2 Output Black Note An example of the race from the first sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (4,4) into the cell (3,3). 3. The white king moves from the cell (2,2) into the cell (2,3). This is cell containing the coin, so the white king wins. <image> An example of the race from the second sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (5,5) into the cell (4,4). 3. The white king moves from the cell (2,2) into the cell (3,3). 4. The black king moves from the cell (4,4) into the cell (3,5). This is the cell, where the coin fell, so the black king wins. <image> In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. <image> Submitted Solution: ``` num=int(input()) a,b=map(int,input().split(' ')) xw=1 yw=1 xb=num yb=num c=0 c2=0 if xb==b: c=yb-a if xw==b: c2=yw-a else: c2=yw-a xw+=c2 yw+=c2 c2+=abs(b-xw) else: c=yb-a xb-=c yb-=c c+=abs(b-xb) if xw==b: c2=yw-a else: c2=yw-a xw+=c2 yw+=c2 c2+=abs(b-xw) if c<c2: print('Black') else: print('White') ```
instruction
0
10,829
15
21,658
No
output
1
10,829
15
21,659
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column. The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)... Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules: As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time. The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win. Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a + 1,b), (a - 1,b), (a,b + 1), (a,b - 1), (a + 1,b - 1), (a + 1,b + 1), (a - 1,b - 1), or (a - 1,b + 1). Going outside of the field is prohibited. Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first. Input The first line contains a single integer n (2 ≀ n ≀ 10^{18}) β€” the length of the side of the chess field. The second line contains two integers x and y (1 ≀ x,y ≀ n) β€” coordinates of the cell, where the coin fell. Output In a single line print the answer "White" (without quotes), if the white king will win, or "Black" (without quotes), if the black king will win. You can print each letter in any case (upper or lower). Examples Input 4 2 3 Output White Input 5 3 5 Output Black Input 2 2 2 Output Black Note An example of the race from the first sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (4,4) into the cell (3,3). 3. The white king moves from the cell (2,2) into the cell (2,3). This is cell containing the coin, so the white king wins. <image> An example of the race from the second sample where both the white king and the black king move optimally: 1. The white king moves from the cell (1,1) into the cell (2,2). 2. The black king moves form the cell (5,5) into the cell (4,4). 3. The white king moves from the cell (2,2) into the cell (3,3). 4. The black king moves from the cell (4,4) into the cell (3,5). This is the cell, where the coin fell, so the black king wins. <image> In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. <image> Submitted Solution: ``` n=int(input()) x,y=map(int,input().split()) a=x-1 b=n-x if(a<b): print("WHITE") else: print("BLACK") ```
instruction
0
10,830
15
21,660
No
output
1
10,830
15
21,661
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.
instruction
0
11,006
15
22,012
Tags: binary search, implementation Correct Solution: ``` def valid(n, m, x, y): return x >= 1 and x <= n and y >= 1 and y <= m def binary_search(n, m, x, y, dx, dy): l, r = 0, n+m+5 while (l+1 < r): mid = l+r >> 1 if (valid(n, m, x + mid*dx, y + mid*dy)): l = mid else: r = mid return l def main(): n, m = map(int, input().split()) x, y = map(int, input().split()) k = int(input()) res = 0 for i in range(k): dx, dy = map(int, input().split()) l = binary_search(n, m, x, y, dx, dy) res += l x += l * dx y += l * dy print(res) main() ```
output
1
11,006
15
22,013
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.
instruction
0
11,007
15
22,014
Tags: binary search, implementation Correct Solution: ``` import re import itertools from collections import Counter class Task: n, m = 0, 0 start, end = 0, 0 vectors = [] answer = 0 def getData(self): self.n, self.m = [int(x) for x in input().split(' ')] self.start, self.end = [int(x) for x in input().split(' ')] numberOfVectors = int(input()) for i in range(0, numberOfVectors): self.vectors += [[int(x) for x in input().split(' ')]] def solve(self): x, y = self.start, self.end for currentVector in self.vectors: #while 1 <= x + currentVector[0] <= self.n and \ # 1 <= y + currentVector[1] <= self.m: # x += currentVector[0] # y += currentVector[1] # self.answer += 1 maxStepsX = self.maxSteps(self.n, x, currentVector[0]) maxStepsY = self.maxSteps(self.m, y, currentVector[1]) x += min(maxStepsX, maxStepsY) * currentVector[0] y += min(maxStepsX, maxStepsY) * currentVector[1] self.answer += min(maxStepsX, maxStepsY) def maxSteps(self, maxX, x, dx): if dx == 0: return 10**9 return (maxX - x) // dx if dx > 0 else (x - 1) // (-dx) def printAnswer(self): print(self.answer) task = Task(); task.getData(); task.solve(); task.printAnswer(); ```
output
1
11,007
15
22,015
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.
instruction
0
11,008
15
22,016
Tags: binary search, implementation Correct Solution: ``` from sys import stdin, stdout def main(): n,m = map(int, stdin.readline().split()) xc, yc = map(int, stdin.readline().split()) k = int(stdin.readline()) total = 0 for _ in range(k): dx, dy = map(int, stdin.readline().split()) maxx = 0 maxy = 0 if dx > 0: maxx = (n-xc)//dx elif dx < 0: maxx = (xc-1)//(-dx) else: maxx = 1e9 if dy > 0: maxy = (m-yc)//dy elif dy < 0: maxy = (yc-1)//(-dy) else: maxy = 1e9 valid = min(maxx, maxy) xc += dx*valid yc += dy*valid total += valid #print("took", valid , "steps, now at " , xc, yc) print(total) return main() ```
output
1
11,008
15
22,017
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.
instruction
0
11,009
15
22,018
Tags: binary search, implementation Correct Solution: ``` R = lambda: map(int, input().split()) n, m = R() x0, y0 = R() k = int(input()) xys = [list(R()) for i in range(k)] res = 0 for dx, dy in xys: l, r = 0, max(n, m) + 7 while l < r: mm = (l + r + 1) // 2 xx, yy = x0 + mm * dx, y0 + mm * dy if 0 < xx <= n and 0 < yy <= m: l = mm else: r = mm - 1 res += l x0, y0 = x0 + l * dx, y0 + l * dy print(res) ```
output
1
11,009
15
22,019
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.
instruction
0
11,010
15
22,020
Tags: binary search, implementation Correct Solution: ``` # ip = open("testdata.txt", "r") # def input(): # return ip.readline().strip() n, m = map(int, input().split()) x0, y0 = map(int, input().split()) k = int(input()) total = 0 for i in range(k): dx, dy = map(int, input().split()) if dx >= 0: s1 = (n - x0)//dx if dx != 0 else float('inf') else: s1 = (x0 - 1)//(-dx) if dy >= 0: s2 = (m - y0)//(dy) if dy != 0 else float('inf') else: s2 = (y0 - 1)//(-dy) s = min(s1, s2) total += s x0 = x0 + s*dx y0 = y0 + s*dy print(total) ```
output
1
11,010
15
22,021
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.
instruction
0
11,011
15
22,022
Tags: binary search, implementation Correct Solution: ``` n,m = list(map(int,input().split())) curx,cury = list(map(int,input().split())) k = int(input()) ans = 0 for i in range(k): x,y = list(map(int,input().split())) sx,sy = 1<<30,1<<30 if x<0: sx = (curx-1)//abs(x) if x>0: sx = (n-curx)//x if y<0: sy = (cury-1)//abs(y) if y>0: sy = (m-cury)//y k = min(sx,sy) curx+= k*x cury+=k*y ans+=k print(ans) ```
output
1
11,011
15
22,023
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.
instruction
0
11,012
15
22,024
Tags: binary search, implementation Correct Solution: ``` # -*- coding: utf-8 -*- """Steps.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/15H6TXUKZ_A8CXwh0JHcGO43Z6QgCkYWd """ def readln(): return tuple(map(int, input().split())) n, m = readln() x, y = readln() k, = readln() ans = 0 for _ in range(k): a, b = readln() va = vb = 1 << 30 if a > 0: va = (n - x) // a if a < 0: va = (1 - x) // a if b > 0: vb = (m - y) // b if b < 0: vb = (1 - y) // b k = min(va, vb) ans += k x += a * k y += b * k print(ans) ```
output
1
11,012
15
22,025
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.
instruction
0
11,013
15
22,026
Tags: binary search, implementation Correct Solution: ``` x,y=map(eval,input().split()) x0,y0=map(eval,input().split()) n=eval(input()) d=[] num=0 for i in range(n): d.append(list(map(eval,input().split()))) for i in d: if i[0]>0 and i[1]>0: a=min((x-x0)//i[0],(y-y0)//i[1]) elif i[0]==0 and i[1]>0: a=(y-y0)//i[1] elif i[1]==0 and i[0]>0: a=(x-x0)//i[0] elif i[0]<0 and i[1]<0: a=min((x0-1)//abs(i[0]),(y0-1)//abs(i[1])) elif i[0]==0 and i[1]<0: a=(y0-1)//abs(i[1]) elif i[1]==0 and i[0]<0: a=(x0-1)//abs(i[0]) elif i[0]<0 and i[1]>0: a=min((x0-1)//abs(i[0]),(y-y0)//i[1]) else: a=min((y0-1)//abs(i[1]),(x-x0)//i[0]) x0+=a*i[0] y0+=a*i[1] num+=abs(a) print(num) ```
output
1
11,013
15
22,027
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard. Submitted Solution: ``` def check(x,y,at_x,at_y,N,M): if x == 0: if y > 0: dy = M - at_y count = dy // y at_x = at_x at_y = at_y + count*y return (count,at_x,at_y) else: dy = at_y - 1 count = dy // abs(y) at_x = at_x at_y = at_y - count*abs(y) return (count,at_x,at_y) elif y == 0: if x > 0: dx = N - at_x count = dx // x at_x = at_x + count*x at_y = at_y return (count,at_x,at_y) else: dx = at_x - 1 count = dx // abs(x) at_x = at_x - count*abs(x) at_y = at_y return (count,at_x,at_y) else: if x > 0 and y > 0: dx = N - at_x dy = M - at_y if dx // x < dy // y: count = dx // x at_x = at_x + count*x at_y = at_y + count*y return (count,at_x,at_y) else: count = dy // y at_x = at_x + count*x at_y = at_y + count*y return (count,at_x,at_y) elif x < 0 and y > 0: dx = at_x - 1 dy = M - at_y if dx // abs(x) < dy // y: count = dx // abs(x) at_x = at_x - count*abs(x) at_y = at_y + count*y return (count,at_x,at_y) else: count = dy // y at_x = at_x - count*abs(x) at_y = at_y + count*y return(count,at_x,at_y) elif x > 0 and y < 0: dx = N - at_x dy = at_y - 1 if dx // abs(x) < dy // abs(y): count = dx // abs(x) at_x = at_x + count*abs(x) at_y = at_y - count*abs(y) return (count,at_x,at_y) else: count = dy // abs(y) at_x = at_x + count*abs(x) at_y = at_y - count*abs(y) return (count,at_x,at_y) elif x < 0 and y < 0: dx = at_x - 1 dy = at_y - 1 if dx // abs(x) < dy // abs(y): count = dx // abs(x) at_x = at_x - count*abs(x) at_y = at_y - count*abs(y) return (count,at_x,at_y) else: count = dy // abs(y) at_x = at_x - count*abs(x) at_y = at_y - count*abs(y) return (count,at_x,at_y) def question2(): row,col = map(int,input().split()) at_x,at_y = map(int,input().split()) vect = int(input()) count = 0 for i in range(vect): x,y = map(int,input().split()) # print("hii") count1,at_x,at_y = check(x,y,at_x,at_y,row,col) count += count1 return count # remained_test_case = int(input()) remained_test_case = 1 while remained_test_case > 0: print(question2()) remained_test_case -= 1 ```
instruction
0
11,014
15
22,028
Yes
output
1
11,014
15
22,029
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard. Submitted Solution: ``` import sys def input(): return sys.stdin.readline().strip() def find_step(x, a, n): if a > 0: return (n-x)//a elif a < 0: return (1-x)//a else: return 10**10 n, m = map(int, input().split()) x, y = map(int, input().split()) k = int(input()) steps = 0 for i in range(k): a, b = map(int, input().split()) k1 = find_step(x, a, n) k2 = find_step(y, b, m) # print(k1, k2) steps += min(k1, k2) x = x + a*min(k1, k2) y = y + b*min(k1, k2) print(steps) ```
instruction
0
11,015
15
22,030
Yes
output
1
11,015
15
22,031
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard. Submitted Solution: ``` #Mamma don't raises quitter................................................. from collections import deque as de import math from math import sqrt as sq from math import floor as fl from math import ceil as ce from sys import stdin, stdout import re from collections import Counter as cnt from functools import reduce from itertools import groupby as gb #from fractions import Fraction as fr from bisect import bisect_left as bl, bisect_right as br def factors(n): return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))) class My_stack(): def __init__(self): self.data = [] def my_push(self, x): return (self.data.append(x)) def my_pop(self): return (self.data.pop()) def my_peak(self): return (self.data[-1]) def my_contains(self, x): return (self.data.count(x)) def my_show_all(self): return (self.data) def isEmpty(self): return len(self.data)==0 arrStack = My_stack() #decimal to binary def decimalToBinary(n): return bin(n).replace("0b", "") #binary to decimal def binarytodecimal(n): return int(n,2) def isPrime(n) : if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True def get_prime_factors(number): prime_factors = [] while number % 2 == 0: prime_factors.append(2) number = number / 2 for i in range(3, int(math.sqrt(number)) + 1, 2): while number % i == 0: prime_factors.append(int(i)) number = number / i if number > 2: prime_factors.append(int(number)) return prime_factors def get_frequency(list): dic={} for ele in list: if ele in dic: dic[ele] += 1 else: dic[ele] = 1 return dic def Log2(x): return (math.log10(x) / math.log10(2)); # Function to get product of digits def getProduct(n): product = 1 while (n != 0): product = product * (n % 10) n = n // 10 return product #function to find LCM of two numbers def lcm(x,y): lcm = (x*y)//math.gcd(x,y) return lcm def isPowerOfTwo(n): return (math.ceil(Log2(n)) == math.floor(Log2(n))); #to check whether the given sorted sequnce is forming an AP or not.... def checkisap(list): d=list[1]-list[0] for i in range(2,len(list)): temp=list[i]-list[i-1] if temp !=d: return False return True #seive of erathanos def primes_method5(n): out ={} sieve = [True] * (n+1) for p in range(2, n+1): if (sieve[p]): out[p]=1 for i in range(p, n+1, p): sieve[i] = False return out #ceil function gives wrong answer after 10^17 so i have to create my own :) # because i don't want to doubt on my solution of 900-1000 problem set. def ceildiv(x,y): return (x+y-1)//y def di():return map(int, input().split()) def ii():return int(input()) def li():return list(map(int, input().split())) def si():return list(map(str, input())) def indict(): dic = {} for index, value in enumerate(input().split()): dic[int(value)] = int(index)+1 return dic def frqdict(): # by default it is for integer input. :) dic={} for index, value in enumerate(input()): if value not in dic: dic[value] =1 else: dic[value] +=1 return dic #inp = open("input.txt","r") #out = open("output.txt","w") #Here we go...................... #practice like your never won #perform like you never lost n, m = map(int, input().split()) x, y = map(int, input().split()) k = int(input()) count = 0 for i in range(k): dx, dy = map(int, input().split()) ans = n + m #print(ans) if dx > 0: ans = min(ans, (n - x) // dx) if dx < 0: ans = min(ans, (x - 1) // -dx) if dy > 0: ans = min(ans, (m - y) // dy) if dy < 0: ans = min(ans, (y - 1) // -dy) count += ans #print(count) x += dx * ans y += dy * ans #print(x,y) print(count) ```
instruction
0
11,016
15
22,032
Yes
output
1
11,016
15
22,033
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard. Submitted Solution: ``` """ β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘ β•šβ•β•β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β•β• β–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β•šβ•β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• β•šβ•β• β•šβ•β•β•β•β•β• β•šβ•β• β•šβ•β•β•β•β•β•β• β•šβ•β•β•β•β•β• β•šβ•β• β•šβ•β•β•β•β• """ __author__ = "Dilshod" def check(x, y, dx, dy, moves): global n, m if x + dx * moves > n or y + dy * moves > m or x + dx * moves <= 0 or y + dy * moves <= 0: return True return False def moves(a, b): global x, y left = 0 right = 1000000000 while left <= right: moves = (left + right) // 2 if left == right - 1: break if check(x, y, a, b, moves): right = moves else: left = moves return left n, m = map(int, input().split()) x, y = map(int, input().split()) k = int(input()) cnt = 0 for i in range(k): a, b = map(int, input().split()) c = moves(a, b) cnt += c x += a * c y += b * c print(cnt) ```
instruction
0
11,017
15
22,034
Yes
output
1
11,017
15
22,035
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard. Submitted Solution: ``` def isValid(y ,i , j , r , c): step = 0 for k in D: while True: if i+k[0] <= r and i+k[0] >0 and j+k[1] <= c and j+k[1] > 0 : i , j = i+k[0] , j+k[1] step +=1 else: break D.remove(D[0]) return step D = [] n , m = map(int,input().split()) x , y = map(int , input().split()) d = int(input()) for _ in range(d): k , z = map(int,input().split()) D.append([k , z]) print(isValid(D , x , y , n , m)) ```
instruction
0
11,018
15
22,036
No
output
1
11,018
15
22,037
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard. Submitted Solution: ``` a,b=map(int,input().split()) x,y=map(int,input().split()) ans=0 for _ in " "*int(input()): u,v=map(int,input().split()) if u==0 : t=0 if v<0: t=(y-1)//(-v) else:t=(y-1)//v y+=v*t ans+=t elif v==0: t=0 if u<0:t=(x-1)//(-u) else:t=(a-x)//u x+=t*u ans+=t else: t,t1=0,0 if u<0: t=(x-1)//(-u) else: t=(a-x)//u if v<0: t1=(y-1)//(-v) else:t1=(b-y)//v t=min(t,t1) ans+=t y+=t*v;x+=t*u print(ans) ```
instruction
0
11,019
15
22,038
No
output
1
11,019
15
22,039
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard. Submitted Solution: ``` # link: https://codeforces.com/contest/152/problem/B if __name__ == "__main__": n,m = map(int, input().split()) new_x,new_y = map(int, input().split()) k = int(input()) steps = 0 new_x -= 1 new_y -= 1 while k: a,b = map(int, input().split()) end = 1100000000 while end>0: if (new_x + a*end >= 0 and new_x + a*end < n) and (new_y + b*end >= 0 and new_y + b*end < m): new_x = new_x + a*end new_y = new_y + b*end steps += end end = int(end / 2) k -= 1 print(steps) ```
instruction
0
11,020
15
22,040
No
output
1
11,020
15
22,041
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular n Γ— m field. The squares have coordinates (x, y) (1 ≀ x ≀ n, 1 ≀ y ≀ m), where x is the index of the row and y is the index of the column. Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m). The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≀ 109, |dx| + |dy| β‰₯ 1). Output Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 4 5 1 1 3 1 1 1 1 0 -2 Output 4 Input 10 10 1 2 1 -1 0 Output 0 Note In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard. Submitted Solution: ``` def get_steps(n, m, x, y, dx, dy): x_all = max(n - (n - x) - 1, n - x) y_all = max(m - (m - y) - 1, m - y) if dx == 0: x_move = 10**18 elif dx != 0: x_move = (x_all//dx) if dy == 0: y_move = 10**18 elif dy != 0: y_move = (y_all//dy) return abs(min(x_move, y_move)) n, m = map(int, input().split()) x, y = map(int, input().split()) k = int(input()) steps = 0 for i in range(k): dx, dy = map(int, input().split()) moves = get_steps(n, m, x, y, dx, dy) if moves == 0: pass else: tmp_x = dx * moves + x tmp_y = dy * moves + y if tmp_x in range(1, n+1) and tmp_y in range(1, m+1): x += dx * moves y += dy * moves steps += moves else: pass if steps == 0: print(0) else: print(steps + 1) ```
instruction
0
11,021
15
22,042
No
output
1
11,021
15
22,043
Provide tags and a correct Python 3 solution for this coding contest problem. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1
instruction
0
11,037
15
22,074
Tags: brute force, implementation Correct Solution: ``` n, m = map(int, input().split()) arr = [[0] * (n+4) for _ in range(n+4)] for c in range(m): x, y = map(int, input().split()) for i in range(x, x + 3): for j in range(y, y + 3): arr[i][j] += 1 if arr[i][j] == 9: print(c + 1) exit() print(-1) ```
output
1
11,037
15
22,075
Provide tags and a correct Python 3 solution for this coding contest problem. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1
instruction
0
11,038
15
22,076
Tags: brute force, implementation Correct Solution: ``` a,b=map(int,input().split()) z=[[0]*(a+4) for i in range(a+4)] def gh(i,j): for i1 in range(i-2,i+1): for j1 in range(j-2,j+1): if i1<1 or j1<1:continue ok=True for i2 in range(i1,i1+3): for j2 in range(j1,j1+3): if z[i2][j2]==0:ok=False;break if not(ok):break if ok:return ok return False for _ in range(1,b+1): u,v=map(int,input().split()) z[u][v]=1 if gh(u,v):exit(print(_)) print(-1) ```
output
1
11,038
15
22,077
Provide tags and a correct Python 3 solution for this coding contest problem. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1
instruction
0
11,039
15
22,078
Tags: brute force, implementation Correct Solution: ``` n,m = map(int,input().split()) grid = [[0 for i in range(n)] for j in range(n)] for tc in range(m): r,c = map(int,input().split()) r -= 1 c -= 1 ok = False for i in range(r-1,r+2): for j in range(c-1,c+2): # print(i,j) if 0 <= i and i < n and 0 <= j and j < n: grid[i][j] += 1 if grid[i][j] == 9: ok = True if ok: print(tc+1) exit() print(-1) ```
output
1
11,039
15
22,079
Provide tags and a correct Python 3 solution for this coding contest problem. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1
instruction
0
11,040
15
22,080
Tags: brute force, implementation Correct Solution: ``` from sys import stdin a,b=map(int,stdin.readline().split()) z=[[0]*(a+4) for i in range(a+4)] def gh(i,j): for i1 in range(i-2,i+1): for j1 in range(j-2,j+1): if i1<1 or j1<1:continue ok=True for i2 in range(i1,i1+3): for j2 in range(j1,j1+3): if z[i2][j2]==0:ok=False;break if not(ok):break if ok:return ok return False for _ in range(1,b+1): u,v=map(int,stdin.readline().split()) z[u][v]=1 if gh(u,v):exit(print(_)) print(-1) ```
output
1
11,040
15
22,081
Provide tags and a correct Python 3 solution for this coding contest problem. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1
instruction
0
11,041
15
22,082
Tags: brute force, implementation Correct Solution: ``` def f(): n, m = map(int, input().split()) p = [[0] * (n + 2) for i in range(n + 2)] for k in range(m): x, y = map(int, input().split()) for i in range(x - 1, x + 2): for j in range(y - 1, y + 2): if p[i][j] == 8: return k + 1 p[i][j] += 1 return -1 print(f()) ```
output
1
11,041
15
22,083
Provide tags and a correct Python 3 solution for this coding contest problem. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1
instruction
0
11,042
15
22,084
Tags: brute force, implementation Correct Solution: ``` def f(): n, m = map(int, input().split()) p = [[0] * (n + 2) for i in range(n + 2)] for k in range(m): x, y = map(int, input().split()) for i in range(x - 1, x + 2): for j in range(y - 1, y + 2): if p[i][j] == 8: return k + 1 p[i][j] += 1 return -1 print(f()) # Made By Mostafa_Khaled ```
output
1
11,042
15
22,085
Provide tags and a correct Python 3 solution for this coding contest problem. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1
instruction
0
11,043
15
22,086
Tags: brute force, implementation Correct Solution: ``` import math from sys import stdin from math import ceil import sys if __name__ == '__main__': numbers = list(map(int, input().split())) n = numbers[0] m = numbers[1] moves = [[0] * (n + 4) for _ in range(n + 4)] for i in range(m): listOfMoves = list(map(int, input().split())) x = listOfMoves[0] y = listOfMoves[1] for a in range(x, x + 3): for b in range(y, y + 3): moves[a][b] = moves[a][b] + 1 if moves[a][b] == 9: print(i + 1) quit() print(-1) ```
output
1
11,043
15
22,087
Provide tags and a correct Python 3 solution for this coding contest problem. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1
instruction
0
11,044
15
22,088
Tags: brute force, implementation Correct Solution: ``` R=lambda:map(int,input().split()) n,m=R() if n<3 or m<9: print(-1) exit() a=[[0]*(n-2) for i in range(n-2)] for i in range(1,m+1): x,y=R() x-=1 y-=1 for x0 in range(x-2,x+1): for y0 in range(y-2,y+1): if x0<0 or y0<0 or x0>=n-2 or y0>=n-2: continue a[x0][y0]+=1 if a[x0][y0]==9: print(i) exit() print(-1) ```
output
1
11,044
15
22,089
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1 Submitted Solution: ``` def solve_case(): n, m = map(int, input().split()) count = [None] * n for i in range(n): count[i] = [0] * n ans = -1 for k in range(m): x, y = map(int, input().split()) x -= 1 y -= 1 found = False for i in range(x-2, x+1): for j in range(y-2, y+1): if i >= 0 and i < n and j >= 0 and j < n: count[i][j] += 1 if count[i][j] == 9: found = True if found: ans = k + 1 break print(ans) solve_case() ```
instruction
0
11,045
15
22,090
Yes
output
1
11,045
15
22,091
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1 Submitted Solution: ``` n, m = map(int, input().split()) c = [[0] * (n + 4) for _ in range(n + 4)] for l in range(m): x, y = map(int, input().split()) for i in range(x, x + 3): for j in range(y, y + 3): c[i][j] += 1 if c[i][j] == 9: print(l + 1) quit() print(-1) ```
instruction
0
11,046
15
22,092
Yes
output
1
11,046
15
22,093
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1 Submitted Solution: ``` n, m = map(int, input().split()) a = [[0 for i in range(n + 2)] for i in range(n + 2)] answer = -1 for i in range(m): x, y = map(int, input().split()) a[x-1][y-1] += 1 a[x-1][y] += 1 a[x-1][y+1] += 1 a[x][y-1] += 1 a[x][y] += 1 a[x][y+1] += 1 a[x+1][y-1] += 1 a[x+1][y] += 1 a[x+1][y+1] += 1 if max(a[x-1][y-1], a[x-1][y], a[x-1][y+1], a[x][y-1], a[x][y], a[x][y+1], a[x+1][y-1], a[x+1][y], a[x+1][y+1]) == 9: answer = i + 1 break print(answer) ```
instruction
0
11,047
15
22,094
Yes
output
1
11,047
15
22,095
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1 Submitted Solution: ``` read = lambda: map(int, input().split()) xy = [[0]*1002 for i in range(1002)] n, m = read() for i in range(m): x, y = read() for j in range(x-1, x+2): for k in range(y-1, y+2): xy[j][k] += 1 if xy[j][k] is 9: print(i+1) exit() print(-1) ```
instruction
0
11,048
15
22,096
Yes
output
1
11,048
15
22,097
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1 Submitted Solution: ``` import bisect import os import gc import sys from io import BytesIO, IOBase from collections import Counter from collections import deque import heapq import math import statistics def sin(): return input() def ain(): return list(map(int, sin().split())) def sain(): return input().split() def iin(): return int(sin()) MAX = float('inf') MIN = float('-inf') MOD = 1000000007 def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 s = set() for p in range(2, n+1): if prime[p]: s.add(p) return s def readTree(n, m): adj = [deque([]) for _ in range(n+1)] for _ in range(m): u,v = ain() adj[u].append(v) adj[v].append(u) return adj def main(): n,m = ain() arr = [[0]*n for _ in range(n)] d = {} for i in range(m): x,y = ain() arr[x-1][y-1] = 1 d[str(x-1)+str(y-1)] = i ans = MAX for i in range(n-2): for j in range(n-2): ss = set() if str(i)+str(j) in d: for k in range(3): for m in range(3): if arr[i+k][j+m] == 1: ss.add(d[str(i+k)+str(j+m)]) if len(ss) == 9: ans = min(ans, max(ss)) if ans == MAX: print(-1) else: print(ans+1) # Fast IO Template starts 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") # Fast IO Template ends if __name__ == "__main__": main() ```
instruction
0
11,049
15
22,098
No
output
1
11,049
15
22,099
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n Γ— n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ min(nΒ·n, 105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≀ xi, yi ≀ n) β€” the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. Output On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. Examples Input 4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1 Output 10 Input 4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1 Output -1 Submitted Solution: ``` def f(): n, m = map(int, input().split()) k, p = 1, [[0] * (n + 1) for i in range(n + 1)] for i in range(m): x, y = map(int, input().split()) p[x][y] = 1 if k > 8: for i in range(max(x - 2, 1), min(x + 2, n) - 1): for j in range(max(y - 2, 1), min(y + 2, n) - 1): if k == 10: print(i, j, p[i][j: j + 3] + p[i + 1][j: j + 3] + p[i + 2][j: j + 3]) if [1, 1, 1] == p[i][j: j + 3] == p[i + 1][j: j + 3] == p[i + 2][j: j + 3]: return k k += 1 return -1 print(f()) ```
instruction
0
11,050
15
22,100
No
output
1
11,050
15
22,101
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost. Input The first line contains an integer n (1 ≀ n ≀ 300), number of cards. The second line contains n numbers li (1 ≀ li ≀ 109), the jump lengths of cards. The third line contains n numbers ci (1 ≀ ci ≀ 105), the costs of cards. Output If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. Examples Input 3 100 99 9900 1 1 1 Output 2 Input 5 10 20 30 40 50 1 1 1 1 1 Output -1 Input 7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 Output 6 Input 8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 Output 7237 Note In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
instruction
0
11,161
15
22,322
Tags: bitmasks, brute force, dp, math Correct Solution: ``` from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop import math from collections import * from functools import reduce,cmp_to_key,lru_cache import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline import sys # input = sys.stdin.readline M = mod = 10 ** 9 + 7 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip().split()] def st():return str(input().rstrip())[2:-1] def val():return int(input().rstrip()) def li2():return [str(i)[2:-1] for i in input().rstrip().split()] def li3():return [int(i) for i in st()] n = val() l = li() c = li() element = l[0] for i in range(1, n):element = math.gcd(element, l[i]) if element != 1: print(-1) exit() myset = {} for ind, i in enumerate(l): for j in list(myset): temp = math.gcd(j, i) if(temp not in myset):myset[temp] = myset[j] + c[ind] else:myset[temp] = min(myset[temp], c[ind] + myset[j]) if i not in myset:myset[i] = c[ind] else:myset[i] = min(myset[i], c[ind]) print(myset[1]) ```
output
1
11,161
15
22,323
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost. Input The first line contains an integer n (1 ≀ n ≀ 300), number of cards. The second line contains n numbers li (1 ≀ li ≀ 109), the jump lengths of cards. The third line contains n numbers ci (1 ≀ ci ≀ 105), the costs of cards. Output If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. Examples Input 3 100 99 9900 1 1 1 Output 2 Input 5 10 20 30 40 50 1 1 1 1 1 Output -1 Input 7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 Output 6 Input 8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 Output 7237 Note In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
instruction
0
11,162
15
22,324
Tags: bitmasks, brute force, dp, math Correct Solution: ``` import sys from math import gcd from collections import defaultdict as dd input=sys.stdin.readline n=int(input()) l=list(map(int,input().split())) c=list(map(int,input().split())) dp=dict() for i in range(n): if dp.get(l[i]): dp[l[i]]=min(dp[l[i]],c[i]) else: dp[l[i]]=c[i] for ll in l: keys=list(dp.keys()) for j in keys: g=gcd(j,ll) if dp.get(g): dp[g]=min(dp[g],dp[ll]+dp[j]) else: dp[g]=dp[ll]+dp[j] if 1 in dp: print(dp[1]) else: print(-1) ```
output
1
11,162
15
22,325
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost. Input The first line contains an integer n (1 ≀ n ≀ 300), number of cards. The second line contains n numbers li (1 ≀ li ≀ 109), the jump lengths of cards. The third line contains n numbers ci (1 ≀ ci ≀ 105), the costs of cards. Output If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. Examples Input 3 100 99 9900 1 1 1 Output 2 Input 5 10 20 30 40 50 1 1 1 1 1 Output -1 Input 7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 Output 6 Input 8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 Output 7237 Note In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
instruction
0
11,163
15
22,326
Tags: bitmasks, brute force, dp, math Correct Solution: ``` import math g=0 n=int(input()) b=list(map(int,input().split())) c=list(map(int,input().split())) dp=dict() dp[0]=0 s=set([0]) for i in range(n): for j in s: g=math.gcd(j,b[i]) if g in dp: dp[g]=min(dp[g],dp[j]+c[i]) else: dp[g]=dp[j]+c[i] s=set(dp.keys()) if 1 in dp.keys(): print(dp[1]) else: print(-1) ```
output
1
11,163
15
22,327
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost. Input The first line contains an integer n (1 ≀ n ≀ 300), number of cards. The second line contains n numbers li (1 ≀ li ≀ 109), the jump lengths of cards. The third line contains n numbers ci (1 ≀ ci ≀ 105), the costs of cards. Output If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. Examples Input 3 100 99 9900 1 1 1 Output 2 Input 5 10 20 30 40 50 1 1 1 1 1 Output -1 Input 7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 Output 6 Input 8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 Output 7237 Note In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
instruction
0
11,164
15
22,328
Tags: bitmasks, brute force, dp, math Correct Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq,bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys import threading from collections import defaultdict threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase sys.setrecursionlimit(300000) 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") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=2**51, func=lambda a, b: a & b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b: max(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] < key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary------------------------------------ n=int(input()) l=list(map(int,input().split())) cost=list(map(int,input().split())) dp=defaultdict(int) dp[0]=0 se=set([0]) for i in range(n): for j in se: k=int(math.gcd(j,l[i])) if dp[k]==0: dp[k]=dp[j]+cost[i] dp[k]=min(dp[k],dp[j]+cost[i]) se=set(dp.keys()) if dp[1]==0: print(-1) else: print(dp[1]) ```
output
1
11,164
15
22,329
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost. Input The first line contains an integer n (1 ≀ n ≀ 300), number of cards. The second line contains n numbers li (1 ≀ li ≀ 109), the jump lengths of cards. The third line contains n numbers ci (1 ≀ ci ≀ 105), the costs of cards. Output If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. Examples Input 3 100 99 9900 1 1 1 Output 2 Input 5 10 20 30 40 50 1 1 1 1 1 Output -1 Input 7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 Output 6 Input 8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 Output 7237 Note In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
instruction
0
11,165
15
22,330
Tags: bitmasks, brute force, dp, math Correct Solution: ``` def main(): input() acc = {0: 0} for p, c in zip(list(map(int, input().split())), list(map(int, input().split()))): adds = [] for b, u in acc.items(): a = p while b: a, b = b, a % b adds.append((a, u + c)) for a, u in adds: acc[a] = min(u, acc.get(a, 1000000000)) print(acc.get(1, -1)) if __name__ == '__main__': main() ```
output
1
11,165
15
22,331
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost. Input The first line contains an integer n (1 ≀ n ≀ 300), number of cards. The second line contains n numbers li (1 ≀ li ≀ 109), the jump lengths of cards. The third line contains n numbers ci (1 ≀ ci ≀ 105), the costs of cards. Output If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. Examples Input 3 100 99 9900 1 1 1 Output 2 Input 5 10 20 30 40 50 1 1 1 1 1 Output -1 Input 7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 Output 6 Input 8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 Output 7237 Note In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
instruction
0
11,166
15
22,332
Tags: bitmasks, brute force, dp, math Correct Solution: ``` from collections import defaultdict from math import gcd n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) dp = defaultdict(lambda: float("inf")) for a, b in zip(A, B): dp[a] = min(dp[a], b) for d in dp.copy(): cur = gcd(a, d) dp[cur] = min(dp[cur], dp[a] + dp[d]) if 1 not in dp: print(-1) else: print(dp[1]) ```
output
1
11,166
15
22,333
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost. Input The first line contains an integer n (1 ≀ n ≀ 300), number of cards. The second line contains n numbers li (1 ≀ li ≀ 109), the jump lengths of cards. The third line contains n numbers ci (1 ≀ ci ≀ 105), the costs of cards. Output If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. Examples Input 3 100 99 9900 1 1 1 Output 2 Input 5 10 20 30 40 50 1 1 1 1 1 Output -1 Input 7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 Output 6 Input 8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 Output 7237 Note In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
instruction
0
11,167
15
22,334
Tags: bitmasks, brute force, dp, math Correct Solution: ``` n = int(input()) l = [int(x) for x in input().split()] c = [int(x) for x in input().split()] def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) gcds = {0: 0} for i in range(n): adds = {} for g in gcds.keys(): x = gcd(g, l[i]) y = gcds.get(x) u = gcds[g] if y is not None: if u + c[i] < y: t = adds.get(x) if t and t > u + c[i] or t is None: adds[x] = u + c[i] else: t = adds.get(x) if t and t > u + c[i]or t is None: adds[x] = u + c[i] gcds.update(adds) if gcds.get(1): print(gcds[1]) else: print(-1) ```
output
1
11,167
15
22,335
Provide tags and a correct Python 3 solution for this coding contest problem. Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost. Input The first line contains an integer n (1 ≀ n ≀ 300), number of cards. The second line contains n numbers li (1 ≀ li ≀ 109), the jump lengths of cards. The third line contains n numbers ci (1 ≀ ci ≀ 105), the costs of cards. Output If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. Examples Input 3 100 99 9900 1 1 1 Output 2 Input 5 10 20 30 40 50 1 1 1 1 1 Output -1 Input 7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 Output 6 Input 8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 Output 7237 Note In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
instruction
0
11,168
15
22,336
Tags: bitmasks, brute force, dp, math Correct Solution: ``` import sys, math import io, os #data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from bisect import bisect_left as bl, bisect_right as br, insort from heapq import heapify, heappush, heappop from collections import defaultdict as dd, deque, Counter # from itertools import permutations,combinations def data(): return sys.stdin.readline().strip() def mdata(): return list(map(int, data().split())) def outl(var): sys.stdout.write(' '.join(map(str, var)) + '\n') def out(var): sys.stdout.write(str(var) + '\n') from decimal import Decimal # from fractions import Fraction # sys.setrecursionlimit(100000) mod = int(1e9) + 7 INF=float('inf') n=int(data()) l=mdata() c=mdata() d=dict() for i in range(n): if d.get(l[i]): d[l[i]]=min(d[l[i]],c[i]) else: d[l[i]]=c[i] for i in l: lis=list(d.keys()) for j in lis: g = math.gcd(i, j) if d.get(g): d[g]=min(d[g],d[i]+d[j]) else: d[g] = d[i] + d[j] if 1 in d: out(d[1]) else: out(-1) ```
output
1
11,168
15
22,337
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost. Input The first line contains an integer n (1 ≀ n ≀ 300), number of cards. The second line contains n numbers li (1 ≀ li ≀ 109), the jump lengths of cards. The third line contains n numbers ci (1 ≀ ci ≀ 105), the costs of cards. Output If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. Examples Input 3 100 99 9900 1 1 1 Output 2 Input 5 10 20 30 40 50 1 1 1 1 1 Output -1 Input 7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 Output 6 Input 8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 Output 7237 Note In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1. Submitted Solution: ``` import os import sys from io import BytesIO, IOBase # 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") # ------------------------------ from math import factorial from collections import Counter, defaultdict from heapq import heapify, heappop, heappush def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) def ctd(chr): return ord(chr)-ord("a") mod = 998244353 INF = float('inf') from math import gcd # ------------------------------ def main(): n = N() larr = RLL() carr = RLL() dic = {} dic[0] = 0 for i in range(n): l, c = larr[i], carr[i] ndic = dic.copy() for j in dic: now = gcd(j, l) if now not in ndic: ndic[now] = c+dic[j] else: ndic[now] = min(ndic[now], dic[j]+c) dic = ndic print(dic.get(1, -1)) if __name__ == "__main__": main() ```
instruction
0
11,169
15
22,338
Yes
output
1
11,169
15
22,339