text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide tags and a correct Python 3 solution for this coding contest problem. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Tags: *special Correct Solution: ``` cnt=0 t1=input() t2=input() for i in range(0,len(t1),2): p1=t1[i:i+2] p2=t2[i:i+2] if(p1=="8<"): if(p2=="[]"): cnt+=1 elif(p2=="()"): cnt-=1 elif(p1=="[]"): if(p2=="()"): cnt+=1 elif(p2=="8<"): cnt-=1 else: if(p2=="8<"): cnt+=1 elif(p2=="[]"): cnt-=1 if(cnt==0): print("TIE") elif(cnt>0): print("TEAM 1 WINS") else: print("TEAM 2 WINS") ```
7,000
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Submitted Solution: ``` def a(sMove1, sMove2): if sMove1 == "8<" and sMove2 == "[]" or sMove1 == "[]" and sMove2 == "()" or sMove1 == "()" and sMove2 == "8<": return 1 elif sMove2 == "8<" and sMove1 == "[]" or sMove2 == "[]" and sMove1 == "()" or sMove2 == "()" and sMove1 == "8<": return 2 else: return 0 t1 = input() t2 = input() s = [0, 0, 0] for i in range(0, len(t1), 2): s[a(t1[i] + t1[i + 1], t2[i] + t2[i + 1])] += 1 if s[1] > s[2]: print('TEAM 1 WINS') elif s[1] < s[2]: print("TEAM 2 WINS") else: print('TIE') ``` Yes
7,001
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Submitted Solution: ``` s1 = input() s2 = input() score1, score2 = 0, 0 for c in range(0, len(s1), 2): if s1[c] == '8': if s2[c] == '[': score1 += 1 if s2[c] == '(': score2 += 1 if s1[c] == '[': if s2[c] == '(': score1 += 1 if s2[c] == '8': score2 += 1 if s1[c] == '(': if s2[c] == '8': score1 += 1 if s2[c] == '[': score2 += 1 if score1 == score2: print ('TIE') else: print('TEAM {} WINS'.format(2 - int(score1 > score2))) ``` Yes
7,002
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Submitted Solution: ``` from math import* def winner(a,b): if a=="[]": if b=="()": return 1 elif b=="[]": return 0 else: return -1 elif a=="8<": if b=="()": return -1 elif b=="[]": return 1 else: return 0 else: if b=="()": return 0 elif b=="[]": return -1 else: return 1 s1=input() s2=input() i=0 ans=0 while i<len(s1): ans+=winner(s1[i:i+2],s2[i:i+2]) i+=2 if ans==0: print("TIE") elif ans<0: print("TEAM 2 WINS") else: print("TEAM 1 WINS") ``` Yes
7,003
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Submitted Solution: ``` s1 = input() s2 = input() def identify(figure): if figure == '8<': return 'scissors' elif figure == '[]': return 'paper' else: return 'stone' first, second = 0, 0 i = 0 while i < len(s1)-1: p1 = identify(s1[i:i+2]) p2 = identify(s2[i:i+2]) # print(p1, p2) if p1 == 'scissors': if p2 == 'paper': first += 1 elif p2 == 'stone': second += 1 elif p1 == 'paper': if p2 == 'stone': first += 1 elif p2 == 'scissors': second += 1 elif p1 == 'stone': if p2 == 'scissors': first += 1 elif p2 == 'paper': second += 1 i += 2 if first == second: print('TIE') elif first < second: print('TEAM 2 WINS') else: print('TEAM 1 WINS') ``` Yes
7,004
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Submitted Solution: ``` s = input() s1 = input() print("TIE") ``` No
7,005
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Submitted Solution: ``` a=input().split('<') b=input().split('<') ans=chk=0 for i in range(1,len(a)): if a[i-1][-1].isdigit() and len(a[i])>1 and (a[i][0]+a[i][1]=='()' or a[i][0]+a[i][1]=='[]'): ans+=1 for i in range(1,len(b)): if b[i-1][-1].isdigit() and len(b[i])>1 and (b[i][0]+b[i][1]=='()' or b[i][0]+b[i][1]=='[]'): chk+=1 print('TEAM 2 WINS') if chk>ans else ('TEAM 1 WINS') if ans<chk else ('TIE') ``` No
7,006
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Submitted Solution: ``` cnt=0 t1=input() t2=input() for i in range(0,len(t1),2): p1=t1[i:i+2] p2=t2[i:i+2] if(p1=="8<"): if(p2=="[]"): cnt+=1 elif(p2=="()"): cnt-=1 elif(p1=="[]"): if(p2=="()"): cnt+=1 elif(p1=="8<"): cnt-=1 else: if(p2=="8<"): cnt+=1 elif(p2=="[]"): cnt-=1 if(cnt==0): print("TIE") elif(cnt>0): print("TEAM 1 WINS") else: print("TEAM 2 WINS") ``` No
7,007
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. Output Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. Examples Input []()[]8&lt; 8&lt;[]()8&lt; Output TEAM 2 WINS Input 8&lt;8&lt;() []8&lt;[] Output TIE Submitted Solution: ``` def x(): s=input() return s.count("8<[]()") t = x()-x() print("TEAM %d WINS"%(1 if t>0 else 2) if t!=0 else "TIE") ``` No
7,008
Provide tags and a correct Python 3 solution for this coding contest problem. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Tags: implementation, math Correct Solution: ``` n,m,k=[int(x) for x in input().split()] park=[input() for i in range(n)] answers=[0]*m for inot in range(n): for j in range(m): if park[inot][j]!='.': i=(j,inot,park[inot][j]) if i[2]=='R': if i[0]+i[1]<m: answers[i[0]+i[1]]+=1 elif i[2]=='L': if i[0]-i[1]>=0: answers[i[0]-i[1]]+=1 elif i[2]=='U': if not i[1]&1: answers[i[0]]+=1 for i in answers: print(i,end=' ') ```
7,009
Provide tags and a correct Python 3 solution for this coding contest problem. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Tags: implementation, math Correct Solution: ``` class CodeforcesTask436BSolution: def __init__(self): self.result = '' self.n_m_k = [] self.field = [] def read_input(self): self.n_m_k = [int(x) for x in input().split(" ")] for x in range(self.n_m_k[0]): self.field.append(input()) def process_task(self): result_row = [0 for x in range(self.n_m_k[1])] for x in range(self.n_m_k[0]): for y in range(self.n_m_k[1]): # print(x, y) field = self.field[x][y] if field == "R": pos = y + x if pos < self.n_m_k[1]: result_row[pos] += 1 elif field == "L": pos = y - x if pos >= 0: result_row[pos] += 1 elif field == "U" and not x % 2: result_row[y] += 1 self.result = " ".join([str(x) for x in result_row]) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask436BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ```
7,010
Provide tags and a correct Python 3 solution for this coding contest problem. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Tags: implementation, math Correct Solution: ``` # -*- coding: utf-8 -*- import sys f = sys.stdin n, m, k = map(int, f.readline().strip().split()) s = f.readline().strip() sp = [0]*m for i in range(1, n): s = f.readline().strip() for l in range(m): if s[l]=='U': if i % 2 == 0: sp[l] += 1 elif s[l]=='R': mi = l+i if mi<m : sp[mi] += 1 elif s[l]=='L': mi = l-i if mi>=0 : sp[mi] += 1 #print(i, s, sp) print(' '.join([str(it) for it in sp])) ```
7,011
Provide tags and a correct Python 3 solution for this coding contest problem. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Tags: implementation, math Correct Solution: ``` def f(): n, m, k = map(int, input().split()) s = [] a = [] for i in range(n): s.append(input()) for j in range(m): ans = 0 for i in range(n): if i + i < n and s[i + i][j] == 'U': ans += 1 if j - i >= 0 and s[i][j - i] == 'R': ans += 1 if j + i < m and s[i][j + i] == 'L': ans += 1 a.append(ans) print(' '.join(map(str, a))) f() ```
7,012
Provide tags and a correct Python 3 solution for this coding contest problem. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Tags: implementation, math Correct Solution: ``` n, m, k = map(int, input().split()) field = [] counts = [] for j in range(m): counts.append(0) for i in range(n): row = list(input()) field.append(row) for i in range(n): for j in range(m): if field[i][j] == "L": jth = j - i if jth >= 0: counts[jth] += 1 elif field[i][j] == "R": jth = i + j if jth < m: counts[jth] += 1 elif field[i][j] == "U": if i % 2 == 0: counts[j] += 1 #counts = [] #for j in range(m): # count = 0 # for i in range(n): # if newfield[i][j] == "S": # count += 1 # counts.append(str(count)) #print(field) #print(newfield) print(" ".join(list(map(str, counts)))) ```
7,013
Provide tags and a correct Python 3 solution for this coding contest problem. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Tags: implementation, math Correct Solution: ``` #!/usr/bin/python import re import inspect from sys import argv, exit def rstr(): return input() def rint(): return int(input()) def rints(splitchar=' '): return [int(i) for i in input().split(splitchar)] def varnames(obj, namespace=globals()): return [name for name in namespace if namespace[name] is obj] def pvar(var, override=False): prnt(varnames(var), var) def prnt(*args, override=False): if '-v' in argv or override: print(*args) class Spider(): def __init__(self, x, y, d): self.x = x self.y = y self.d = d def get_copy(self): return Spider(self.x, self.y, self.d) def move(self): if self.d == 'R': self.x += 1 elif self.d == 'L': self.x -= 1 elif self.d == 'U': self.y -= 1 elif self.d == 'D': self.y += 1 else: raise Exception('WHAT HAVE YOU DONE', self.d) def main(i, n, m, lders, rders, uders): sightings = 0 iders = [s for s in uders if s.x == i and s.y % 2 == 0] sightings += len(iders) prnt('id', len(iders)) ulders = [s for s in rders if s.y == (i - s.x)] sightings += len(ulders) prnt('uld', len(ulders)) urders = [s for s in lders if s.y == (s.x - i)] sightings += len(urders) prnt('urd', len(urders)) return str(sightings) if __name__ == '__main__': (n,m,k) = rints() field = [rstr() for i in range(n)] si = [0 for i in range(m)] spiders = [] for j,row in enumerate(field): for i,space in enumerate(row): if space == 'R': if i+j < len(si): si[i+j] += 1 if space == 'L': if i-j >= 0: si[i-j] += 1 if space == 'U': if j % 2 == 0: si[i] += 1 print(' '.join([str(i) for i in si])) ```
7,014
Provide tags and a correct Python 3 solution for this coding contest problem. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Tags: implementation, math Correct Solution: ``` def solve(): answer = [0] * m for i in range(1, n): for j in range(m): if j - i >= 0: if park[i][j - i] == 'R': answer[j] += 1 if j + i < m: if park[i][j + i] == 'L': answer[j] += 1 if 2 * i < n: if park[2 * i][j] == 'U': answer[j] += 1 return answer n, m, k = tuple(map(int, input().split())) park = [input() for i in range(n)] print(' '.join(map(str, solve()))) ```
7,015
Provide tags and a correct Python 3 solution for this coding contest problem. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Tags: implementation, math Correct Solution: ``` n, m, k = map(int, str.split(input())) f = tuple(map(lambda _: str.strip(input()), range(n))) r = [] for x in range(m): cr = sum(map(lambda y: f[y][x] == "U", range(0, n, 2))) for cx in range(max(0, x + 1 - n), x): cr += f[x - cx][cx] == "R" for cx in range(x + 1, min(m, n + x)): cr += f[cx - x][cx] == "L" r.append(cr) print(str.join(" ", map(str, r))) ```
7,016
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Submitted Solution: ``` n, m, k = map(int, input().split()) input() s = [0] * m for i in range(1, n): for j, x in enumerate(input()): if x == 'L': if j - i >= 0: s[j - i] += 1 elif x == 'R': if j + i < m: s[j + i] += 1 elif x == 'U' and i & 1 == 0: s[j] += 1 print(' '.join(map(str, s))) ``` Yes
7,017
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Submitted Solution: ``` I = lambda:map(int,input().split()) n,m,k = I() f = [['.' for _ in range(m)] for _ in range(n)] for i in range(n): s = input() for j in range(len(s)): if s[j] != '.': f[i][j] = s[j] ans = [0] * m for i in range(n): for j in range(m): if j - i >= 0 and f[i][j - i] == 'R': ans[j] += 1 if j + i < m and f[i][j + i] == 'L': ans[j] += 1 if f[0][j] == 'D': ans[j] += 1 if i + i < n and f[i + i][j] == 'U': ans[j] += 1 print (*ans) ``` Yes
7,018
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Submitted Solution: ``` n, m, k = map( int, input().split() ) ans = [0] * m for i in range( n ): field = input() for j in range( m ): if ( field[j] == 'U' ) and ( i % 2 == 0 ): ans[j] += 1 elif ( field[j] == 'L' ) and ( j >= i ): ans[j - i] += 1 elif ( field[j] == 'R' ) and ( j + i < m ): ans[j + i] += 1 print ( ' '.join( map( str, ans ) ) ) ``` Yes
7,019
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Submitted Solution: ``` n, m, k = map( int, input().split() ) ans = [0] * m for i in range( n ): field = input() for j in range( m ): if ( field[j] == 'U' ) and ( i % 2 == 0 ): ans[j] += 1 elif ( field[j] == 'L' ) and ( j >= i ): ans[j - i] += 1 elif ( field[j] == 'R' ) and ( j + i < m ): ans[j + i] += 1 print ( ' '.join( map( str, ans ) ) ) # Made By Mostafa_Khaled ``` Yes
7,020
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Submitted Solution: ``` class CodeforcesTask436BSolution: def __init__(self): self.result = '' self.n_m_k = [] self.field = [] def read_input(self): self.n_m_k = [int(x) for x in input().split(" ")] for x in range(self.n_m_k[0]): self.field.append(input()) def process_task(self): result_row = [0 for x in range(self.n_m_k[1])] for x in range(self.n_m_k[0]): for y in range(self.n_m_k[1]): # print(x, y) field = self.field[x][y] if field == "R": for j in range(y + x, min(self.n_m_k[1], y + 1 + x)): result_row[j] += 1 elif field == "L": for j in range(max(0, y - x), y): result_row[j] += 1 elif field == "U" and x > 1: result_row[y] += 1 self.result = " ".join([str(x) for x in result_row]) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask436BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ``` No
7,021
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Submitted Solution: ``` def f(): n, m, k = map(int, input().split()) a = [] for i in range(n): a.append(list(input().strip())) res = [0 for i in range(n)] for i in range(m): for j in range(1, n): if j >= i and a[j][i - j] == 'R': res[i] += 1 if j < n - i and a[j][i + j] == 'L': res[i] += 1 if j + j < m and a[j + j][i] == 'U': res[i] += 1 print(' '.join(list(map(str, res)))) f() ``` No
7,022
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Submitted Solution: ``` # -*- coding: utf-8 -*- import sys f = sys.stdin n, m, k = map(int, f.readline().strip().split()) sp = [0]*m for i in range(n): s = f.readline().strip() for l in range(m): if s[l]=='U' and i>1: sp[l] += 1 elif s[l]=='R': mi = l+i if mi<m : sp[mi] += 1 elif s[l]=='L': mi = l-i if mi>=0 : sp[mi] += 1 print(' '.join([str(it) for it in sp])) ``` No
7,023
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n Γ— m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time. Om Nom isn't yet sure where to start his walk from but he definitely wants: * to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); * to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries. Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. Input The first line contains three integers n, m, k (2 ≀ n, m ≀ 2000; 0 ≀ k ≀ m(n - 1)). Each of the next n lines contains m characters β€” the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. Output Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. Examples Input 3 3 4 ... R.L R.U Output 0 2 2 Input 2 2 2 .. RL Output 1 1 Input 2 2 2 .. LR Output 0 0 Input 3 4 8 .... RRLL UUUU Output 1 3 3 1 Input 2 2 2 .. UU Output 0 0 Note Consider the first sample. The notes below show how the spider arrangement changes on the field over time: ... ... ..U ... R.L -> .*U -> L.R -> ... R.U .R. ..R ... Character "*" represents a cell that contains two spiders at the same time. * If Om Nom starts from the first cell of the first row, he won't see any spiders. * If he starts from the second cell, he will see two spiders at time 1. * If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. Submitted Solution: ``` n,m,k=map(int,input().split()) seen = [0 for _ in range(m)] for i in range(n): S = list(input()) for j in range(m): if S[j] == 'U': seen[j]+=1 elif S[j] == 'L' and j-i>=0: seen[j-i]+=1 elif S[j] == 'R' and j+i<m: seen[j+i]+=1 print(*seen, sep=" ") ``` No
7,024
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Tags: combinatorics, constructive algorithms, math Correct Solution: ``` a = input() b = a.split() n = int(b[0]) k = int(b[1]) d = int(b[2]) raw = [' ']*n if n > k**d: print(-1) exit() table = [[1]*d] p = 1 for i in range(d): cv = 1 for j in range(n): if j % p == 0 and j != 0: cv = cv + 1 if cv == k+1: cv = 1 raw[j]= str(cv) print(' '.join(raw)) p = p * k ```
7,025
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Tags: combinatorics, constructive algorithms, math Correct Solution: ``` import os import sys from io import BytesIO, IOBase import math def main(): pass # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") import os import sys from io import BytesIO, IOBase def main(): pass # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") n,k,d=map(int,input().split()) if(n>k**d): print(-1) else: ans=[i for i in range(0,n)] for i in range(0,d): for j in range(0,n): print(ans[j]%k+1,end=" ") ans[j]//=k print("") ```
7,026
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Tags: combinatorics, constructive algorithms, math Correct Solution: ``` #http://codeforces.com/problemset/problem/459/C # Input of the values n, k, d = map(int, input().split()) #Convention : Busses are numbered from 1 to k # Students are numbered from 0 to k - 1 # Days are numbered from 0 to k - 1 # Variable for success success = True # answer is a 2-d array that stores the bus number each student used on a particular day # Students are numbered from 0 to n-1 answer = [[] for x in range(n)] # Initialising the first students details manually # The first student uses the bus 1 for all of the days answer[0] = [1 for x in range(d)] # we will be filling the details for each and every student and # would print -1 only if we cannot allot any bus number to the next student for i_th_student in range(1,n): #answer for previous value i_minus_one_student = answer[i_th_student - 1] # Answer to the current student current_student = list(i_minus_one_student) # Days are also numbered from 0 to d-1 for i_th_day in reversed(range(d)): # the condition to change the bus number if i_minus_one_student[i_th_day] < k : current_student[i_th_day] += 1 # all the numbers next to it are reset for i in range(i_th_day + 1, d): current_student[i] = 1 break #Save the value answer[i_th_student] = current_student #Failed if current_student == i_minus_one_student: success = False break # printing the output if not(success): print ("-1") else: # ans_trans is used for giving the output fast answer_trans= [[] for i in range(d)] for y in range(n): i = 0 for x in answer[y]: answer_trans[i].append(x) i += 1 for bla in answer_trans: print (' '.join(map(str, bla))) ```
7,027
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Tags: combinatorics, constructive algorithms, math Correct Solution: ``` import time,math,bisect,sys sys.setrecursionlimit(100000) from sys import stdin,stdout from collections import deque from fractions import Fraction from collections import Counter from collections import OrderedDict pi=3.14159265358979323846264338327950 def II(): # to take integer input return int(stdin.readline()) def IO(): # to take string input return stdin.readline() def IP(): # to take tuple as input return map(int,stdin.readline().split()) def L(): # to take list as input return list(map(int,stdin.readline().split())) def P(x): # to print integer,list,string etc.. return stdout.write(str(x)+"\n") def PI(x,y): # to print tuple separatedly return stdout.write(str(x)+" "+str(y)+"\n") def lcm(a,b): # to calculate lcm return (a*b)//gcd(a,b) def gcd(a,b): # to calculate gcd if a==0: return b elif b==0: return a if a>b: return gcd(a%b,b) else: return gcd(a,b%a) def readTree(): # to read tree v=int(input()) adj=[set() for i in range(v+1)] for i in range(v-1): u1,u2=In() adj[u1].add(u2) adj[u2].add(u1) return adj,v def bfs(adj,v): # a schema of bfs visited=[False]*(v+1) q=deque() while q: pass def sieve(): li=[True]*1000001 li[0],li[1]=False,False for i in range(2,len(li),1): if li[i]==True: for j in range(i*i,len(li),i): li[j]=False prime=[] for i in range(1000001): if li[i]==True: prime.append(i) return prime def setBit(n): count=0 while n!=0: n=n&(n-1) count+=1 return count ##################################################################################### mx=10**9+7 def solve(): n,k,d=IP() if n>pow(k,d): print(-1) return else: li=[[0 for j in range(n)] for i in range(d)] student=0 for i in range(1,n): for j in range(d): li[j][i]=li[j][i-1] for j in range(d): li[j][i]=(li[j][i]+1)%k if li[j][i]: break for i in range(d): for j in range(n): print(int(li[i][j])+1,end=" ") print() return #t=II() #for i in range(t): solve() ####### # # ####### # # # #### # # # # # # # # # # # # # # # #### # # #### #### # # ###### # # #### # # # # # ```
7,028
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Tags: combinatorics, constructive algorithms, math Correct Solution: ``` from sys import stdin, stdout, setrecursionlimit input = stdin.readline # import string # characters = string.ascii_lowercase # digits = string.digits # setrecursionlimit(int(1e6)) # dir = [-1,0,1,0,-1] # moves = 'NESW' inf = float('inf') from functools import cmp_to_key from collections import defaultdict as dd from collections import Counter, deque from heapq import * import math from math import floor, ceil, sqrt def geti(): return map(int, input().strip().split()) def getl(): return list(map(int, input().strip().split())) def getis(): return map(str, input().strip().split()) def getls(): return list(map(str, input().strip().split())) def gets(): return input().strip() def geta(): return int(input()) def print_s(s): stdout.write(s+'\n') def solve(): n, k, d = geti() num = 1 for i in range(1, d+1): num *= k if num >= n: break else: print(-1) return def convert(prev): ans = prev[:] i = d - 1 ans[i] += 1 while ans[i] > k and i > 0: ans[i-1] += 1 ans[i] = 1 i -= 1 return ans ans = [[1]*d] for i in range(1, n+1): ans.append(convert(ans[i-1])) # print(ans) for i in range(d): print(*[ans[j][i] for j in range(n)]) # 1 2 1 2 1 2 2 # 2 1 2 1 1 2 2 # 1 2 2 1 1 2 1 # # # 2 1 1 1 1 2 1 1 # 1 2 1 1 1 2 2 1 # 1 1 2 1 1 1 2 1 # 1 1 1 2 1 1 1 1 # 1 1 1 1 2 1 1 1 if __name__=='__main__': solve() ```
7,029
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Tags: combinatorics, constructive algorithms, math Correct Solution: ``` def f(t): i = -1 t[i] += 1 while t[i] > k: t[i] = 1 i -= 1 t[i] += 1 return list(map(str, t)) n, k, d = map(int, input().split()) if k ** d < n: print(-1) else: t = [1] * d t[-1] = 0 s = [f(t) for i in range(n)] print('\n'.join([' '.join(t) for t in zip(*s)])) ```
7,030
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Tags: combinatorics, constructive algorithms, math Correct Solution: ``` n,k,d = map(int, input().split()) if (n > k ** d): print(-1) exit() for i in range(d): power = k**i resArray = [((j // power) % k +1) for j in range(n)] #for j in range(n//(k**i)+1): # resArray.extend([(j % k) + 1] * (k ** i)) print(' '.join(map(str,resArray))) ```
7,031
Provide tags and a correct Python 3 solution for this coding contest problem. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Tags: combinatorics, constructive algorithms, math Correct Solution: ``` # -*- coding: utf-8 -*- import math import collections import bisect import heapq import time import random import itertools import sys from typing import List """ created by shhuan at 2020/1/8 21:47 """ def bit(val, n, l): x = [] while val > 0: x.append(val % n) val //= n return [0] * (l - len(x)) + x[::-1] def solve(N, K, D): if math.log(N, K) > D or K ** D < N: print(-1) return a = [] for i in range(N): x = bit(i, K, D) a.append(x) ans = [] for d in range(D): ans.append(' '.join(map(str, [a[i][d] + 1 for i in range(N)]))) print('\n'.join(ans)) N, K, D = map(int, input().split()) solve(N, K, D) ```
7,032
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Submitted Solution: ``` #!/usr/bin/python3 import sys n, k, d = list(map(int, sys.stdin.readline().split())) x = 1 while x ** d < n: x += 1 if x > k: sys.stdout.write("-1\n") sys.exit(0) ans = [[1 for i in range(d)] for j in range(n)] for i in range(1, n): for j in range(d): ans[i][j] = ans[i - 1][j] ans[i][d - 1] += 1 memo = 0 for j in range(d - 1, -1, -1): ans[i][j] += memo memo = 0 if ans[i][j] > x: memo = ans[i][j] - x ans[i][j] = 1 for i in range(d): sys.stdout.write(' '.join([str(ans[j][i]) for j in range(n)]) + '\n') ``` Yes
7,033
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Submitted Solution: ``` n, k, d = map(int, input().split()) bits = n for i in range(d): bits = bits//k + (bits % k != 0) if bits > 1: print("-1") exit() for _ in range(d): i = 0 res = 1 while i < n: for j in range(bits): print(res, end=' ') i += 1 if i == n: break res += 1 if res > k: res = 1 bits *= k print() ``` Yes
7,034
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Submitted Solution: ``` #!/usr/bin/python3 import sys # 1 <= n, d <= 1000, 1 <= k <= 10**9 n, k, d = map(int, sys.stdin.readline().split()) no_sol = False solution = [[1 for j in range(n)] for i in range(d)] def schedule(i, j, level): global no_sol if level >= d: no_sol = True return count = j - i chunk = count // k extra = count % k r = i for t in range(min(k, count)): size = chunk + (1 if t < extra else 0) for z in range(size): solution[level][r+z] = t+1 if size > 1: schedule(r, r + size, level + 1) r += size if k == 1: if n > 1: no_sol = True else: schedule(0, n, 0) if no_sol: print(-1) else: for l in solution: print(' '.join(str(x) for x in l)) ``` Yes
7,035
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Submitted Solution: ``` import time,math,bisect,sys sys.setrecursionlimit(100000) from sys import stdin,stdout from collections import deque from fractions import Fraction from collections import Counter from collections import OrderedDict pi=3.14159265358979323846264338327950 def II(): # to take integer input return int(stdin.readline()) def IO(): # to take string input return stdin.readline() def IP(): # to take tuple as input return map(int,stdin.readline().split()) def L(): # to take list as input return list(map(int,stdin.readline().split())) def P(x): # to print integer,list,string etc.. return stdout.write(str(x)+"\n") def PI(x,y): # to print tuple separatedly return stdout.write(str(x)+" "+str(y)+"\n") def lcm(a,b): # to calculate lcm return (a*b)//gcd(a,b) def gcd(a,b): # to calculate gcd if a==0: return b elif b==0: return a if a>b: return gcd(a%b,b) else: return gcd(a,b%a) def readTree(): # to read tree v=int(input()) adj=[set() for i in range(v+1)] for i in range(v-1): u1,u2=In() adj[u1].add(u2) adj[u2].add(u1) return adj,v def bfs(adj,v): # a schema of bfs visited=[False]*(v+1) q=deque() while q: pass def sieve(): li=[True]*1000001 li[0],li[1]=False,False for i in range(2,len(li),1): if li[i]==True: for j in range(i*i,len(li),i): li[j]=False prime=[] for i in range(1000001): if li[i]==True: prime.append(i) return prime def setBit(n): count=0 while n!=0: n=n&(n-1) count+=1 return count ##################################################################################### mx=10**9+7 def solve(): n,k,d=IP() if n>pow(k,d): print(-1) return elif d==1: li=[i for i in range(1,n+1)] print(*li) else: li=[[0 for j in range(n)] for i in range(d)] student=0 for i in range(1,n): for j in range(d): li[j][i]=li[j][i-1] for j in range(d): li[j][i]=(li[j][i]+1)%k if li[j][i]: break for i in range(d): for j in range(n): print(int(li[i][j])+1,end=" ") print() return #t=II() #for i in range(t): solve() ####### # # ####### # # # #### # # # # # # # # # # # # # # # #### # # #### #### # # ###### # # #### # # # # # ``` Yes
7,036
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Submitted Solution: ``` import sys input=sys.stdin.readline n,k,d=map(int,input().split()) if k==1: print(-1) exit() size=n for i in range(d): size=size//k+(size%k!=0) if size>1: print(-1) else: x=1 for i in range(d): ans=[1+i//x%k for i in range(n)] print(*ans) x*=k ``` No
7,037
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Submitted Solution: ``` # -*- coding: utf-8 -*- import math import collections import bisect import heapq import time import random import itertools import sys from typing import List """ created by shhuan at 2020/1/8 21:47 """ def bit(val, n, l): x = [] while val > 0: x.append(val % n) val //= n return [0] * (l - len(x)) + x[::-1] def solve(N, K, D): if math.log(N, K) > D or K ** D < N: print(-1) return a = [] for i in range(1, N+1): x = bit(i, K, D) a.append(x) ans = [] for d in range(D): ans.append(' '.join(map(str, [a[i][d] + 1 for i in range(N)]))) print('\n'.join(ans)) N, K, D = map(int, input().split()) solve(N, K, D) ``` No
7,038
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Submitted Solution: ``` m=list(input().split()) n=int(m[0]) k=int(m[1]) d=int(m[2]) if n==1: for i in range(d): print("1") exit() if k==1: print("-1") exit() if k>=n: m=list(i for i in range(n)) for i in range(d): #print(m) o=str(m[0]) for j in range(1,n): o+=" "+str(m[j]) print(o) exit() if d==1: print('-1') exit() if d>=n: m=list("1" for i in range(n)) for i in range(n): m[i]="2" #print(m) o=str(m[0]) for j in range(1,n): o+=" "+str(m[j]) print(o) #m[i]="1" for i in range(n,d): #print(m) print(o) exit() from math import ceil, floor tc=ceil(n/k) #days tf=floor(n/k) #len k if d<tf: print('-1') exit() sc=1 for stp in range(1,d+1): o="" kc=0 while kc<n: for kk in range(1,k+1): for i in range (sc): if kc<n: o+=' '+str(kk) kc+=1 else: break sc*=2 print(o[1:]) ``` No
7,039
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers n, k, d (1 ≀ n, d ≀ 1000; 1 ≀ k ≀ 109). Output If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. Submitted Solution: ``` import time,math,bisect,sys sys.setrecursionlimit(100000) from sys import stdin,stdout from collections import deque from fractions import Fraction from collections import Counter from collections import OrderedDict pi=3.14159265358979323846264338327950 def II(): # to take integer input return int(stdin.readline()) def IO(): # to take string input return stdin.readline() def IP(): # to take tuple as input return map(int,stdin.readline().split()) def L(): # to take list as input return list(map(int,stdin.readline().split())) def P(x): # to print integer,list,string etc.. return stdout.write(str(x)+"\n") def PI(x,y): # to print tuple separatedly return stdout.write(str(x)+" "+str(y)+"\n") def lcm(a,b): # to calculate lcm return (a*b)//gcd(a,b) def gcd(a,b): # to calculate gcd if a==0: return b elif b==0: return a if a>b: return gcd(a%b,b) else: return gcd(a,b%a) def readTree(): # to read tree v=int(input()) adj=[set() for i in range(v+1)] for i in range(v-1): u1,u2=In() adj[u1].add(u2) adj[u2].add(u1) return adj,v def bfs(adj,v): # a schema of bfs visited=[False]*(v+1) q=deque() while q: pass def sieve(): li=[True]*1000001 li[0],li[1]=False,False for i in range(2,len(li),1): if li[i]==True: for j in range(i*i,len(li),i): li[j]=False prime=[] for i in range(1000001): if li[i]==True: prime.append(i) return prime def setBit(n): count=0 while n!=0: n=n&(n-1) count+=1 return count ##################################################################################### mx=10**9+7 def solve(): n,k,d=IP() if n>k**d: print(-1) return else: li=[[0 for j in range(n)] for i in range(d)] student=0 for i in range(n): x=bin(i)[2::].rjust(d,'0') for j in range(d): li[j][student]=x[j] student+=1 for i in range(d): for j in range(n): print(int(li[i][j])+1,end=" ") print() return #t=II() #for i in range(t): solve() ####### # # ####### # # # #### # # # # # # # # # # # # # # # #### # # #### #### # # ###### # # #### # # # # # ``` No
7,040
Provide tags and a correct Python 3 solution for this coding contest problem. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Tags: binary search, greedy, implementation Correct Solution: ``` def main(): from bisect import bisect_left n, l, x, y = map(int, input().split()) aa, d = list(map(int, input().split())), {} for z in (x, y, y + x): for a in aa: a += z if a > l: break b = aa[bisect_left(aa, a)] if b <= a: if b == a: d[z] = a break if len(d) == 2: break if d: if x in d and y in d: res = [] elif x in d: res = [y] elif y in d: res = [x] elif y + x in d: res = [d[y + x] - y] else: z, tmp = y - x, [] for a in aa: a += z if a > l: break b = aa[bisect_left(aa, a)] if b == a: tmp.append(a) for a in tmp: if a > y: res = [a - y] break elif a + x < l: res = [a + x] break else: res = [x, y] print(len(res)) if res: print(*res) if __name__ == '__main__': main() ```
7,041
Provide tags and a correct Python 3 solution for this coding contest problem. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Tags: binary search, greedy, implementation Correct Solution: ``` import itertools import math def can_measure(a, d): return any(i + d in a for i in a) def main(): n, l, x, y = map(int, input().split()) a = set(map(int, input().split())) can_x = can_measure(a, x) can_y = can_measure(a, y) if can_x and can_y: print(0) elif can_x: print(1) print(y) elif can_y: print(1) print(x) else: for i in a: if i + x + y in a: print(1) print(i + x) break else: t = i + x - y in a if 0 <= i + x <= l and t: print(1) print(i + x) break; if 0 <= i - y <= l and t: print(1) print(i - y) break; else: print(2) print(x, y) if __name__ == "__main__": main() ```
7,042
Provide tags and a correct Python 3 solution for this coding contest problem. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Tags: binary search, greedy, implementation Correct Solution: ``` def main(): import sys tokens = [int(i) for i in sys.stdin.read().split()] tokens.reverse() n, l, x, y = [tokens.pop() for i in range(4)] marks = set(tokens) flag_x = flag_y = False index = -1 for i in marks: if i + x in marks: flag_x = True index = y if i + y in marks: flag_y = True index = x if i + x + y in marks: index = i + x if i + y - x in marks and i - x >= 0: index = i - x if i + y - x in marks and i + y <= l: index = i + y if flag_x and flag_y: print(0) elif index != -1: print(1) print(index) else: print(2) print(x, y) main() ```
7,043
Provide tags and a correct Python 3 solution for this coding contest problem. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Tags: binary search, greedy, implementation Correct Solution: ``` class CodeforcesTask480BSolution: def __init__(self): self.result = '' self.n_l_x_y = [] self.ruler = [] def read_input(self): self.n_l_x_y = [int(x) for x in input().split(" ")] self.ruler = [int(x) for x in input().split(" ")] def process_task(self): dists = {} for a in self.ruler: dists[a] = True hasx = False hasy = False for a in self.ruler: try: if dists[a - self.n_l_x_y[2]]: hasx = True except KeyError: pass try: if dists[a + self.n_l_x_y[2]]: hasx = True except KeyError: pass try: if dists[a - self.n_l_x_y[3]]: hasy = True except KeyError: pass try: if dists[a - self.n_l_x_y[3]]: hasy = True except KeyError: pass if hasx and hasy: break if hasx and hasy: self.result = "0" elif hasx: self.result = "1\n{0}".format(self.n_l_x_y[3]) elif hasy: self.result = "1\n{0}".format(self.n_l_x_y[2]) else: res = [0, 0] sgn = False dst = self.n_l_x_y[2] + self.n_l_x_y[3] diff = self.n_l_x_y[3] - self.n_l_x_y[2] for a in self.ruler: try: if dists[a - dst]: if a - self.n_l_x_y[2] > 0: sgn = True res = a - self.n_l_x_y[2] except KeyError: pass try: if dists[a + dst]: if a + self.n_l_x_y[2] < self.n_l_x_y[1]: sgn = True res = a + self.n_l_x_y[2] except KeyError: pass try: if dists[a - diff]: if a + self.n_l_x_y[2] < self.n_l_x_y[1]: sgn = True res = a + self.n_l_x_y[2] except KeyError: pass try: if dists[a + diff]: if a - self.n_l_x_y[2] > 0: sgn = True res = a - self.n_l_x_y[2] except KeyError: pass if sgn: break if sgn: self.result = "1\n{0}".format(res) else: self.result = "2\n{0} {1}".format(self.n_l_x_y[2], self.n_l_x_y[3]) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask480BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ```
7,044
Provide tags and a correct Python 3 solution for this coding contest problem. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Tags: binary search, greedy, implementation Correct Solution: ``` n, l, x, y = map(int, input().split()) s = set(map(int, input().split())) def f(d): return any(i + d in s for i in s) def g(): for i in s: if i + x + y in s: return i + x return 0 def h(): for i in s: if i + y - x in s: if i - x >= 0: return i - x if i + y <= l: return i + y return 0 def e(d): print(1) print(d) if f(x): if f(y): print(0) else: e(y) elif f(y): e(x) else: z = g() if z: e(z) else: z = h() if z: e(z) else: print(2) print(x, y) ```
7,045
Provide tags and a correct Python 3 solution for this coding contest problem. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Tags: binary search, greedy, implementation Correct Solution: ``` from collections import defaultdict class LongJumps(): def __init__(self, n, l, x, y, a): self.n, self.l, self.x, self.y, self.a = n,l,x,y,a def get_markers(self): st = defaultdict(set) req_pts = [self.x,self.y] exist_check = defaultdict(bool) value_check = defaultdict(bool) for v in self.a: exist_check[v] = True for v in self.a: for i in range(len(req_pts)): if v - req_pts[i] >= 0: st[v - req_pts[i]].add(i) if exist_check[v - req_pts[i]]: value_check[i] = True if v + req_pts[i] <= l: st[v+req_pts[i]].add(i) if exist_check[v + req_pts[i]]: value_check[i] = True if value_check[0] and value_check[1]: print(0) return sol_status = 2 status1_marker = None for v in st: if len(st[v]) == 2: sol_status = 1 status1_marker = v elif len(st[v]) == 1: if exist_check[v]: sol_status = 1 status1_marker = req_pts[1-st[v].pop()] if sol_status == 1: print(1) print(status1_marker) return else: print(2) print(x, y) n, l, x, y = list(map(int,input().strip(' ').split(' '))) a = list(map(int,input().strip(' ').split(' '))) lj = LongJumps(n,l,x,y,a) lj.get_markers() ```
7,046
Provide tags and a correct Python 3 solution for this coding contest problem. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Tags: binary search, greedy, implementation Correct Solution: ``` n, l, x, y = map(int, input().split()) def f(t, q): i = j = 0 while j < n: d = t[j] - t[i] if d < q: j += 1 elif d > q: i += 1 else: return 0 return q def g(t): q = x + y i = j = 0 while j < n: d = t[j] - t[i] if d < q: j += 1 elif d > q: i += 1 else: return t[j] return 0 def h(t): q = y - x i = j = 0 while j < n: d = t[j] - t[i] if d < q: j += 1 elif d > q: i += 1 else: a, b = t[i] - x, t[j] + x if a >= 0: return [a] if b <= l:return [b] j += 1 return [x, y] def e(t): print(len(t)) print(' '.join(map(str, t))) t = list(map(int, input().split())) t.sort() x = f(t, x) y = f(t, y) if x and y: z = g(t) if z: e([z - y]) else: e(h(t)) elif x: e([x]) elif y: e([y]) else: e([]) ```
7,047
Provide tags and a correct Python 3 solution for this coding contest problem. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Tags: binary search, greedy, implementation Correct Solution: ``` import itertools import math def can_measure(a, d): return any(i + d in a for i in a) def main(): n, l, x, y = map(int, input().split()) a = set(map(int, input().split())) can_x = can_measure(a, x) can_y = can_measure(a, y) if can_x and can_y: print(0) elif can_x: print(1) print(y) elif can_y: print(1) print(x) else: for i in a: if i + x + y in a: print(1) print(i + x) break else: t = i + x - y in a if 0 <= i + x <= l and t: print(1) print(i + x) break; if 0 <= i - y <= l and t: print(1) print(i - y) break; else: print(2) print(x, y) if __name__ == "__main__": main() # Made By Mostafa_Khaled ```
7,048
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Submitted Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): for i in arr: stdout.write(str(i)+' ') stdout.write('\n') range = xrange # not for python 3.0+ # main code n,l1,x,y=in_arr() l=in_arr() d=Counter(l) f1=0 f2=0 for i in range(n): if d[l[i]+x]: f1=1 if d[l[i]+y]: f2=1 if f1 and f2: print 0 elif f1 or f2: print 1 if f2: print x else: print y else: f=0 for i in range(n): if l[i]+x<=l1 and ( d[l[i]+x+y] or d[l[i]+x-y]): print 1 #print 'a' print l[i]+x exit() break if l[i]-x>=0 and (d[l[i]+y-x] or d[l[i]-y-x]): print 1 print l[i]-x exit() print 2 print x,y ``` Yes
7,049
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Submitted Solution: ``` # -*- coding: utf-8 -*- import math import collections import bisect import heapq import time import random import itertools import sys from typing import List """ created by shhuan at 2020/1/13 20:48 """ def solve(N, L, X, Y, A): vs = set(A) mx = any([a+X in vs for a in A]) my = any([a+Y in vs for a in A]) if mx and my: print(0) elif mx: print(1) print(Y) elif my: print(1) print(X) else: # try to add 1 mark for a in vs: for b, c in [(a + X, Y), (a + Y, X), (a - X, Y), (a - Y, X)]: if 0 <= b <= L: if (b + c <= L and b + c in vs) or (b - c >= 0 and b - c in vs): print(1) print(b) return # add 2 marks print(2) print('{} {}'.format(X, Y)) N, L, X, Y = map(int, input().split()) A = [int(x) for x in input().split()] solve(N, L, X, Y, A) ``` Yes
7,050
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Submitted Solution: ``` def main(): n,l,x,y = map(int,input().split()) arr = set(map(int,input().split())) first = False second = False for i in arr: if i+x in arr: first = True if i+y in arr: second = True if first and not second: print(1) print(y) return if second and not first: print(1) print(x) return if first and second: print(0) return found = False for i in arr: if i+x-y in arr and i+x <= l: found = True coord = i+x break if i+y-x in arr and i+y <= l: found = True coord = i+y break if i+x+y in arr and i+min(x,y) <= l: found = True coord = i+min(x,y) if i-x-y in arr and i-max(x,y) >= 0: found = True coord = i-max(x,y) if i-x+y in arr and i-x >= 0: found = True coord = i-x break if i-y+x in arr and i-y >= 0: found = True coord = i-y break if found: break if found: print(1) print(coord) return print(2) print(x,y) main() ``` Yes
7,051
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Submitted Solution: ``` def main(): import sys tokens = [int(i) for i in sys.stdin.read().split()] tokens.reverse() n, l, x, y = [tokens.pop() for i in range(4)] marks = set(tokens) x_index = y_index = sum_index = sub_index1 = sub_index2 = -1 for i in marks: if i + x in marks: x_index = y if i + y in marks: y_index = x if i + x + y in marks: sum_index = i + x if i + y - x in marks and i - x >= 0: sub_index1 = i - x if i + y - x in marks and i + y <= l: sub_index2 = i + y if x_index != -1 and y_index != -1: print(0) else: for i in (x_index, y_index, sum_index, sub_index1, sub_index2): if i != -1: print(1) print(i) break else: print(2) print(x, y) main() ``` Yes
7,052
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Submitted Solution: ``` n, l, x, y = map(int, input().split()) a = set(map(int, input().split())) ok1 = ok2 = ok3 = False for c in a: if c + x in a: ok1 = True if c + y in a: ok2 = True if c - x > 0 and c - x + y in a: ok3 = True mark = c - x if c + x < l and c + x - y in a: ok3 = True mark = c + x if c + x + y in a: ok3 = True mark = c + x if c - x - y in a: ok3 = True mark = c - x if ok1 and ok2: print(0) elif (not ok1) and (not ok2): if ok3: print(1) print(mark) else: print(2) print(x, y) else: print(1) print(y if ok1 else x) ``` Yes
7,053
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Submitted Solution: ``` if __name__ == "__main__": n, l, x, y = list(map(int, input().split())) v = list(map(int, input().split())) s = set(v) cx = 0 for i in range(n): if v[i]+x in s: cx = 1 break cy = 0 for i in range(n): if v[i]+y in s: cy = 1 break count = 0 ans = [] if cx==0: count += 1 ans.append(x) if cy==0: count += 1 ans.append(y) if count==2: for i in range(n): if (v[i]+x+y in s): count = 1 ans = [v[i]+x] break if count==2: for i in range(n): if (v[i]+x-y in s): if v[i]+x<=l: count = 1 ans = [v[i]+x] break elif v[i]-y<=l: count = 1 ans = [v[i]-y] break print(count) if count!=0: print(*ans) ``` No
7,054
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Submitted Solution: ``` class CodeforcesTask480BSolution: def __init__(self): self.result = '' self.n_l_x_y = [] self.ruler = [] def read_input(self): self.n_l_x_y = [int(x) for x in input().split(" ")] self.ruler = [int(x) for x in input().split(" ")] def process_task(self): dists = {} for a in self.ruler: dists[a] = True hasx = False hasy = False for a in self.ruler: try: if dists[a - self.n_l_x_y[2]]: hasx = True except KeyError: pass try: if dists[a + self.n_l_x_y[2]]: hasx = True except KeyError: pass try: if dists[a - self.n_l_x_y[3]]: hasy = True except KeyError: pass try: if dists[a - self.n_l_x_y[3]]: hasy = True except KeyError: pass if hasx and hasy: break if hasx and hasy: self.result = "0" elif hasx: self.result = "1\n{0}".format(self.n_l_x_y[3]) elif hasy: self.result = "1\n{1}".format(self.n_l_x_y[2]) else: res = [0, 0] sgn = False dst = self.n_l_x_y[2] + self.n_l_x_y[3] diff = self.n_l_x_y[3] - self.n_l_x_y[2] for a in self.ruler: try: if dists[a - dst]: sgn = True res = a - self.n_l_x_y[2] except KeyError: pass try: if dists[a + dst]: sgn = True res = a + self.n_l_x_y[2] except KeyError: pass try: if dists[a - diff]: sgn = True res = a + self.n_l_x_y[2] except KeyError: pass try: if dists[a + diff]: sgn = True res = a - self.n_l_x_y[2] except KeyError: pass if sgn: break if sgn: self.result = "1\n{0}".format(res) else: self.result = "2\n{0} {1}".format(self.n_l_x_y[2], self.n_l_x_y[3]) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask480BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ``` No
7,055
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Submitted Solution: ``` def search(a,k,n): i=0 l=n-1 while(i<=l): mid=(i+l)//2 if a[mid]==k: return 1 elif a[mid]<k: i=mid+1 else: l=mid-1 return 0 def count(b,x,n): sum=0 i=0 l=0 while(i<n and l<n): #print(sum) if sum<x: #print(l) sum=sum+b[l] l=l+1 if sum>x: sum=sum-b[i] i=i+1 if sum==x: return 1 return 0 def two(a,n): for i in range(n): t=a[i]+x p=a[i]-x q=a[i]+y w=a[i]-y if search(a,t+y,n)!=0 or search(a,abs(t-y),n)!=0: return t if search(a,p+y,n)!=0 or search(a,abs(p-y),n)!=0: return p if search(a,q+x,n)!=0 or search(a,abs(q-x),n)!=0: return q if search(a,w+x,n)!=0 or search(a,abs(w-x),n)!=0: return w return 0 n,l,x,y=[int(i) for i in input().split()] a=[int(i) for i in input().split()] b=[] for i in range(n-1): t=a[i+1]-a[i] b.append(t) #print(b) nl=len(b) k1=count(b,x,nl) k2=count(b,y,nl) #print(k1,k2) if k1==1 and k2==1: print(0) elif k1==0 and k2==0: z=two(a,n) if z>l: z=z-(x+y) if z<0: z=0 if z==0: print(2) print(x,y) else: print(1) print(z) elif k1==0: print(1) print(x) else: print(1) print(y) ``` No
7,056
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≀ i ≀ j ≀ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≀ n ≀ 105, 2 ≀ l ≀ 109, 1 ≀ x < y ≀ l) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v β€” the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≀ pi ≀ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. Submitted Solution: ``` if __name__ == "__main__": n, l, x, y = list(map(int, input().split())) v = list(map(int, input().split())) s = set(v) cx = 0 for i in range(n): if v[i]+x in s: cx = 1 break cy = 0 for i in range(n): if v[i]+y in s: cy = 1 break count = 0 ans = [] if cx==0: count += 1 ans.append(x) if cy==0: count += 1 ans.append(y) if count==2: for i in range(n): if (v[i]+x+y in s) or (v[i]+x-y in s): ans = [v[i]+x] break print(count) if count!=0: print(*ans) ``` No
7,057
Provide tags and a correct Python 3 solution for this coding contest problem. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Tags: dfs and similar, dp, dsu, graphs Correct Solution: ``` def dfs_paths(graph, start, goal, path=list()): if not path: path.append(start) if start == goal: yield path for vertex in graph[start] - set(path): yield from dfs_paths(graph, vertex, goal, path=path + [vertex]) n, m = map(int, input().split()) graph = {} for _ in range(m): a, b, c = map(int, input().split()) if c not in graph: graph[c] = {} if a not in graph[c]: graph[c][a] = set() if b not in graph[c]: graph[c][b] = set() graph[c][a].add(b) graph[c][b].add(a) q = int(input()) for _ in range(q): u, v = map(int, input().split()) count = 0 for k in graph: if u not in graph[k] or v not in graph[k]: continue if len(list(dfs_paths(graph[k], u, v, []))) > 0: count += 1 print(count) ```
7,058
Provide tags and a correct Python 3 solution for this coding contest problem. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Tags: dfs and similar, dp, dsu, graphs Correct Solution: ``` class Solution: def __init__(self, n, m, edges): self.n = n self.m = m self.edges = [[[] for _ in range(m+1)] for _ in range(n+1)] for _from, _to, _color in edges: self.edges[_from][_color].append(_to) self.edges[_to][_color].append(_from) self.id = 0 def initialize(self): self.discovered = [[0]*(self.n+1) for i in range(self.m+1)] def bfs(self, x0, color): self.id += 1 stack = [x0] while stack: n = stack.pop() if not self.discovered[color][n]: self.discovered[color][n] = self.id for neigh in self.edges[n][color]: stack.append(neigh) def solve(graph, colors, u, v): counter = 0 for color in colors: if not graph.discovered[color][u] and not graph.discovered[color][v]: graph.bfs(u, color) if graph.discovered[color][u] == graph.discovered[color][v]: counter += 1 return counter def main(): n, m = map(int, input().split()) edges = [] colors = set() for _ in range(m): a, b, c = map(int, input().split()) colors.add(c) edges.append((a, b, c)) graph = Solution(n, m, edges) graph.initialize() q = int(input()) for _ in range(q): u, v = map(int, input().split()) print(solve(graph, colors, u, v)) if __name__ == '__main__': main() ```
7,059
Provide tags and a correct Python 3 solution for this coding contest problem. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Tags: dfs and similar, dp, dsu, graphs Correct Solution: ``` import sys from collections import defaultdict from functools import lru_cache from collections import Counter def mi(s): return map(int, s.strip().split()) def lmi(s): return list(mi(s)) def mf(f, s): return map(f, s) def lmf(f, s): return list(mf(f, s)) def path(graph, u, v, color, visited): if u == v: return True elif u in visited: return False visited.add(u) for child, c in graph[u]: if color == c and path(graph, child, v, color, visited): return True return False def main(graph, queries, colors): ans = [] for u, v in queries: c = 0 for color in colors: if path(graph, u, v, color, set()): c += 1 ans.append(c) for n in ans: print(n) if __name__ == "__main__": queries = [] colors = set() for e, line in enumerate(sys.stdin.readlines()): if e == 0: n, ed = mi(line) graph = defaultdict(list) for i in range(1, n + 1): graph[i] k = 0 elif ed > 0: a, b, c = mi(line) # Undirected graph. graph[a].append((b, c)) graph[b].append((a, c)) colors.add(c) ed -= 1 elif ed == 0: ed -= 1 continue else: queries.append(lmi(line)) main(graph, queries, colors) ```
7,060
Provide tags and a correct Python 3 solution for this coding contest problem. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Tags: dfs and similar, dp, dsu, graphs Correct Solution: ``` class CodeforcesTask505BSolution: def __init__(self): self.result = '' self.n_m = [] self.edges = [] self.q = 0 self.queries = [] def read_input(self): self.n_m = [int(x) for x in input().split(" ")] for x in range(self.n_m[1]): self.edges.append([int(y) for y in input().split(" ")]) self.q = int(input()) for x in range(self.q): self.queries.append([int(y) for y in input().split(" ")]) def process_task(self): graphs = [[[] for x in range(self.n_m[0])] for c in range(self.n_m[1])] for edge in self.edges: graphs[edge[2] - 1][edge[0] - 1].append(edge[1]) graphs[edge[2] - 1][edge[1] - 1].append(edge[0]) results = [] for query in self.queries: to_visit = [(query[0], c) for c in range(self.n_m[1])] used = set() visited = [[False] * self.n_m[0] for x in range(self.n_m[1])] while to_visit: visiting = to_visit.pop(-1) if visiting[1] not in used and not visited[visiting[1]][visiting[0] - 1]: visited[visiting[1]][visiting[0] - 1] = True if visiting[0] == query[1]: used.add(visiting[1]) else: to_visit.extend([(x, visiting[1]) for x in graphs[visiting[1]][visiting[0] - 1]]) colors = len(used) results.append(colors) self.result = "\n".join([str(x) for x in results]) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask505BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ```
7,061
Provide tags and a correct Python 3 solution for this coding contest problem. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Tags: dfs and similar, dp, dsu, graphs Correct Solution: ``` class Graph(object): def __init__(self, num_nodes): self.num_nodes = num_nodes self.adj_list = {} def __add_directional_edge(self, a, b, c): if a in self.adj_list: if b in self.adj_list[a]: if c not in self.adj_list[a][b]: self.adj_list[a][b].append(c) else: self.adj_list[a][b] = [] self.adj_list[a][b].append(c) else: self.adj_list[a] = {} self.adj_list[a][b] = [] self.adj_list[a][b].append(c) def add_edge(self, a, b, c): self.__add_directional_edge(a, b, c) self.__add_directional_edge(b, a, c) def print_graph(self): print(self.adj_list) def tc(): g = readGraph() # g.print_graph() for k in range(1, g.num_nodes + 1): for i in range(1, g.num_nodes + 1): for j in range(1, g.num_nodes + 1): l1 = g.adj_list.get(i, {}).get(k, []) l2 = g.adj_list.get(k, {}).get(j, []) for color in l1: if color in l2: g.add_edge(i,j,color) # g.print_graph() return g def readGraph(): n, m = map(int, input().split()) g = Graph(n) for _ in range(m): a, b, c = map(int, input().split()) g.add_edge(a,b,c) return g def read_query(): q = int(input()) q_list = [] for _ in range(q): u,v = map(int, input().split()) q_list.append((u,v)) return q_list def solve_query(g, q_list): for u,v in q_list: if u in g.adj_list: if v in g.adj_list[u]: print(len(g.adj_list[u][v])) else: print("0") else: print("0") def main(): g = tc() q_list = read_query() solve_query(g, q_list) if __name__ == "__main__": main() ```
7,062
Provide tags and a correct Python 3 solution for this coding contest problem. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Tags: dfs and similar, dp, dsu, graphs Correct Solution: ``` from collections import defaultdict def DFS(d,a,visited): visited[a] = 1 if a in d: for i in d[a]: if visited[i] == 1: continue else: DFS(d,i,visited) n,m = map(int,input().split()) l = [defaultdict(list) for i in range(m+1)] for i in range(m): a,b,c = map(int,input().split()) l[c][a].append(b) l[c][b].append(a) q = int(input()) for i in range(q): a,b = map(int,input().split()) r = 0 for j in l: visited = [0 for i in range(n+1)] DFS(j,a,visited) if visited[a] == 1 and visited[b] == 1: r = r + 1 print(r) ```
7,063
Provide tags and a correct Python 3 solution for this coding contest problem. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Tags: dfs and similar, dp, dsu, graphs Correct Solution: ``` n,m=map(int,input().split()) g=[[] for _ in range(n)] for _ in range(m): a,b,c=map(int,input().split()) g[a-1].append((b-1,c-1)) g[b-1].append((a-1,c-1)) def dfs(x,c,t): if x==t:return True v[x]=1 for j in g[x]: if j[1]==c and v[j[0]]==0: if dfs(j[0],c,t):return True return False q=int(input()) o=[0]*q v=[] for i in range(q): f,y=map(int,input().split()) for c in range(100): v=[0]*n if dfs(f-1,c,y-1):o[i]+=1 print('\n'.join(list(map(str,o)))) ```
7,064
Provide tags and a correct Python 3 solution for this coding contest problem. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Tags: dfs and similar, dp, dsu, graphs Correct Solution: ``` def get_connected_matrix(adjacency_matrix): n = len(adjacency_matrix) non_visited_vertices = set(i for i in range(n)) cluster_numbers = [0] * n cluster_number = 1 def traverse(u): non_visited_vertices.remove(u) cluster_numbers[u] = cluster_number for v in range(n): if v in non_visited_vertices: if adjacency_matrix[u][v]: traverse(v) while non_visited_vertices: vertex = non_visited_vertices.pop() non_visited_vertices.add(vertex) traverse(vertex) cluster_number += 1 connected_matrix = [[False] * n for _ in range(n)] for u in range(n): for v in range(n): if u == v: continue connected_matrix[u][v] = connected_matrix[v][u] = (cluster_numbers[u] == cluster_numbers[v]) return connected_matrix def main(): n, m = [int(t) for t in input().split()] matrices = [[[False] * n for _ in range(n)] for _ in range(m)] for _ in range(m): a, b, c = [int(t) - 1 for t in input().split()] matrices[c][a][b] = True matrices[c][b][a] = True connected_matrices = [get_connected_matrix(matrix) for matrix in matrices] q = int(input()) for _ in range(q): u, v = [int(t) - 1 for t in input().split()] total_connection = sum(1 for connected_matrix in connected_matrices if connected_matrix[u][v]) print(total_connection) if __name__ == '__main__': main() ```
7,065
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Submitted Solution: ``` from collections import defaultdict def main(): n, m = map(int, input().split()) edges = defaultdict(lambda: defaultdict(list)) for _ in range(m): a, b, c = map(int, input().split()) d = edges[c] d[a].append(b) d[b].append(a) def dfs(t): chain.add(t) dd = color.get(t, ()) for y in dd: if y not in chain: dfs(y) res = [] chain = set() for _ in range(int(input())): a, b = map(int, input().split()) x = 0 for color in edges.values(): chain.clear() dfs(a) if b in chain: x += 1 res.append(str(x)) print('\n'.join(res)) if __name__ == '__main__': main() ``` Yes
7,066
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Submitted Solution: ``` from collections import defaultdict from sys import stdin, stdout par = defaultdict(list) def build(n): global par, size for c in range(101): par[c] = [] for i in range(n): par[c].append(i) return par def find(c, i): global par if par[c][i] == i: return i par[c][i] = find(c, par[c][i]) return par[c][i] def union(a, b, c): global par, scc, size p = find(c, a) q = find(c, b) if p == q: return par[c][q] = par[c][p] def main(): (n, m) = map(int, stdin.readline().strip().split(' ')) par = build(n) max_c = 0 for i in range(m): (a, b, c) = map(lambda i: int(i) - 1, stdin.readline().strip().split(' ')) union(a, b, c) max_c = max(max_c, c) q = int(stdin.readline().strip()) for i in range(q): (a, b) = map(lambda i: int(i) - 1, stdin.readline().strip().split(' ')) count = 0 for c in range(max_c + 1): p = find(c, a) q = find(c, b) if p == q: count += 1 stdout.write('{}\n'.format(count)) main() ``` Yes
7,067
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Submitted Solution: ``` class Union: def __init__(self, size): self.ancestor = [i for i in range(size+1)] def find(self, node): if self.ancestor[node] == node: return node self.ancestor[node] = self.find(self.ancestor[node]) return self.ancestor[node] def merge(self, a, b): a, b = self.find(a), self.find(b) self.ancestor[a] = b n, m = map(int, input().split()) unions = [Union(n) for _ in range(m+1)] graph = [[] for _ in range(n+1)] for _ in range(m): a, b, c = map(int, input().split()) graph[a].append((b, c)) graph[b].append((a, c)) unions[c].merge(a, b) for _ in range(int(input())): a, b = map(int, input().split()) ans = 0 for i in range(1, m+1): ans += unions[i].find(a) == unions[i].find(b) print(ans) ``` Yes
7,068
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Submitted Solution: ``` def dfs(place,target): vis[place]=True if target==place:total[0]+=1;return() anyAdj=0 for i in j[place]: if not vis[i]:anyAdj=1;dfs(i,target) if anyAdj==0:return() v,e=map(int,input().split()) edges=[] for i in range(e):edges.append(list(map(int,input().split()))) colors=[] for i in edges:colors.append(i[2]) colors=list(set(colors)) colorAdjs=[] for i in colors: colorAdjs.append([[] for w in range(v)]) for j in edges: if j[2]==i: colorAdjs[-1][j[0]-1].append(j[1]-1) colorAdjs[-1][j[1]-1].append(j[0]-1) q=int(input()) for i in range(q): total=[0] a,b=map(int,input().split()) a-=1;b-=1 for j in colorAdjs: vis=[False]*v dfs(a,b) print(total[0]) ``` Yes
7,069
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Submitted Solution: ``` import sys import math from collections import defaultdict import itertools MAXNUM = math.inf MINNUM = -1 * math.inf def getInt(): return int(sys.stdin.readline().rstrip()) def getInts(): return map(int, sys.stdin.readline().rstrip().split(" ")) def getString(): return sys.stdin.readline().rstrip() def printOutput(ans): sys.stdout.write() pass def dfs(a, b, edgeList): total = 0 for nxt, col in edgeList[a]: explored = dict() explored[a] = True explored[nxt] = True total += dfs_helper(nxt, b, edgeList, col, explored) return total def dfs_helper(cur, goal, edgeList, color, explored): if cur == goal: return 1 for nxt, col in edgeList[cur]: if col == color and nxt not in explored: explored[nxt] = True if dfs_helper(nxt, goal, edgeList, color, explored) == 1: return 1 return 0 def solve(n, m, edgeList, queries): for a, b in queries: print(dfs(a, b, edgeList)) def readinput(): n, m = getInts() edgeList = defaultdict(list) for _ in range(m): a, b, c = getInts() edgeList[a].append((b, c)) edgeList[b].append((a, c)) queries = [] q = getInt() for _ in range(q): queries.append(tuple(getInts())) (solve(n, m, edgeList, queries)) readinput() ``` No
7,070
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Submitted Solution: ``` def gerarGrafo(cor): adjancencias = {} for aresta in arestas: if aresta[2] == cor: if adjancencias.get(aresta[0]) == None: adjancencias[aresta[0]] = [] if adjancencias.get(aresta[1]) == None: adjancencias[aresta[1]] = [] adjancencias[aresta[0]].append(aresta[1]) adjancencias[aresta[1]].append(aresta[0]) return adjancencias def dfs(u, w, adjacencias): pilha = [u] visitados = {u: 1} existeCaminhoUV = False if adjacencias.get(u) == None or adjacencias.get(w) == None: return existeCaminhoUV while len(pilha) > 0: vertice = pilha.pop() for v in adjacencias[vertice]: if visitados.get(v) == None: visitados[v] = 1 pilha.append(v) if visitados.get(w) != None: existeCaminhoUV = True return existeCaminhoUV entrada = input().split() n = int(entrada[0]) m = int(entrada[1]) cores = set() arestas = [] i = 0 while i < m: aresta = input().split() cores.add(aresta[2]) arestas.append(aresta) i += 1 q = int(input()) paresVertices = [] i = 0 while i < q: paresVertices.append(input().split()) i += 1 res = [0] * q for cor in cores: grafo = gerarGrafo(cor) print(grafo) i = 0 for par in paresVertices: if dfs(par[0], par[1], grafo): res[i] += 1 i += 1 for e in res: print(e) ``` No
7,071
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Submitted Solution: ``` import sys import math from collections import defaultdict import itertools MAXNUM = math.inf MINNUM = -1 * math.inf def getInt(): return int(sys.stdin.readline().rstrip()) def getInts(): return map(int, sys.stdin.readline().rstrip().split(" ")) def getString(): return sys.stdin.readline().rstrip() def printOutput(ans): sys.stdout.write() pass def dfs(a, b, edgeList): total = 0 for nxt, col in edgeList[a]: explored = dict() explored[a] = True explored[nxt] = True total += dfs_helper(nxt, b, edgeList, col, explored) return total def dfs_helper(cur, goal, edgeList, color, explored): if cur == goal: return 1 for nxt, col in edgeList[cur]: if col == color and nxt not in explored: explored[nxt] = True if dfs_helper(nxt, goal, edgeList, color, explored) == 1: return 1 del explored[nxt] return 0 def solve(n, m, edgeList, queries): for a, b in queries: print(dfs(a, b, edgeList)) def readinput(): n, m = getInts() edgeList = defaultdict(list) for _ in range(m): a, b, c = getInts() edgeList[a].append((b, c)) edgeList[b].append((a, c)) queries = [] q = getInt() for _ in range(q): queries.append(tuple(getInts())) (solve(n, m, edgeList, queries)) readinput() ``` No
7,072
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers β€” ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β‰  j, (ai, bi, ci) β‰  (aj, bj, cj). The next line contains a integer β€” q (1 ≀ q ≀ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers β€” ui and vi (1 ≀ ui, vi ≀ n). It is guaranteed that ui β‰  vi. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Note Let's consider the first sample. <image> The figure above shows the first sample. * Vertex 1 and vertex 2 are connected by color 1 and 2. * Vertex 3 and vertex 4 are connected by color 3. * Vertex 1 and vertex 4 are not connected by any single color. Submitted Solution: ``` def build_graph(): line1 = input().strip().split() n = int(line1[0]) m = int(line1[1]) graph = {} for _ in range(m): line = input().strip().split() u = int(line[0]) v = int(line[1]) c = int(line[2]) if c not in graph: graph[c] = {j: [] for j in range(1, n+1)} graph[c][u].append(v) graph[c][v].append(u) return graph parent_history = {} def no_of_paths(u, v, graph): x = 0 for c in graph: if c in parent_history: if v in parent_history[c]: parent = parent_history[c] if u in parent: x += 1 elif u in parent_history[c]: parent = parent_history[c] if v in parent: x += 1 else: parent = {} parent = dfs_visit(v, graph[c], parent) if len(parent_history[c]) < len(parent): parent_history[c] = parent else: parent = {} parent = dfs_visit(v, graph[c], parent) parent_history[c] = parent if u in parent: x += 1 return x def dfs_visit(i, adj_list, parent): for j in adj_list[i]: if j not in parent: parent[j] = i dfs_visit(j, adj_list, parent) return parent if __name__ == "__main__": graph = build_graph() for _ in range(int(input())): line = input().strip().split() print(no_of_paths(int(line[0]), int(line[1]), graph)) ``` No
7,073
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a finite sequence of opening and closing brackets. In order not to fall asleep in a lecture, Polycarpus is having fun with his sequence. He is able to perform two operations: * adding any bracket in any position (in the beginning, the end, or between any two existing brackets); * cyclic shift β€” moving the last bracket from the end of the sequence to the beginning. Polycarpus can apply any number of operations to his sequence and adding a cyclic shift in any order. As a result, he wants to get the correct bracket sequence of the minimum possible length. If there are several such sequences, Polycarpus is interested in the lexicographically smallest one. Help him find such a sequence. Acorrect bracket sequence is a sequence of opening and closing brackets, from which you can get a correct arithmetic expression by adding characters "1" and "+" . Each opening bracket must correspond to a closed one. For example, the sequences "(())()", "()", "(()(()))" are correct and ")(", "(()" and "(()))(" are not. The sequence a1 a2... an is lexicographically smaller than sequence b1 b2... bn, if there is such number i from 1 to n, thatak = bk for 1 ≀ k < i and ai < bi. Consider that "(" < ")". Input The first line contains Polycarpus's sequence consisting of characters "(" and ")". The length of a line is from 1 to 1 000 000. Output Print a correct bracket sequence of the minimum length that Polycarpus can obtain by his operations. If there are multiple such sequences, print the lexicographically minimum one. Examples Input ()(()) Output (())() Input ()( Output (()) Note The sequence in the first example is already correct, but to get the lexicographically minimum answer, you need to perform four cyclic shift operations. In the second example you need to add a closing parenthesis between the second and third brackets and make a cyclic shift. You can first make the shift, and then add the bracket at the end. Submitted Solution: ``` import sys line = sys.stdin.readline() counter = 0 max_counter = 0 longest_seq_end = 0 if line[0] == '(': i = 1 while line[i] == '(': i += 1 line = line[i:-1] + line[:i] for i in range(1, len(line)): if line[i] == '(': counter += 1 else: if counter > max_counter: longest_seq_end = i max_counter = counter counter = 0 elif counter == max_counter: for j in range(1, len(line) - 1): if line[(longest_seq_end + j) % len(line)] == '(' and line[(i + j) % len(line)] == ')': break elif line[(longest_seq_end + j) % len(line)] == ')' and line[(i + j) % len(line)] == '(': longest_seq_end = i line = line[longest_seq_end - max_counter:longest_seq_end] + line[longest_seq_end:] + line[:longest_seq_end - max_counter] addition = str() for i in range(1, len(line) - line.count('(')): addition += ')' line = line + addition else: addition = str() for i in range(0, len(line)): addition += '(' line = addition + line print(line) ``` No
7,074
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a finite sequence of opening and closing brackets. In order not to fall asleep in a lecture, Polycarpus is having fun with his sequence. He is able to perform two operations: * adding any bracket in any position (in the beginning, the end, or between any two existing brackets); * cyclic shift β€” moving the last bracket from the end of the sequence to the beginning. Polycarpus can apply any number of operations to his sequence and adding a cyclic shift in any order. As a result, he wants to get the correct bracket sequence of the minimum possible length. If there are several such sequences, Polycarpus is interested in the lexicographically smallest one. Help him find such a sequence. Acorrect bracket sequence is a sequence of opening and closing brackets, from which you can get a correct arithmetic expression by adding characters "1" and "+" . Each opening bracket must correspond to a closed one. For example, the sequences "(())()", "()", "(()(()))" are correct and ")(", "(()" and "(()))(" are not. The sequence a1 a2... an is lexicographically smaller than sequence b1 b2... bn, if there is such number i from 1 to n, thatak = bk for 1 ≀ k < i and ai < bi. Consider that "(" < ")". Input The first line contains Polycarpus's sequence consisting of characters "(" and ")". The length of a line is from 1 to 1 000 000. Output Print a correct bracket sequence of the minimum length that Polycarpus can obtain by his operations. If there are multiple such sequences, print the lexicographically minimum one. Examples Input ()(()) Output (())() Input ()( Output (()) Note The sequence in the first example is already correct, but to get the lexicographically minimum answer, you need to perform four cyclic shift operations. In the second example you need to add a closing parenthesis between the second and third brackets and make a cyclic shift. You can first make the shift, and then add the bracket at the end. Submitted Solution: ``` def check(s): level = 0 for i in s: if i == '(': level+=1 if i == ')': level-=1 if level<0: return level return level def moved(s, times = 1): buff = '' n = 0 for i in range(-times, len(s)-times): buff+=s[i] return buff def number(s): n = 0 for i in s: if i == '(': n+=1 else: return n s = input() if not check(s) == 0: #print('Inn') if check(s)>0: for i in range(check(s)): s=s+')' if check(s)<0: for i in range(check(s), 0): s=s+'(' #print(s) arr = [] for i in range(len(s)-1): string = moved(s, i) if check(string): arr.append(number(string)) else: arr.append(-300) m = 0 b = 0 for i in arr: if i > b: b = arr.index(i) if i < m: m = arr.index(i) print(moved(s, b)) ``` No
7,075
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a finite sequence of opening and closing brackets. In order not to fall asleep in a lecture, Polycarpus is having fun with his sequence. He is able to perform two operations: * adding any bracket in any position (in the beginning, the end, or between any two existing brackets); * cyclic shift β€” moving the last bracket from the end of the sequence to the beginning. Polycarpus can apply any number of operations to his sequence and adding a cyclic shift in any order. As a result, he wants to get the correct bracket sequence of the minimum possible length. If there are several such sequences, Polycarpus is interested in the lexicographically smallest one. Help him find such a sequence. Acorrect bracket sequence is a sequence of opening and closing brackets, from which you can get a correct arithmetic expression by adding characters "1" and "+" . Each opening bracket must correspond to a closed one. For example, the sequences "(())()", "()", "(()(()))" are correct and ")(", "(()" and "(()))(" are not. The sequence a1 a2... an is lexicographically smaller than sequence b1 b2... bn, if there is such number i from 1 to n, thatak = bk for 1 ≀ k < i and ai < bi. Consider that "(" < ")". Input The first line contains Polycarpus's sequence consisting of characters "(" and ")". The length of a line is from 1 to 1 000 000. Output Print a correct bracket sequence of the minimum length that Polycarpus can obtain by his operations. If there are multiple such sequences, print the lexicographically minimum one. Examples Input ()(()) Output (())() Input ()( Output (()) Note The sequence in the first example is already correct, but to get the lexicographically minimum answer, you need to perform four cyclic shift operations. In the second example you need to add a closing parenthesis between the second and third brackets and make a cyclic shift. You can first make the shift, and then add the bracket at the end. Submitted Solution: ``` 123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123 ``` No
7,076
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a finite sequence of opening and closing brackets. In order not to fall asleep in a lecture, Polycarpus is having fun with his sequence. He is able to perform two operations: * adding any bracket in any position (in the beginning, the end, or between any two existing brackets); * cyclic shift β€” moving the last bracket from the end of the sequence to the beginning. Polycarpus can apply any number of operations to his sequence and adding a cyclic shift in any order. As a result, he wants to get the correct bracket sequence of the minimum possible length. If there are several such sequences, Polycarpus is interested in the lexicographically smallest one. Help him find such a sequence. Acorrect bracket sequence is a sequence of opening and closing brackets, from which you can get a correct arithmetic expression by adding characters "1" and "+" . Each opening bracket must correspond to a closed one. For example, the sequences "(())()", "()", "(()(()))" are correct and ")(", "(()" and "(()))(" are not. The sequence a1 a2... an is lexicographically smaller than sequence b1 b2... bn, if there is such number i from 1 to n, thatak = bk for 1 ≀ k < i and ai < bi. Consider that "(" < ")". Input The first line contains Polycarpus's sequence consisting of characters "(" and ")". The length of a line is from 1 to 1 000 000. Output Print a correct bracket sequence of the minimum length that Polycarpus can obtain by his operations. If there are multiple such sequences, print the lexicographically minimum one. Examples Input ()(()) Output (())() Input ()( Output (()) Note The sequence in the first example is already correct, but to get the lexicographically minimum answer, you need to perform four cyclic shift operations. In the second example you need to add a closing parenthesis between the second and third brackets and make a cyclic shift. You can first make the shift, and then add the bracket at the end. Submitted Solution: ``` per = input() t = len(per) hach = 0 cou = 0 for i in range(t-1,-1,-1): if per[i] == '(': hach += 2**cou cou +=1 else: cou+=1 answer = hach for i in range(t-1,-1,-1): s = (hach - (0 if per[i] == ')' else 1))//2 s += (0 if per[i] == ')' else 2**(t-1)) answer = max(answer, s) hach = s c = bin(answer) cou = 0 for i in range(t): if per[i] == '(': cou -=1 else: cou +=1 if cou >= 0: print(cou * '(' , end = '') for i in range(2,t+2): if c[i] == '0': print(')', end = '') else: print('(', end = '') if cou < 0: print(-cou * ')') ``` No
7,077
Provide tags and a correct Python 2 solution for this coding contest problem. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Tags: combinatorics, dp, math Correct Solution: ``` # File: o (Python 3.4) #045c8f47ec44c634 import math n = input() wow = 0 ans = 1 mod = 1000000007 for i in range(0,n): tmp = input() if (i == 0): wow = wow + tmp else: ans = ans * math.factorial(wow + tmp - 1)/math.factorial(wow) '''for j in range(1, tmp): wow = wow + 1 ans = (ans * wow) % mod''' ans = ans/math.factorial(tmp - 1) % mod wow = wow + tmp print ans ```
7,078
Provide tags and a correct Python 3 solution for this coding contest problem. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Tags: combinatorics, dp, math Correct Solution: ``` def c(k,l): d=1 for i in range(k+1,l+k+1): d*=i for i in range(l): d//=(i+1) return d%1000000007 ans=1 n=int(input()) k=int(input()) for t in range(1,n): a=int(input()) ans*=c(k,a-1)%1000000007 k+=a print(ans%1000000007) ```
7,079
Provide tags and a correct Python 3 solution for this coding contest problem. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Tags: combinatorics, dp, math Correct Solution: ``` from math import factorial n,ans,s = int(input()),1,0 for i in range(n) : a = int(input()) ans=(ans*factorial(s+a-1)//factorial(s)//factorial(a-1))%1000000007 s+=a print(ans) #copied... # Made By Mostafa_Khaled ```
7,080
Provide tags and a correct Python 3 solution for this coding contest problem. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Tags: combinatorics, dp, math Correct Solution: ``` from math import factorial as f n=int(input()) d=0 out=1 for i in range(n) : m=int(input()) out=out*f(d+m-1)//f(d)//f(m-1)%1000000007 d+=m print(out) ```
7,081
Provide tags and a correct Python 3 solution for this coding contest problem. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Tags: combinatorics, dp, math Correct Solution: ``` k = int(input()) colors = [] for i in range(k) : colors.append(int(input())) ans = 1 accum = colors[0] for i in range(1, k): for j in range(1, colors[i]): ans *= (accum + j) for j in range(1, colors[i]): ans //= j accum += colors[i] print(ans % 1000000007) ```
7,082
Provide tags and a correct Python 3 solution for this coding contest problem. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Tags: combinatorics, dp, math Correct Solution: ``` #!/usr/bin/python3 import sys from functools import lru_cache MOD = 1000000007 cnk = [[1 for i in range(1001)] for j in range(1001)] for i in range(1, 1001): for j in range(1, i): cnk[i][j] = cnk[i - 1][j - 1] + cnk[i - 1][j] k = int(input()) cs = [int(input()) for i in range(k)] ans = 1 sm = 0 for c in cs: sm += c ans = (ans * cnk[sm - 1][c - 1]) % MOD print(ans) ```
7,083
Provide tags and a correct Python 3 solution for this coding contest problem. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Tags: combinatorics, dp, math Correct Solution: ``` from math import exp def pfold (arr): return arr[0] * pfold (arr[1:]) if arr else 1 def pscan (x, arr): arr = (pscan (x * arr[-1], arr[:-1]) if arr else []) arr.append(x) return arr def P(n, r): return pfold(range(n, n-r, -1)) def F(n): return P(n, n) #return reduce(op.mul, range(n, 0, -1), 1) def C(n, r): if (r > n): return 0 r = min(r, n-r) return P(n, r)//F(r) def AC(a, b): return C(a+b-1, b-1) def CCC(n, k): return AC(n, k)/F(k) def Cat(n): return C(2*n,n+1)/n def Cat2(n): return def dot(a,b): return sum(i*j for i, j in zip(a,b)) def Catray(n): arr = [1] for i in range(1,n): arr = arr + [dot(arr,arr[::-1])] return arr # binomial distribution: # with an event E which has probability p of occuring every try, # what is the chance of E occuring exactly k times from n tries def Bd (n, p, k): return C(n,k)*p**k*(1-p)**(n-k) # evaluate the sum of the binomial distribution in the range [0, k] def BdS (n, p, k): return sum(Bd(n, p, i) for i in range(k+1)) def Normal (mu, sigma, k): return 0 # exponential distribution def Ed (p, k): return (1-p)*p**(k-1) def EdS (p, k): return 1 - p**k def Pd (x, k): return x**k/F(k) * exp(-x) def PdS (x, k): return (sum(pscan (1, [x/i for i in range (k, 0, -1)]))) * exp(-x) #def PPP(n, k, m): def nTermsSumToXInRangeAToB(n, x, a, b): # a <= b x -= a*n b -= a if x < 0 or x > b*n: return 0 else: return nTermsUnderASumToX(n, x, b+1, 0) def nTermsUnderASumToX(n, x, a, b): if x < 0: return 0 else: return AC(x,n) - (n-b)*nTermsUnderASumToX(n, x-a, a, b+1) def f(n, x, a, b): return nTermsSumToXInRangeAToB(n, x, a, b) M = 10**9+7 k = int(input()) v = 1 n = 0 for _ in range(k): c = int(input()) v *= C(n+c-1,c-1) v %= M n+= c print(v) ```
7,084
Provide tags and a correct Python 3 solution for this coding contest problem. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Tags: combinatorics, dp, math Correct Solution: ``` def binomialCoefficient(n, k): if(k > n - k): k = n - k res = 1 for i in range(k): res = res * (n - i) res = res // (i + 1) return res k=int(input()) sum=0 ans=1 m=1000000007 for i in range(k): a=int(input()) ans=(ans%m)*(binomialCoefficient(sum+a-1,a-1))%m sum+=a print(ans) ```
7,085
Provide tags and a correct Python 3 solution for this coding contest problem. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Tags: combinatorics, dp, math Correct Solution: ``` n = int(input()) balls = [] for i in range (n): balls.append(int(input())) ans = 1 urns = balls[0] + 1 def theorem(n, k): # n urns k balls ret = 1 for i in range(1, k+1): ret = ret * (n+k-i) for i in range(1, k+1): ret = ret // i return ret for i in range (1, n): ans *= theorem(urns, balls[i]-1) % 1000000007 urns += balls[i] print (ans % 1000000007) ```
7,086
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Submitted Solution: ``` t = int(input()) c = 1 s = int(input()) for _ in range(t-1): n = int(input()) s += n k = 1 for i in range(1, n): k = k*(s-i)//i c = (c*k) % (10**9+7) print(c) ``` Yes
7,087
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Submitted Solution: ``` # coding: utf-8 # In[6]: matrix = [[0 for x in range(1001)] for y in range(1001)] mod = 1000000007 def pascal(): matrix[0][0]=1; for i in range(1001): for j in range(1001): if j==0 or j==i: matrix[i][j]=1 else: matrix[i][j] = (matrix[i-1][j-1]+matrix[i-1][j])%mod a = int(input()) b = [] for i in range(a): b.append(int(input())) pascal() r = 1 s = b[0] for i in range(1,a): r = (r*matrix[s + b[i]-1][b[i]-1])%mod s += b[i] print(r) ``` Yes
7,088
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Submitted Solution: ``` mod = 10 ** 9 + 7 import math def f(n): return math.factorial(n) k = int(input()) c = [int(input()) for i in range(k)] s, cnt = 0, 1 for i in c: cnt *= f(s + i - 1) // f(i - 1) // f(s) cnt %= mod s += i print(cnt) ``` Yes
7,089
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Submitted Solution: ``` from math import factorial n = int(input()) ans = 1 s = 0 mod = (10**9) + 7 for i in range(n): a = int(input()) ans *= factorial(s+a-1)//(factorial(s) * factorial(a-1)) ans = ans%mod s += a print(ans) ``` Yes
7,090
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Submitted Solution: ``` def fact(i): if(i==0 or i==1): return 1 else: return(fact(i-1)*i) n=int(input()) list1=[] sum=0 product=1 for x in range(n): list1.append(int(input())) sum+=list1[x] for x in range(n-2): product*=fact(list1[x]) product=product*fact(list1[n-2]-1)*fact(list1[n-1]-1) print((fact(sum-2)//product)%1000000007) ``` No
7,091
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Submitted Solution: ``` import math def nCr(n, r): return (math.factorial(n) / (math.factorial(r) * math.factorial(n - r))) mod = 1000000007 n = int(input()) data = [] for i in range(n): data.append(int(input())) ans = 1 total = data[0] for i in range(1,n): total += data[i] ans *= int(nCr(total-1,data[i]-1)) ans %= mod print(int(ans)) ``` No
7,092
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Submitted Solution: ``` k=int(input()) l=[int(input()) for i in range(k)] s=0 ans=[0]*k ans[0]=1 MOD=10**9+7 fact=[0]*(10**6+5) fact[0]=1 for i in range(1,10**6+5): fact[i]=(fact[i-1]*i)%MOD #print(fact[0:10]) def c(n,k): if k>=n: return 1 if k==0 or k==n: return 1 return fact[n]//(fact[k]*fact[n-k])%MOD ans=1 sm=l[0] for i in range(1,k): curr=l[i] #ans[i]=ans[i-1]+ ans=ans*(c(sm+curr-1,curr-1)) ans%=MOD sm+=l[i] print(ans) ``` No
7,093
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. Input The first line of input will have one integer k (1 ≀ k ≀ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≀ ci ≀ 1000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 Submitted Solution: ``` from math import exp def pfold (arr): return arr[0] * pfold (arr[1:]) if arr else 1 def pscan (x, arr): arr = (pscan (x * arr[-1], arr[:-1]) if arr else []) arr.append(x) return arr def P(n, r): return pfold(range(n, n-r, -1)) def F(n): return P(n, n) #return reduce(op.mul, range(n, 0, -1), 1) def C(n, r): if (r > n): return 0 r = min(r, n-r) return P(n, r)//F(r) def AC(a, b): return C(a+b-1, b-1) def CCC(n, k): return AC(n, k)/F(k) def Cat(n): return C(2*n,n+1)/n def Cat2(n): return def dot(a,b): return sum(i*j for i, j in zip(a,b)) def Catray(n): arr = [1] for i in range(1,n): arr = arr + [dot(arr,arr[::-1])] return arr # binomial distribution: # with an event E which has probability p of occuring every try, # what is the chance of E occuring exactly k times from n tries def Bd (n, p, k): return C(n,k)*p**k*(1-p)**(n-k) # evaluate the sum of the binomial distribution in the range [0, k] def BdS (n, p, k): return sum(Bd(n, p, i) for i in range(k+1)) def Normal (mu, sigma, k): return 0 # exponential distribution def Ed (p, k): return (1-p)*p**(k-1) def EdS (p, k): return 1 - p**k def Pd (x, k): return x**k/F(k) * exp(-x) def PdS (x, k): return (sum(pscan (1, [x/i for i in range (k, 0, -1)]))) * exp(-x) #def PPP(n, k, m): def nTermsSumToXInRangeAToB(n, x, a, b): # a <= b x -= a*n b -= a if x < 0 or x > b*n: return 0 else: return nTermsUnderASumToX(n, x, b+1, 0) def nTermsUnderASumToX(n, x, a, b): if x < 0: return 0 else: return AC(x,n) - (n-b)*nTermsUnderASumToX(n, x-a, a, b+1) def f(n, x, a, b): return nTermsSumToXInRangeAToB(n, x, a, b) M = 10**9+7 k = int(input()) v = 1 n = 0 for _ in range(k): c = int(input()) v *= C(n+c-1,c-1) c %= M n+= c print(v) ``` No
7,094
Provide tags and a correct Python 3 solution for this coding contest problem. Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company! Input The first line of the input contains two space-separated integers, n and d (1 ≀ n ≀ 105, <image>) β€” the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively. Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type mi, si (0 ≀ mi, si ≀ 109) β€” the amount of money and the friendship factor, respectively. Output Print the maximum total friendship factir that can be reached. Examples Input 4 5 75 5 0 100 150 20 75 1 Output 100 Input 5 100 0 7 11 32 99 10 46 8 87 54 Output 111 Note In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse. In the second sample test we can take all the friends. Tags: binary search, sortings, two pointers Correct Solution: ``` def kac(l,d): l.sort(reverse=True) ma=l[0][1] v=l[0][1] i=j=0 for i in range(1,len(l)): v=v+l[i][1] while abs(l[i][0]-l[j][0])>=d: v-=l[j][1] j+=1 if(v>ma): ma=v print(ma) return n,d=map(int,input().split()) l1=[] for i in range(n): m=list(map(int,input().split())) l1.append(m) kac(l1,d) ```
7,095
Provide tags and a correct Python 3 solution for this coding contest problem. Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company! Input The first line of the input contains two space-separated integers, n and d (1 ≀ n ≀ 105, <image>) β€” the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively. Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type mi, si (0 ≀ mi, si ≀ 109) β€” the amount of money and the friendship factor, respectively. Output Print the maximum total friendship factir that can be reached. Examples Input 4 5 75 5 0 100 150 20 75 1 Output 100 Input 5 100 0 7 11 32 99 10 46 8 87 54 Output 111 Note In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse. In the second sample test we can take all the friends. Tags: binary search, sortings, two pointers Correct Solution: ``` n, d = map(int, input().split()) friends = [] for i in range(n): friends.append(tuple(map(int, input().split()))) friends.sort(key=lambda x: x[0]) prefix = [0] * n prefix[0] = friends[0][1] for i in range(1, n): prefix[i] += prefix[i - 1] le = 0 r = 0 curr_sum = 0 ans = max(f[1] for f in friends) while le < n: while r < n and abs(friends[r][0] - friends[le][0]) < d: curr_sum += friends[r][1] r += 1 ans = max(ans, curr_sum) curr_sum -= friends[le][1] le += 1 print(ans) ```
7,096
Provide tags and a correct Python 3 solution for this coding contest problem. Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company! Input The first line of the input contains two space-separated integers, n and d (1 ≀ n ≀ 105, <image>) β€” the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively. Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type mi, si (0 ≀ mi, si ≀ 109) β€” the amount of money and the friendship factor, respectively. Output Print the maximum total friendship factir that can be reached. Examples Input 4 5 75 5 0 100 150 20 75 1 Output 100 Input 5 100 0 7 11 32 99 10 46 8 87 54 Output 111 Note In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse. In the second sample test we can take all the friends. Tags: binary search, sortings, two pointers Correct Solution: ``` [friendsSize, moneyDiff] = [int(x) for x in input().split()] friends = [] for i in range(friendsSize): friendMoney, currentPoints = map(int, input().split()) friends.append([friendMoney, currentPoints]) friends = sorted(friends) currentPoints = 0 maxPoints = 0 j = 0 for i in range(friendsSize): while j < friendsSize and abs(friends[i][0] - friends[j][0]) < moneyDiff: currentPoints += friends[j][1] j += 1 if currentPoints >= maxPoints: maxPoints = currentPoints currentPoints -= friends[i][1] print(maxPoints) ```
7,097
Provide tags and a correct Python 3 solution for this coding contest problem. Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company! Input The first line of the input contains two space-separated integers, n and d (1 ≀ n ≀ 105, <image>) β€” the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively. Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type mi, si (0 ≀ mi, si ≀ 109) β€” the amount of money and the friendship factor, respectively. Output Print the maximum total friendship factir that can be reached. Examples Input 4 5 75 5 0 100 150 20 75 1 Output 100 Input 5 100 0 7 11 32 99 10 46 8 87 54 Output 111 Note In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse. In the second sample test we can take all the friends. Tags: binary search, sortings, two pointers Correct Solution: ``` n,d=map(int,input().split()) a=[] for i in range(n): a.append((list(map(int,input().split())))) a.sort() x=0 y=0 m=0 f=0 while y<n: if a[y][0]-a[x][0]<d: m+=a[y][1] y+=1 f=max(m,f) else: m-=a[x][1] x+=1 print(f) ```
7,098
Provide tags and a correct Python 3 solution for this coding contest problem. Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company! Input The first line of the input contains two space-separated integers, n and d (1 ≀ n ≀ 105, <image>) β€” the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively. Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type mi, si (0 ≀ mi, si ≀ 109) β€” the amount of money and the friendship factor, respectively. Output Print the maximum total friendship factir that can be reached. Examples Input 4 5 75 5 0 100 150 20 75 1 Output 100 Input 5 100 0 7 11 32 99 10 46 8 87 54 Output 111 Note In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse. In the second sample test we can take all the friends. Tags: binary search, sortings, two pointers Correct Solution: ``` user_input=input() first_line=user_input.split(' ') n=int(first_line[0]) d=int(first_line[1]) long_list=[] for i in range(n): extended_list=[] user_input = input() line = user_input.split(' ') extended_list.extend([int(line[0]),int(line[1])]) long_list.append(extended_list) long_list.sort() index=0 maximum=0 for i in range(len(long_list)): if maximum<long_list[i][1]: maximum=long_list[i][1] answer=maximum maximum=0 i=0 count=0 looped="false" while i in range(len(long_list)): if long_list[i][0]-long_list[index][0]>d: if count>0 and looped=="false": i-=count count=0 index=i looped="true" else: index=i maximum=0 if long_list[i][0]-long_list[index][0]<d: count+=1 if maximum==0: maximum=long_list[index][1] else: maximum+=long_list[i][1] answer=max(answer,maximum) i+=1 print(answer) ```
7,099