message
stringlengths
2
28.7k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
21
109k
cluster
float64
7
7
__index_level_0__
int64
42
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a picture consisting of n rows and m columns. Rows are numbered from 1 to n from the top to the bottom, columns are numbered from 1 to m from the left to the right. Each cell is painted either black or white. You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers x and y, where 1 ≀ x ≀ n and 1 ≀ y ≀ m, such that all cells in row x and all cells in column y are painted black. For examples, each of these pictures contain crosses: <image> The fourth picture contains 4 crosses: at (1, 3), (1, 5), (3, 3) and (3, 5). Following images don't contain crosses: <image> You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black. What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross? You are also asked to answer multiple independent queries. Input The first line contains an integer q (1 ≀ q ≀ 5 β‹… 10^4) β€” the number of queries. The first line of each query contains two integers n and m (1 ≀ n, m ≀ 5 β‹… 10^4, n β‹… m ≀ 4 β‹… 10^5) β€” the number of rows and the number of columns in the picture. Each of the next n lines contains m characters β€” '.' if the cell is painted white and '*' if the cell is painted black. It is guaranteed that βˆ‘ n ≀ 5 β‹… 10^4 and βˆ‘ n β‹… m ≀ 4 β‹… 10^5. Output Print q lines, the i-th line should contain a single integer β€” the answer to the i-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross. Example Input 9 5 5 ..*.. ..*.. ***** ..*.. ..*.. 3 4 **** .*.. .*.. 4 3 *** *.. *.. *.. 5 5 ***** *.*.* ***** ..*.* ..*** 1 4 **** 5 5 ..... ..*.. .***. ..*.. ..... 5 3 ... .*. .*. *** .*. 3 3 .*. *.* .*. 4 4 *.** .... *.** *.** Output 0 0 0 0 0 4 1 1 2 Note The example contains all the pictures from above in the same order. The first 5 pictures already contain a cross, thus you don't have to paint anything. You can paint (1, 3), (3, 1), (5, 3) and (3, 5) on the 6-th picture to get a cross in (3, 3). That'll take you 4 minutes. You can paint (1, 2) on the 7-th picture to get a cross in (4, 2). You can paint (2, 2) on the 8-th picture to get a cross in (2, 2). You can, for example, paint (1, 3), (3, 1) and (3, 3) to get a cross in (3, 3) but that will take you 3 minutes instead of 1. There are 9 possible crosses you can get in minimum time on the 9-th picture. One of them is in (1, 1): paint (1, 2) and (2, 1). Submitted Solution: ``` req_cnt = int(input()) for i in range(req_cnt): n, m = map(int, input().split()) matrix = [None] * n for i in range(n): matrix[i] = input() countsLN = [0] * n countsCL = [0] * m for i in range(n): for j in range(m): if matrix[i][j] == '*': countsLN[i] += 1 countsCL[j] += 1 bst_res = 999999999 for i in range(n): for j in range(m): lackLN = n - countsLN[i] lackCL = m - countsCL[j] if lackLN + lackCL < bst_res: bst_res = lackLN + lackCL print(bst_res) ```
instruction
0
80,917
7
161,834
No
output
1
80,917
7
161,835
Provide tags and a correct Python 3 solution for this coding contest problem. You've gotten an n Γ— m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make it not connected. A set of painted squares is called connected, if for every two squares a and b from this set there is a sequence of squares from the set, beginning in a and ending in b, such that in this sequence any square, except for the last one, shares a common side with the square that follows next in the sequence. An empty set and a set consisting of exactly one square are connected by definition. Input The first input line contains two space-separated integers n and m (1 ≀ n, m ≀ 50) β€” the sizes of the sheet of paper. Each of the next n lines contains m characters β€” the description of the sheet of paper: the j-th character of the i-th line equals either "#", if the corresponding square is painted (belongs to set A), or equals "." if the corresponding square is not painted (does not belong to set A). It is guaranteed that the set of all painted squares A is connected and isn't empty. Output On the first line print the minimum number of squares that need to be deleted to make set A not connected. If it is impossible, print -1. Examples Input 5 4 #### #..# #..# #..# #### Output 2 Input 5 5 ##### #...# ##### #...# ##### Output 2 Note In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore. The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The deleted squares are marked with crosses. <image>
instruction
0
81,062
7
162,124
Tags: constructive algorithms, graphs, trees Correct Solution: ``` def add(vertex,neighbour): if vertex in graph: graph[vertex].append(neighbour) else: graph[vertex]=[neighbour] if neighbour in graph: #####for undirected part remove to get directed graph[neighbour].append(vertex) else: graph[neighbour]=[vertex] def dfs(graph,n,currnode): visited=[False for x in range(n+1)] stack=[currnode] ans=[] while stack: currnode=stack[-1] if visited[currnode]==False: visited[currnode]=True ans.append(currnode) if currnode in graph1: for neighbour in graph[currnode]: if visited[neighbour]==False: visited[neighbour]=True stack.append(neighbour) ans.append(neighbour) break #####if we remove break it becomes bfs else: stack.pop() ####we are backtracking to previous node which is in our stack else: stack.pop() return ans n,m=[int(x) for x in input().split()] nodes=n*m arr=[None for i in range(nodes)] for i in range(n): s=input() for j in range(m): arr[i*m+j]=s[j] graph={} for i in range(m,nodes): if i%m!=0 and arr[i]=="#": if arr[i-1]=="#": add(i,i-1) r=i//m;c=i%m if arr[(r-1)*m+c]=="#": add(i,(r-1)*m+c) elif i%m==0 and arr[i]=="#": #if arr[i+1]=="#": #add(i,i+1) r=i//m if arr[(r-1)*m]=="#": add((r-1)*m,i) for i in range(1,m): if arr[i]=="#" and arr[i-1]=="#": add(i,i-1) for i in range(nodes): if arr[i]=="#" and i not in graph: graph[i]=[] graph1=graph.copy() if len(graph)<3: print(-1) else: found=False firstnode=[];secondnnode=[] for key,val in graph.items(): if len(firstnode)==0: firstnode=[key,val] d=len(dfs(graph,nodes,firstnode[0])) elif len(secondnnode)==0: secondnnode=[key,val] else: del graph1[key] if len(dfs(graph1,nodes,firstnode[0]))-1!=d-1: found=True break graph1[key]=val else: del graph1[firstnode[0]] if len(dfs(graph1,nodes,secondnnode[0]))-1!=d-1: found=True graph1[firstnode[0]]=firstnode[1] del graph1[secondnnode[0]] if len(dfs(graph1,nodes,firstnode[0]))-1!=d-1: found=True graph1[secondnnode[0]]=secondnnode[1] if found==True: print(1) else: print(2) ```
output
1
81,062
7
162,125
Provide tags and a correct Python 3 solution for this coding contest problem. You've gotten an n Γ— m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make it not connected. A set of painted squares is called connected, if for every two squares a and b from this set there is a sequence of squares from the set, beginning in a and ending in b, such that in this sequence any square, except for the last one, shares a common side with the square that follows next in the sequence. An empty set and a set consisting of exactly one square are connected by definition. Input The first input line contains two space-separated integers n and m (1 ≀ n, m ≀ 50) β€” the sizes of the sheet of paper. Each of the next n lines contains m characters β€” the description of the sheet of paper: the j-th character of the i-th line equals either "#", if the corresponding square is painted (belongs to set A), or equals "." if the corresponding square is not painted (does not belong to set A). It is guaranteed that the set of all painted squares A is connected and isn't empty. Output On the first line print the minimum number of squares that need to be deleted to make set A not connected. If it is impossible, print -1. Examples Input 5 4 #### #..# #..# #..# #### Output 2 Input 5 5 ##### #...# ##### #...# ##### Output 2 Note In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore. The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The deleted squares are marked with crosses. <image>
instruction
0
81,063
7
162,126
Tags: constructive algorithms, graphs, trees Correct Solution: ``` n, m = map(int, input().split()) a = [list(input()) for i in range(n)] dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)] def valid(x, y): return 0 <= x < n and 0 <= y < m def dfs(): cnt, p = 0, -1 for i in range(n): for j in range(m): if a[i][j] == '#': cnt += 1 p = (i, j) if p == -1: return False vis = [[0]*m for _ in range(n)] vis[p[0]][p[1]] = 1 cnt_vis = 1 stk = [(p[0], p[1])] while stk: x, y = stk.pop() for dx, dy in dirs: nx = x + dx ny = y + dy if valid(nx, ny) and not vis[nx][ny] and a[nx][ny] == '#': vis[nx][ny] = 1 cnt_vis += 1 stk.append((nx, ny)) return cnt_vis != cnt cnt, flag = 0, 0 f = False for i in range(n): for j in range(m): if a[i][j] == '.': continue cnt += 1 a[i][j] = '.' if dfs(): flag = True a[i][j] = '#' if cnt <= 2: print(-1) elif flag: print(1) else: print(2) ```
output
1
81,063
7
162,127
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've gotten an n Γ— m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make it not connected. A set of painted squares is called connected, if for every two squares a and b from this set there is a sequence of squares from the set, beginning in a and ending in b, such that in this sequence any square, except for the last one, shares a common side with the square that follows next in the sequence. An empty set and a set consisting of exactly one square are connected by definition. Input The first input line contains two space-separated integers n and m (1 ≀ n, m ≀ 50) β€” the sizes of the sheet of paper. Each of the next n lines contains m characters β€” the description of the sheet of paper: the j-th character of the i-th line equals either "#", if the corresponding square is painted (belongs to set A), or equals "." if the corresponding square is not painted (does not belong to set A). It is guaranteed that the set of all painted squares A is connected and isn't empty. Output On the first line print the minimum number of squares that need to be deleted to make set A not connected. If it is impossible, print -1. Examples Input 5 4 #### #..# #..# #..# #### Output 2 Input 5 5 ##### #...# ##### #...# ##### Output 2 Note In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore. The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The deleted squares are marked with crosses. <image> Submitted Solution: ``` n, m = map(int, input().split()) arr = list(input() for _ in range(n)) adj, vis = [], {} x = 0 for i in range(n): for j, q in enumerate(arr[i]): if q == '.': continue vis[(i, j)] = x adj.append([]) if (i, j - 1) in vis: adj[x].append(x - 1) adj[x - 1].append(x) if (i - 1, j) in vis: y = vis[(i - 1, j)] adj[x].append(y) adj[y].append(x) x += 1 if len(adj) < 3: print(-1) exit() for j in range(len(adj)): d = [1] * len(adj) d[j] = 0 s = [adj[j][0]] while s: j = s.pop() if not d[j]: continue d[j] = 0 if d[adj[j][0]]: s.append(adj[j][0]) if d[adj[j][1]]: s.append(adj[j][1]) if 1 in d: print(1) break else: print(2) ```
instruction
0
81,064
7
162,128
No
output
1
81,064
7
162,129
Provide tags and a correct Python 3 solution for this coding contest problem. Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone. In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip. You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares? Input The first line contains four space-separated integers a1, a2, a3, a4 (0 ≀ a1, a2, a3, a4 ≀ 104). The second line contains string s (1 ≀ |s| ≀ 105), where the Ρ–-th character of the string equals "1", if on the i-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip. Output Print a single integer β€” the total number of calories that Jury wastes. Examples Input 1 2 3 4 123214 Output 13 Input 1 5 3 2 11221 Output 13
instruction
0
81,145
7
162,290
Tags: implementation Correct Solution: ``` a = [int(x) for x in input().split()] n = input() l = [int(d) for d in str(n)] ans = 0 for i in range(len(l)): ans=ans+a[l[i]-1]; print(ans) ```
output
1
81,145
7
162,291
Provide tags and a correct Python 3 solution for this coding contest problem. Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone. In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip. You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares? Input The first line contains four space-separated integers a1, a2, a3, a4 (0 ≀ a1, a2, a3, a4 ≀ 104). The second line contains string s (1 ≀ |s| ≀ 105), where the Ρ–-th character of the string equals "1", if on the i-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip. Output Print a single integer β€” the total number of calories that Jury wastes. Examples Input 1 2 3 4 123214 Output 13 Input 1 5 3 2 11221 Output 13
instruction
0
81,146
7
162,292
Tags: implementation Correct Solution: ``` x = list(map(int,input().split())) y = input() s = 0 for i in y: if int(i) == 1: s = s + x[0] elif int(i) == 2: s = s + x[1] elif int(i) == 3: s = s + x[2] else: s = s + x[3] print(s) ```
output
1
81,146
7
162,293
Provide tags and a correct Python 3 solution for this coding contest problem. Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone. In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip. You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares? Input The first line contains four space-separated integers a1, a2, a3, a4 (0 ≀ a1, a2, a3, a4 ≀ 104). The second line contains string s (1 ≀ |s| ≀ 105), where the Ρ–-th character of the string equals "1", if on the i-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip. Output Print a single integer β€” the total number of calories that Jury wastes. Examples Input 1 2 3 4 123214 Output 13 Input 1 5 3 2 11221 Output 13
instruction
0
81,147
7
162,294
Tags: implementation Correct Solution: ``` a1,a2,a3,a4 = [ int(x) for x in input().split(' ') ] s = input() c=0 for i in range(len(s)): if s[i]=='1': c+=a1 elif s[i] == '2': c+=a2 elif s[i] == '3': c+=a3 elif s[i] == '4': c+=a4 print(c) ```
output
1
81,147
7
162,295
Provide tags and a correct Python 3 solution for this coding contest problem. Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone. In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip. You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares? Input The first line contains four space-separated integers a1, a2, a3, a4 (0 ≀ a1, a2, a3, a4 ≀ 104). The second line contains string s (1 ≀ |s| ≀ 105), where the Ρ–-th character of the string equals "1", if on the i-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip. Output Print a single integer β€” the total number of calories that Jury wastes. Examples Input 1 2 3 4 123214 Output 13 Input 1 5 3 2 11221 Output 13
instruction
0
81,148
7
162,296
Tags: implementation Correct Solution: ``` a=[int(x) for x in input().split()] s=input() length=len(s) ans=0 for i in range(length): ans=ans+a[int(s[i])-1] print(ans) ```
output
1
81,148
7
162,297
Provide tags and a correct Python 3 solution for this coding contest problem. Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone. In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip. You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares? Input The first line contains four space-separated integers a1, a2, a3, a4 (0 ≀ a1, a2, a3, a4 ≀ 104). The second line contains string s (1 ≀ |s| ≀ 105), where the Ρ–-th character of the string equals "1", if on the i-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip. Output Print a single integer β€” the total number of calories that Jury wastes. Examples Input 1 2 3 4 123214 Output 13 Input 1 5 3 2 11221 Output 13
instruction
0
81,149
7
162,298
Tags: implementation Correct Solution: ``` l=list(map(int,input().split())) s=input() #s=list(s) #print(s) d={'1':l[0],'2':l[1],'3':l[2],'4':l[3]} c=0 for i in range(len(s)): c+=d[s[i]] #print(a) print(c) ```
output
1
81,149
7
162,299
Provide tags and a correct Python 3 solution for this coding contest problem. Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone. In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip. You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares? Input The first line contains four space-separated integers a1, a2, a3, a4 (0 ≀ a1, a2, a3, a4 ≀ 104). The second line contains string s (1 ≀ |s| ≀ 105), where the Ρ–-th character of the string equals "1", if on the i-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip. Output Print a single integer β€” the total number of calories that Jury wastes. Examples Input 1 2 3 4 123214 Output 13 Input 1 5 3 2 11221 Output 13
instruction
0
81,150
7
162,300
Tags: implementation Correct Solution: ``` a,b,c,d = map(int,input().split()) e = list(input()) n = 0 for i in range(len(e)): e[i] = int(e[i]) for i in range(len(e)): if e[i] == 1: n += a elif e[i] == 2: n += b elif e[i] == 3: n += c else: n += d print(n) ```
output
1
81,150
7
162,301
Provide tags and a correct Python 3 solution for this coding contest problem. Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone. In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip. You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares? Input The first line contains four space-separated integers a1, a2, a3, a4 (0 ≀ a1, a2, a3, a4 ≀ 104). The second line contains string s (1 ≀ |s| ≀ 105), where the Ρ–-th character of the string equals "1", if on the i-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip. Output Print a single integer β€” the total number of calories that Jury wastes. Examples Input 1 2 3 4 123214 Output 13 Input 1 5 3 2 11221 Output 13
instruction
0
81,151
7
162,302
Tags: implementation Correct Solution: ``` stripes = [int(i) for i in input().split()] digits = input() calories = 0 for c in digits: calories += stripes[int(c) - 1] print (calories) ```
output
1
81,151
7
162,303
Provide tags and a correct Python 3 solution for this coding contest problem. Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone. In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip. You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares? Input The first line contains four space-separated integers a1, a2, a3, a4 (0 ≀ a1, a2, a3, a4 ≀ 104). The second line contains string s (1 ≀ |s| ≀ 105), where the Ρ–-th character of the string equals "1", if on the i-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip. Output Print a single integer β€” the total number of calories that Jury wastes. Examples Input 1 2 3 4 123214 Output 13 Input 1 5 3 2 11221 Output 13
instruction
0
81,152
7
162,304
Tags: implementation Correct Solution: ``` l = list(map(int,input().split())) s = list(input()) su = 0 se = set(s) for i in se: su += (s.count(i))*l[int(i)-1] print(su) ```
output
1
81,152
7
162,305
Provide tags and a correct Python 3 solution for this coding contest problem. The Hedgehog recently remembered one of his favorite childhood activities, β€” solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one. Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces. All puzzle pieces turn out to be of the same size X Γ— Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A Γ— B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers). However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over). Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size. Input The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20. Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters. Output In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β€” the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly β€” by the length X. Examples Input 2 4 ABDC ABDC Output 3 2 1 Input 2 6 ABCCBA ABCCBA Output 1 2 6 Note The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4).
instruction
0
81,217
7
162,434
Tags: hashing, implementation Correct Solution: ``` def rotate(puzzle): n_puzzle = [] for y in range(len(puzzle) - 1, -1, -1): n_puzzle.append(puzzle[y]) result = [] for x in range(len(puzzle[0])): col = [] for y in range(len(puzzle)): col.append(n_puzzle[y][x]) result.append(col) return result def puzzle_match(p1, p2): r1 = p2 r2 = rotate(p2) r3 = rotate(r2) r4 = rotate(r3) variants = [r1, r2, r3, r4] return p1 in variants def make_puzzles(puzzle, x_cuts, y_cuts, a, b): x_size = a // x_cuts y_size = b // y_cuts result = [] for x in range(x_cuts): for y in range(y_cuts): p = [] for i in range(x_size): row = [] for j in range(y_size): row.append(puzzle[x * x_size + i][y * y_size + j]) p.append(row) result.append(p) return result class CodeforcesTask54BSolution: def __init__(self): self.result = '' self.a_b = [] self.puzzle = [] def read_input(self): self.a_b = [int(x) for x in input().split(" ")] for x in range(self.a_b[0]): self.puzzle.append(list(input())) def process_task(self): x_cuts = [x for x in range(1, self.a_b[0] + 1) if not self.a_b[0] % x][::-1] y_cuts = [x for x in range(1, self.a_b[1] + 1) if not self.a_b[1] % x][::-1] variants = 0 varianted = [] for x in x_cuts: for y in y_cuts: puzzles = make_puzzles(self.puzzle, x, y, self.a_b[0], self.a_b[1]) rotated = [] valid = True for p in puzzles: r1 = p r2 = rotate(r1) r3 = rotate(r2) r4 = rotate(r3) for r in (r1, r2, r3, r4): if r in rotated: valid = False break if not valid: break rotated += [r1, r2, r3, r4] if valid: variants += 1 varianted.append((self.a_b[0] // x, self.a_b[1] // y)) varianted.sort(key=lambda x: x[0]*x[1]) self.result = "{0}\n{1} {2}".format(variants, varianted[0][0], varianted[0][1]) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask54BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ```
output
1
81,217
7
162,435
Provide tags and a correct Python 3 solution for this coding contest problem. The Hedgehog recently remembered one of his favorite childhood activities, β€” solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one. Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces. All puzzle pieces turn out to be of the same size X Γ— Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A Γ— B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers). However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over). Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size. Input The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20. Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters. Output In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β€” the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly β€” by the length X. Examples Input 2 4 ABDC ABDC Output 3 2 1 Input 2 6 ABCCBA ABCCBA Output 1 2 6 Note The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4).
instruction
0
81,218
7
162,436
Tags: hashing, implementation Correct Solution: ``` # /******************************************************************************* # * Author : Quantum Of Excellence # * email : quantumofexcellence (at) gmail (dot) com # * copyright : 2014 - 2015 # * date : 28 - 10 - 2015 # * Judge Status : # * file name : 54B.cpp # * version : 1.0 # * # * TERMS OF USE - Write a mail to the author before copying or reusing the content of this file # * seeking our permission for the same. # * Copying/reuse of the below code without the permission of the author is prohibited and illegal. # * # * All rights reserved by Quantum Of Excellence. # ******************************************************************************/ # /******************************************************************************* # * some pointers on the logic/idea - # * # * ............. # *******************************************************************************/ def rotate(B): l = len(B[0]) b = len(B) C = [[0 for _ in range(b)]for _ in range(l)] for p in range(l): for q in range(b): C[p][q] = B[q][l - 1 - p] return C #def cut(A,R,C,l,b): # s = set() # #print(A) # e = int(R / l) # f = int(C / b) # #print("e=%d,f=%d"%(e,f)) # for i in range(e): # for j in range(f): # #print("R=%d,l=%d,int(R/l)=%d,i=%d"%(R,l,int(R/l),i)) # B = [[A[i * l + x][j * b + y]for y in range(b)]for x in range(l)] # #print(B) # t = [''for k in range(4)] # for k in range(4): # for u in range(len(B)): # t[k]+=''.join(B[u]) # #print(t) # if(t[k] in s):return False # B = rotate(B) # #print(set(t[k])) # for k in range(4): # s.add(t[k]) # return True def cut(A,R,C,l,b): #print(A) e = int(R / l) f = int(C / b) #print("e=%d,f=%d"%(e,f)) for i in range(e): for j in range(f): #print("R=%d,l=%d,int(R/l)=%d,i=%d"%(R,l,int(R/l),i)) B = [[A[i * l + x][j * b + y]for y in range(b)]for x in range(l)] #print(B) ctr=0 for k in range(4): #print(t) g,h=len(B),len(B[0]) #if(0==R%g and 0==C%h): if(l==g and b==h): for p in range(int(R/g)): for q in range(int(C/h)): if(p==i and q==j):continue bo=0 for u in range(g): for v in range(h): if(B[u][v]!=A[p*g+u][q*h+v]): bo=1 break if(bo):break if(0==bo):return False B = rotate(B) return True #import sys #fi = open("G:\DUMP\input.in","r") #sys.stdin = fi u = input R,C = map(int,u().split()) A = [list(u()) for _ in range(R)] #print(A) r1 = 21 c1 = 21 cnt = 0 for l in range(1,R + 1): for b in range(1,C + 1): if(l==2 and b==6): d=0; if(R % l or C % b):continue if(cut(A,R,C,l,b)): if(l==2 and b==6): d=0; cnt+=1 #print("interim l=%d,b=%d" % (l,b)) if(r1 * c1 > l * b or (r1 * c1 == l * b and r1 > l)): r1,c1 = l,b print(cnt) print(r1,c1) ```
output
1
81,218
7
162,437
Provide tags and a correct Python 3 solution for this coding contest problem. The Hedgehog recently remembered one of his favorite childhood activities, β€” solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one. Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces. All puzzle pieces turn out to be of the same size X Γ— Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A Γ— B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers). However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over). Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size. Input The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20. Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters. Output In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β€” the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly β€” by the length X. Examples Input 2 4 ABDC ABDC Output 3 2 1 Input 2 6 ABCCBA ABCCBA Output 1 2 6 Note The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4).
instruction
0
81,219
7
162,438
Tags: hashing, implementation Correct Solution: ``` # /******************************************************************************* # * Author : Quantum Of Excellence # * email : quantumofexcellence (at) gmail (dot) com # * copyright : 2014 - 2015 # * date : 28 - 10 - 2015 # * Judge Status : # * file name : 54B.cpp # * version : 1.0 # * # * TERMS OF USE - Write a mail to the author before copying or reusing the content of this file # * seeking our permission for the same. # * Copying/reuse of the below code without the permission of the author is prohibited and illegal. # * # * All rights reserved by Quantum Of Excellence. # ******************************************************************************/ # /******************************************************************************* # * some pointers on the logic/idea - # * # * ............. # *******************************************************************************/ def rotate(B): l = len(B[0]) b = len(B) C = [[0 for _ in range(b)]for _ in range(l)] for p in range(l): for q in range(b): C[p][q] = B[q][l - 1 - p] return C #def cut(A,R,C,l,b): # s = set() # #print(A) # e = int(R / l) # f = int(C / b) # #print("e=%d,f=%d"%(e,f)) # for i in range(e): # for j in range(f): # #print("R=%d,l=%d,int(R/l)=%d,i=%d"%(R,l,int(R/l),i)) # B = [[A[i * l + x][j * b + y]for y in range(b)]for x in range(l)] # #print(B) # t = [''for k in range(4)] # for k in range(4): # for u in range(len(B)): # t[k]+=''.join(B[u]) # #print(t) # if(t[k] in s):return False # B = rotate(B) # #print(set(t[k])) # for k in range(4): # s.add(t[k]) # return True def cut(A,R,C,l,b): #print(A) e = int(R / l) f = int(C / b) #print("e=%d,f=%d"%(e,f)) for i in range(e): for j in range(f): #print("R=%d,l=%d,int(R/l)=%d,i=%d"%(R,l,int(R/l),i)) B = [[A[i * l + x][j * b + y]for y in range(b)]for x in range(l)] #print(B) ctr=0 for k in range(4): #print(t) g,h=len(B),len(B[0]) #if(0==R%g and 0==C%h): if(l==g and b==h): for p in range(int(R/g)): for q in range(int(C/h)): if(p==i and q==j):continue bo=0 for u in range(g): for v in range(h): if(B[u][v]!=A[p*g+u][q*h+v]): bo=1 break if(bo):break if(0==bo):return False B = rotate(B) return True #import sys #fi = open("G:\DUMP\input.in","r") #sys.stdin = fi u = input R,C = map(int,u().split()) A = [list(u()) for _ in range(R)] #print(A) r1 = 21 c1 = 21 cnt = 0 for l in range(1,R + 1): for b in range(1,C + 1): if(l==2 and b==6): d=0; if(R % l or C % b):continue if(cut(A,R,C,l,b)): if(l==2 and b==6): d=0; cnt+=1 #print("interim l=%d,b=%d" % (l,b)) if(r1 * c1 > l * b or (r1 * c1 == l * b and r1 > l)): r1,c1 = l,b print(cnt) print(r1,c1) # Made By Mostafa_Khaled ```
output
1
81,219
7
162,439
Provide tags and a correct Python 3 solution for this coding contest problem. The Hedgehog recently remembered one of his favorite childhood activities, β€” solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one. Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces. All puzzle pieces turn out to be of the same size X Γ— Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A Γ— B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers). However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over). Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size. Input The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20. Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters. Output In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β€” the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly β€” by the length X. Examples Input 2 4 ABDC ABDC Output 3 2 1 Input 2 6 ABCCBA ABCCBA Output 1 2 6 Note The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4).
instruction
0
81,220
7
162,440
Tags: hashing, implementation Correct Solution: ``` n, m = map(int, input().split()) grid = [ input().strip() for r in range(n) ] def flatten(piece): return '.'.join([ ''.join(row) for row in piece ]) def rotate(piece): n, m = len(piece), len(piece[0]) rotated = [ [ None for c in range(n) ] for r in range(m) ] for r in range(n): for c in range(m): rotated[c][n - 1 - r] = piece[r][c] return rotated def get_unique(piece): result = flatten(piece) for i in range(3): piece = rotate(piece) attempt = flatten(piece) if attempt < result: result = attempt return result def is_feasible(x, y): pieces = set() for r in range(0, n, x): for c in range(0, m, y): piece = [ grid[r + i][c : c + y] for i in range(x) ] piece = get_unique(piece) if piece in pieces: return False pieces.add(piece) return True best_x = n best_y = m count = 0 for x in range(1, n + 1): if n % x == 0: for y in range(1, m + 1): if m % y == 0: #print('testing %d, %d' % (x, y)) if is_feasible(x, y): #print(' feasible') count += 1 if x * y > best_x * best_y: continue if x * y == best_x * best_y and x >= best_x: continue best_x = x best_y = y print(count) print(best_x, best_y) ```
output
1
81,220
7
162,441
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Hedgehog recently remembered one of his favorite childhood activities, β€” solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one. Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces. All puzzle pieces turn out to be of the same size X Γ— Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A Γ— B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers). However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over). Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size. Input The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20. Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters. Output In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β€” the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly β€” by the length X. Examples Input 2 4 ABDC ABDC Output 3 2 1 Input 2 6 ABCCBA ABCCBA Output 1 2 6 Note The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4). Submitted Solution: ``` # /******************************************************************************* # * Author : Quantum Of Excellence # * email : quantumofexcellence (at) gmail (dot) com # * copyright : 2014 - 2015 # * date : 28 - 10 - 2015 # * Judge Status : # * file name : 54B.cpp # * version : 1.0 # * # * TERMS OF USE - Write a mail to the author before copying or reusing the content of this file # * seeking our permission for the same. # * Copying/reuse of the below code without the permission of the author is prohibited and illegal. # * # * All rights reserved by Quantum Of Excellence. # ******************************************************************************/ # /******************************************************************************* # * some pointers on the logic/idea - # * # * ............. # *******************************************************************************/ def rotate(B): l = len(B[0]) b = len(B) C = [[0 for _ in range(b)]for _ in range(l)] for p in range(l): for q in range(b): C[p][q] = B[q][l - 1 - p] return C def cut(A,R,C,l,b): s = set() #print(A) e = int(R / l) f = int(C / b) #print("e=%d,f=%d"%(e,f)) for i in range(e): for j in range(f): #print("R=%d,l=%d,int(R/l)=%d,i=%d"%(R,l,int(R/l),i)) B = [[A[i * l + x][j * b + y]for y in range(b)]for x in range(l)] #print(B) t = [''for k in range(4)] for k in range(4): for u in range(len(B)): t[k]+=''.join(B[u]) #print(t) if(t[k] in s):return False B = rotate(B) #print(set(t[k])) for k in range(4): s.add(t[k]) return True #import sys #fi = open("G:\DUMP\input.in","r") #sys.stdin = fi u = input R,C = map(int,u().split()) A = [list(u()) for _ in range(R)] #print(A) r1 = 20 c1 = 20 cnt = 0 for l in range(1,R + 1): for b in range(1,C + 1): if(R % l or C % b):continue if(cut(A,R,C,l,b)): cnt+=1 #print("interim l=%d,b=%d" % (l,b)) if(r1 * c1 > l * b or (r1 * c1 == l * b and r1 > l)): r1,c1 = l,b print(cnt) print(r1,c1) ```
instruction
0
81,221
7
162,442
No
output
1
81,221
7
162,443
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Hedgehog recently remembered one of his favorite childhood activities, β€” solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one. Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces. All puzzle pieces turn out to be of the same size X Γ— Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A Γ— B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers). However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over). Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size. Input The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20. Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters. Output In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β€” the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly β€” by the length X. Examples Input 2 4 ABDC ABDC Output 3 2 1 Input 2 6 ABCCBA ABCCBA Output 1 2 6 Note The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4). Submitted Solution: ``` n, m = map(int, input().split()) grid = [ input().strip() for r in range(n) ] def flatten(piece): return ''.join([ ''.join(row) for row in piece ]) def rotate(piece): n, m = len(piece), len(piece[0]) rotated = [ [ None for c in range(n) ] for r in range(m) ] for r in range(n): for c in range(m): rotated[c][n - 1 - r] = piece[r][c] return rotated def get_unique(piece): result = flatten(piece) for i in range(3): piece = rotate(piece) attempt = flatten(piece) if attempt < result: result = attempt return result def is_feasible(x, y): pieces = set() for r in range(0, n, x): for c in range(0, m, y): piece = [ grid[r + i][c : c + y] for i in range(x) ] piece = get_unique(piece) if piece in pieces: return False pieces.add(piece) return True best_x = n best_y = m count = 0 for x in range(1, n + 1): if n % x == 0: for y in range(1, m + 1): if m % y == 0: if is_feasible(x, y): count += 1 if x * y > best_x * best_y: continue if x * y == best_x * best_y and x >= best_x: continue best_x = x best_y = y print(count) print(best_x, best_y) ```
instruction
0
81,222
7
162,444
No
output
1
81,222
7
162,445
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Hedgehog recently remembered one of his favorite childhood activities, β€” solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one. Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces. All puzzle pieces turn out to be of the same size X Γ— Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A Γ— B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers). However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over). Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size. Input The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20. Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters. Output In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β€” the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly β€” by the length X. Examples Input 2 4 ABDC ABDC Output 3 2 1 Input 2 6 ABCCBA ABCCBA Output 1 2 6 Note The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4). Submitted Solution: ``` def rotate(puzzle): n_puzzle = [] for y in range(len(puzzle) - 1, -1, -1): n_puzzle.append(puzzle[y]) result = [] for x in range(len(puzzle[0])): col = [] for y in range(len(puzzle)): col.append(n_puzzle[y][x]) result.append(col) return result def puzzle_match(p1, p2): r1 = p2 r2 = rotate(p2) r3 = rotate(r2) r4 = rotate(r3) variants = [r1, r2, r3, r4] return p1 in variants def make_puzzles(puzzle, x_cuts, y_cuts, a, b): x_size = a // x_cuts y_size = b // y_cuts result = [] for x in range(x_cuts): for y in range(y_cuts): p = [] for i in range(x_size): row = [] for j in range(y_size): row.append(puzzle[x * x_size + i][y * y_size + j]) p.append(row) result.append(p) return result class CodeforcesTask54BSolution: def __init__(self): self.result = '' self.a_b = [] self.puzzle = [] def read_input(self): self.a_b = [int(x) for x in input().split(" ")] for x in range(self.a_b[0]): self.puzzle.append(list(input())) def process_task(self): x_cuts = [x for x in range(1, self.a_b[0] + 1) if not self.a_b[0] % x][::-1] y_cuts = [x for x in range(1, self.a_b[1] + 1) if not self.a_b[1] % x][::-1] variants = 0 varianted = [] for x in x_cuts: for y in y_cuts: puzzles = make_puzzles(self.puzzle, x, y, self.a_b[0], self.a_b[1]) rotated = [] valid = True for p in puzzles: r1 = p r2 = rotate(r1) r3 = rotate(r2) r4 = rotate(r3) for r in (r1, r2, r3, r4): if r in rotated: valid = False break if not valid: break rotated += [r1, r2, r3, r4] if valid: variants += 1 varianted.append((self.a_b[0] // x, self.a_b[1] // y)) self.result = "{0}\n{1} {2}".format(variants, varianted[0][0], varianted[0][1]) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask54BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ```
instruction
0
81,223
7
162,446
No
output
1
81,223
7
162,447
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp has a checkered sheet of paper of size n Γ— m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square. You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length. Input The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each β€” the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white. Output Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1. Examples Input 5 4 WWWW WWWB WWWB WWBB WWWW Output 5 Input 1 2 BB Output -1 Input 3 3 WWW WWW WWW Output 1 Note In the first example it is needed to paint 5 cells β€” (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2). In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square. In the third example all cells are colored white, so it's sufficient to color any cell black. Submitted Solution: ``` row, col = map(int, input().split()) lm,rm,tm,bm = 101,-1,101,-1 count = 0 for i in range(row): current = input() for j in range(len(current)): if current[j] == "B": count += 1 if j < lm: lm = j if j > rm: rm = j if i < tm: tm = i if i > bm: bm = i lengths = (rm - lm + 1, bm - tm + 1) if lengths[0] < lengths[1]: if lengths[1] > col: print("-1") else: print(lengths[1]**2 - count) elif lengths[1]<lengths[0]: if lengths[0] > row: print("-1") else: print(lengths[0]**2 - count) else: if count == 0: print(1) else: print(lengths[0]**2 - count) ```
instruction
0
81,368
7
162,736
Yes
output
1
81,368
7
162,737
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp has a checkered sheet of paper of size n Γ— m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square. You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length. Input The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each β€” the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white. Output Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1. Examples Input 5 4 WWWW WWWB WWWB WWBB WWWW Output 5 Input 1 2 BB Output -1 Input 3 3 WWW WWW WWW Output 1 Note In the first example it is needed to paint 5 cells β€” (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2). In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square. In the third example all cells are colored white, so it's sufficient to color any cell black. Submitted Solution: ``` a, b= map(int, input().split()) m = [[0 for i in range(b)] for j in range(a)] l = b r = -1 v = -1 k = 0 for i in range(a): p = input() for j in range(b): m[i][j] = p[j] #for i in range(a): #for j in range(b): #print(m[i][j], end=' ') #print() for i in range(a): for j in range(b): if m[i][j] == 'B': if v == -1: v = i n = i k += 1 if l > j: l = j if r < j: r = j if k == 0: print(1) else: s = max(n - v, r - l) + 1 if s > a or s > b: print(-1) else: #print(s) print(s*s - k) ```
instruction
0
81,369
7
162,738
Yes
output
1
81,369
7
162,739
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp has a checkered sheet of paper of size n Γ— m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square. You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length. Input The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each β€” the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white. Output Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1. Examples Input 5 4 WWWW WWWB WWWB WWBB WWWW Output 5 Input 1 2 BB Output -1 Input 3 3 WWW WWW WWW Output 1 Note In the first example it is needed to paint 5 cells β€” (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2). In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square. In the third example all cells are colored white, so it's sufficient to color any cell black. Submitted Solution: ``` while True: try: n,m=map(int,input().split()) a=[] x1=y1=11111111 x2=y2=z=0 for i in range(0, n): x=list(input()) a.append(x) for i in range(0,n): for j in range(0,m): if a[i][j]=='B': z+=1 x1 = min(x1, i) x2 = max(x2, i) y1 = min(y1, j) y2 = max(y2, j) if z==0: print(1) continue ans=max(x2-x1,y2-y1)+1 if ans>n or ans>m: print(-1) else: print(ans*ans-z) except EOFError: break ```
instruction
0
81,370
7
162,740
Yes
output
1
81,370
7
162,741
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp has a checkered sheet of paper of size n Γ— m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square. You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length. Input The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each β€” the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white. Output Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1. Examples Input 5 4 WWWW WWWB WWWB WWBB WWWW Output 5 Input 1 2 BB Output -1 Input 3 3 WWW WWW WWW Output 1 Note In the first example it is needed to paint 5 cells β€” (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2). In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square. In the third example all cells are colored white, so it's sufficient to color any cell black. Submitted Solution: ``` n,k=map(int,input().split()) list1=[] list2=[] black = 0 side = 0 for i in range(n): line=input() for a in range(len(line)): if line[a]=='B': list1.append(i) list2.append(a) black+=1 if black==0: print(1) else: side=max(max(list1)-min(list1)+1,max(list2)-min(list2)+1) if n<side or k<side: print(-1) else: print(side*side-black) ```
instruction
0
81,371
7
162,742
Yes
output
1
81,371
7
162,743
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp has a checkered sheet of paper of size n Γ— m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square. You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length. Input The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each β€” the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white. Output Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1. Examples Input 5 4 WWWW WWWB WWWB WWBB WWWW Output 5 Input 1 2 BB Output -1 Input 3 3 WWW WWW WWW Output 1 Note In the first example it is needed to paint 5 cells β€” (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2). In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square. In the third example all cells are colored white, so it's sufficient to color any cell black. Submitted Solution: ``` from sys import stdin,stdout n,m = (int(i) for i in stdin.readline().split()) x1,x2,y1,y2 = 10**6,0,0,0 c = 0 flag = False for i in range(n): st = stdin.readline() if st.count('B') > 0: c += st.count('B') if not(flag): y1 = i flag = True if x1 > st.index('B'): x1 = st.index('B') if x2 < st.rindex('B'): x2 = st.rindex('B')+1 y2 = i + 1 #print(x1,x2,y1,y2) height = y2 - y1 widht = x2 - x1 #print(widht,height) if c == 0: stdout.write('1') else: if max(widht,height) > min(n,m): stdout.write('-1') else: stdout.write(str(max(widht,height)**2-c)) ```
instruction
0
81,372
7
162,744
No
output
1
81,372
7
162,745
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp has a checkered sheet of paper of size n Γ— m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square. You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length. Input The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each β€” the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white. Output Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1. Examples Input 5 4 WWWW WWWB WWWB WWBB WWWW Output 5 Input 1 2 BB Output -1 Input 3 3 WWW WWW WWW Output 1 Note In the first example it is needed to paint 5 cells β€” (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2). In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square. In the third example all cells are colored white, so it's sufficient to color any cell black. Submitted Solution: ``` nandm = [int(i) for i in input().split()] n = nandm[0] m = nandm[1] b = [] numB = 0 for i in range(n): string = list(input()) for j in range(len(string)): if string[j]=="B": b.append((j, n-i-1)) numB+=1 #print(b) maxx = -1 maxy = -1 minx = m+1 miny = n+1 for x in b: if x[0]>maxx: maxx = x[0] if x[0]<minx: minx = x[0] if x[1]>maxy: maxy = x[1] if x[1]<miny: miny = x[1] #print(maxx, maxy, minx, miny) side = max(maxx-minx, maxy-miny)+1 if numB==0: print(1) elif side<n and side<m: print(side*side-numB) else: print(-1) ```
instruction
0
81,373
7
162,746
No
output
1
81,373
7
162,747
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp has a checkered sheet of paper of size n Γ— m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square. You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length. Input The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each β€” the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white. Output Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1. Examples Input 5 4 WWWW WWWB WWWB WWBB WWWW Output 5 Input 1 2 BB Output -1 Input 3 3 WWW WWW WWW Output 1 Note In the first example it is needed to paint 5 cells β€” (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2). In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square. In the third example all cells are colored white, so it's sufficient to color any cell black. Submitted Solution: ``` n, m = [int(i) for i in input().split()] height = 0 widht = 0 count = 0 count1 = 0 flag2 = False flag = False for i in range(n): st = input() if st.count('B') > 0: flag2 = True count += st.count('B') flag = True height = i + 1 if widht < len(st.strip('W')): widht = len(st.strip('W')) elif len(st) == st.count('B'): widht = len(st) if not (flag2): count1 += 1 if not (flag): print(1) exit() if max(widht, height - count1) > min(n, m): print(-1) else: if max(widht, height - count1) ** 2 - count == 46 : print(397) elif max(widht, height - count1) ** 2 - count == 2593: print(-1) else: print(max(widht, height - count1) ** 2 - count) ```
instruction
0
81,374
7
162,748
No
output
1
81,374
7
162,749
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp has a checkered sheet of paper of size n Γ— m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square. You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length. Input The first line contains two integers n and m (1 ≀ n, m ≀ 100) β€” the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each β€” the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white. Output Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1. Examples Input 5 4 WWWW WWWB WWWB WWBB WWWW Output 5 Input 1 2 BB Output -1 Input 3 3 WWW WWW WWW Output 1 Note In the first example it is needed to paint 5 cells β€” (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2). In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square. In the third example all cells are colored white, so it's sufficient to color any cell black. Submitted Solution: ``` def int_map(): return map(int, input().split(' ')) n, m = int_map() res = 0 ml = -1 mr = -1 mu = -1 md = -1 canv = [] for i in range(n): b = input() canv.append(b) # print(b) for x, j in enumerate(b): if j == 'B': # left if ml != -1: ml = min(x, ml) else: ml = x # right if mr != -1: mr = max(x, mr) else: mr = x # up if mu != -1: mu = min(i, mu) else: mu = i # down if md != -1: md = max(i, md) else: md = i ed = max(mr - ml + 1, md - mu + 1) # if ml > 0 and mr - ed + 1 >= 0: # ml = mr - ed + 1 # elif mr < n and ml + ed - 1 < n: # mr = ml + ed - 1 # else: # print(-1) # exit() # # if mu > 0 and md - ed + 1 >= 0: # mu = md - ed + 1 # elif md < n and ml + md - 1 < n: # md = ml + ed - 1 # else: # print(-1) # exit() if ml == -1: print(1) exit() while ml > 0 and mr - ml + 1 < ed: ml -= 1 while mr < n-1 and mr - ml + 1 < ed: mr += 1 while mu > 0 and md - mu + 1 < ed: mu -= 1 while md < n-1 and md - mu + 1 < ed: md += 1 if mr - ml + 1 != ed or md - mu + 1 != ed: print(-1) exit() for i in range(mu, md+1): res += canv[i].count('W', ml, mr+1) # print(ml, mr, mu, md) print(res) ```
instruction
0
81,375
7
162,750
No
output
1
81,375
7
162,751
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428
instruction
0
81,522
7
163,044
"Correct Solution: ``` import itertools n,c=map(int,input().split()) ld=[list(map(int,input().split())) for i in range(c)] lc=[list(map(int,input().split())) for i in range(n)] ct=[[0 for i in range(c)] for i in range(3)] for i in range(n): for j in range(n): ct[(i+j+2)%3][lc[i][j]-1]+=1 L=list(itertools.permutations([i for i in range(c)],3)) ans=[] for i in range(len(L)): ct2=0 for j in range(3): for k in range(c): ct2+=ld[k][L[i][j]]*ct[j][k] ans.append(ct2) print(min(ans)) ```
output
1
81,522
7
163,045
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428
instruction
0
81,523
7
163,046
"Correct Solution: ``` N,C = map(int,input().split()) D = [list(map(int,input().split())) for _ in range(C)] c = [list(map(lambda x:int(x)-1,input().split())) for _ in range(N)] n = [[0]*C for _ in range(3)] for i in range(N): for j in range(N): n[(i+j)%3][c[i][j]] += 1 ans = 500*500*1000 for c0 in range(C): for c1 in range(C): if c1 == c0: continue for c2 in range(C): if c2 in (c0,c1): continue score = 0 for a in range(C): score += n[0][a] * D[a][c0] + n[1][a] * D[a][c1] + n[2][a] * D[a][c2] ans = min(ans,score) print(ans) ```
output
1
81,523
7
163,047
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428
instruction
0
81,524
7
163,048
"Correct Solution: ``` import itertools N,C=map(int, input().split()) D=[list(map(int, input().split())) for _ in range(C)] cl_cnt=[[0 for _ in range(C) ] for _ in range(3)] for i in range(N): ci=list(map(int, input().split())) for j, cij in enumerate(ci): cl_cnt[(i+j)%3][cij-1]+=1 cost=[[0 for _ in range(C) ] for _ in range(3)] for i in range(3): for j in range(C): for k in range(C): cost[i][k]+=(D[j][k])*cl_cnt[i][j] ans=1000*500*500 for i, j, k in itertools.permutations(range(C),3): ans=min(cost[0][i]+cost[1][j]+cost[2][k], ans) print(ans) ```
output
1
81,524
7
163,049
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428
instruction
0
81,525
7
163,050
"Correct Solution: ``` n , c = map(int,input().split()) iwaka = [list(map(int,input().split())) for i in range(c)] masu = [list(map(int,input().split())) for i in range(n)] amari = [{} for i in range(3)] for i in range(n): for j in range(n): amari[(i+j)%3][masu[i][j]] = amari[(i+j)%3].get(masu[i][j],0) + 1 kouho = [] for i in range(c): for j in range(c): for k in range(c): if i != j and j != k and k != i: kouho.append((i+1,j+1,k+1)) ans = 10**9 for p in kouho: cou = 0 for i in range(3): for k , v in amari[i].items(): cou += iwaka[k-1][p[i]-1]*v ans = min(ans,cou) print(ans) ```
output
1
81,525
7
163,051
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428
instruction
0
81,526
7
163,052
"Correct Solution: ``` from itertools import permutations N,C=map(int,input().split()) D=[] for c in range(C): D.append(list(map(int,input().split()))) c=[] for i in range(N): c.append(list(map(lambda x:int(x)-1,input().split()))) color=[[0]*(C) for i in range(3)] cost=[[0]*(C) for i in range(3)] for i in range(N): for j in range(N): mod=(i+j+2)%3 oldc=c[i][j] for ci,cl in enumerate(D[oldc]): cost[mod][ci]+=cl mnc=float('inf') for i,j,k in permutations(range(C),3): cst=cost[0][i] + cost[1][j]+cost[2][k] if cst<mnc: mnc=cst print(mnc) ```
output
1
81,526
7
163,053
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428
instruction
0
81,527
7
163,054
"Correct Solution: ``` N,C = map(int,input().split()) D = [] c = [] for i in range(C): D.append(list(map(int,input().split()))) for i in range(N): c.append(list(map(int,input().split()))) d = {} d[0] = {} d[1] = {} d[2] = {} for i in range(C): d[0][i] = 0 d[1][i] = 0 d[2][i] = 0 for k in range(C): for i in range(N): for j in range(N): d[(i+j) % 3][k] += D[c[i][j]-1][k] import itertools ans = 10**50 l = [] for i in range(C): l.append(i) for li in itertools.permutations(l,3): ans = min(ans , d[0][li[0]] + d[1][li[1]] + d[2][li[2]]) print(ans) ```
output
1
81,527
7
163,055
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428
instruction
0
81,528
7
163,056
"Correct Solution: ``` from collections import Counter from itertools import permutations def main(): N, C = map(int, input().split()) D = tuple(tuple(map(int, input().split())) for _ in range(C)) cnt = [Counter() for _ in range(3)] for i in range(N): c = [int(x)-1 for x in input().split()] for j in range(3): cnt[j].update(c[(3-(i+2)+j)%3::3]) ans = 1000*500*500+5 for p in permutations(range(C), 3): s = 0 for j in range(3): s += sum(D[k][p[j]] * v for k, v in cnt[j].items()) if s < ans: ans = s print(ans) main() ```
output
1
81,528
7
163,057
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428
instruction
0
81,529
7
163,058
"Correct Solution: ``` import sys input = sys.stdin.buffer.readline n, c = map(int, input().split()) D = [list(map(int, input().split())) for _ in range(c)] C = [list(map(int, input().split())) for _ in range(n)] A = [[0]*c for _ in range(3)] for i in range(n): for j in range(n): x = C[i][j] - 1 r = (i+j)%3 for y in range(c): A[r][y] += D[x][y] ans = float("inf") for i in range(c): for j in range(c): for k in range(c): if i == j or j == k or k == i: continue temp = A[0][i] + A[1][j] + A[2][k] ans = min(ans, temp) print(ans) ```
output
1
81,529
7
163,059
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428 Submitted Solution: ``` from itertools import permutations N, C = map(int, input().split()) d = [[0]*C for i in range(C)] for i in range(C): d[i] = [int(c) for c in input().split()] grid = [[0]*C for i in range(3)] for i in range(N): inf = [int(c)-1 for c in input().split()] for j in range(N): x = (j+i+2)%3 g = inf[j] grid[x][g] += 1 record = [[0]*C for i in range(3)] for i in range(3): for j in range(C): m = 0 for h in range(C): m += grid[i][h]*d[h][j] record[i][j] = m ans = 10**9 seq = range(C) for i,j,h in permutations(seq,3): m = record[0][i]+record[1][j]+record[2][h] if m<ans: ans = m print(ans) ```
instruction
0
81,530
7
163,060
Yes
output
1
81,530
7
163,061
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428 Submitted Solution: ``` from itertools import permutations n, C = map(int, input().split()) d = [list(map(int, input().split())) for i in range(C)] c = [list(map(int, input().split())) for i in range(n)] l = [[0] * C for i in range(3)] for i in range(n): for j, k in enumerate(c[i]): l[(i + j) % 3][k - 1] += 1 ans = float("inf") for i in permutations(range(C), 3): cnt = 0 for j in range(3): for k in range(C): cnt += d[k][i[j]] * l[j][k] ans = min(ans, cnt) print(ans) ```
instruction
0
81,531
7
163,062
Yes
output
1
81,531
7
163,063
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428 Submitted Solution: ``` from collections import Counter from itertools import product, permutations def main(): n, c = map(int, input().split()) D = [list(map(int, input().split())) for _ in range(c)] C = [list(map(lambda x:int(x)-1, input().split())) for _ in range(n)] group = [Counter([C[i][j] for i,j in product(range(n), range(n)) if (i+j+2) %3 == k]) for k in range(3)] ans = float('inf') for color in permutations(range(c), 3): diff = 0 for i in range(3): diff += sum(D[before_color][color[i]]*count for before_color, count in group[i].items()) ans = min(ans, diff) print(ans) main() ```
instruction
0
81,532
7
163,064
Yes
output
1
81,532
7
163,065
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428 Submitted Solution: ``` from itertools import permutations INF=10**18 n,c=map(int,input().split()) d=[list(map(int,input().split())) for _ in range(c)] a=[list(map(int,input().split())) for _ in range(n)] dp=[[0 for _ in range(c)]for i in range(3)] for i in range(n): for j in range(n): dp[(i+j)%3][a[i][j]-1]+=1 li=list(permutations(list(range(c)),3)) fans=INF for i,j,k in li: ans=0 for x in range(c): ans+=dp[0][x]*d[x][i] ans+=dp[1][x]*d[x][j] ans+=dp[2][x]*d[x][k] fans=min(fans,ans) print(fans) ```
instruction
0
81,533
7
163,066
Yes
output
1
81,533
7
163,067
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428 Submitted Solution: ``` from itertools import product N, C = map(int,input().split()) D = [] for _ in range(C): D.append(list(map(int,input().split()))) c = [] for _ in range(N): c.append(list(map(int,input().split()))) ans = 10 ** 8 for L in product([i for i in range(C)], repeat=3): if len(set(L)) != 3: continue cnt = 0 for i in range(N): for j in range(N): cnt += D[c[i][j]-1][L[(i+j)%3]] ans = min(ans, cnt) print(ans) ```
instruction
0
81,534
7
163,068
No
output
1
81,534
7
163,069
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428 Submitted Solution: ``` N,C = list(map(int,(input()).split())) Dlist = [] clist = [] for i in range(C): Dlist.append(list(map(int,(input()).split()))) for i in range(N): clist.append(list(map(int,(input()).split()))) iwakan = [[],[],[]] for k in range(C): iwakanTemp = [0,0,0] for i in range(N): for j in range(N): iwakanTemp[(i + j) % 3] += Dlist[clist[i][j]-1][k] for i in range(3): iwakan[i].append(iwakanTemp[i]) sumTemp = 1145141919 for i in range(C): for j in range(C): for k in range(C): if not (i-j)*(j-k)*(k-i) == 0: hoge = iwakan[0][i]+iwakan[1][j]+iwakan[2][k] if hoge < sumTemp: sumTemp = hoge print(sumTemp) ```
instruction
0
81,535
7
163,070
No
output
1
81,535
7
163,071
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428 Submitted Solution: ``` def main(): N, Ma, Mb = map(int, input().split()) yakuhin = [] sa = 0 sb = 0 for i in range(N): yakuhin.append(list(map(int, input().split()))) sa += yakuhin[i][0] sb += yakuhin[i][1] INF = 10**15 DP = [[[INF for _ in range(sb+1)] for _ in range(sa+1)] for _ in range(N+1)] for i in range(N): DP[i][0][0] = 0 for i in range(N): for j in range(sa+1): for k in range(sb+1): if(j >= yakuhin[i][0] and k >= yakuhin[i][1]): DP[i+1][j][k] = min(DP[i][j][k], DP[i][j-yakuhin[i][0]][k-yakuhin[i][1]]+yakuhin[i][2]) else: DP[i+1][j][k] = min(DP[i+1][j][k], DP[i][j][k]) ans = INF for i in range(1, N): for j in range(1, sa+1): for k in range(1, sb+1): if(j*Mb == k*Ma): ans = min(ans, DP[i][j][k]) print(ans if ans != INF else -1) main() ```
instruction
0
81,536
7
163,072
No
output
1
81,536
7
163,073
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}. We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N: * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same. * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y. We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}. Find the minimum possible sum of the wrongness of all the squares. Constraints * 1 \leq N \leq 500 * 3 \leq C \leq 30 * 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) * 1 \leq c_{i,j} \leq C * All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x, print x. Examples Input 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Output 3 Input 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Output 428 Submitted Solution: ``` N, C = map(int, input().split()) D = [list(map(int, input().split())) for _ in range(C)] c = [list(map(int, input().split())) for _ in range(N)] from itertools import permutations colors = tuple(i for i in range(C)) lst = tuple(permutations(colors, 3)) table = [[0 for i in range(C)] for j in range(3)] for color in colors: for col in range(N): for row in range(N): table[(col+row)%3][color]+=D[c[col][row]-1][color] ans = 10**9 for i, j, k in lst: ans = min(ans, table[0][i]+table[1][j]+table[2][k]) print(ans) ```
instruction
0
81,537
7
163,074
No
output
1
81,537
7
163,075
Provide tags and a correct Python 3 solution for this coding contest problem. Boboniu gives you * r red balls, * g green balls, * b blue balls, * w white balls. He allows you to do the following operation as many times as you want: * Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations. Input The first line contains one integer T (1≀ T≀ 100) denoting the number of test cases. For each of the next T cases, the first line contains four integers r, g, b and w (0≀ r,g,b,w≀ 10^9). Output For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No". Example Input 4 0 1 1 1 8 1 9 3 0 0 0 0 1000000000 1000000000 1000000000 1000000000 Output No Yes Yes Yes Note In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome. In the second test case, after doing one operation, changing (8,1,9,3) to (7,0,8,6), one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr". A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome.
instruction
0
81,886
7
163,772
Tags: brute force, math Correct Solution: ``` import sys # sys.stdin = open("input.txt") def main(): tn = int(sys.stdin.readline()) for ti in range(tn): r, g, b, w = map(int, sys.stdin.readline().split()) n = r + g + b + w a, c = 0, 0 for v in (r, g, b): if v % 2 == 0: a += 1 if v == 0: c += 1 if a == 3 or a == 0: print("Yes") elif n % 2 == 0: print("No") elif a == 2: print("Yes") else: print("Yes" if not c else "No") if __name__ == "__main__": main() ```
output
1
81,886
7
163,773
Provide tags and a correct Python 3 solution for this coding contest problem. Boboniu gives you * r red balls, * g green balls, * b blue balls, * w white balls. He allows you to do the following operation as many times as you want: * Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations. Input The first line contains one integer T (1≀ T≀ 100) denoting the number of test cases. For each of the next T cases, the first line contains four integers r, g, b and w (0≀ r,g,b,w≀ 10^9). Output For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No". Example Input 4 0 1 1 1 8 1 9 3 0 0 0 0 1000000000 1000000000 1000000000 1000000000 Output No Yes Yes Yes Note In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome. In the second test case, after doing one operation, changing (8,1,9,3) to (7,0,8,6), one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr". A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome.
instruction
0
81,887
7
163,774
Tags: brute force, math Correct Solution: ``` def odd_checker(v): tnt = v[0] % 2 + v[1] % 2 + v[2] % 2 + v[3] % 2 return tnt def main(): test = int(input()) for i in range(test): v = list(map(int, input().split())) p=odd_checker(v) if p<=1: print('Yes') else: mon=min(v[0],v[1],v[2]) if mon%2==0 and mon!=0: mon-=1 v[0] -= mon v[1] -= mon v[2] -= mon v[3] += (mon*3) num=odd_checker(v) if num<=1: print('Yes') else: print('No') if __name__ == "__main__": main() ```
output
1
81,887
7
163,775
Provide tags and a correct Python 3 solution for this coding contest problem. Boboniu gives you * r red balls, * g green balls, * b blue balls, * w white balls. He allows you to do the following operation as many times as you want: * Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations. Input The first line contains one integer T (1≀ T≀ 100) denoting the number of test cases. For each of the next T cases, the first line contains four integers r, g, b and w (0≀ r,g,b,w≀ 10^9). Output For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No". Example Input 4 0 1 1 1 8 1 9 3 0 0 0 0 1000000000 1000000000 1000000000 1000000000 Output No Yes Yes Yes Note In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome. In the second test case, after doing one operation, changing (8,1,9,3) to (7,0,8,6), one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr". A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome.
instruction
0
81,888
7
163,776
Tags: brute force, math Correct Solution: ``` import sys input = sys.stdin.readline for _ in range(int(input())): a = list(map(int,input().split())) parity = [] for i in a: if i%2: parity.append("o") else: parity.append("e") o = parity.count("o") if o==2 or (o==3 and a[0]*a[1]*a[2]==0): print("No") else: print("Yes") ```
output
1
81,888
7
163,777
Provide tags and a correct Python 3 solution for this coding contest problem. Boboniu gives you * r red balls, * g green balls, * b blue balls, * w white balls. He allows you to do the following operation as many times as you want: * Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations. Input The first line contains one integer T (1≀ T≀ 100) denoting the number of test cases. For each of the next T cases, the first line contains four integers r, g, b and w (0≀ r,g,b,w≀ 10^9). Output For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No". Example Input 4 0 1 1 1 8 1 9 3 0 0 0 0 1000000000 1000000000 1000000000 1000000000 Output No Yes Yes Yes Note In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome. In the second test case, after doing one operation, changing (8,1,9,3) to (7,0,8,6), one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr". A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome.
instruction
0
81,889
7
163,778
Tags: brute force, math Correct Solution: ``` from fractions import Fraction import bisect import os import io from collections import Counter import bisect from collections import defaultdict import math import random import heapq from math import sqrt import sys from functools import reduce, cmp_to_key from collections import deque import threading from itertools import combinations from io import BytesIO, IOBase from itertools import accumulate from queue import Queue # sys.setrecursionlimit(200000) # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def tinput(): return input().split() def rinput(): return map(int, tinput()) def rlinput(): return list(rinput()) mod = int(1e9)+7 def factors(n): return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))) # ---------------------------------------------------- # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') t = 1 t = iinput() for _ in range(t): r, g, b, w = rinput() odds = 0 for i in [r, g, b,w]: if i % 2: odds += 1 if min(r, g, b) == 0: print(["No", "Yes"][odds <= 1]) else: odds = 0 for i in [r, g, b]: if i % 2: odds += 1 if w%2: print(["No", "Yes"][odds == 0 or odds == 2 or odds == 3]) else: print(["No", "Yes"][odds <= 1 or odds == 3]) # n, m = rinput() # a = rlinput() # b = rlinput() # ans = [] # decided = [-1]*(n+1) # for i in range(10,0,-1): # got = False # for j in a: # if j & (1 << (i - 1)): # ans.append(got) ```
output
1
81,889
7
163,779
Provide tags and a correct Python 3 solution for this coding contest problem. Boboniu gives you * r red balls, * g green balls, * b blue balls, * w white balls. He allows you to do the following operation as many times as you want: * Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations. Input The first line contains one integer T (1≀ T≀ 100) denoting the number of test cases. For each of the next T cases, the first line contains four integers r, g, b and w (0≀ r,g,b,w≀ 10^9). Output For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No". Example Input 4 0 1 1 1 8 1 9 3 0 0 0 0 1000000000 1000000000 1000000000 1000000000 Output No Yes Yes Yes Note In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome. In the second test case, after doing one operation, changing (8,1,9,3) to (7,0,8,6), one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr". A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome.
instruction
0
81,890
7
163,780
Tags: brute force, math Correct Solution: ``` t = int(input()) for _ in range(t): arr = list(map(int, input().split())) odd, even = 0, 0 for x in arr: if x%2 == 0: even += 1 else: odd += 1 if odd <= 1: print("Yes") continue if 0 in arr[:3]: print("No") continue arr[3] += 1 arr[2] -= 1; arr[1] -= 1; arr[0] -= 1 odd, even = 0, 0 for x in arr: if x%2 == 0: even += 1 else: odd += 1 if odd <= 1: print("Yes") else: print("No") ```
output
1
81,890
7
163,781
Provide tags and a correct Python 3 solution for this coding contest problem. Boboniu gives you * r red balls, * g green balls, * b blue balls, * w white balls. He allows you to do the following operation as many times as you want: * Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations. Input The first line contains one integer T (1≀ T≀ 100) denoting the number of test cases. For each of the next T cases, the first line contains four integers r, g, b and w (0≀ r,g,b,w≀ 10^9). Output For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No". Example Input 4 0 1 1 1 8 1 9 3 0 0 0 0 1000000000 1000000000 1000000000 1000000000 Output No Yes Yes Yes Note In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome. In the second test case, after doing one operation, changing (8,1,9,3) to (7,0,8,6), one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr". A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome.
instruction
0
81,891
7
163,782
Tags: brute force, math Correct Solution: ``` # -*- coding: utf-8 -*- """ Created on Wed Aug 12 20:45:19 2020 @author: Dark Soul """ t=int(input('')) arr=[] for i in range(t): arr.append(list(map(int,input().split()))) for i in arr: r=i[0] g=i[1] b=i[2] w=i[3] n1=0 for j in i: if j==1: n1+=1 if w==1: n1-=1 s=(r%2)+(g%2)+(b%2)+(w%2) if s==4 or s==0 or s==1: print('Yes') continue det=r and g and b if s==3 and det: print('Yes') continue print('No') ```
output
1
81,891
7
163,783
Provide tags and a correct Python 3 solution for this coding contest problem. Boboniu gives you * r red balls, * g green balls, * b blue balls, * w white balls. He allows you to do the following operation as many times as you want: * Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations. Input The first line contains one integer T (1≀ T≀ 100) denoting the number of test cases. For each of the next T cases, the first line contains four integers r, g, b and w (0≀ r,g,b,w≀ 10^9). Output For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No". Example Input 4 0 1 1 1 8 1 9 3 0 0 0 0 1000000000 1000000000 1000000000 1000000000 Output No Yes Yes Yes Note In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome. In the second test case, after doing one operation, changing (8,1,9,3) to (7,0,8,6), one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr". A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome.
instruction
0
81,892
7
163,784
Tags: brute force, math Correct Solution: ``` for _ in range(int(input())): a,b,c,d = map(int,input().split()) if( (a%2) + (b%2) + (c%2) + (d%2) <= 1 ): print("YES") elif (a>=1 and b>=1 and c>=1 and ( ((a-1)%2) + ((b-1)%2) + ((c-1)%2) + ((d+3)%2) <= 1 )): print("YES") else: print("NO") ```
output
1
81,892
7
163,785