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 |
|---|---|---|---|---|---|
Provide a correct Python 3 solution for this coding contest problem.
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
* 1≦N≦1000
* 2≦K≦1000
* The correct answer is at most 2^{31}-1.
Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10 | instruction | 0 | 63,006 | 7 | 126,012 |
"Correct Solution:
```
iN,iK = [int(_) for _ in input().split()]
print(iK*((iK-1)**(iN-1)))
``` | output | 1 | 63,006 | 7 | 126,013 |
Provide a correct Python 3 solution for this coding contest problem.
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
* 1≦N≦1000
* 2≦K≦1000
* The correct answer is at most 2^{31}-1.
Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10 | instruction | 0 | 63,007 | 7 | 126,014 |
"Correct Solution:
```
# ABC 046 B
N, K = map(int, input().split())
print(K * (K-1)**(N-1))
``` | output | 1 | 63,007 | 7 | 126,015 |
Provide a correct Python 3 solution for this coding contest problem.
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
* 1≦N≦1000
* 2≦K≦1000
* The correct answer is at most 2^{31}-1.
Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10 | instruction | 0 | 63,008 | 7 | 126,016 |
"Correct Solution:
```
N,K = map(int, input().split())
pattern = K*(K-1)**(N-1)
print(pattern)
``` | output | 1 | 63,008 | 7 | 126,017 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
* 1≦N≦1000
* 2≦K≦1000
* The correct answer is at most 2^{31}-1.
Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10
Submitted Solution:
```
n,k=map(int, input().split())
case=k*((k-1)**(n-1))
print(case)
``` | instruction | 0 | 63,009 | 7 | 126,018 |
Yes | output | 1 | 63,009 | 7 | 126,019 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
* 1≦N≦1000
* 2≦K≦1000
* The correct answer is at most 2^{31}-1.
Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10
Submitted Solution:
```
a=list(map(int,input().split()))
print(((a[1]-1)**(a[0]-1))*a[1])
``` | instruction | 0 | 63,010 | 7 | 126,020 |
Yes | output | 1 | 63,010 | 7 | 126,021 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
* 1≦N≦1000
* 2≦K≦1000
* The correct answer is at most 2^{31}-1.
Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10
Submitted Solution:
```
N, K = map(int, input().split())
ans = K * (K - 1)**(N - 1)
print(ans)
``` | instruction | 0 | 63,011 | 7 | 126,022 |
Yes | output | 1 | 63,011 | 7 | 126,023 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
* 1≦N≦1000
* 2≦K≦1000
* The correct answer is at most 2^{31}-1.
Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10
Submitted Solution:
```
import math
N,K=map(int,input().split())
print(K*((K-1)**(N-1)))
``` | instruction | 0 | 63,012 | 7 | 126,024 |
Yes | output | 1 | 63,012 | 7 | 126,025 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
* 1≦N≦1000
* 2≦K≦1000
* The correct answer is at most 2^{31}-1.
Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10
Submitted Solution:
```
ball, color = map(int, input().split())
first = color * 1
second = 0
if ball != 1: second = (color - 1) ** (ball-1)
print(first * second)
``` | instruction | 0 | 63,013 | 7 | 126,026 |
No | output | 1 | 63,013 | 7 | 126,027 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
* 1≦N≦1000
* 2≦K≦1000
* The correct answer is at most 2^{31}-1.
Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10
Submitted Solution:
```
N,K =map(int,input().split())
print(N*((K-1)**(N-1)))
``` | instruction | 0 | 63,014 | 7 | 126,028 |
No | output | 1 | 63,014 | 7 | 126,029 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
* 1≦N≦1000
* 2≦K≦1000
* The correct answer is at most 2^{31}-1.
Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10
Submitted Solution:
```
a=list(map(int,input().split()))
print(a[1]+(a[0]-1)*(a[1]-1))
``` | instruction | 0 | 63,015 | 7 | 126,030 |
No | output | 1 | 63,015 | 7 | 126,031 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
* 1≦N≦1000
* 2≦K≦1000
* The correct answer is at most 2^{31}-1.
Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10
Submitted Solution:
```
a=list(map(int,input().split()))
print(a[1]+(a[0]-1)*(a[1]-1)
``` | instruction | 0 | 63,016 | 7 | 126,032 |
No | output | 1 | 63,016 | 7 | 126,033 |
Provide a correct Python 3 solution for this coding contest problem.
Problem Statement
Let's consider operations on monochrome images that consist of hexagonal pixels, each of which is colored in either black or white. Because of the shape of pixels, each of them has exactly six neighbors (e.g. pixels that share an edge with it.)
"Filtering" is an operation to determine the color of a pixel from the colors of itself and its six neighbors. Examples of filterings are shown below.
Example 1: Color a pixel in white when all of its neighboring pixels are white. Otherwise the color will not change.
<image>
Performing this operation on all the pixels simultaneously results in "noise canceling," which removes isolated black pixels.
Example 2: Color a pixel in white when its all neighboring pixels are black. Otherwise the color will not change.
<image>
Performing this operation on all the pixels simultaneously results in "edge detection," which leaves only the edges of filled areas.
Example 3: Color a pixel with the color of the pixel just below it, ignoring any other neighbors.
<image>
Performing this operation on all the pixels simultaneously results in "shifting up" the whole image by one pixel.
Applying some filter, such as "noise canceling" and "edge detection," twice to any image yields the exactly same result as if they were applied only once. We call such filters idempotent. The "shifting up" filter is not idempotent since every repeated application shifts the image up by one pixel.
Your task is to determine whether the given filter is idempotent or not.
Input
The input consists of multiple datasets. The number of dataset is less than $100$. Each dataset is a string representing a filter and has the following format (without spaces between digits).
> $c_0c_1\cdots{}c_{127}$
$c_i$ is either '0' (represents black) or '1' (represents white), which indicates the output of the filter for a pixel when the binary representation of the pixel and its neighboring six pixels is $i$. The mapping from the pixels to the bits is as following:
<image>
and the binary representation $i$ is defined as $i = \sum_{j=0}^6{\mathit{bit}_j \times 2^j}$, where $\mathit{bit}_j$ is $0$ or $1$ if the corresponding pixel is in black or white, respectively. Note that the filter is applied on the center pixel, denoted as bit 3.
The input ends with a line that contains only a single "#".
Output
For each dataset, print "yes" in a line if the given filter is idempotent, or "no" otherwise (quotes are for clarity).
Sample Input
00000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000111111111
10000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111
01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
Output for the Sample Input
yes
yes
no
Example
Input
00000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000111111111
10000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111
01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
#
Output
yes
yes
no | instruction | 0 | 63,110 | 7 | 126,220 |
"Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
# 13
# 12 11
# 13 06 13
# 05 04
# 10 03 09
# 02 01
# 13 00 13
# 08 07
# 13
def main():
rr = []
tt = [
[13, 7, 8, 0, 1, 2, 3],
[7, 13, 0, 1, 9, 3, 4],
[8, 0, 13, 2, 3, 10, 5],
[0, 1, 2, 3, 4, 5, 6],
[1, 9, 3, 4, 13, 6, 11],
[2, 3, 10, 5, 6, 13, 12],
[3, 4, 5, 6, 11, 12, 13]
]
while True:
s = S()
if s == '#':
break
fil = [int(c) for c in s]
i2 = [2**i for i in range(128)]
f3 = {}
for k in range(3**7):
a = []
kk = k
for _ in range(7):
a.append(k%3)
k //= 3
if 2 in a:
continue
a.reverse()
e = 0
for c in a:
e *= 2
e += c
f3[kk] = fil[e]
for k in range(3**7):
a = []
kk = k
for _ in range(7):
a.append(k%3)
k //= 3
if 2 not in a:
continue
a.reverse()
ki = a.index(2)
e = 0
y = 0
for i in range(7):
e *= 3
y *= 3
if i == ki:
y += 1
else:
e += a[i]
y += a[i]
fe = f3[e]
fy = f3[y]
if fe == fy:
f3[kk] = fe
else:
f3[kk] = 2
f = True
for k in range(2**13):
ba = [1 if i2[i] & k else 0 for i in range(13)] + [2]
ca = []
for i in range(7):
ti = tt[i]
e = 0
for c in ti:
e *= 3
e += ba[c]
ca.append(f3[e])
y = 0
for c in ca:
y *= 3
y += c
if ca[3] != f3[y]:
f = False
break
if f:
rr.append('yes')
else:
rr.append('no')
return '\n'.join(rr)
print(main())
``` | output | 1 | 63,110 | 7 | 126,221 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Problem Statement
Let's consider operations on monochrome images that consist of hexagonal pixels, each of which is colored in either black or white. Because of the shape of pixels, each of them has exactly six neighbors (e.g. pixels that share an edge with it.)
"Filtering" is an operation to determine the color of a pixel from the colors of itself and its six neighbors. Examples of filterings are shown below.
Example 1: Color a pixel in white when all of its neighboring pixels are white. Otherwise the color will not change.
<image>
Performing this operation on all the pixels simultaneously results in "noise canceling," which removes isolated black pixels.
Example 2: Color a pixel in white when its all neighboring pixels are black. Otherwise the color will not change.
<image>
Performing this operation on all the pixels simultaneously results in "edge detection," which leaves only the edges of filled areas.
Example 3: Color a pixel with the color of the pixel just below it, ignoring any other neighbors.
<image>
Performing this operation on all the pixels simultaneously results in "shifting up" the whole image by one pixel.
Applying some filter, such as "noise canceling" and "edge detection," twice to any image yields the exactly same result as if they were applied only once. We call such filters idempotent. The "shifting up" filter is not idempotent since every repeated application shifts the image up by one pixel.
Your task is to determine whether the given filter is idempotent or not.
Input
The input consists of multiple datasets. The number of dataset is less than $100$. Each dataset is a string representing a filter and has the following format (without spaces between digits).
> $c_0c_1\cdots{}c_{127}$
$c_i$ is either '0' (represents black) or '1' (represents white), which indicates the output of the filter for a pixel when the binary representation of the pixel and its neighboring six pixels is $i$. The mapping from the pixels to the bits is as following:
<image>
and the binary representation $i$ is defined as $i = \sum_{j=0}^6{\mathit{bit}_j \times 2^j}$, where $\mathit{bit}_j$ is $0$ or $1$ if the corresponding pixel is in black or white, respectively. Note that the filter is applied on the center pixel, denoted as bit 3.
The input ends with a line that contains only a single "#".
Output
For each dataset, print "yes" in a line if the given filter is idempotent, or "no" otherwise (quotes are for clarity).
Sample Input
00000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000111111111
10000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111
01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
Output for the Sample Input
yes
yes
no
Example
Input
00000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000111111111
10000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111
01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
#
Output
yes
yes
no
Submitted Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
# 18
# 17 16
# 15 06 14
# 05 04
# 13 03 12
# 02 01
# 11 00 10
# 09 08
# 07
def main():
rr = []
tt = [
[3, 0, 2, 5, 6, 4, 1],
[0, 7, 9, 2, 3, 1, 8],
[2, 9, 11, 13, 5, 3, 0],
[5, 2, 13 ,15, 17, 6, 3],
[6, 3, 5, 17, 18, 16, 4],
[4, 1, 3, 6, 16, 14, 12],
[1, 8, 0, 3, 4, 12, 10]
]
while True:
s = S()
if s == '#':
break
fil = int(s[::-1], 2)
ii = [2**i for i in range(128)]
m = {}
def u(a):
key = tuple(a)
if key in m:
return m[key]
t = 0
for i in range(7):
if a[i]:
t |= ii[tt[0][i]]
if fil & ii[t]:
m[key] = 1
return 1
m[key] = 0
return 0
f = True
for k in range(ii[19]):
# if k % ii[17] == 0:
# print(k)
a = [1 if k & ii[i] else 0 for i in range(19)]
b = [u(list(map(lambda x: a[x], ta))) for ta in tt]
if u(b) != b[0]:
# print('false', a, b, u(b))
f = False
break
if f:
rr.append('yes')
else:
rr.append('no')
# print(rr)
return '\n'.join(rr)
print(main())
``` | instruction | 0 | 63,111 | 7 | 126,222 |
No | output | 1 | 63,111 | 7 | 126,223 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A state with $n$ flags of ON or OFF can be represented by a sequence of bits where $0, 1, ..., n-1$ -th flag corresponds to 1 (ON) or 0 (OFF). The state can be managed by the corresponding decimal integer, because the sequence of bits is a binary representation where each bit is 0 or 1.
Given a sequence of bits with 64 flags which represent a state, perform the following operations. Note that each flag of the bits is initialized by OFF.
* test(i): Print 1 if $i$-th flag is ON, otherwise 0
* set(i): Set $i$-th flag to ON
* clear(i): Set $i$-th flag to OFF
* flip(i): Inverse $i$-th flag
* all: Print 1 if all flags are ON, otherwise 0
* any: Print 1 if at least one flag is ON, otherwise 0
* none: Print 1 if all flags are OFF, otherwise 0
* count: Print the number of ON flags
* val: Print the decimal value of the state
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq i < 64$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given in the following format:
0 $i$
or
1 $i$
or
2 $i$
or
3 $i$
or
4
or
5
or
6
or
7
or
8
The first digit 0, 1,...,8 represents the operation test(i), set(i), clear(i), flip(i), all, any, none, count or val respectively.
Output
Print the result in a line for each test, all, any, none, count and val operation.
Example
Input
14
1 0
1 1
1 2
2 1
0 0
0 1
0 2
0 3
3 3
4
5
6
7
8
Output
1
0
1
0
0
1
0
3
13
Submitted Solution:
```
import sys
class BitFlag:
ALL_ON = 0xffffffffffffffff
ALL_OFF = 0x0000000000000000
def __init__(self):
self.FLAGS = self.ALL_OFF
self.Masks = [1 << i for i in range(64)]
def _test(self, i):
return self.FLAGS & self.Masks[i] != 0b00
def _set(self, i):
self.FLAGS |= self.Masks[i]
def _clear(self, i):
self.FLAGS &= ~self.Masks[i]
def _flip(self, i):
self.FLAGS ^= self.Masks[i]
def _all(self):
return self.FLAGS & self.ALL_ON == self.ALL_ON
def _any(self):
return self.FLAGS & self.ALL_ON != self.ALL_OFF
def _none(self):
return self.FLAGS & self.ALL_ON == self.ALL_OFF
def _count(self):
return bin(self.FLAGS).count('1')
def _val(self):
return self.FLAGS
bf = BitFlag()
commands = {'0': bf._test,
'1': bf._set,
'2': bf._clear,
'3': bf._flip,
'4': bf._all,
'5': bf._any,
'6': bf._none,
'7': bf._count,
'8': bf._val
}
qn = int(input())
lines = sys.stdin.readlines()
ans = [None] * qn
for i in range(qn):
q, *arg = lines[i].split()
if len(arg):
ans[i] = commands[q](int(arg[0]))
else:
ans[i] = commands[q]()
[print(int(x)) for x in ans if x is not None]
``` | instruction | 0 | 63,151 | 7 | 126,302 |
Yes | output | 1 | 63,151 | 7 | 126,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play the following game. Initially they have a checkered piece of paper with a painted n × m rectangle (only the border, no filling). Anna and Maria move in turns and Anna starts. During each move one should paint inside the last-painted rectangle a new lesser rectangle (along the grid lines). The new rectangle should have no common points with the previous one. Note that when we paint a rectangle, we always paint only the border, the rectangles aren't filled.
Nobody wins the game — Anna and Maria simply play until they have done k moves in total. Count the number of different ways to play this game.
Input
The first and only line contains three integers: n, m, k (1 ≤ n, m, k ≤ 1000).
Output
Print the single number — the number of the ways to play the game. As this number can be very big, print the value modulo 1000000007 (109 + 7).
Examples
Input
3 3 1
Output
1
Input
4 4 1
Output
9
Input
6 7 2
Output
75
Note
Two ways to play the game are considered different if the final pictures are different. In other words, if one way contains a rectangle that is not contained in the other way.
In the first sample Anna, who performs her first and only move, has only one possible action plan — insert a 1 × 1 square inside the given 3 × 3 square.
In the second sample Anna has as much as 9 variants: 4 ways to paint a 1 × 1 square, 2 ways to insert a 1 × 2 rectangle vertically, 2 more ways to insert it horizontally and one more way is to insert a 2 × 2 square. | instruction | 0 | 63,257 | 7 | 126,514 |
Tags: combinatorics, dp
Correct Solution:
```
MOD = 10 ** 9 + 7
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
def combination(n,k,MOD):
if n < k or k < 0:
return 0
ans = 1
for i in range(n - k + 1, n + 1):
ans *= i
ans %= MOD
for i in range(1,k+1):
ans *= modinv(i,MOD)
ans %= MOD
return ans
n,m,k = map(int,input().split())
print(combination(n-1,n-2 * k-1,MOD) * combination(m-1,m - 2 * k - 1,MOD) % MOD)
``` | output | 1 | 63,257 | 7 | 126,515 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play the following game. Initially they have a checkered piece of paper with a painted n × m rectangle (only the border, no filling). Anna and Maria move in turns and Anna starts. During each move one should paint inside the last-painted rectangle a new lesser rectangle (along the grid lines). The new rectangle should have no common points with the previous one. Note that when we paint a rectangle, we always paint only the border, the rectangles aren't filled.
Nobody wins the game — Anna and Maria simply play until they have done k moves in total. Count the number of different ways to play this game.
Input
The first and only line contains three integers: n, m, k (1 ≤ n, m, k ≤ 1000).
Output
Print the single number — the number of the ways to play the game. As this number can be very big, print the value modulo 1000000007 (109 + 7).
Examples
Input
3 3 1
Output
1
Input
4 4 1
Output
9
Input
6 7 2
Output
75
Note
Two ways to play the game are considered different if the final pictures are different. In other words, if one way contains a rectangle that is not contained in the other way.
In the first sample Anna, who performs her first and only move, has only one possible action plan — insert a 1 × 1 square inside the given 3 × 3 square.
In the second sample Anna has as much as 9 variants: 4 ways to paint a 1 × 1 square, 2 ways to insert a 1 × 2 rectangle vertically, 2 more ways to insert it horizontally and one more way is to insert a 2 × 2 square. | instruction | 0 | 63,258 | 7 | 126,516 |
Tags: combinatorics, dp
Correct Solution:
```
import time
def Binc(bcs, n, k):
if (k > n): return 0
if k > n // 2: k = n - k
if k == 0: return 1
if k == 1: return n
while len(bcs) < n - 3:
for i in range(len(bcs), n - 3):
r = []
for j in range(2, i // 2 + 3): r.append(Binc(bcs, i + 3, j - 1) + Binc(bcs, i + 3, j))
bcs.append(r)
r = bcs[n - 4]
if len(r) < k - 1:
for i in range(len(r), k - 1): r.append(Binc(bcs, n - 1, k - 1) + Binc(bcs, n - 1, k))
return bcs[n - 4][k - 2]
bcs = []
bcs_ = []
mod_ = 1000000007
n, m, k = map(int, input().split())
nk = Binc(bcs, n - 1, 2*k) % mod_
mk = Binc(bcs_, m - 1, 2*k) % mod_
print((nk * mk) % mod_)
``` | output | 1 | 63,258 | 7 | 126,517 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play the following game. Initially they have a checkered piece of paper with a painted n × m rectangle (only the border, no filling). Anna and Maria move in turns and Anna starts. During each move one should paint inside the last-painted rectangle a new lesser rectangle (along the grid lines). The new rectangle should have no common points with the previous one. Note that when we paint a rectangle, we always paint only the border, the rectangles aren't filled.
Nobody wins the game — Anna and Maria simply play until they have done k moves in total. Count the number of different ways to play this game.
Input
The first and only line contains three integers: n, m, k (1 ≤ n, m, k ≤ 1000).
Output
Print the single number — the number of the ways to play the game. As this number can be very big, print the value modulo 1000000007 (109 + 7).
Examples
Input
3 3 1
Output
1
Input
4 4 1
Output
9
Input
6 7 2
Output
75
Note
Two ways to play the game are considered different if the final pictures are different. In other words, if one way contains a rectangle that is not contained in the other way.
In the first sample Anna, who performs her first and only move, has only one possible action plan — insert a 1 × 1 square inside the given 3 × 3 square.
In the second sample Anna has as much as 9 variants: 4 ways to paint a 1 × 1 square, 2 ways to insert a 1 × 2 rectangle vertically, 2 more ways to insert it horizontally and one more way is to insert a 2 × 2 square. | instruction | 0 | 63,259 | 7 | 126,518 |
Tags: combinatorics, dp
Correct Solution:
```
from sys import stdin, stdout, stderr, exit
from math import log, factorial
import re
# inputf = open('input.txt', 'r')
# outputf = open('output.txt', 'w')
def readil():
return list(map(int, stdin.readline().strip().split()))
def C(n, k):
if(n-k >= 0):
return factorial(n) // factorial(k) // factorial(n-k)
else:
return 0
def main():
n, m, k = readil()
stdout.write(str((C(n - 1, 2 * k) * C(m - 1, 2 * k)) % 1000000007))
if __name__ == '__main__':
main()
# inputf.close()
# outputf.close()
``` | output | 1 | 63,259 | 7 | 126,519 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play the following game. Initially they have a checkered piece of paper with a painted n × m rectangle (only the border, no filling). Anna and Maria move in turns and Anna starts. During each move one should paint inside the last-painted rectangle a new lesser rectangle (along the grid lines). The new rectangle should have no common points with the previous one. Note that when we paint a rectangle, we always paint only the border, the rectangles aren't filled.
Nobody wins the game — Anna and Maria simply play until they have done k moves in total. Count the number of different ways to play this game.
Input
The first and only line contains three integers: n, m, k (1 ≤ n, m, k ≤ 1000).
Output
Print the single number — the number of the ways to play the game. As this number can be very big, print the value modulo 1000000007 (109 + 7).
Examples
Input
3 3 1
Output
1
Input
4 4 1
Output
9
Input
6 7 2
Output
75
Note
Two ways to play the game are considered different if the final pictures are different. In other words, if one way contains a rectangle that is not contained in the other way.
In the first sample Anna, who performs her first and only move, has only one possible action plan — insert a 1 × 1 square inside the given 3 × 3 square.
In the second sample Anna has as much as 9 variants: 4 ways to paint a 1 × 1 square, 2 ways to insert a 1 × 2 rectangle vertically, 2 more ways to insert it horizontally and one more way is to insert a 2 × 2 square. | instruction | 0 | 63,260 | 7 | 126,520 |
Tags: combinatorics, dp
Correct Solution:
```
def inv(a):
return pow(a, mod - 2, mod)
n, m, k = map(int, input().split())
if (2 * k >= min(n, m)):
print(0)
exit()
fact = [1]
mod = 1000000007
for i in range(1, 2001):
fact.append(fact[-1] * i % mod)
print(fact[n - 1] * inv(fact[2 * k]) * inv(fact[n - 2 * k - 1]) * fact[m - 1] * inv(fact[2 * k]) * inv(fact[m - 2 * k - 1]) % mod)
``` | output | 1 | 63,260 | 7 | 126,521 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play the following game. Initially they have a checkered piece of paper with a painted n × m rectangle (only the border, no filling). Anna and Maria move in turns and Anna starts. During each move one should paint inside the last-painted rectangle a new lesser rectangle (along the grid lines). The new rectangle should have no common points with the previous one. Note that when we paint a rectangle, we always paint only the border, the rectangles aren't filled.
Nobody wins the game — Anna and Maria simply play until they have done k moves in total. Count the number of different ways to play this game.
Input
The first and only line contains three integers: n, m, k (1 ≤ n, m, k ≤ 1000).
Output
Print the single number — the number of the ways to play the game. As this number can be very big, print the value modulo 1000000007 (109 + 7).
Examples
Input
3 3 1
Output
1
Input
4 4 1
Output
9
Input
6 7 2
Output
75
Note
Two ways to play the game are considered different if the final pictures are different. In other words, if one way contains a rectangle that is not contained in the other way.
In the first sample Anna, who performs her first and only move, has only one possible action plan — insert a 1 × 1 square inside the given 3 × 3 square.
In the second sample Anna has as much as 9 variants: 4 ways to paint a 1 × 1 square, 2 ways to insert a 1 × 2 rectangle vertically, 2 more ways to insert it horizontally and one more way is to insert a 2 × 2 square. | instruction | 0 | 63,261 | 7 | 126,522 |
Tags: combinatorics, dp
Correct Solution:
```
from math import *
n, m, k = map(int, input().split())
c = lambda n, k: 0 if k > n else factorial(n) // (factorial(k) * factorial(n - k))
print(c(n - 1, 2 * k) * c(m - 1, 2 * k) % 1000000007)
``` | output | 1 | 63,261 | 7 | 126,523 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play the following game. Initially they have a checkered piece of paper with a painted n × m rectangle (only the border, no filling). Anna and Maria move in turns and Anna starts. During each move one should paint inside the last-painted rectangle a new lesser rectangle (along the grid lines). The new rectangle should have no common points with the previous one. Note that when we paint a rectangle, we always paint only the border, the rectangles aren't filled.
Nobody wins the game — Anna and Maria simply play until they have done k moves in total. Count the number of different ways to play this game.
Input
The first and only line contains three integers: n, m, k (1 ≤ n, m, k ≤ 1000).
Output
Print the single number — the number of the ways to play the game. As this number can be very big, print the value modulo 1000000007 (109 + 7).
Examples
Input
3 3 1
Output
1
Input
4 4 1
Output
9
Input
6 7 2
Output
75
Note
Two ways to play the game are considered different if the final pictures are different. In other words, if one way contains a rectangle that is not contained in the other way.
In the first sample Anna, who performs her first and only move, has only one possible action plan — insert a 1 × 1 square inside the given 3 × 3 square.
In the second sample Anna has as much as 9 variants: 4 ways to paint a 1 × 1 square, 2 ways to insert a 1 × 2 rectangle vertically, 2 more ways to insert it horizontally and one more way is to insert a 2 × 2 square. | instruction | 0 | 63,262 | 7 | 126,524 |
Tags: combinatorics, dp
Correct Solution:
```
from math import factorial
MOD = 1000000007
def c(n, k):
if k > n:
return 0
return factorial(n) //(factorial(k) * factorial(n - k))
n, m, k = map(int, input().split())
print ((c(n - 1, 2 * k) * c(m - 1, 2 * k)) % MOD)
``` | output | 1 | 63,262 | 7 | 126,525 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play the following game. Initially they have a checkered piece of paper with a painted n × m rectangle (only the border, no filling). Anna and Maria move in turns and Anna starts. During each move one should paint inside the last-painted rectangle a new lesser rectangle (along the grid lines). The new rectangle should have no common points with the previous one. Note that when we paint a rectangle, we always paint only the border, the rectangles aren't filled.
Nobody wins the game — Anna and Maria simply play until they have done k moves in total. Count the number of different ways to play this game.
Input
The first and only line contains three integers: n, m, k (1 ≤ n, m, k ≤ 1000).
Output
Print the single number — the number of the ways to play the game. As this number can be very big, print the value modulo 1000000007 (109 + 7).
Examples
Input
3 3 1
Output
1
Input
4 4 1
Output
9
Input
6 7 2
Output
75
Note
Two ways to play the game are considered different if the final pictures are different. In other words, if one way contains a rectangle that is not contained in the other way.
In the first sample Anna, who performs her first and only move, has only one possible action plan — insert a 1 × 1 square inside the given 3 × 3 square.
In the second sample Anna has as much as 9 variants: 4 ways to paint a 1 × 1 square, 2 ways to insert a 1 × 2 rectangle vertically, 2 more ways to insert it horizontally and one more way is to insert a 2 × 2 square. | instruction | 0 | 63,263 | 7 | 126,526 |
Tags: combinatorics, dp
Correct Solution:
```
from math import factorial
n, m, k = map(int, input().split())
def combinacion(n, k):
if k > n:
return 0
return factorial(n)//(factorial(k)*factorial(n-k))
result = (combinacion(n-1, 2 * k) * combinacion(m-1, 2 * k)) % 1000000007
print(result)
``` | output | 1 | 63,263 | 7 | 126,527 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a card deck of n cards, numbered from top to bottom, i. e. the top card has index 1 and bottom card — index n. Each card has its color: the i-th card has color a_i.
You should process q queries. The j-th query is described by integer t_j. For each query you should:
* find the highest card in the deck with color t_j, i. e. the card with minimum index;
* print the position of the card you found;
* take the card and place it on top of the deck.
Input
The first line contains two integers n and q (2 ≤ n ≤ 3 ⋅ 10^5; 1 ≤ q ≤ 3 ⋅ 10^5) — the number of cards in the deck and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 50) — the colors of cards.
The third line contains q integers t_1, t_2, ..., t_q (1 ≤ t_j ≤ 50) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.
Output
Print q integers — the answers for each query.
Example
Input
7 5
2 1 1 4 3 3 1
3 2 1 1 4
Output
5 2 3 1 5
Note
Description of the sample:
1. the deck is [2, 1, 1, 4, \underline{3}, 3, 1] and the first card with color t_1 = 3 has position 5;
2. the deck is [3, \underline{2}, 1, 1, 4, 3, 1] and the first card with color t_2 = 2 has position 2;
3. the deck is [2, 3, \underline{1}, 1, 4, 3, 1] and the first card with color t_3 = 1 has position 3;
4. the deck is [\underline{1}, 2, 3, 1, 4, 3, 1] and the first card with color t_4 = 1 has position 1;
5. the deck is [1, 2, 3, 1, \underline{4}, 3, 1] and the first card with color t_5 = 4 has position 5. | instruction | 0 | 63,370 | 7 | 126,740 |
Tags: brute force, data structures, implementation, trees
Correct Solution:
```
import sys
from collections import deque
def read_ints():
return [int(i) for i in sys.stdin.readline().strip().split()]
def read_int():
return int(sys.stdin.readline().strip())
def rfind(seq, el): # distance from end, zero is last element
for i, e in enumerate(seq[::-1]):
if e == el:
return i
tops = [-1] * 51 # position of the top card of each colour (0-indexed)
n, q = read_ints()
ns = read_ints()
qs = read_ints()
for i, col in enumerate(ns):
if tops[col] == -1:
tops[col] = i
#print(tops)
small_deck = []
found = set()
results = []
for col in qs:
if col in found:
# check small deck
pos = rfind(small_deck, col)
idx = len(small_deck) - pos - 1
small_deck = small_deck[:idx] + small_deck[idx+1:] + [col]
results.append(pos + 1)
else:
# top card in the big deck
pos = tops[col]
found.add(col)
small_deck.append(col)
for i in range(51):
if tops[i] < pos:
tops[i] += 1
results.append(pos + 1)
#print("small deck: ", small_deck)
#print("found: ", found)
print(" ".join(str(i) for i in results))
``` | output | 1 | 63,370 | 7 | 126,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a card deck of n cards, numbered from top to bottom, i. e. the top card has index 1 and bottom card — index n. Each card has its color: the i-th card has color a_i.
You should process q queries. The j-th query is described by integer t_j. For each query you should:
* find the highest card in the deck with color t_j, i. e. the card with minimum index;
* print the position of the card you found;
* take the card and place it on top of the deck.
Input
The first line contains two integers n and q (2 ≤ n ≤ 3 ⋅ 10^5; 1 ≤ q ≤ 3 ⋅ 10^5) — the number of cards in the deck and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 50) — the colors of cards.
The third line contains q integers t_1, t_2, ..., t_q (1 ≤ t_j ≤ 50) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.
Output
Print q integers — the answers for each query.
Example
Input
7 5
2 1 1 4 3 3 1
3 2 1 1 4
Output
5 2 3 1 5
Note
Description of the sample:
1. the deck is [2, 1, 1, 4, \underline{3}, 3, 1] and the first card with color t_1 = 3 has position 5;
2. the deck is [3, \underline{2}, 1, 1, 4, 3, 1] and the first card with color t_2 = 2 has position 2;
3. the deck is [2, 3, \underline{1}, 1, 4, 3, 1] and the first card with color t_3 = 1 has position 3;
4. the deck is [\underline{1}, 2, 3, 1, 4, 3, 1] and the first card with color t_4 = 1 has position 1;
5. the deck is [1, 2, 3, 1, \underline{4}, 3, 1] and the first card with color t_5 = 4 has position 5. | instruction | 0 | 63,371 | 7 | 126,742 |
Tags: brute force, data structures, implementation, trees
Correct Solution:
```
import os,sys;from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno();self.buffer = BytesIO();self.writable = "x" in file.mode or "r" not in file.mode;self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:break
ptr = self.buffer.tell();self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE));self.newlines = b.count(b"\n") + (not b);ptr = self.buffer.tell();self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:os.write(self._fd, self.buffer.getvalue());self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file);self.flush = self.buffer.flush;self.writable = self.buffer.writable;self.write = lambda s: self.buffer.write(s.encode("ascii"));self.read = lambda: self.buffer.read().decode("ascii");self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
try:sys.stdin,sys.stdout=open('in.txt','r'),open('out.txt','w')
except:pass
ii1=lambda:int(sys.stdin.readline().strip()) # for interger
is1=lambda:sys.stdin.readline().strip() # for str
iia=lambda:list(map(int,sys.stdin.readline().strip().split())) # for List[int]
isa=lambda:sys.stdin.readline().strip().split() # for List[str]
# mod=int(1e9 + 7);from collections import *;from math import *
# abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# sys.setrecursionlimit(500000)
###################### Start Here ######################
# from functools import lru_cache
# from collections import defaultdict as dd
from collections import deque,defaultdict
from collections import deque as dq
n,q = iia()
arr = iia()
query = iia()
dq = []
first = defaultdict(int)
for idx,v in enumerate(arr):
if v not in first:
first[v]=idx
dq_map = defaultdict(int)
ans = []
extra = 0
for q in query:
if q not in dq_map:
dq_map[q]=1
idx = first[q]
dq.insert(0,q)
for key,val in first.items():
if idx>val:
first[key]+=1
ans.append(idx+1)
else:
for j in range(len(dq)):
if dq[j] == q:
val = dq.pop(j)
ans.append(j+1)
dq.insert(0,q)
print(*ans)
``` | output | 1 | 63,371 | 7 | 126,743 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a card deck of n cards, numbered from top to bottom, i. e. the top card has index 1 and bottom card — index n. Each card has its color: the i-th card has color a_i.
You should process q queries. The j-th query is described by integer t_j. For each query you should:
* find the highest card in the deck with color t_j, i. e. the card with minimum index;
* print the position of the card you found;
* take the card and place it on top of the deck.
Input
The first line contains two integers n and q (2 ≤ n ≤ 3 ⋅ 10^5; 1 ≤ q ≤ 3 ⋅ 10^5) — the number of cards in the deck and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 50) — the colors of cards.
The third line contains q integers t_1, t_2, ..., t_q (1 ≤ t_j ≤ 50) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.
Output
Print q integers — the answers for each query.
Example
Input
7 5
2 1 1 4 3 3 1
3 2 1 1 4
Output
5 2 3 1 5
Note
Description of the sample:
1. the deck is [2, 1, 1, 4, \underline{3}, 3, 1] and the first card with color t_1 = 3 has position 5;
2. the deck is [3, \underline{2}, 1, 1, 4, 3, 1] and the first card with color t_2 = 2 has position 2;
3. the deck is [2, 3, \underline{1}, 1, 4, 3, 1] and the first card with color t_3 = 1 has position 3;
4. the deck is [\underline{1}, 2, 3, 1, 4, 3, 1] and the first card with color t_4 = 1 has position 1;
5. the deck is [1, 2, 3, 1, \underline{4}, 3, 1] and the first card with color t_5 = 4 has position 5. | instruction | 0 | 63,372 | 7 | 126,744 |
Tags: brute force, data structures, implementation, trees
Correct Solution:
```
n, q = [int(token) for token in input().split()]
a = [int(token) for token in input().split()]
t = [int(token) for token in input().split()]
first_index_of_color = [-1 for i in range(50)]
for i in range(n):
if first_index_of_color[a[i] - 1] == -1:
first_index_of_color[a[i] - 1] = i + 1
result = []
for current_t in t:
current_t -= 1
answer_index = first_index_of_color[current_t]
result.append(answer_index)
for i in range(50):
if first_index_of_color[i] < answer_index and first_index_of_color[i] != -1:
first_index_of_color[i] += 1
elif first_index_of_color[i] == answer_index:
first_index_of_color[i] = 1
print(*result)
``` | output | 1 | 63,372 | 7 | 126,745 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a card deck of n cards, numbered from top to bottom, i. e. the top card has index 1 and bottom card — index n. Each card has its color: the i-th card has color a_i.
You should process q queries. The j-th query is described by integer t_j. For each query you should:
* find the highest card in the deck with color t_j, i. e. the card with minimum index;
* print the position of the card you found;
* take the card and place it on top of the deck.
Input
The first line contains two integers n and q (2 ≤ n ≤ 3 ⋅ 10^5; 1 ≤ q ≤ 3 ⋅ 10^5) — the number of cards in the deck and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 50) — the colors of cards.
The third line contains q integers t_1, t_2, ..., t_q (1 ≤ t_j ≤ 50) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.
Output
Print q integers — the answers for each query.
Example
Input
7 5
2 1 1 4 3 3 1
3 2 1 1 4
Output
5 2 3 1 5
Note
Description of the sample:
1. the deck is [2, 1, 1, 4, \underline{3}, 3, 1] and the first card with color t_1 = 3 has position 5;
2. the deck is [3, \underline{2}, 1, 1, 4, 3, 1] and the first card with color t_2 = 2 has position 2;
3. the deck is [2, 3, \underline{1}, 1, 4, 3, 1] and the first card with color t_3 = 1 has position 3;
4. the deck is [\underline{1}, 2, 3, 1, 4, 3, 1] and the first card with color t_4 = 1 has position 1;
5. the deck is [1, 2, 3, 1, \underline{4}, 3, 1] and the first card with color t_5 = 4 has position 5. | instruction | 0 | 63,373 | 7 | 126,746 |
Tags: brute force, data structures, implementation, trees
Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 16 09:12:19 2021
@author: MridulSachdeva
"""
n, q = map(int, input().split())
s = list(map(int, input().split()))
t = list(map(int, input().split()))
ans = []
for i in range(q):
index = s.index(t[i])
ans.append(index + 1)
s[:index+1] = [t[i]] + s[:index]
print(*ans)
``` | output | 1 | 63,373 | 7 | 126,747 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a card deck of n cards, numbered from top to bottom, i. e. the top card has index 1 and bottom card — index n. Each card has its color: the i-th card has color a_i.
You should process q queries. The j-th query is described by integer t_j. For each query you should:
* find the highest card in the deck with color t_j, i. e. the card with minimum index;
* print the position of the card you found;
* take the card and place it on top of the deck.
Input
The first line contains two integers n and q (2 ≤ n ≤ 3 ⋅ 10^5; 1 ≤ q ≤ 3 ⋅ 10^5) — the number of cards in the deck and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 50) — the colors of cards.
The third line contains q integers t_1, t_2, ..., t_q (1 ≤ t_j ≤ 50) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.
Output
Print q integers — the answers for each query.
Example
Input
7 5
2 1 1 4 3 3 1
3 2 1 1 4
Output
5 2 3 1 5
Note
Description of the sample:
1. the deck is [2, 1, 1, 4, \underline{3}, 3, 1] and the first card with color t_1 = 3 has position 5;
2. the deck is [3, \underline{2}, 1, 1, 4, 3, 1] and the first card with color t_2 = 2 has position 2;
3. the deck is [2, 3, \underline{1}, 1, 4, 3, 1] and the first card with color t_3 = 1 has position 3;
4. the deck is [\underline{1}, 2, 3, 1, 4, 3, 1] and the first card with color t_4 = 1 has position 1;
5. the deck is [1, 2, 3, 1, \underline{4}, 3, 1] and the first card with color t_5 = 4 has position 5. | instruction | 0 | 63,374 | 7 | 126,748 |
Tags: brute force, data structures, implementation, trees
Correct Solution:
```
from collections import deque
n, q = list(map(int, input().split(" ")))
colors = deque(list(map(int, input().split(" "))))
Qs = list(map(int, input().split(" ")))
for i in Qs:
ind = colors.index(i)
print(ind + 1, end=' ')
colors.remove(i)
colors.appendleft(i)
``` | output | 1 | 63,374 | 7 | 126,749 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a card deck of n cards, numbered from top to bottom, i. e. the top card has index 1 and bottom card — index n. Each card has its color: the i-th card has color a_i.
You should process q queries. The j-th query is described by integer t_j. For each query you should:
* find the highest card in the deck with color t_j, i. e. the card with minimum index;
* print the position of the card you found;
* take the card and place it on top of the deck.
Input
The first line contains two integers n and q (2 ≤ n ≤ 3 ⋅ 10^5; 1 ≤ q ≤ 3 ⋅ 10^5) — the number of cards in the deck and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 50) — the colors of cards.
The third line contains q integers t_1, t_2, ..., t_q (1 ≤ t_j ≤ 50) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.
Output
Print q integers — the answers for each query.
Example
Input
7 5
2 1 1 4 3 3 1
3 2 1 1 4
Output
5 2 3 1 5
Note
Description of the sample:
1. the deck is [2, 1, 1, 4, \underline{3}, 3, 1] and the first card with color t_1 = 3 has position 5;
2. the deck is [3, \underline{2}, 1, 1, 4, 3, 1] and the first card with color t_2 = 2 has position 2;
3. the deck is [2, 3, \underline{1}, 1, 4, 3, 1] and the first card with color t_3 = 1 has position 3;
4. the deck is [\underline{1}, 2, 3, 1, 4, 3, 1] and the first card with color t_4 = 1 has position 1;
5. the deck is [1, 2, 3, 1, \underline{4}, 3, 1] and the first card with color t_5 = 4 has position 5. | instruction | 0 | 63,375 | 7 | 126,750 |
Tags: brute force, data structures, implementation, trees
Correct Solution:
```
n,q = map(int, input().split())
li = list(map(int, input().split()))
d = {}
cnt = 0
front = 0
qr = list(map(int, input().split()))
for i in range(n):
if li[i] not in d.keys():
d[li[i]] = i+1
# print(d)
for i in qr:
tmp = d[i]
print(d[i],end=' ')
d[i] = 0
for j in d:
if d[j] < tmp:
d[j] += 1
``` | output | 1 | 63,375 | 7 | 126,751 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a card deck of n cards, numbered from top to bottom, i. e. the top card has index 1 and bottom card — index n. Each card has its color: the i-th card has color a_i.
You should process q queries. The j-th query is described by integer t_j. For each query you should:
* find the highest card in the deck with color t_j, i. e. the card with minimum index;
* print the position of the card you found;
* take the card and place it on top of the deck.
Input
The first line contains two integers n and q (2 ≤ n ≤ 3 ⋅ 10^5; 1 ≤ q ≤ 3 ⋅ 10^5) — the number of cards in the deck and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 50) — the colors of cards.
The third line contains q integers t_1, t_2, ..., t_q (1 ≤ t_j ≤ 50) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.
Output
Print q integers — the answers for each query.
Example
Input
7 5
2 1 1 4 3 3 1
3 2 1 1 4
Output
5 2 3 1 5
Note
Description of the sample:
1. the deck is [2, 1, 1, 4, \underline{3}, 3, 1] and the first card with color t_1 = 3 has position 5;
2. the deck is [3, \underline{2}, 1, 1, 4, 3, 1] and the first card with color t_2 = 2 has position 2;
3. the deck is [2, 3, \underline{1}, 1, 4, 3, 1] and the first card with color t_3 = 1 has position 3;
4. the deck is [\underline{1}, 2, 3, 1, 4, 3, 1] and the first card with color t_4 = 1 has position 1;
5. the deck is [1, 2, 3, 1, \underline{4}, 3, 1] and the first card with color t_5 = 4 has position 5. | instruction | 0 | 63,376 | 7 | 126,752 |
Tags: brute force, data structures, implementation, trees
Correct Solution:
```
def main():
n,q=map(int,input().split())
t=list(map(int,input().split()))
queries=list(map(int,input().split()))
for query in queries:
ind=t.index(query)
num=t[ind]
t[1:ind+1]=t[:ind]
t[0]=num
print(ind+1,end=" ")
main()
``` | output | 1 | 63,376 | 7 | 126,753 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a card deck of n cards, numbered from top to bottom, i. e. the top card has index 1 and bottom card — index n. Each card has its color: the i-th card has color a_i.
You should process q queries. The j-th query is described by integer t_j. For each query you should:
* find the highest card in the deck with color t_j, i. e. the card with minimum index;
* print the position of the card you found;
* take the card and place it on top of the deck.
Input
The first line contains two integers n and q (2 ≤ n ≤ 3 ⋅ 10^5; 1 ≤ q ≤ 3 ⋅ 10^5) — the number of cards in the deck and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 50) — the colors of cards.
The third line contains q integers t_1, t_2, ..., t_q (1 ≤ t_j ≤ 50) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.
Output
Print q integers — the answers for each query.
Example
Input
7 5
2 1 1 4 3 3 1
3 2 1 1 4
Output
5 2 3 1 5
Note
Description of the sample:
1. the deck is [2, 1, 1, 4, \underline{3}, 3, 1] and the first card with color t_1 = 3 has position 5;
2. the deck is [3, \underline{2}, 1, 1, 4, 3, 1] and the first card with color t_2 = 2 has position 2;
3. the deck is [2, 3, \underline{1}, 1, 4, 3, 1] and the first card with color t_3 = 1 has position 3;
4. the deck is [\underline{1}, 2, 3, 1, 4, 3, 1] and the first card with color t_4 = 1 has position 1;
5. the deck is [1, 2, 3, 1, \underline{4}, 3, 1] and the first card with color t_5 = 4 has position 5. | instruction | 0 | 63,377 | 7 | 126,754 |
Tags: brute force, data structures, implementation, trees
Correct Solution:
```
n, q = map(int, input().split())
ans = [0]*51
nums = list(map(int, input().split()))
for i in range(n):
a = nums[i]
if not ans[a]:
ans[a] = i+1
query = list(map(int, input().split()))
for i in query:
print(ans[i], end=' ')
for j in range(51):
if ans[j] < ans[i]:
ans[j] += 1
ans[i] = 1
``` | output | 1 | 63,377 | 7 | 126,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Like all children, Alesha loves New Year celebration. During the celebration he and his whole family dress up the fir-tree. Like all children, Alesha likes to play with garlands — chains consisting of a lightbulbs.
Alesha uses a grid field sized n × m for playing. The rows of the field are numbered from 1 to n from the top to the bottom and columns are numbered from 1 to m from the left to the right.
Alesha has k garlands which he places at the field. He does so in the way such that each lightbulb of each garland lies in the center of some cell in the field, and each cell contains at most one lightbulb. Of course lightbulbs, which are neighbours in some garland, appears in cells neighbouring by a side.
<image>
The example of garland placing.
Each garland is turned off or turned on at any moment. If some garland is turned on then each of its lightbulbs is turned on, the same applies for garland turned off. Each lightbulb in the whole garland set is unique, and thus, being turned on, brings Alesha some pleasure, described by an integer value. Turned off lightbulbs don't bring Alesha any pleasure.
Alesha can turn garlands on and off and wants to know the sum of pleasure value which the lightbulbs, placed in the centers of the cells in some rectangular part of the field, bring him. Initially all the garlands are turned on.
Alesha is still very little and can't add big numbers. He extremely asks you to help him.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m, k ≤ 2000) — the number of field rows, the number of field columns and the number of garlands placed at the field respectively.
Next lines contains garlands set description in the following format:
The first line of a single garland description contains a single integer len (1 ≤ len ≤ 2000) — the number of lightbulbs in the garland.
Each of the next len lines contains three integers i, j and w (1 ≤ i ≤ n, 1 ≤ j ≤ m, 1 ≤ w ≤ 109) — the coordinates of the cell containing a lightbullb and pleasure value Alesha gets from it if it is turned on. The lightbulbs are given in the order they are forming a chain in the garland. It is guaranteed that neighbouring lightbulbs are placed in the cells neighbouring by a side.
The next line contains single integer q (1 ≤ q ≤ 106) — the number of events in Alesha's game. The next q lines describes events in chronological order. The i-th of them describes the i-th event in the one of the following formats:
* SWITCH i — Alesha turns off i-th garland if it is turned on, or turns it on if it is turned off. It is guaranteed that 1 ≤ i ≤ k.
* ASK x1 y1 x2 y2 — Alesha wants to know the sum of pleasure values the lightbulbs, placed in a rectangular part of the field. Top-left cell of a part has coordinates (x1, y1) and right-bottom cell has coordinates (x2, y2). It is guaranteed that 1 ≤ x1 ≤ x2 ≤ n and 1 ≤ y1 ≤ y2 ≤ m. There is no more than 2000 events of this type in the input.
All the numbers in the input are integers.
Please note that the input is quite large, so be careful while using some input ways. In particular, it's not recommended to use cin in codes on C++ and class Scanner in codes on Java.
Output
For each ASK operation print the sum Alesha wants to know in a separate line. Print the answers in chronological order.
Examples
Input
4 4 3
5
1 1 2
1 2 3
2 2 1
2 1 4
3 1 7
4
1 3 1
2 3 3
2 4 3
1 4 1
7
4 1 1
4 2 9
3 2 8
3 3 3
4 3 4
4 4 1
3 4 1
2
ASK 2 2 3 3
ASK 1 1 4 4
Output
15
52
Input
4 4 1
8
4 1 1
3 1 2
2 1 1
1 1 7
1 2 5
2 2 4
2 3 1
1 3 1
3
ASK 1 1 3 2
SWITCH 1
ASK 1 1 3 2
Output
19
0
Note
<image>
This image illustrates the first sample case.
Submitted Solution:
```
import sys
import collections
n, m, q = map(int, str.split(sys.stdin.readline()))
shelve = collections.ChainMap(collections.defaultdict(int))
shelve["count"] = 0
for _ in range(q):
shelve = shelve.new_child()
op, *args = map(int, str.split(sys.stdin.readline()))
args = tuple(args)
if op == 1:
if not shelve[args]:
shelve[args] = 1
shelve["count"] += 1
elif op == 2:
if shelve[args]:
shelve[args] = 0
shelve["count"] -= 1
elif op == 3:
for j in range(1, m + 1):
index = args[0], j
if shelve[index]:
shelve["count"] -= 1
else:
shelve["count"] += 1
shelve[index] ^= 1
elif op == 4:
previous = collections.ChainMap(*shelve.maps[-args[0] - 1:])
for index, value in previous.items():
shelve[index] = value
sys.stdout.write(str.format("{}\n", shelve["count"]))
``` | instruction | 0 | 63,576 | 7 | 127,152 |
No | output | 1 | 63,576 | 7 | 127,153 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2 | instruction | 0 | 63,704 | 7 | 127,408 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
N = int(input())
a = [0,1] * N
a2 = [1,0] * N
pt1 = []
pt2 = []
cnt = 0
for j in range(N):
arr = input()
for k in range(N):
if j % 2 == 1 and int(arr[k]) == a2[k]:
cnt += 1
elif j % 2 == 0 and int(arr[k]) == a[k]:
cnt += 1
pt1.append(cnt)
pt2.append(N*N-cnt)
cnt = 0
s = input()
for j in range(N):
arr = input()
for k in range(N):
if j % 2 == 1 and int(arr[k]) == a2[k]:
cnt += 1
elif j % 2 == 0 and int(arr[k]) == a[k]:
cnt += 1
pt1.append(cnt)
pt2.append(N*N-cnt)
s = input()
cnt = 0
for j in range(N):
arr = input()
for k in range(N):
if j % 2 == 1 and int(arr[k]) == a2[k]:
cnt += 1
elif j % 2 == 0 and int(arr[k]) == a[k]:
cnt += 1
pt1.append(cnt)
pt2.append(N*N-cnt)
s = input()
cnt = 0
for j in range(N):
arr = input()
for k in range(N):
if j % 2 == 1 and int(arr[k]) == a2[k]:
cnt += 1
elif j % 2 == 0 and int(arr[k]) == a[k]:
cnt += 1
pt1.append(cnt)
pt2.append(N*N-cnt)
pt1.sort()
pt2.sort()
print(pt1[0]+pt1[1]+pt2[0]+pt2[1])
``` | output | 1 | 63,704 | 7 | 127,409 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2 | instruction | 0 | 63,705 | 7 | 127,410 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
import math
from decimal import Decimal
def na():
n = int(input())
b = [int(x) for x in input().split()]
return n,b
def nab():
n = int(input())
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
return n,b,c
def dv():
n, m = map(int, input().split())
return n,m
def dva():
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
return n,m,b
def eratosthenes(n):
sieve = list(range(n + 1))
for i in sieve:
if i > 1:
for j in range(i + i, len(sieve), i):
sieve[j] = 0
return sorted(set(sieve))
def nm():
n = int(input())
b = [int(x) for x in input().split()]
m = int(input())
c = [int(x) for x in input().split()]
return n,b,m,c
def dvs():
n = int(input())
m = int(input())
return n, m
n = int(input())
d = [0] * 4
for i in range(4):
for j in range(n):
s = input()
for k in range(n):
if (k + j) % 2 == 0:
if s[k] == '1':
d[i] += 1
else:
if s[k] == '0':
d[i] += 1
if i < 3:
input()
dop = n * n
d = sorted(d)
ans = dop * 2 + d[0] + d[1] - d[2] - d[3]
print(ans)
``` | output | 1 | 63,705 | 7 | 127,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2 | instruction | 0 | 63,706 | 7 | 127,412 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
n = int(input())
l = list()
for i in range(4):
l.append([list(input()) for x in range(n)])
if i < 3:
input()
lt = [0, 0, 0, 0]
min_f = (n * 2) ** 2
for i in range(1, 4):
ll = [1, 2, 3]
lt[1] = ll.pop(i - 1)
for x in range(2):
lb = list()
lt[2] = ll[x]
lt[3] = ll[x - 1]
for y in range(n * 2):
if y < n:
lb.append(list(map(int, l[lt[0]][y % n] + l[lt[1]][y % n])))
else:
lb.append(list(map(int, l[lt[2]][y % n] + l[lt[3]][y % n])))
for y in range(2):
f = 0
for y1 in range(n * 2):
for x1 in range(n * 2):
if (x1 + y1) % 2:
if lb[y1][x1] != y:
f += 1
else:
if lb[y1][x1] == y:
f += 1
if f < min_f:
min_f = f
print(min_f)
``` | output | 1 | 63,706 | 7 | 127,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2 | instruction | 0 | 63,707 | 7 | 127,414 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
n=int(input())
s1=""
s2=""
for i in range(0,n):
s1+=str(i%2)
s2+=str((i+1)%2)
a=[]
for i in range(0,7):
b=0
if i%2==0:
for j in range(0,n):
s=input()
if j%2==0:
for k in range(0,n):
if s1[k]!=s[k]:
b+=1
else:
for k in range(0,n):
if s2[k]!=s[k]:
b+=1
a.append(b)
a.append(n*n-b)
else:
x=input()
print(min(a[0]+a[2]+a[5]+a[7],a[0]+a[3]+a[4]+a[7],a[0]+a[3]+a[5]+a[6],a[1]+a[2]+a[4]+a[7],a[1]+a[2]+a[5]+a[6],a[1]+a[3]+a[4]+a[6]))
``` | output | 1 | 63,707 | 7 | 127,415 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2 | instruction | 0 | 63,708 | 7 | 127,416 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
import sys
n=int(input())
M=[['0' for k in range(n)]for _ in range(n)]
L=[['0' for k in range(n)]for _ in range(n)]
for i in range(n):
for j in range(n):
if j%2==i%2:
M[i][j]='1'
for i in range(n):
for j in range(n):
if j%2!=i%2:
L[i][j]='1'
def test(K):
m=0
for i in range(n):
for j in range(n):
if M[i][j]!=K[i][j]:
m+=1
return m
A=[]
for _ in range(4):
K=[]
for i in range(n):
K.append(list(input()))
f=sys.stdin.readline()
A.append(test(K))
mini=100000
a=min(A[0]+A[1]+2*n*n-A[2]-A[3],A[0]+A[2]+2*n*n-A[1]-A[3],A[0]+A[3]+2*n*n-A[2]-A[1])
b=min(A[1]+A[2]+2*n*n-A[0]-A[3],A[1]+A[3]+2*n*n-A[2]-A[0])
c=A[2]+A[3]+2*n*n-A[1]-A[0]
mini=min(a,b,c)
print(mini)
``` | output | 1 | 63,708 | 7 | 127,417 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2 | instruction | 0 | 63,709 | 7 | 127,418 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
def read_data():
n = int(input().strip())
pieces = []
for j in range(4):
a = []
for i in range(n):
a.append(input().strip())
if j<3:
input().strip()
pieces.append(a)
return n, pieces
def solve():
counts = []
for piece in pieces:
cur = 0
sum = 0
for i in range(n):
for j in range(n):
if ord(piece[i][j])-48 == cur:
sum += 1
cur += 1
cur %= 2
counts.append(sum)
max= n*n
counts.sort()
sum = 0
for i in range(4):
if i < 2:
sum += counts[i]
else:
sum += max - counts[i]
return sum
n, pieces = read_data()
print(solve())
``` | output | 1 | 63,709 | 7 | 127,419 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2 | instruction | 0 | 63,710 | 7 | 127,420 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
n = int(input())
p = [0] * 4
for k in range(4):
for i in range(n):
s = input()
for j in range(n):
if int(s[j]) != (i + j) % 2:
p[k] += 1
if k < 3:
input()
p.sort()
print(p[0] + p[1] + 2*n*n - p[2] - p[3])
``` | output | 1 | 63,710 | 7 | 127,421 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2 | instruction | 0 | 63,711 | 7 | 127,422 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
## necessary imports
import sys
input = sys.stdin.readline
#from math import ceil, floor, factorial;
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp
## gcd function
def gcd(a,b):
if b == 0:
return a
return gcd(b, a % b);
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0):
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x:
lo = mid+1
else:
hi = mid
return lo
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e6 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
# spf = [0 for i in range(MAXN)]
# spf_sieve()
def factoriazation(x):
ret = {};
while x != 1:
ret[spf[x]] = ret.get(spf[x], 0) + 1;
x = x//spf[x]
return ret
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
## taking integer array input
def int_array():
return list(map(int, input().strip().split()))
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
n = int(input());
board = [];
for _ in range(4):
mine = [];
for i in range(n):
mine.append(list(input().strip()));
board.append(mine);
if _ != 3:
literally_useless = input().strip();
one = [['0' for _ in range(n)] for __ in range(n)];
zero = [['1' for _ in range(n)] for __ in range(n)];
for i in range(n):
for j in range(n):
if (i + j) & 1:
one[i][j] = '1';
zero[i][j] = '0';
one_d = []; zero_d = [];
for i in range(4):
count_one = 0; count_zero = 0;
for j in range(n):
for k in range(n):
if board[i][j][k] != one[j][k]:
count_one += 1;
if board[i][j][k] != zero[j][k]:
count_zero += 1;
one_d.append(count_one);
zero_d.append(count_zero);
ans = INF;
for i in range(4):
for j in range(4):
if i == j:
continue;
this = [0, 1, 2, 3]; this.remove(i); this.remove(j);
kaguya = one_d[i] + one_d[j] + zero_d[this[0]] + zero_d[this[1]];
ans = min(ans, kaguya);
print(ans);
``` | output | 1 | 63,711 | 7 | 127,423 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2
Submitted Solution:
```
n=int(input())
l=[]
for i in range(4):
l.append(0)
for q in range(4):
for i in range(1,n+1):
s=input()
for j in range(1,n+1):
if i%2==j%2 and s[j-1]=='1':
l[q]+=1
if i%2!=j%2 and s[j-1]=='0':
l[q]+=1
if q<3:
z=input()
#print(l)
l.sort()
n2=n**2
count=0
count+=l[0]+l[1]
count+=2*n2-l[2]-l[3]
print(count)
``` | instruction | 0 | 63,712 | 7 | 127,424 |
Yes | output | 1 | 63,712 | 7 | 127,425 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2
Submitted Solution:
```
n=int(input())
a1=[]
for i in range(n):
a=str(input())
a=list(a)
a1.append(a)
x1=str(input())
a2=[]
for i in range(n):
a=str(input())
a=list(a)
a2.append(a)
x1=str(input())
a3=[]
for i in range(n):
a=str(input())
a=list(a)
a3.append(a)
x1=str(input())
a4=[]
for i in range(n):
a=str(input())
a=list(a)
a4.append(a)
ab=[]
ab.append(a1)
ab.append(a2)
ab.append(a3)
ab.append(a4)
x=[]
one=[]
for i in range(n):
z=[]
for j in range(n):
if((i+j)%2==0):
z.append('1')
else:
z.append('0')
one.append(z)
two=[]
for i in range(n):
z=[]
for j in range(n):
if((i+j)%2==0):
z.append('0')
else:
z.append('1')
two.append(z)
for i in range(4):
e1=0
o1=0
for j in range(n):
for k in range(n):
if(ab[i][j][k]!=one[j][k]):
e1+=1
if(ab[i][j][k]!=two[j][k]):
o1+=1
x.append([e1,o1])
s=x[0][0]+x[1][1]+x[2][0]+x[3][1]
if(x[0][0]+x[1][0]+x[2][1]+x[3][1]<s):
s=x[0][0]+x[1][0]+x[2][1]+x[3][1]
if(x[0][0]+x[1][1]+x[2][1]+x[3][0]<s):
s=x[0][0]+x[1][1]+x[2][1]+x[3][0]
if(x[0][1]+x[1][0]+x[2][1]+x[3][0]<s):
s=x[0][1]+x[1][0]+x[2][1]+x[3][0]
if(x[0][1]+x[1][1]+x[2][0]+x[3][0]<s):
s=x[0][1]+x[1][1]+x[2][0]+x[3][0]
if(x[0][1]+x[1][0]+x[2][0]+x[3][1]<s):
s=x[0][1]+x[1][0]+x[2][0]+x[3][1]
print(s)
``` | instruction | 0 | 63,713 | 7 | 127,426 |
Yes | output | 1 | 63,713 | 7 | 127,427 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2
Submitted Solution:
```
from itertools import permutations
n = int(input())
p1, _, p2, _, p3, _, p4 = [input() for _ in range(n)], input(), [input() for _ in range(n)], input(), [input() for _ in range(n)], input(), [input() for _ in range(n)]
def count(a,b,c,d):
board = [a[i] + b[i] for i in range(n)] + [c[i] + d[i] for i in range(n)]
res = 0
for i in range(2*n):
for j in range(2*n):
clr = '1' if (i+j)%2 == 0 else '0'
res += board[i][j] != clr
return res
print(min(count(*p) for p in permutations([p1, p2, p3, p4])))
``` | instruction | 0 | 63,714 | 7 | 127,428 |
Yes | output | 1 | 63,714 | 7 | 127,429 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2
Submitted Solution:
```
n = int(input())
vs = [0]*4
for i in range(4):
w=v=0
for _ in range(n):
for k in input():
w^=1
if w==int(k): v+=1
vs[i]=v
if i < 3: input()
base = 2*n**2+sum(vs)
minv = 4*n**2
for i in range(4):
for j in range(i+1,4):
minv = min(minv, base-2*(vs[i]+vs[j]))
print(minv)
``` | instruction | 0 | 63,715 | 7 | 127,430 |
Yes | output | 1 | 63,715 | 7 | 127,431 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2
Submitted Solution:
```
n=int(input())
if n==1:
d=dict()
d[0]=0
d[1]=0
for i in range(4):
x=int(input())
d[x]+=1
if i<3:
vide=input()
v=list(d.values())
print(abs(v[0]-2))
else:
ch1='01'*(n//2)+'0'
ch2='10'*(n//2)+'1'
c1=(ch1+ch2)*(n//2)+ch1
c2=(ch2+ch1)*(n//2)+ch2
d=dict()
cas1=0
cas2=0
for j in range(4):
ch=''
for i in range(n):
ch+=input()
if j<3:
vide=input()
com1=com2=0
for i in range(len(c1)):
if c1[i]== ch[i]:
com1+=1
else:
com2+=1
if com1>com2:
d[(ch,j)]=[1,n**2-com1]
cas1+=1
else:
d[(ch,j)]=[2,n**2-com2]
cas2+=1
v=list(d.keys())
if cas1<cas2:
while cas1<cas2:
m=0
p=tuple()
for i in v:
if d[i][0]==2:
if d[i][1]>m:
m=d[i][1]
p=i
d[p]=[1,n**2-m]
cas1+=1
elif cas2<cas1:
while cas2<cas1:
m=0
p=tuple()
for i in v:
if d[i][0]==1:
if d[i][1]>m:
m=d[i][1]
p=i
d[p]=[2,n**2-d[p][1]]
cas2+=2
ans=0
for i in v:
ans+=d[i][1]
print(ans)
``` | instruction | 0 | 63,716 | 7 | 127,432 |
No | output | 1 | 63,716 | 7 | 127,433 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2
Submitted Solution:
```
a=int(input())
l=[]
total=0
for i in range(4):
line=''
for x in [0]*a:
line+=input()
l.append(line)
input() if i!=3 else 0
l=sorted(l,key=lambda i: i[0::2].count('1'))[::-1]
for z,v in enumerate(l):
if z<2:
for i in range(a**2):
total += v[i]!='0' if i%2 else v[i]!='1'
else:
for i in range(a**2):
total += v[i]!='1' if i%2 else v[i]!='0'
print(total)
``` | instruction | 0 | 63,717 | 7 | 127,434 |
No | output | 1 | 63,717 | 7 | 127,435 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2
Submitted Solution:
```
def main():
count=int(input())
value=[0,0,0,0]
for x in range(3):
store=0
pole=True
for y in range(count):
arr=list(input())
for z in range(count):
if arr[z]=="1" and pole:
store+=1
if arr[z]=="0" and not pole:
store+=1
pole=not pole
value[x]=store
y=input()
#print(value)
mini=value[0]+value[1]+2*count*count-(value[2]+value[3])
for a in range(15):
hit=0
for b in range(4):
if 1<<b & a:
hit+=1
if hit==2:
store=0
for b in range(4):
if 1<<b & a:
store+=value[b]
else:
store+=count*count-value[b]
#print(a,store)
mini=min(mini,store)
print(mini)
main()
``` | instruction | 0 | 63,718 | 7 | 127,436 |
No | output | 1 | 63,718 | 7 | 127,437 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples
Input
1
0
0
1
0
Output
1
Input
3
101
010
101
101
000
101
010
101
011
010
101
010
Output
2
Submitted Solution:
```
import itertools
n = int(input())
l = [None] * 4
for _ in range(4):
tmp = 0
for i in range(n):
line = input().strip()
for j, s in enumerate(line):
tmp += (i + j) % 2 and s == '1' or (not (i + j) % 2) and s == '0'
l[_] = tmp
if _ != 3:
input()
min_ = 1000000
for i1, i2, i3, i4 in itertools.combinations([0, 1, 2, 3], 4):
min_ = min(min_, l[i1] + l[i2] + n * n - l[i3] + n * n - l[i4])
print(min_)
``` | instruction | 0 | 63,719 | 7 | 127,438 |
No | output | 1 | 63,719 | 7 | 127,439 |
Provide a correct Python 3 solution for this coding contest problem.
We will create an artwork by painting black some squares in a white square grid with 10^9 rows and N columns.
The current plan is as follows: for the i-th column from the left, we will paint the H_i bottommost squares and will not paint the other squares in that column.
Before starting to work, you can choose at most K columns (possibly zero) and change the values of H_i for these columns to any integers of your choice between 0 and 10^9 (inclusive).
Different values can be chosen for different columns.
Then, you will create the modified artwork by repeating the following operation:
* Choose one or more consecutive squares in one row and paint them black. (Squares already painted black can be painted again, but squares not to be painted according to the modified plan should not be painted.)
Find the minimum number of times you need to perform this operation.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 0 \leq H_i \leq 10^9
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
H_1 H_2 ... H_N
Output
Print the minimum number of operations required.
Examples
Input
4 1
2 3 4 1
Output
3
Input
6 2
8 6 9 1 2 1
Output
7
Input
10 0
1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000
Output
4999999996 | instruction | 0 | 63,768 | 7 | 127,536 |
"Correct Solution:
```
n,k=map(int,input().split())
dp=[]
for i in range(n+2):
dp.append([])
for j in range(n+2):
dp[-1].append([])
for l in range(k+1):
dp[-1][-1].append(10**12)
for l in range(k+1):
dp[0][0][l]=0
h=list(map(int,input().split()))
h.append(0)
h.reverse()
h.append(0)
h.reverse()
for i in range(1,n+2):
for j in range(n+2):
for l in range(k+1):
dp[i][i][l]=min(dp[i][i][l],dp[i-1][j][l]+max(0,h[i]-h[j]))
if l>0:
dp[i][j][l]=min(dp[i][j][l],dp[i-1][j][l-1])
dp[i][0][l]=min(dp[i][0][l],dp[i-1][j][l-1]+h[j])
ans=dp[n+1][0][0]
for j in range(n+2):
for l in range(k+1):
ans=min(ans,dp[n+1][j][l])
print(ans)
``` | output | 1 | 63,768 | 7 | 127,537 |
Provide a correct Python 3 solution for this coding contest problem.
We will create an artwork by painting black some squares in a white square grid with 10^9 rows and N columns.
The current plan is as follows: for the i-th column from the left, we will paint the H_i bottommost squares and will not paint the other squares in that column.
Before starting to work, you can choose at most K columns (possibly zero) and change the values of H_i for these columns to any integers of your choice between 0 and 10^9 (inclusive).
Different values can be chosen for different columns.
Then, you will create the modified artwork by repeating the following operation:
* Choose one or more consecutive squares in one row and paint them black. (Squares already painted black can be painted again, but squares not to be painted according to the modified plan should not be painted.)
Find the minimum number of times you need to perform this operation.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 0 \leq H_i \leq 10^9
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
H_1 H_2 ... H_N
Output
Print the minimum number of operations required.
Examples
Input
4 1
2 3 4 1
Output
3
Input
6 2
8 6 9 1 2 1
Output
7
Input
10 0
1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000
Output
4999999996 | instruction | 0 | 63,769 | 7 | 127,538 |
"Correct Solution:
```
N,K = map(int,input().split())
H = [0] + list(map(int,input().split()))
INF = float('inf')
dp = [[INF]*(N-K+1) for _ in range(N+1)]
dp[0][0] = 0
for i,h in enumerate(H):
if i==0: continue
for j in range(N-K,0,-1):
for k in range(i):
c = max(0, h - H[k])
dp[i][j] = min(dp[i][j], dp[k][j-1] + c)
print(min(row[-1] for row in dp))
``` | output | 1 | 63,769 | 7 | 127,539 |
Provide a correct Python 3 solution for this coding contest problem.
We will create an artwork by painting black some squares in a white square grid with 10^9 rows and N columns.
The current plan is as follows: for the i-th column from the left, we will paint the H_i bottommost squares and will not paint the other squares in that column.
Before starting to work, you can choose at most K columns (possibly zero) and change the values of H_i for these columns to any integers of your choice between 0 and 10^9 (inclusive).
Different values can be chosen for different columns.
Then, you will create the modified artwork by repeating the following operation:
* Choose one or more consecutive squares in one row and paint them black. (Squares already painted black can be painted again, but squares not to be painted according to the modified plan should not be painted.)
Find the minimum number of times you need to perform this operation.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 0 \leq H_i \leq 10^9
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
H_1 H_2 ... H_N
Output
Print the minimum number of operations required.
Examples
Input
4 1
2 3 4 1
Output
3
Input
6 2
8 6 9 1 2 1
Output
7
Input
10 0
1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000
Output
4999999996 | instruction | 0 | 63,770 | 7 | 127,540 |
"Correct Solution:
```
# dp[i][j] := i 列目まで見て、i 列目を使うとして、
# j 列無視した場合の min
N, K = map(int, input().split())
H = [0] + list(map(int, input().split())) + [0]
N += 1
inf = 10**18
dp = [[inf]*(K+1) for _ in range(N+1)]
dp[0][0] = 0
for i, h in enumerate(H[1:], 1):
for j in range(K+1):
for k in range(K+1): # k 列無視する
if i-k-1 < 0 or j-k < 0:
continue
h_p = H[i-k-1]
dp[i][j] = min(dp[i][j], dp[i-k-1][j-k] + max(0, h - h_p))
#for d in dp:
# print(d)
print(min(dp[-1]))
``` | output | 1 | 63,770 | 7 | 127,541 |
Provide a correct Python 3 solution for this coding contest problem.
We will create an artwork by painting black some squares in a white square grid with 10^9 rows and N columns.
The current plan is as follows: for the i-th column from the left, we will paint the H_i bottommost squares and will not paint the other squares in that column.
Before starting to work, you can choose at most K columns (possibly zero) and change the values of H_i for these columns to any integers of your choice between 0 and 10^9 (inclusive).
Different values can be chosen for different columns.
Then, you will create the modified artwork by repeating the following operation:
* Choose one or more consecutive squares in one row and paint them black. (Squares already painted black can be painted again, but squares not to be painted according to the modified plan should not be painted.)
Find the minimum number of times you need to perform this operation.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 0 \leq H_i \leq 10^9
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
H_1 H_2 ... H_N
Output
Print the minimum number of operations required.
Examples
Input
4 1
2 3 4 1
Output
3
Input
6 2
8 6 9 1 2 1
Output
7
Input
10 0
1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000
Output
4999999996 | instruction | 0 | 63,771 | 7 | 127,542 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
N, K = MAP()
M = N - K
A = [0] + LIST()
# dp[i][j] := 列をi個使って、最後に使ったのがj列目の時の最小コスト
dp = list2d(M+1, N+1, INF)
dp[0][0] = 0
for i in range(1, M+1):
for j, h1 in enumerate(A):
for k in range(max(j-N, 0), j):
h2 = A[k]
dp[i][j] = min(dp[i][j], dp[i-1][k] + max(h1 - h2, 0))
print(min(dp[M]))
``` | output | 1 | 63,771 | 7 | 127,543 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.