message stringlengths 2 28.7k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 21 109k | cluster float64 7 7 | __index_level_0__ int64 42 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Berland crossword is a puzzle that is solved on a square grid with n rows and n columns. Initially all the cells are white.
To solve the puzzle one has to color some cells on the border of the grid black in such a way that:
* exactly U cells in the top row are black;
* exactly R cells in the rightmost column are black;
* exactly D cells in the bottom row are black;
* exactly L cells in the leftmost column are black.
Note that you can color zero cells black and leave every cell white.
Your task is to check if there exists a solution to the given puzzle.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains 5 integers n, U, R, D, L (2 ≤ n ≤ 100; 0 ≤ U, R, D, L ≤ n).
Output
For each testcase print "YES" if the solution exists and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
5 2 5 3 1
3 0 0 0 0
4 4 1 4 0
2 1 1 1 1
Output
YES
YES
NO
YES
Note
Here are possible solutions to testcases 1, 2 and 4:
<image>
Submitted Solution:
```
for i in range(int(input())):
l=list(map(int,input().split()))
n=l[0]
u=l[1]
d=l[3]
r=l[2]
le=l[4]
if u+d+r+le <= 4*n -8:
print("YES")
else:
c1=0
c2=0
c3=0
c4=0
if u==n-1 and d==n-1 and r==n-1 and le==n-1:
print("YES")
else:
if u==n:
c1=c2=1
if d==n:
c3=c4=1
if r==n:
c2=c4=1
if le==n:
c1=c3=1
if u==n-1 and c1==0 and c2==0:
if le<r:
c2=1
else:
c1=1
if d==n-1 and c3==0 and c4==0:
if le<r:
c4=1
else:
c3=1
if r==n-1 and c4==0 and c2==0:
if u<d:
c4=1
else:
c2=1
if le==n-1 and c1==0 and c3==0:
if u<d:
c3=1
else:
c1=1
if u-c1-c2<=n-2 and u-c1-c2 >= 0 and d-c3-c4<=n-2 and d-c3-c4>=0 and r-c2-c4<=n-2 and r-c2-c4>=0 and le-c1-c3<=n-2 and le-c1-c3>=0:
print("YES")
else:
print("NO")
``` | instruction | 0 | 73,572 | 7 | 147,144 |
No | output | 1 | 73,572 | 7 | 147,145 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Berland crossword is a puzzle that is solved on a square grid with n rows and n columns. Initially all the cells are white.
To solve the puzzle one has to color some cells on the border of the grid black in such a way that:
* exactly U cells in the top row are black;
* exactly R cells in the rightmost column are black;
* exactly D cells in the bottom row are black;
* exactly L cells in the leftmost column are black.
Note that you can color zero cells black and leave every cell white.
Your task is to check if there exists a solution to the given puzzle.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains 5 integers n, U, R, D, L (2 ≤ n ≤ 100; 0 ≤ U, R, D, L ≤ n).
Output
For each testcase print "YES" if the solution exists and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
5 2 5 3 1
3 0 0 0 0
4 4 1 4 0
2 1 1 1 1
Output
YES
YES
NO
YES
Note
Here are possible solutions to testcases 1, 2 and 4:
<image>
Submitted Solution:
```
for s in[*open(0)][1:]:
n,*a=map(int,s.split());a*=2
print('NYOE S'[all(x<n-1for x in a)or all(x>1for x in a)or all(0<x<n for x in a)or any(0<u<n and
0<v<n and x<n-1>y or u<n-1and 0<v<n and x>1and 0<y<n for u,v,x,y in zip(a,a[1:],a[2:],a[3:]))::2])
``` | instruction | 0 | 73,573 | 7 | 147,146 |
No | output | 1 | 73,573 | 7 | 147,147 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Berland crossword is a puzzle that is solved on a square grid with n rows and n columns. Initially all the cells are white.
To solve the puzzle one has to color some cells on the border of the grid black in such a way that:
* exactly U cells in the top row are black;
* exactly R cells in the rightmost column are black;
* exactly D cells in the bottom row are black;
* exactly L cells in the leftmost column are black.
Note that you can color zero cells black and leave every cell white.
Your task is to check if there exists a solution to the given puzzle.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains 5 integers n, U, R, D, L (2 ≤ n ≤ 100; 0 ≤ U, R, D, L ≤ n).
Output
For each testcase print "YES" if the solution exists and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
5 2 5 3 1
3 0 0 0 0
4 4 1 4 0
2 1 1 1 1
Output
YES
YES
NO
YES
Note
Here are possible solutions to testcases 1, 2 and 4:
<image>
Submitted Solution:
```
import sys
import math
import bisect
# import numpy as np
fast_reader=sys.stdin.readline
fast_writer=sys.stdout.write
def input():
return fast_reader().strip()
def print(*argv):
fast_writer(' '.join((str(i)) for i in argv))
fast_writer('\n')
# flush=sys.stdout.flush
#############################################################################################
import math
for T in range(int(input())):
n,u,r,d,l = map(int,input().split())
pos = True
if(r == n):
if(u == 0 or d == 0 or (u == 1 and l == n) or (d == 1 and l == n)):
pos = False
if(d == n):
if(r == 0 or l == 0 or (r == 1 and u == n) or (l == 1 and u == n)):
pos = False
if(l == n):
if(u == 0 or d == 0 or (u == 1 and r == n) or (d == 1 and r == n)):
pos = False
if(u==n):
if(l == 0 or r == 0 or (l == 1 and d == n) or (r == 1 and d == n)):
pos = False
if(r == n-1):
if(u == 0 and d == 0 or (u == 1 and l == n) and (d == 1 and l == n)):
pos = False
if(d == n-1):
if(r == 0 and l == 0 or (r == 1 and u == n) and (l == 1 and u == n)):
pos = False
if(l == n-1):
if(u == 0 and d == 0 or (u == 1 and r == n) and (d == 1 and r == n)):
pos = False
if(u==n-1):
if(l == 0 and r == 0 or (l == 1 and d == n) and (r == 1 and d == n)):
pos = False
if(pos):
print("YES")
else:
print("NO")
``` | instruction | 0 | 73,574 | 7 | 147,148 |
No | output | 1 | 73,574 | 7 | 147,149 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1). | instruction | 0 | 73,642 | 7 | 147,284 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
n, m = map(int, input().split())
x, y = set(), set()
for i in range(n):
l = list(map(str, input().split()))
for j in range(m):
if l[j] == '1':
x.add(i)
y.add(j)
ans = 4
for i in x:
if i == 0 or i == n-1:
ans = 2
break
for i in y:
if i == 0 or i == m-1:
ans = 2
break
print(ans)
``` | output | 1 | 73,642 | 7 | 147,285 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1). | instruction | 0 | 73,643 | 7 | 147,286 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
n,m = map(int,input().split())
arr = []
for i in range(n):
arr.append(input().split())
for i in range(n):
for j in range(m):
if arr[i][j] == '1' and (i == 0 or j == 0 or i == n-1 or j == m-1):
print(2)
quit()
print(4)
``` | output | 1 | 73,643 | 7 | 147,287 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1). | instruction | 0 | 73,644 | 7 | 147,288 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
n, m = list(map(int, input().split()))
edge = False
matrix = []
for i in range(n):
arr = list(map(int, input().split()))
if arr[0] == 1 or arr[-1] == 1:
edge = True
matrix.append(arr)
for i in range(m):
if matrix[0][i] == 1 or matrix[-1][i] == 1:
edge = True
corner = False
if matrix[0][0] == 1 or matrix[0][-1] == 1 \
or matrix[-1][0] == 1 or matrix[-1][-1] == 1:
corner = True
if corner:
print(1)
elif edge:
print(2)
else:
print(4)
``` | output | 1 | 73,644 | 7 | 147,289 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1). | instruction | 0 | 73,645 | 7 | 147,290 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
n,m=map(int,input().split())
l=[]
f=0
for i in range(n):
l.append(input().split())
for i in range(n):
for j in range(m):
if(l[i][j]=='1'):
if((i==0 or i==n-1) or (j==0 or j==m-1)):
f=1
break
if(f==1):
break
if(f==0):
print(4)
else:
print(2)
``` | output | 1 | 73,645 | 7 | 147,291 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1). | instruction | 0 | 73,646 | 7 | 147,292 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
def main():
(n, m) = map(int, input().split(' '))
good = []
for i in range(n):
line = list(map(int, input().split(' ')))
for j in range(m):
if line[j] == 1:
good.append((i, j))
ret = 4
for (i, j) in good:
if i == 0 or j == 0 or i == n - 1 or j == m - 1:
ret = 2
return ret
print(main())
``` | output | 1 | 73,646 | 7 | 147,293 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1). | instruction | 0 | 73,647 | 7 | 147,294 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
'''
Created on Jan 28, 2015
@author: mohamed265
'''
t = input().split()
n = int(t[0])
m = int(t[1])
slon = 9999
for i in range(n):
t = input().split()
for j in range(m):
if t[j] == '1':
if i == 0 or i == n - 1 or j == 0 or j == m - 1:
slon = 2
else:
slon = min(4, slon)
print(slon)
``` | output | 1 | 73,647 | 7 | 147,295 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1). | instruction | 0 | 73,648 | 7 | 147,296 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
n,m=map(int,input().split())
ans=4
import sys
for i in range(n):
l1=list(map(int,input().split()))
if i==0 or i==n-1:
if l1.count(1)>0:
ans=2
print(2)
sys.exit()
else :
if l1[0]==1 or l1[m-1]==1:
print(2)
sys.exit()
print(4)
``` | output | 1 | 73,648 | 7 | 147,297 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1). | instruction | 0 | 73,649 | 7 | 147,298 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
'''input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
'''
from math import sqrt
n, m = map(int, input().split())
found = False
def solve(n, m):
for i in range(n):
s = input().split()
for j, elem in enumerate(s):
if elem != '0':
if i == 0 or i == n-1 or j == 0 or j == m-1:
return 2
return 4
print(solve(n,m))
``` | output | 1 | 73,649 | 7 | 147,299 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1).
Submitted Solution:
```
n,m=[int(x) for x in input("").split()]
l=[]
for i in range(n):
ar=list(map(int, input("").split()))
l.append(ar)
dr=0
for i in l:
if(i[0]==1 or i[m-1]==1):
dr=1
zipped_rows = zip(*l)
t= [list(row) for row in zipped_rows]
for i in t:
if(i[0]==1 or i[n-1]==1):
dr=1
print([4,2][dr])
``` | instruction | 0 | 73,650 | 7 | 147,300 |
Yes | output | 1 | 73,650 | 7 | 147,301 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1).
Submitted Solution:
```
data = input().split()
N, M = int(data[0]), int(data[1])
table = []
for i in range(N):
table.append(input().split())
answer = 0
## If there's 1 in first or last row or first and last column so answer
## will be : 2
## Otherwise, will equal to 4
## for first row
if any(e == '1' for e in table[0]):
answer = 2
## for last row
elif any(e == '1' for e in table[-1]):
answer = 2
## for first and last column
else:
for i in range(N):
if table[i][0] == '1':
answer = 2
elif table[i][-1] == '1':
answer = 2
if answer == 0:
print('4')
else:
print(answer)
``` | instruction | 0 | 73,651 | 7 | 147,302 |
Yes | output | 1 | 73,651 | 7 | 147,303 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1).
Submitted Solution:
```
import sys
n, m, *l = map(int, sys.stdin.read().split())
n = int(n)
m = int(m)
r = 4
l.reverse()
for i in range(n):
for j in range(m):
x = l.pop()
if x == 1 and (i==0 or i==n-1 or j ==0 or j==m-1):
r = 2
print(r)
``` | instruction | 0 | 73,652 | 7 | 147,304 |
Yes | output | 1 | 73,652 | 7 | 147,305 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1).
Submitted Solution:
```
n,m=[int(i) for i in input().split()]
mat=[]
for i in range(n):
l=[int(i) for i in input().split()]
mat.append(l)
l=[]
for i in range(0,n):
for j in range(0,m):
if(mat[i][j]==1 and (i==0 or i==n-1 or (j==0 or j==m-1))):
l.append(2)
elif(mat[i][j]==1):
l.append(4)
print(min(l))
``` | instruction | 0 | 73,653 | 7 | 147,306 |
Yes | output | 1 | 73,653 | 7 | 147,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1).
Submitted Solution:
```
n,m=map(int,input().split())
mini=10
for k in range(n):
s=input().split()
for j in range(m):
if(s[j]=="1"):
if(j!=0 and k!=0):
mini=min(4,mini)
else:
mini=min(2,mini)
print(mini)
``` | instruction | 0 | 73,654 | 7 | 147,308 |
No | output | 1 | 73,654 | 7 | 147,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1).
Submitted Solution:
```
n, m = map(int, input().split())
l = []
for i in range(n):
l.append(list(map(int, input().split())))
y = False
for i in range(n):
for j in range(m):
if l[i][j] == 1:
if i == 0 or i == n or j == 0 or j == m:
y = True
break
print(2 if y else 4)
``` | instruction | 0 | 73,655 | 7 | 147,310 |
No | output | 1 | 73,655 | 7 | 147,311 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1).
Submitted Solution:
```
n,m=map(int,input().split())
for i in range(n):
a=list(map(int,input().split()))
b=n+m
if b%2==0:
print((b//2)+1)
else:
print((b//2)-1)
``` | instruction | 0 | 73,656 | 7 | 147,312 |
No | output | 1 | 73,656 | 7 | 147,313 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Output
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note
In the first sample, the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (2, 2) and corner (1, 1).
* For the second time you need to choose cell (2, 2) and corner (3, 3).
* For the third time you need to choose cell (2, 2) and corner (3, 1).
* For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
<image>
* For the first time you need to choose cell (3, 1) and corner (4, 3).
* For the second time you need to choose cell (2, 3) and corner (1, 1).
Submitted Solution:
```
def solve(a):
check2 = [x + y for x, y in zip(a[0], a[-1])]
if any(x > 0 and y > 0 for x, y in zip(check2[: -1], check2[1: ])):
return 2
checkn = [sum(l) for l in zip(*a)]
if any(x > 0 and y > 0 for x, y in zip(check2[: -1], checkn[1: ])):
return 3
if any(x > 0 and y > 0 for x, y in zip(checkn[: -1], check2[1: ])):
return 3
return 4
n, m = [int(x) for x in input().split()]
a = []
for i in range(n):
a.append([int(x) for x in input().split()])
print(min(solve(a), solve(list(zip(*a)))))
``` | instruction | 0 | 73,657 | 7 | 147,314 |
No | output | 1 | 73,657 | 7 | 147,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required. | instruction | 0 | 73,722 | 7 | 147,444 |
Tags: greedy, implementation
Correct Solution:
```
import sys
input = []
input_index = 0
def next(type, number = None):
def next():
global input, input_index
while input_index == len(input):
if sys.stdin:
input = sys.stdin.readline().split()
input_index = 0
else:
raise Exception()
input_index += 1
return input[input_index - 1]
if number is None:
result = type(next())
else:
result = [type(next()) for _ in range(number)]
return result
n, m = next(int, 2)
iis = [next(str) for _ in range(n)]
count = 0
vs = [0] * n
for j in range(m - 1, -1, -1):
for i in range(n - 1, -1, -1):
c = iis[i][j]
if c == "W" and vs[i] != 1:
count += 1
d = 1 - vs[i]
for k in range(i + 1):
vs[k] += d
elif c == "B" and vs[i] != -1:
count += 1
d = -1 - vs[i]
for k in range(i + 1):
vs[k] += d
print(count)
``` | output | 1 | 73,722 | 7 | 147,445 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required. | instruction | 0 | 73,723 | 7 | 147,446 |
Tags: greedy, implementation
Correct Solution:
```
import sys
def solve():
n, m = map(int, input().split())
res = 0
tab = [list(input()) for _ in range(n)]
for row in range(n):
for col in range(m):
tab[row][col] = 1 if tab[row][col] == 'W' else -1
for row in range(n - 1, -1, -1):
for col in range(m - 1, -1, -1):
if tab[row][col] != 0:
diff = tab[row][col]
res+=1
for i in range(row + 1):
for j in range(col + 1):
tab[i][j] -= diff
return res
if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
print(solve())
``` | output | 1 | 73,723 | 7 | 147,447 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required. | instruction | 0 | 73,724 | 7 | 147,448 |
Tags: greedy, implementation
Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,copy
from itertools import chain, dropwhile, permutations, combinations
from collections import defaultdict, deque
def VI(): return list(map(int,input().split()))
def LIST(n,m=None): return [0]*n if m is None else [[0]*m for i in range(n)]
# interesting problem
# good to think of this in reverse order
# and, as usual, KISS helps: keep it simple, stupid!
# cannot break the time limit with this python implementation!
# but got the idea!
def run2(n,m,f):
# works, but doesn't pass time limit
c = LIST(n,m)
ans = 0
for i in range(n-1,-1,-1):
for j in range(m-1,-1,-1):
if f[i][j] != c[i][j]:
ans += 1
for k in range(i+1):
for l in range(j+1):
c[k][l] += f[i][j] - c[i][j]
print(ans)
def main2(info=0):
n,m = VI()
f = list(range(n))
for i in range(n):
f[i] = [1 if x=='W' else -1 for x in input()]
run2(n,m,f)
# Z = ord('W')-1
# def run(n,m,f):
# c = [chr(Z)*m for i in range(n)]
# ans = 0
# for i in range(n-1,-1,-1):
# for j in range(m-1,-1,-1):
# if f[i][j] != c[i][j]:
# ans += 1
# print(ans,c)
# for k in range(i+1):
# #c[k] = c[k] --- no, doesn't work with strings as well.
# # needs some faster language implementation, maybe pypy?
# for l in range(j+1):
# c[k][l] += f[i][j] - c[i][j]
# print(ans)
# def main(info=0):
# n,m = VI()
# f = list(range(n))
# for i in range(n):
# f[i] = input().replace('B',chr(Z-1))
# run(n,m,f)
if __name__ == "__main__":
main2()
``` | output | 1 | 73,724 | 7 | 147,449 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required. | instruction | 0 | 73,725 | 7 | 147,450 |
Tags: greedy, implementation
Correct Solution:
```
n, m = map(int, input().split())
p = [input() for i in range(n)]
t = [0] * m
k = 0
for i in range(1, n + 1):
s = 0
for j in range(1, m + 1):
s += t[-j]
q = s + 2 * (p[-i][-j] == 'W') - 1
t[-j] -= q
s -= q
if q: k += 1
print(k)
``` | output | 1 | 73,725 | 7 | 147,451 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required. | instruction | 0 | 73,726 | 7 | 147,452 |
Tags: greedy, implementation
Correct Solution:
```
n, m = map(int, input().split(' '))
p = [input() for i in range(n)]
p1 = [[1 if p[i][j] == 'B' else -1 for j in range(m)]
for i in range(n)]
tm = [0 for i in range(m)]
r = 0
for i in range(n-1, -1, -1):
for j in range(m-1, -1, -1):
if tm[j] != p1[i][j]:
r = r + 1
tp = p1[i][j] - tm[j]
for l in range(j + 1):
tm[l] = tm[l] + tp
print(r)
``` | output | 1 | 73,726 | 7 | 147,453 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required. | instruction | 0 | 73,727 | 7 | 147,454 |
Tags: greedy, implementation
Correct Solution:
```
# -*- coding: utf-8 -*-
a = []
k = []
n, m = map(int, input().split())
for i in range(n + 1):
if i < n:
a.append([-1 if c == 'B' else 1 for c in input()])
k.append([0 for j in range(m + 1)])
ans = 0
for i in range(n - 1, -1, -1):
add = 0
for j in range(m - 1, -1, -1):
k[i][j] = add + k[i + 1][j]
if k[i][j] != a[i][j]:
ans += 1
add += a[i][j] - k[i][j]
k[i][j] = a[i][j]
print(ans)
``` | output | 1 | 73,727 | 7 | 147,455 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required. | instruction | 0 | 73,728 | 7 | 147,456 |
Tags: greedy, implementation
Correct Solution:
```
import sys
def solve():
n, m = map(int, input().split())
res = 0
tab = [list(input()) for _ in range(n)]
for row in range(n):
for col in range(m):
tab[row][col] = 10000 if tab[row][col] == 'W' else -38234327
for row in range(n - 1, -1, -1):
for col in range(m - 1, -1, -1):
if tab[row][col] != 0:
diff = tab[row][col]
res+=1
for i in range(row + 1):
for j in range(col + 1):
tab[i][j] -= diff
return res
if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
print(solve())
``` | output | 1 | 73,728 | 7 | 147,457 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required. | instruction | 0 | 73,729 | 7 | 147,458 |
Tags: greedy, implementation
Correct Solution:
```
# -*- coding: utf-8 -*-
a = []
k = []
n, m = map(int, input().split())
for i in range(n + 1):
if i < n:
a.append(input())
k.append([0 for j in range(m + 1)])
ans = 0
for i in range(n - 1, -1, -1):
add = 0
for j in range(m - 1, -1, -1):
sign = 1
if a[i][j] == 'B':
sign = -1
k[i][j] = add + k[i + 1][j]
if k[i][j] != sign:
ans += 1
add += sign - k[i][j]
k[i][j] = sign
print(ans)
``` | output | 1 | 73,729 | 7 | 147,459 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required.
Submitted Solution:
```
n,m=map(int,input().strip().split())
s=[]
for i in range(n):
s.append(input())
f=[[0 for i in range(m+1)] for j in range(n+1)]
ans=0
for k in range(n+m,1,-1):
for i in range(n,0,-1):
j=k-i
if (j>m or j<1):
continue
#print(str(i)+" "+str(j)+s[i-1][j-1])
tmp=0
if (s[i-1][j-1]=='W'):
tmp=1-f[i][j]
else:
tmp=-1-f[i][j]
if (tmp!=0):
ans=ans+1
for x in range(1,i+1):
for y in range(1,j+1):
f[x][y]=f[x][y]+tmp
print(ans)
``` | instruction | 0 | 73,730 | 7 | 147,460 |
Yes | output | 1 | 73,730 | 7 | 147,461 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required.
Submitted Solution:
```
n,m=[int(i) for i in input().split()]
l=[]
for i in range(n):
l.append(input())
la=[0 for i in range(m)]
s=0
for i in range(n-1,-1,-1):
cv=0
for j in range(m-1,-1,-1):
la[j]+=cv
if l[i][j]=='W':
if la[j]!=1:
cv+=1-la[j]
la[j]=1
s+=1
else:
if la[j]!=-1:
cv+=-1-la[j]
la[j]=-1
s+=1
print (s)
``` | instruction | 0 | 73,731 | 7 | 147,462 |
Yes | output | 1 | 73,731 | 7 | 147,463 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required.
Submitted Solution:
```
a = []
k = []
n, m = map(int, input().split())
for i in range(n + 1):
if i < n:
a.append([-1 if c == 'B' else 1 for c in input()])
k.append([0 for j in range(m)])
ans = 0
for i in reversed(range(n)):
add = 0
for j in reversed(range(m)):
k[i][j] = add + k[i + 1][j]
if k[i][j] != a[i][j]:
ans += 1
add += a[i][j] - k[i][j]
k[i][j] = a[i][j]
print(ans)
``` | instruction | 0 | 73,732 | 7 | 147,464 |
Yes | output | 1 | 73,732 | 7 | 147,465 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required.
Submitted Solution:
```
n, m = [int(x) for x in input().split()]
M = [[1 if k == 'B' else -1 for k in input()] for i in range(n)]
P = [[0 for j in range(m+1)] for i in range(n+1)]
for i in range(n):
for j in range(m):
P[i][j] += M[i][j]
P[i+1][j+1] += M[i][j]
P[i+1][j] -= M[i][j]
P[i][j+1] -= M[i][j]
print(n*m - sum(P[i][1:].count(0) for i in range(1, n+1)))
``` | instruction | 0 | 73,733 | 7 | 147,466 |
Yes | output | 1 | 73,733 | 7 | 147,467 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required.
Submitted Solution:
```
__author__ = 'Данила'
n, m = map(int, input().split())
mat = [['' for i in range(m)] for j in range(n)]
for i in range(n):
s = input().rstrip()
for j in range(len(s)):
mat[i][j] = s[j]
mat1 = [[0 for i in range(m)] for j in range(n)]
mat1[0][0] = 1
for i in range(1, m):
if mat[0][i] != mat[0][i - 1]:
mat1[0][i] = mat1[0][i - 1] + 1
else:
mat1[0][i] = mat1[0][i - 1]
for i in range(1, n):
if mat[i][0] != mat[i - 1][0]:
mat1[i][0] = mat1[i - 1][0] + 1
else:
mat1[i][0] = mat1[i - 1][0]
for i in range(1, n):
for j in range(1, m):
if mat[i][j] == mat[i - 1][j] == mat[i][j - 1]:
mat1[i][j] = max(mat1[i - 1][j], mat1[i][j - 1])
elif mat[i][j] == mat[i - 1][j] or mat[i][j] == mat[i][j - 1]:
mat1[i][j] = max(mat1[i - 1][j], mat1[i][j - 1])
else:
mat1[i][j] = mat1[i - 1][j - 1] + 3
print(mat1[n - 1][m - 1])
``` | instruction | 0 | 73,734 | 7 | 147,468 |
No | output | 1 | 73,734 | 7 | 147,469 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required.
Submitted Solution:
```
n, m = map(int, input().split())
p = [input() for i in range(n)]
s = 1
for i in range(n):
for j in range(m):
q = p[i][j]
if i > 0 and j > 0: s += p[i][j - 1] != q != p[i - 1][j]
if i < n - 1:
if j < m - 1: s += p[i][j + 1] != q != p[i + 1][j]
else: s += q != p[i + 1][j]
elif j < m - 1: s += p[i][j + 1] != q
print(s)
``` | instruction | 0 | 73,735 | 7 | 147,470 |
No | output | 1 | 73,735 | 7 | 147,471 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required.
Submitted Solution:
```
# -*- coding: utf-8 -*-
a = []
k = []
n, m = map(int, input().split())
for i in range(n + 1):
if i < n:
a.append([-1 if c == 'B' else 1 for c in input()])
k.append([0 for j in range(m + 1)])
ans = 0
for i in range(n - 1, -1, -1):
add = 0
for j in range(m - 1, -1, -1):
sign = 1
k[i][j] = add + k[i + 1][j]
if k[i][j] != a[i][j]:
ans += 1
add += a[i][j] - k[i][j]
k[i][j] = sign
print(ans)
``` | instruction | 0 | 73,736 | 7 | 147,472 |
No | output | 1 | 73,736 | 7 | 147,473 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.
Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.
A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.
To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.
Some examples of the most popular Haar features are given below.
<image>
Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.
A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.
You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values at any prefix rectangle, multiply it by any integer and add to variable value.
You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.
Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Output
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
Examples
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
2
Input
3 3
WBW
BWW
WWW
Output
4
Input
3 6
WWBBWW
WWBBWW
WWBBWW
Output
3
Input
4 4
BBBB
BBBB
BBBB
BBBW
Output
4
Note
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:
1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); <image>
2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. <image>
Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required.
Submitted Solution:
```
n, m = map(int, input().split())
p = [input() for i in range(n)]
s = 1
for i in range(n):
for j in range(m):
d, q = 0, p[i][j]
if i < n - 1:
if j < m - 1:
d |= p[i][j + 1] != q != p[i + 1][j]
d |= p[i + 1][j] != p[i + 1][j + 1] != p[i][j + 1]
else: d |= q != p[i + 1][j]
elif j < m - 1: d |= p[i][j + 1] != q
s += d
print(s)
``` | instruction | 0 | 73,737 | 7 | 147,474 |
No | output | 1 | 73,737 | 7 | 147,475 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bananistan is a beautiful banana republic. Beautiful women in beautiful dresses. Beautiful statues of beautiful warlords. Beautiful stars in beautiful nights.
In Bananistan people play this crazy game – Bulbo. There’s an array of bulbs and player at the position, which represents one of the bulbs. The distance between two neighboring bulbs is 1. Before each turn player can change his position with cost |posnew - posold|. After that, a contiguous set of bulbs lights-up and player pays the cost that’s equal to the distance to the closest shining bulb. Then, all bulbs go dark again. The goal is to minimize your summed cost. I tell you, Bananistanians are spending their nights playing with bulbs.
Banana day is approaching, and you are hired to play the most beautiful Bulbo game ever. A huge array of bulbs is installed, and you know your initial position and all the light-ups in advance. You need to play the ideal game and impress Bananistanians, and their families.
Input
The first line contains number of turns n and initial position x. Next n lines contain two numbers lstart and lend, which represent that all bulbs from interval [lstart, lend] are shining this turn.
* 1 ≤ n ≤ 5000
* 1 ≤ x ≤ 109
* 1 ≤ lstart ≤ lend ≤ 109
Output
Output should contain a single number which represents the best result (minimum cost) that could be obtained by playing this Bulbo game.
Examples
Input
5 4
2 7
9 16
8 10
9 17
1 6
Output
8
Note
Before 1. turn move to position 5
Before 2. turn move to position 9
Before 5. turn move to position 8
Submitted Solution:
```
inp = input().split()
n = int(inp[0])
x = int(inp[1])
intervals = []
pos = [x, x]
S = 0; # штраф
for i in range(0, n):
inp = input().split()
lstart = int(inp[0])
lend = int(inp[1])
intervals.append((lstart, lend))
for i in range(0, n):
pair = intervals[i]
S1 = 0;
if (pos[0] >= pair[0] and pos[1] <= pair[1]):
pass;
elif (pos[0] < pair[0] and pos[1] <= pair[1]):
if (pos[1] >= pair[0]):
#S1 = pair[0] - pos[0];
pos[0] = pair[0];
else:
S1 = pair[0] - pos[1];
pos[0] = pos[1];
pos[1] = pair[0];
elif (pos[0] >= pair[0] and pos[1] > pair[1]):
if (pos[0] <= pair[1]):
#S1 = pos[1] - pair[1];
pos[1] = pair[1];
else:
S1 = pos[0] - pair[1];
pos[1] = pos[0];
pos[0] = pair[1];
S += S1;
print(S);
``` | instruction | 0 | 73,739 | 7 | 147,478 |
No | output | 1 | 73,739 | 7 | 147,479 |
Provide a correct Python 3 solution for this coding contest problem.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020 | instruction | 0 | 73,944 | 7 | 147,888 |
"Correct Solution:
```
def main():
A, B, C, D = map(int, input().split())
mod = 998244353
dp = [[0]*(D+2) for i in range(C+2)]
dp[A][B] = 1
for i in range(A, C+1):
for j in range(B, D+1):
ret = dp[i][j]
if i-A == j-B == 1:
ret -= (i-1)*(j-1)
else:
ret -= dp[i-1][j-1]*(j-1)*(i-1)
ret %= mod
dp[i][j] = ret
dp[i+1][j] += ret * j
dp[i][j+1] += ret * i
print(dp[-2][-2])
main()
``` | output | 1 | 73,944 | 7 | 147,889 |
Provide a correct Python 3 solution for this coding contest problem.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020 | instruction | 0 | 73,945 | 7 | 147,890 |
"Correct Solution:
```
A,B,C,D = map(int, input().split())
mod = 998244353
dp = [[0]*(D+1) for _ in range(C+1)]
for i in range(A, C+1):
for j in range(B, D+1):
if i == A and j == B:
dp[i][j] = 1
elif i == A and j > B:
dp[i][j] = i*dp[i][j-1]
elif i > A and j == B:
dp[i][j] = j*dp[i-1][j]
else:
dp[i][j] = j*dp[i-1][j] + i*dp[i][j-1] - dp[i-1][j-1]*(i-1)*(j-1)
dp[i][j] %= mod
print(dp[-1][-1])
``` | output | 1 | 73,945 | 7 | 147,891 |
Provide a correct Python 3 solution for this coding contest problem.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020 | instruction | 0 | 73,946 | 7 | 147,892 |
"Correct Solution:
```
a,s,d,f=map(int,input().split())
dp=[[0]*(f+1) for i in range(d+1)]
dp[a][s]=1
mod=998244353
for x in range(a,d+1):
for y in range(s,f+1):
if x==a and y==s:continue
dp[x][y]=(dp[x-1][y]*y+dp[x][y-1]*x-dp[x-1][y-1]*(x-1)*(y-1))%mod
print(dp[d][f])
``` | output | 1 | 73,946 | 7 | 147,893 |
Provide a correct Python 3 solution for this coding contest problem.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020 | instruction | 0 | 73,947 | 7 | 147,894 |
"Correct Solution:
```
def main():
mod = 998244353
a, b, c, d = map(int, input().split())
dp = [[0]*(d+1) for _ in [0]*(c+1)]
dp[a][b] = 1
for i in range(a+b+1, c+d+1):
for p in range(c, a-1, -1):
q = i-p
if q > d:
break
if q < 0:
continue
dp[p][q] = (q*dp[p-1][q]+p*dp[p][q-1] -
(p-1)*(q-1)*dp[p-1][q-1]) % mod
print(dp[c][d])
main()
``` | output | 1 | 73,947 | 7 | 147,895 |
Provide a correct Python 3 solution for this coding contest problem.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020 | instruction | 0 | 73,948 | 7 | 147,896 |
"Correct Solution:
```
# coding: utf-8
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
"""
各マスのdp
下から来たものと左から来たものを足す
その後、重なっているところを引く
"""
MOD = 998244353
A, B, C, D = lr() # Cは縦
dp = [[0] * (D+1) for _ in range(C+1)] # 1-indexed
dp[A][B] = 1
for i in range(A, C+1):
for j in range(B, D+1):
# iが縦
if i > A:
dp[i][j] += dp[i-1][j] * j
if j > B:
dp[i][j] += dp[i][j-1] * i
if i > A and j > B:
dp[i][j] -= dp[i-1][j-1] * (i-1) * (j-1)
dp[i][j] %= MOD
answer = dp[C][D]
print(answer%MOD)
``` | output | 1 | 73,948 | 7 | 147,897 |
Provide a correct Python 3 solution for this coding contest problem.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020 | instruction | 0 | 73,949 | 7 | 147,898 |
"Correct Solution:
```
MOD = 998244353
def print_dp(dp, c):
for i in range(1, c+1):
print(dp[i][1:])
def main():
a, b, c, d = map(int, input().split())
dp = [[0]*(d+1) for _ in range(c+1)]
t = [[0]*(d+1) for _ in range(c+1)]
dp[a][b] = 1
for i in range(a, c+1):
for j in range(b, d+1):
dp[i][j] += dp[i-1][j]*j
dp[i][j] += dp[i][j-1]*i
dp[i][j] -= t[i][j-1] * (i-1)
dp[i][j] %= MOD
t[i][j] += dp[i-1][j]*j
t[i][j] %= MOD
print(dp[c][d])
if __name__ == "__main__":
main()
``` | output | 1 | 73,949 | 7 | 147,899 |
Provide a correct Python 3 solution for this coding contest problem.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020 | instruction | 0 | 73,950 | 7 | 147,900 |
"Correct Solution:
```
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 998244353
Max = sys.maxsize
def l(): #intのlist
return list(map(int,input().split()))
def m(): #複数文字
return map(int,input().split())
def onem(): #Nとかの取得
return int(input())
def s(x): #圧縮
a = []
if len(x) == 0:
return []
aa = x[0]
su = 1
for i in range(len(x)-1):
if aa != x[i+1]:
a.append([aa,su])
aa = x[i+1]
su = 1
else:
su += 1
a.append([aa,su])
return a
def jo(x): #listをスペースごとに分ける
return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
return max(map(max,x))
def In(x,a): #aがリスト(sorted)
k = bs.bisect_left(a,x)
if k != len(a) and a[k] == x:
return True
else:
return False
def pow_k(x, n):
ans = 1
while n:
if n % 2:
ans *= x
x *= x
n >>= 1
return ans
"""
def nibu(x,n,r):
ll = 0
rr = r
while True:
mid = (ll+rr)//2
if rr == mid:
return ll
if (ここに評価入れる):
rr = mid
else:
ll = mid+1
"""
a,b,c,d = m()
ans = 0
po = c-a
poo = d-b
dp = [[0 for i in range(poo+1)]for j in range(po+1)]
dp[0][0] = 1
for i in range(po+1):
for j in range(poo+1):
if i == 0:
if j != 0:
dp[i][j] = dp[i][j-1] * a
dp[i][j] %= mod
else:
if j == 0:
dp[i][j] = dp[i-1][j] * b
dp[i][j] %= mod
else:
dp[i][j] = dp[i-1][j] * (b+j) + dp[i][j-1] * (a+i) - (a+i-1)*(b+j-1)*dp[i-1][j-1]
dp[i][j] %= mod
print(dp[-1][-1])
``` | output | 1 | 73,950 | 7 | 147,901 |
Provide a correct Python 3 solution for this coding contest problem.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020 | instruction | 0 | 73,951 | 7 | 147,902 |
"Correct Solution:
```
a,b,c,d = map(int,input().split())
m = c-a
n = d-b
t = [[1]]
for i in range(m):
t[0].append((b*t[0][i])%998244353)
for i in range(n):
t.append([(a*t[i][0])%998244353]+[0]*m)
for i in range(1,n+1):
for j in range(1,m+1):
t[i][j] = t[i][j-1]*(b+i) + t[i-1][j]*(a+j) - t[i-1][j-1]*((a+j-1)*(b+i-1))
t[i][j] %= 998244353
print(t[n][m])
``` | output | 1 | 73,951 | 7 | 147,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020
Submitted Solution:
```
import sys
sys.setrecursionlimit(10**7)
MOD = 998244353
A,B,C,D= map(int,input().split())
#A,B,C,D = 1,1,3000,3000
def dpinit(ps, val=0):
res = [val for i in [0]*ps[-1]]
for i in ps[:-1][::-1]:
res = [res[:] for k in [0]*i]
return res
memo = dpinit((C+1,D+1))
memo[A][B] = 1
for Y in range(A,C+1):
memo[Y][B] = pow(B,Y-A,MOD)
for X in range(B,D+1):
memo[A][X] = pow(A,X-B,MOD)
for y in range(A+1,C+1):
for x in range(B+1,D+1):
memo[y][x] = (x*memo[y-1][x] + y*memo[y][x-1] - (x-1)*(y-1)*memo[y-1][x-1]) % MOD
print(memo[C][D])
``` | instruction | 0 | 73,952 | 7 | 147,904 |
Yes | output | 1 | 73,952 | 7 | 147,905 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020
Submitted Solution:
```
#import numpy as np
import math
import collections
import bisect
def main():
a, b, c, d = map(int, input().split())
dp0 = [[1 for i in range(d + 1)] for j in range(c + 1)]
dp1 = [[0 for i in range(d + 1)] for j in range(c + 1)]
for i in range(c + 1):
for j in range(d + 1):
if j < b or i < a:
dp0[i][j] = 0
dp1[i][j] = 0
for i in range(a, c + 1):
for j in range(b, d + 1):
# print(i, j, dp[i][j])
if a == i and b == j:
continue
dp0[i][j] = ((i * dp0[i][j - 1]) % 998244353 + dp1[i][j - 1]) % 998244353
dp1[i][j] = (j * (dp0[i - 1][j] + dp1[i - 1][j]) % 998244353) % 998244353
print((dp0[c][d] + dp1[c][d]) % 998244353)
if __name__ == '__main__':
main()
``` | instruction | 0 | 73,953 | 7 | 147,906 |
Yes | output | 1 | 73,953 | 7 | 147,907 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020
Submitted Solution:
```
from sys import stdin
# stdin = open('b_1.txt')
input = stdin.readline
MOD = 998244353
def main():
A, B, C, D = map(int, input().rstrip().split())
DP = [[0 for _ in range(D + 1)] for _ in range(C + 1)]
for i in range(A, C + 1):
for j in range(B, D + 1):
if i == A and j == B:
DP[i][j] = 1
continue
DP[i][j] = ((DP[i -1][j] * j) + (DP[i][j -1] * i) - (DP[i -1][j - 1] * (i - 1) * (j - 1))) % MOD
print(DP[C][D] % MOD)
if __name__ == "__main__":
main()
``` | instruction | 0 | 73,954 | 7 | 147,908 |
Yes | output | 1 | 73,954 | 7 | 147,909 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020
Submitted Solution:
```
a, b, c, d = map(int, input().split())
mod = 998244353
DP = [[0] * (d+1) for _ in range(c+1)]
DP[a][b] = 1
for y in range(a+1, c+1):
DP[y][b] = DP[y-1][b] * b % mod
for x in range(b+1, d+1):
DP[a][x] = DP[a][x-1] * a % mod
for y in range(a+1, c+1):
for x in range(b+1, d+1):
DP[y][x] = (DP[y-1][x] * x + (DP[y][x-1] - DP[y-1][x-1] * (x-1)) * y + DP[y-1][x-1] * (x-1)) % mod
print(DP[c][d])
``` | instruction | 0 | 73,955 | 7 | 147,910 |
Yes | output | 1 | 73,955 | 7 | 147,911 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020
Submitted Solution:
```
a,b,c,d=map(int,input().split())
m=c-a +1
n=d-b+1
n_list=[[0 for i in range(m)] for j in range(n)]
n_list[0][0]=1
for i in range(1,m) :
n_list[0][i]=n_list[0][i-1]*b
for i in range(1,n) :
n_list[i][0]=n_list[i-1][0]*a
for i in range(1,n) :
for j in range(1,m) :
n_list[i][j]=n_list[i-1][j]*(a+j)+n_list[i][j-1]*(b+i)-n_list[i-1][j-1]*(a+j-1)*(b+i-1)
print(n_list[-1][-1]%998244353)
``` | instruction | 0 | 73,956 | 7 | 147,912 |
No | output | 1 | 73,956 | 7 | 147,913 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:
* Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".
* If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.
* If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.
* Then, paint one of the added squares black, and the other squares white.
Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.
Constraints
* 1 \leq A \leq C \leq 3000
* 1 \leq B \leq D \leq 3000
* A, B, C, and D are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the number of ways in which the squares can be painted in the end, modulo 998244353.
Examples
Input
1 1 2 2
Output
3
Input
2 1 3 4
Output
65
Input
31 41 59 265
Output
387222020
Submitted Solution:
```
def main():
MOD = 998244353
A, B, H, W = map(int, input().split())
dp = [[0] * (W + 1) for _ in range(H + 1)]
dp[A][B] = 1
for h in range(A, H + 1):
for w in range(B, W + 1):
if h == A and w == B: continue
t = 0
if h > A:
t += dp[h - 1][w] * w
if w > B:
t += dp[h][w - 1] * h
if h > A and w > B:
t -= dp[h - 1][w - 1] * (h - 1) * (w - 1)
dp[h][w] = t % MOD
print(dp[H][W])
if __name__ == '__main__':
main()
``` | instruction | 0 | 73,957 | 7 | 147,914 |
No | output | 1 | 73,957 | 7 | 147,915 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.