message
stringlengths
2
67k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
463
109k
cluster
float64
19
19
__index_level_0__
int64
926
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value. Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero? You can assume that initially Vanya had infinitely many cards with each integer number from - x to x. Input The first line contains two integers: n (1 ≀ n ≀ 1000) β€” the number of found cards and x (1 ≀ x ≀ 1000) β€” the maximum absolute value of the number on a card. The second line contains n space-separated integers β€” the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value. Output Print a single number β€” the answer to the problem. Examples Input 3 2 -1 1 2 Output 1 Input 2 3 -2 -2 Output 2 Note In the first sample, Vanya needs to find a single card with number -2. In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value. Submitted Solution: ``` n,x=[int(x) for x in input().split()] l=[int(x) for x in input().split()] y=sum(l) r=-1*y if abs(r)>x: if r%x==0: r=abs(int(r/x)) else: r=abs(int(r/x))+1 print(r) else: if r==0: print (0) else: print(1) ```
instruction
0
55,140
19
110,280
Yes
output
1
55,140
19
110,281
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value. Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero? You can assume that initially Vanya had infinitely many cards with each integer number from - x to x. Input The first line contains two integers: n (1 ≀ n ≀ 1000) β€” the number of found cards and x (1 ≀ x ≀ 1000) β€” the maximum absolute value of the number on a card. The second line contains n space-separated integers β€” the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value. Output Print a single number β€” the answer to the problem. Examples Input 3 2 -1 1 2 Output 1 Input 2 3 -2 -2 Output 2 Note In the first sample, Vanya needs to find a single card with number -2. In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value. Submitted Solution: ``` n,x=[int(x) for x in input().split()] l=list(map(int,input().split())) if abs(sum(l))<=x: print(1) else: print((abs(sum(l))//x)+1) ```
instruction
0
55,141
19
110,282
No
output
1
55,141
19
110,283
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value. Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero? You can assume that initially Vanya had infinitely many cards with each integer number from - x to x. Input The first line contains two integers: n (1 ≀ n ≀ 1000) β€” the number of found cards and x (1 ≀ x ≀ 1000) β€” the maximum absolute value of the number on a card. The second line contains n space-separated integers β€” the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value. Output Print a single number β€” the answer to the problem. Examples Input 3 2 -1 1 2 Output 1 Input 2 3 -2 -2 Output 2 Note In the first sample, Vanya needs to find a single card with number -2. In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value. Submitted Solution: ``` n,m=map(int,input().split()) a=list(map(int,input().split())) x=abs(sum(a)) if(x<=m): print(1) else: if(x%m==0): print(x//m) else: print(x//m+1) ```
instruction
0
55,142
19
110,284
No
output
1
55,142
19
110,285
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value. Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero? You can assume that initially Vanya had infinitely many cards with each integer number from - x to x. Input The first line contains two integers: n (1 ≀ n ≀ 1000) β€” the number of found cards and x (1 ≀ x ≀ 1000) β€” the maximum absolute value of the number on a card. The second line contains n space-separated integers β€” the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value. Output Print a single number β€” the answer to the problem. Examples Input 3 2 -1 1 2 Output 1 Input 2 3 -2 -2 Output 2 Note In the first sample, Vanya needs to find a single card with number -2. In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value. Submitted Solution: ``` n, x = [int(string) for string in input().split()] s_cards = sum([int(string) for string in input().split()]) delta = abs(s_cards) print(1 if delta <= x else 1 + delta // x) ```
instruction
0
55,143
19
110,286
No
output
1
55,143
19
110,287
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value. Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero? You can assume that initially Vanya had infinitely many cards with each integer number from - x to x. Input The first line contains two integers: n (1 ≀ n ≀ 1000) β€” the number of found cards and x (1 ≀ x ≀ 1000) β€” the maximum absolute value of the number on a card. The second line contains n space-separated integers β€” the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value. Output Print a single number β€” the answer to the problem. Examples Input 3 2 -1 1 2 Output 1 Input 2 3 -2 -2 Output 2 Note In the first sample, Vanya needs to find a single card with number -2. In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value. Submitted Solution: ``` n, x = map(int, input().split()) s = sum(map(int, input().split())) print(abs(s//x) + (s>round(s))) # r, c = map(int,input().split()) # ir, ic =[],[] # m = [] # for i in range(r): # s = input() # m = m + [s] # for j in range(len(s)): # if s[j] == 'S': # ir = ir + [i] # ic = ic + [j] # numb = 0 # print(ir) # print(ic) # print(m) # for i in m: # for j in range(len(i)): # if i not in ir or j not in ic: # numb +=1 # print(numb) ```
instruction
0
55,144
19
110,288
No
output
1
55,144
19
110,289
Provide tags and a correct Python 3 solution for this coding contest problem. Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers. To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets. Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options? Input The first line contains a single integer n β€” the length of the sequence of games (1 ≀ n ≀ 105). The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record. Output In the first line print a single number k β€” the number of options for numbers s and t. In each of the following k lines print two integers si and ti β€” the option for numbers s and t. Print the options in the order of increasing si, and for equal si β€” in the order of increasing ti. Examples Input 5 1 2 1 2 1 Output 2 1 3 3 1 Input 4 1 1 1 1 Output 3 1 4 2 2 4 1 Input 4 1 2 1 2 Output 0 Input 8 2 1 2 1 1 1 1 1 Output 3 1 6 2 3 6 1
instruction
0
55,165
19
110,330
Tags: binary search Correct Solution: ``` n = int(input()) line = input().split() lst = [] for num in line: lst.append(int(num)) cnt1 = [0] cnt2 = [0] c1 = 0 c2 = 0 for num in lst: if num == 1: c1 += 1 cnt1.append(c2) else: c2 += 1 cnt2.append(c1) w = lst[n - 1] ans = [] c1 = len(cnt1) c2 = len(cnt2) for t in range(n, 0, -1): s1 = 0 s2 = 0 i1 = 0 i2 = 0 l = 1 while i1 < c1 and i2 < c2: if i1 + t >= c1 and i2 + t >= c2: if l == 1 and l == w and i1 + 1 == c1 and s1 > s2: ans.append((s1, t)) elif l == 2 and l == w and i2 + 1 == c2 and s2 > s1: ans.append((s2, t)) break elif i2 + t >= c2: s1 += 1 l = 1 i1 += t i2 = cnt1[i1] elif i1 + t >= c1: s2 += 1 l = 2 i2 += t i1 = cnt2[i2] else: if cnt1[i1 + t] < i2 + t: s1 += 1 l = 1 i1 += t i2 = cnt1[i1] else: s2 += 1 l = 2 i2 += t i1 = cnt2[i2] ans.sort() print(int(len(ans))) for line in ans: print(str(line[0]) + ' ' + str(line[1])) ```
output
1
55,165
19
110,331
Provide tags and a correct Python 3 solution for this coding contest problem. Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers. To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets. Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options? Input The first line contains a single integer n β€” the length of the sequence of games (1 ≀ n ≀ 105). The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record. Output In the first line print a single number k β€” the number of options for numbers s and t. In each of the following k lines print two integers si and ti β€” the option for numbers s and t. Print the options in the order of increasing si, and for equal si β€” in the order of increasing ti. Examples Input 5 1 2 1 2 1 Output 2 1 3 3 1 Input 4 1 1 1 1 Output 3 1 4 2 2 4 1 Input 4 1 2 1 2 Output 0 Input 8 2 1 2 1 1 1 1 1 Output 3 1 6 2 3 6 1
instruction
0
55,166
19
110,332
Tags: binary search Correct Solution: ``` #!/usr/bin/env python3 import itertools n = int(input()) a = [int(x) for x in input().split()] winner = a[-1] looser = 3 - winner serve_win_cnt, serve_loose_cnt, win_pos, loose_pos, result = [0], [0], [-1], [-1], [] win_cnt = a.count(winner) for i in range(n): if a[i] == winner: win_pos.append(i) else: loose_pos.append(i) serve_win_cnt.append(serve_win_cnt[-1] + (a[i] == winner)) serve_loose_cnt.append(serve_loose_cnt[-1] + (a[i] == looser)) win_pos += [n * 10] * n loose_pos += [n * 10] * n serve_win_cnt += [0] * n serve_loose_cnt += [0] * n for t in itertools.chain(range(1, 1 + win_cnt // 2), [win_cnt]): s = l = i = 0 sw = sl = 0 while i < n: xw = win_pos[serve_win_cnt[i] + t] xl = loose_pos[serve_loose_cnt[i] + t] if xw < xl: s += 1 else: l += 1 i = min(xw, xl) + 1 if s > l and i <= n and serve_win_cnt[i] == win_cnt: result.append((s, t)) print(len(result)) for (x, y) in sorted(result): print(x, y) # Made By Mostafa_Khaled ```
output
1
55,166
19
110,333
Provide tags and a correct Python 3 solution for this coding contest problem. Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers. To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets. Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options? Input The first line contains a single integer n β€” the length of the sequence of games (1 ≀ n ≀ 105). The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record. Output In the first line print a single number k β€” the number of options for numbers s and t. In each of the following k lines print two integers si and ti β€” the option for numbers s and t. Print the options in the order of increasing si, and for equal si β€” in the order of increasing ti. Examples Input 5 1 2 1 2 1 Output 2 1 3 3 1 Input 4 1 1 1 1 Output 3 1 4 2 2 4 1 Input 4 1 2 1 2 Output 0 Input 8 2 1 2 1 1 1 1 1 Output 3 1 6 2 3 6 1
instruction
0
55,167
19
110,334
Tags: binary search Correct Solution: ``` #!/usr/bin/env python3 import itertools n = int(input()) a = [int(x) for x in input().split()] winner = a[-1] looser = 3 - winner serve_win_cnt, serve_loose_cnt, win_pos, loose_pos, result = [0], [0], [-1], [-1], [] win_cnt = a.count(winner) for i in range(n): if a[i] == winner: win_pos.append(i) else: loose_pos.append(i) serve_win_cnt.append(serve_win_cnt[-1] + (a[i] == winner)) serve_loose_cnt.append(serve_loose_cnt[-1] + (a[i] == looser)) win_pos += [n * 10] * n loose_pos += [n * 10] * n serve_win_cnt += [0] * n serve_loose_cnt += [0] * n for t in itertools.chain(range(1, 1 + win_cnt // 2), [win_cnt]): s = l = i = 0 sw = sl = 0 while i < n: xw = win_pos[serve_win_cnt[i] + t] xl = loose_pos[serve_loose_cnt[i] + t] if xw < xl: s += 1 else: l += 1 i = min(xw, xl) + 1 if s > l and i <= n and serve_win_cnt[i] == win_cnt: result.append((s, t)) print(len(result)) for (x, y) in sorted(result): print(x, y) ```
output
1
55,167
19
110,335
Provide tags and a correct Python 3 solution for this coding contest problem. Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers. To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets. Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options? Input The first line contains a single integer n β€” the length of the sequence of games (1 ≀ n ≀ 105). The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record. Output In the first line print a single number k β€” the number of options for numbers s and t. In each of the following k lines print two integers si and ti β€” the option for numbers s and t. Print the options in the order of increasing si, and for equal si β€” in the order of increasing ti. Examples Input 5 1 2 1 2 1 Output 2 1 3 3 1 Input 4 1 1 1 1 Output 3 1 4 2 2 4 1 Input 4 1 2 1 2 Output 0 Input 8 2 1 2 1 1 1 1 1 Output 3 1 6 2 3 6 1
instruction
0
55,168
19
110,336
Tags: binary search Correct Solution: ``` import itertools n = int(input()) a = [int(x) for x in input().split()] winner = a[-1] looser = 3 - winner serve_win_cnt, serve_loose_cnt, win_pos, loose_pos, result = [0], [0], [-1], [-1], [] win_cnt = a.count(winner) for i in range(n): if a[i] == winner: win_pos.append(i) else: loose_pos.append(i) serve_win_cnt.append(serve_win_cnt[-1] + (a[i] == winner)) serve_loose_cnt.append(serve_loose_cnt[-1] + (a[i] == looser)) win_pos += [n * 10] * n loose_pos += [n * 10] * n serve_win_cnt += [0] * n serve_loose_cnt += [0] * n for t in itertools.chain(range(1, 1 + win_cnt // 2), [win_cnt]): s = l = i = 0 sw = sl = 0 while i < n: xw = win_pos[serve_win_cnt[i] + t] xl = loose_pos[serve_loose_cnt[i] + t] if xw < xl: s += 1 else: l += 1 i = min(xw, xl) + 1 if s > l and i <= n and serve_win_cnt[i] == win_cnt: result.append((s, t)) print(len(result)) for (x, y) in sorted(result): print(x, y) ```
output
1
55,168
19
110,337
Provide tags and a correct Python 3 solution for this coding contest problem. Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers. To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets. Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options? Input The first line contains a single integer n β€” the length of the sequence of games (1 ≀ n ≀ 105). The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record. Output In the first line print a single number k β€” the number of options for numbers s and t. In each of the following k lines print two integers si and ti β€” the option for numbers s and t. Print the options in the order of increasing si, and for equal si β€” in the order of increasing ti. Examples Input 5 1 2 1 2 1 Output 2 1 3 3 1 Input 4 1 1 1 1 Output 3 1 4 2 2 4 1 Input 4 1 2 1 2 Output 0 Input 8 2 1 2 1 1 1 1 1 Output 3 1 6 2 3 6 1
instruction
0
55,169
19
110,338
Tags: binary search Correct Solution: ``` from itertools import chain def main(n,a, info=False): winner = a[-1] looser = 3-winner csw, csl, pw, pl, ans = [0], [0], [-1], [-1], [] nw,nl = a.count(winner), a.count(looser) for i in range(n): if a[i]==winner: pw.append(i) else: pl.append(i) csw.append(csw[-1] + int(a[i]==winner)) csl.append(csl[-1] + int(a[i]==looser)) pw += [n*10]*n pl += [n*10]*n csw += [0]*n csl += [0]*n if info: print("a: ",a) print("csw: ",csw) print("csl: ",csl) print("pw: ",pw) print("pl: ",pl) for t in chain(range(1,nw//2+1),[nw]): s = l = i = 0 sw = sl = 0 while i < n: xw = pw[csw[i]+t] xl = pl[csl[i]+t] if xw < xl: s += 1 else: l += 1 i = min(xw,xl)+1 if info: print(s,t,": ",t,i,s,l,xw,xl) if s>l and i<=n and csw[i]==nw: ans.append((s,t)) print(len(ans)) for x,y in sorted(ans): print(x,y) def main_input(): n = int(input()) a = [int(i) for i in input().split()] main(n,a) if __name__ == "__main__": main_input() ```
output
1
55,169
19
110,339
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers. To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets. Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options? Input The first line contains a single integer n β€” the length of the sequence of games (1 ≀ n ≀ 105). The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record. Output In the first line print a single number k β€” the number of options for numbers s and t. In each of the following k lines print two integers si and ti β€” the option for numbers s and t. Print the options in the order of increasing si, and for equal si β€” in the order of increasing ti. Examples Input 5 1 2 1 2 1 Output 2 1 3 3 1 Input 4 1 1 1 1 Output 3 1 4 2 2 4 1 Input 4 1 2 1 2 Output 0 Input 8 2 1 2 1 1 1 1 1 Output 3 1 6 2 3 6 1 Submitted Solution: ``` import math n = int(input()) games = list(map(int, input().split())) PETYA = 0 GENA = 1 pontos = [[0], [0]] for i in range(0, n): if (games[i] == 1): pontos[PETYA].append(pontos[PETYA][i] + 1) pontos[GENA].append(pontos[GENA][i]) else: pontos[PETYA].append(pontos[PETYA][i]) pontos[GENA].append(pontos[GENA][i] + 1) res = [] k = 0 impossivel = False winner = (games[-1] == 2) loser = not winner if (pontos[PETYA][-1] > pontos[GENA][-1] and winner == GENA) or (pontos[PETYA][-1] < pontos[GENA][-1] and winner == PETYA): impossivel = True if (not impossivel): maxPoints = max(pontos[PETYA][-1], pontos[GENA][-1]) for s in range(1, maxPoints + 1): for t in range(1, (maxPoints // s) + 1): ganhou = 0 pontos[PETYA][0] = 0 pontos[GENA][0] = 0 contW = 0; contL = 0; while (ganhou < n): try: petya = pontos[PETYA].index(pontos[PETYA][0] + t) except ValueError: petya = n + t try: gena = pontos[GENA].index(pontos[GENA][0] + t) except ValueError: gena = n + t ganhou = min(petya, gena) if (ganhou <= n): pontos[PETYA][0] = pontos[PETYA][ganhou] pontos[GENA][0] = pontos[GENA][ganhou] if (winner == PETYA): if (ganhou == petya): contW += 1 else: contL += 1 if (winner == GENA): if (ganhou == gena): contW += 1 else: contL += 1 if (ganhou == n and contW == s and contW != contL): res.append((s, t)) print(len(res)) for i in range(0, len(res)): x = res[i] print("{} {}".format(int(x[0]), int(x[1]))) ```
instruction
0
55,170
19
110,340
No
output
1
55,170
19
110,341
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers. To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets. Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options? Input The first line contains a single integer n β€” the length of the sequence of games (1 ≀ n ≀ 105). The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record. Output In the first line print a single number k β€” the number of options for numbers s and t. In each of the following k lines print two integers si and ti β€” the option for numbers s and t. Print the options in the order of increasing si, and for equal si β€” in the order of increasing ti. Examples Input 5 1 2 1 2 1 Output 2 1 3 3 1 Input 4 1 1 1 1 Output 3 1 4 2 2 4 1 Input 4 1 2 1 2 Output 0 Input 8 2 1 2 1 1 1 1 1 Output 3 1 6 2 3 6 1 Submitted Solution: ``` def main(): n = int(input()) rdl = list(map(int,input().split())) kl1 = 0 kl2 = 0 for i in rdl: if i == 1: kl1 += 1 else: kl2+=1 ik = max(kl1, kl2) out = [] for i in range(ik+1): if i != 0: finding(i,rdl,out) out.sort() print(len(out)) for i in out: for j in range(len(i)): print(i[j], end=" ") print(end='\n') def finding(t,rdl,out): kl1 = 0 kl2 = 0 s1 = 0 s2 = 0 for i in rdl: if i == 1: kl1 += 1 if kl1 == t: s1 += 1 kl1 = 0 kl2 = 0 else: kl2+=1 if kl2 == t: s2 += 1 kl1 = 0 kl2 = 0 if (s1 > 0 or s2 > 0) and (kl1 == 0 and kl2 == 0): if s1 != s2: out.append([max(s1,s2),t]) main() ```
instruction
0
55,171
19
110,342
No
output
1
55,171
19
110,343
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers. To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets. Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options? Input The first line contains a single integer n β€” the length of the sequence of games (1 ≀ n ≀ 105). The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record. Output In the first line print a single number k β€” the number of options for numbers s and t. In each of the following k lines print two integers si and ti β€” the option for numbers s and t. Print the options in the order of increasing si, and for equal si β€” in the order of increasing ti. Examples Input 5 1 2 1 2 1 Output 2 1 3 3 1 Input 4 1 1 1 1 Output 3 1 4 2 2 4 1 Input 4 1 2 1 2 Output 0 Input 8 2 1 2 1 1 1 1 1 Output 3 1 6 2 3 6 1 Submitted Solution: ``` import math n = int(input()) games = list(map(int, input().split())) PETYA = 0 GENA = 1 pontos = [[0], [0]] for i in range(0, n): if (games[i] == 1): pontos[PETYA].append(pontos[PETYA][i] + 1) pontos[GENA].append(pontos[GENA][i]) else: pontos[PETYA].append(pontos[PETYA][i]) pontos[GENA].append(pontos[GENA][i] + 1) res = [] k = 0 impossivel = False if ((pontos[PETYA] > pontos[GENA] and games[-1] == 1) or (pontos[PETYA] < pontos[GENA] and games[-1] == 2)): impossivel = True if (pontos[PETYA][-1] != pontos[GENA][-1] and not impossivel): maxPoints = max(pontos[PETYA][-1], pontos[GENA][-1]) for i in range(1, maxPoints + 1): if (maxPoints % i == 0): s = i t = maxPoints // i ganhou = 0 pontos[PETYA][0] = 0 pontos[GENA][0] = 0 while (ganhou < n): try: petya = pontos[PETYA].index(pontos[PETYA][0] + t) except ValueError: petya = n + t try: gena = pontos[GENA].index(pontos[GENA][0] + t) except ValueError: gena = n + t ganhou = min(petya, gena) if (ganhou < n): pontos[PETYA][0] = pontos[PETYA][ganhou] pontos[GENA][0] = pontos[GENA][ganhou] if (ganhou == n): res.append((s, t)) print(len(res)) for i in range(0, len(res)): x = res[i] print("{} {}".format(int(x[0]), int(x[1]))) ```
instruction
0
55,172
19
110,344
No
output
1
55,172
19
110,345
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers. To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets. Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options? Input The first line contains a single integer n β€” the length of the sequence of games (1 ≀ n ≀ 105). The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record. Output In the first line print a single number k β€” the number of options for numbers s and t. In each of the following k lines print two integers si and ti β€” the option for numbers s and t. Print the options in the order of increasing si, and for equal si β€” in the order of increasing ti. Examples Input 5 1 2 1 2 1 Output 2 1 3 3 1 Input 4 1 1 1 1 Output 3 1 4 2 2 4 1 Input 4 1 2 1 2 Output 0 Input 8 2 1 2 1 1 1 1 1 Output 3 1 6 2 3 6 1 Submitted Solution: ``` import math def isok(n,i,j,a): count_s_1 = 0 count_s_2 = 0 count__1 = 0 count__2 = 0 c1 = 0 for f in range(len(a)): if (a[f] == 1): count__1 = count__1 + 1 else: count__2 = count__2 + 1 if (((count__1 % j == 0) and (count__1 != 0)) or ((count__2 % j == 0) and (count__2 != 0))): if (count__1 > count__2): count_s_1 = count_s_1 + 1 elif (count__2 > count__1): count_s_2 = count_s_2 + 1 else: if (a[i] == 1): count_s_2 = count_s_2 + 1 else: count_s_1 = count_s_1 + 1 count__1 = 0 count__2 = 0 if ((count_s_1 == i) or (count_s_2 == i)): c1 = -1 break if ((count_s_1 != count_s_2) and ((count_s_1 == i) or (count_s_2 == i)) and (f >= len(a)-1)): return True else: return False n = int(input()) a = list(map(int,input().split())) b = [] s = 0 count_1 = 0 count_2 = 0 h = 0 for i in range(len(a)): if (a[i] == 1): count_1 = count_1 + 1 else: count_2 = count_2 + 1 for i in range(1,max(count_1,count_2)+1): h = math.floor(n / i) for j in range(1,h+1): if (count_1 == (j * i)): if (count_2 < count_1): if (isok(n,i,j,a) == True): s = s + 1 b.append([i,j]) if (count_2 == (j * i)): if (count_1 < count_2): if (isok(n,i,j,a) == True): s = s + 1 b.append([i,j]) print(s) if (s != 0): for i in range(len(b)): print(*(b[i])) ```
instruction
0
55,173
19
110,346
No
output
1
55,173
19
110,347
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal.
instruction
0
55,190
19
110,380
Tags: implementation, math, number theory Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) for i in range(len(a)) : while(a[i]%2==0) : a[i] = a[i]//2 while(a[i]%3==0) : a[i] = a[i]//3 flag = 1 #print(a) for i in range(1,len(a)) : if a[i]!=a[0] : print("No") flag = 0 break if flag : print("Yes") ```
output
1
55,190
19
110,381
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal.
instruction
0
55,191
19
110,382
Tags: implementation, math, number theory Correct Solution: ``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- # AUTHOR: haya14busa import sys import io def div(n): while n % 2 == 0: n //= 2 while n % 3 == 0: n //= 3 return n def solve(n, bs): # n: n players (including Limak himself) # bs: the bids of players. i-th of them has bid with size b_i dollars. assert 2 <= n <= 10 ** 5 bs = list(set(bs)) for b in bs: assert 1 <= b <= 10 ** 9 b, rest = bs[0], bs[1:] v = div(b) for r in rest: if div(r) != v: return 'No' return 'Yes' def getinput(): def getint(): return int(input()) def getints_line(): return list(map(int, input().split(' '))) def getints(n): return [getint() for _ in range(n)] def getints_lines(n): return [getints_line() for _ in range(n)] return [getint(), getints_line()] def iosolve(): return str(solve(*getinput())) # return 'YES' if solve(*getinput()) else 'NO' # for boolean output # return '\n'.join(map(str, solve(*getinput()))) # for multiple line output def main(): if sys.stdin.isatty(): test() stdin_lines = getstdin_lines() sys.stdin = io.StringIO('\n'.join(stdin_lines)) if stdin_lines: print(iosolve()) else: test() def test(): IO_TEST_CASES = [ ( # INPUT '''\ 4 75 150 75 50 ''', # EXPECT '''\ Yes ''' ), ( # INPUT '''\ 3 100 150 250 ''', # EXPECT '''\ No ''' ), ] # List[(List[arg for solve()], expect)] TEST_CASES = [ # ([], None), ] # You do need to see below import unittest # to save memory, import only if test required import sys import io class Assert(unittest.TestCase): def equal(self, a, b): self.assertEqual(a, b) def float_equal(self, actual, expect, tolerance): self.assertTrue(expect - tolerance < actual < expect + tolerance) art = Assert() for inputs, expect in TEST_CASES: art.equal(solve(*inputs), expect) for stdin, expect in IO_TEST_CASES: sys.stdin = io.StringIO(stdin.strip()) art.equal(iosolve(), expect.strip()) # art.float_equal(float(iosolve()), float(expect.strip()), 10 ** -6) def getstdin_lines(): stdin = [] while 1: try: stdin.append(input()) except EOFError: break return stdin if __name__ == '__main__': main() ```
output
1
55,191
19
110,383
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal.
instruction
0
55,192
19
110,384
Tags: implementation, math, number theory Correct Solution: ``` N = int(input()) x = [int(i) for i in input().split()] for i in range(N): while x[i] % 2 == 0: x[i] //= 2 while x[i] % 3 == 0: x[i] //= 3 T = True for i in range(N - 1): if x[i] != x[i+1]: T = False print('Yes' if T else 'No') ```
output
1
55,192
19
110,385
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal.
instruction
0
55,193
19
110,386
Tags: implementation, math, number theory Correct Solution: ``` def fun(num): while num%2==0: num//=2 while num%3==0: num//=3 return num n=int(input()); flag=True; p=0; c=0 for i in input().split(): if p==0: p=fun(int(i)) else: c=fun(int(i)) if c!=p: flag=False print("Yes") if flag else print("No") ```
output
1
55,193
19
110,387
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal.
instruction
0
55,194
19
110,388
Tags: implementation, math, number theory Correct Solution: ``` import fractions n = int(input()) l = [int(i) for i in input().split()] gcd = l[0] for i in range(1,n): gcd = fractions.gcd(gcd,l[i]) while gcd%2 == 0 : gcd/=2 while gcd%3 == 0 : gcd/=3 ans = 'YES' for i in range(n): k = l[i] t=1 t1=1 while k%(2*t) == 0 : t=t*2 while k%(t1*3) == 0: t1=t1*3 if gcd*t*t1 != l[i] : ans = 'NO' break print(ans) ```
output
1
55,194
19
110,389
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal.
instruction
0
55,195
19
110,390
Tags: implementation, math, number theory Correct Solution: ``` def divide(n): while True: if(n%2 == 0): n//=2 elif(n%3 == 0): n//=3 else: break return n n = int(input()) l = [int(i) for i in input().split()] itJustWorks = True rem = divide(l[0]) for i in range(1,n): if(rem!=divide(l[i])): itJustWorks = False break print("Yes") if itJustWorks else print("No") ```
output
1
55,195
19
110,391
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal.
instruction
0
55,196
19
110,392
Tags: implementation, math, number theory Correct Solution: ``` from sys import stdin, stdout input = stdin.readline def nod(a,b): if b == 0: return a else: return nod(b, a%b) def good(a): while a > 1: if a % 2 == 0: a //= 2 elif a % 3 == 0: a //= 3 else: return False return True n = int(input()) a = list(map(int, input().split())) n = a[0] for el in a[1:]: n = nod(n, el) ans = None if all(list(map(lambda x: good(x // n), a))): ans = "Yes" else: ans = "No" stdout.write(ans) ```
output
1
55,196
19
110,393
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal.
instruction
0
55,197
19
110,394
Tags: implementation, math, number theory Correct Solution: ``` from sys import stdin,stdout from collections import Counter from math import ceil from bisect import bisect_left from bisect import bisect_right import math ai = lambda: list(map(int, stdin.readline().split())) ei = lambda: map(int, stdin.readline().split()) ip = lambda: int(stdin.readline().strip()) n = ip() li = ai() for i in range(n): while li[i] % 2==0: li[i] //= 2 while li[i] %3 == 0:li[i] //= 3 for i in range(1,n): if li[i] != li[0]: exit(print('No')) print('Yes') ```
output
1
55,197
19
110,395
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal. Submitted Solution: ``` def solve(arr,n): for i in range(n): while(arr[i] % 2 == 0): arr[i] //= 2 while(arr[i] % 3 == 0): arr[i] //= 3 arr.sort() if arr[0] == arr[-1]: print('Yes') else: print('No') n = int(input()) arr = list(map(int,input().split())) solve(arr,n) ```
instruction
0
55,198
19
110,396
Yes
output
1
55,198
19
110,397
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal. Submitted Solution: ``` n=int(input()) x=[int(i) for i in input().split(' ')] for j in range(len(x)): while x[j]%2==0: x[j]/=2 while x[j]%3==0: x[j]/=3 if len(set(x))==1: print('Yes') else: print('No') ```
instruction
0
55,199
19
110,398
Yes
output
1
55,199
19
110,399
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal. Submitted Solution: ``` def dividers(n): if n == 1: return set() elif n == 2: a = set() a.add(2) return a elif n == 3: a = set() a.add(3) return a i = 2 dividers = [] while n > 1 and i * i <= n: if n % i == 0: if (i in dividers): if (i != 2 and i != 3): dividers.append(i) else: dividers.append(i) n //= i continue i += 1 if i * i > n and n != 2 and n != 3: dividers.append(n) ans = set() i1 = 0 i2 = 0 while i2 < len(dividers): if dividers[i2] != dividers[i1]: ans.add(dividers[i1] ** (i2 - i1)) i1 = i2 else: i2 += 1 ans.add(dividers[i1] ** (i2 - i1)) return ans n = int(input()) mas = list(set(int(e) for e in input().split())) mas[0] = dividers(mas[0]) ans = True for_help = set() for_help.add(2) for_help.add(3) for i in range(len(mas) - 1): mas[i + 1] = dividers(mas[i + 1]) if len((mas[i] ^ mas[i + 1]) - for_help) != 0: ans = False break if ans: print("Yes") else: print("No") ```
instruction
0
55,200
19
110,400
Yes
output
1
55,200
19
110,401
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal. Submitted Solution: ``` def main(): gcd = lambda a, b: a if b == 0 else gcd(b, a % b) n = int(input()) a = list(map(int, input().split())) G = a[0] for i in range(n): while a[i] % 3 == 0: a[i] //= 3 while a[i] % 2 == 0: a[i] //= 2 G = gcd(a[i], G) f = not bool(sum(G != a[i] for i in range(n))) print('Yes' if f else 'No') main() ```
instruction
0
55,201
19
110,402
Yes
output
1
55,201
19
110,403
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal. Submitted Solution: ``` def p(n): result = 1 i = 2 while i < n + 1: if n % i == 0 and i != 2 and i != 3: result *= i n /= i if n % i != 0 or i in [2, 3]: i += 1 return result ### n = int(input()) array = list(map(int, input().split())) f = True matrix = [] a = p(array[0]) for i in array: if i % a != 0: f = False if f: print("Yes") else: print("No") ```
instruction
0
55,202
19
110,404
No
output
1
55,202
19
110,405
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal. Submitted Solution: ``` from fractions import gcd a=int(input()) strinp=str(input()).split() lis=[] for h in range(a): lis.append(int(strinp[h])) res = gcd(*lis[:2]) #get the gcd of first two numbers for x in lis[2:]: #now iterate over the list starting from the 3rd element res = gcd(res,x) p=lis[0] for h in range(1,a): p=(p*lis[h])/res z=int(p) if(z%6==0): print("Yes") else: print("No") ```
instruction
0
55,203
19
110,406
No
output
1
55,203
19
110,407
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal. Submitted Solution: ``` #In the name of Allah from sys import stdin, stdout input = stdin.readline n = int(input()) a = list(map(int, input().split())) def fac(n): ans = [] for i in range(2, int(n ** .5) + 1): while n % i == 0: ans.append(i) n //= i return ans ma = max(a) for i in a: if ma % i == 0: f = fac(ma // i) else: stdout.write("No") exit(0) if f == []: continue n = len(f) for i in f: if i == 2 or i == 3: n -= 1 if n != 0: stdout.write("No") exit(0) stdout.write("Yes") ```
instruction
0
55,204
19
110,408
No
output
1
55,204
19
110,409
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot? Input First line of input contains an integer n (2 ≀ n ≀ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players. Output Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise. Examples Input 4 75 150 75 50 Output Yes Input 3 100 150 250 Output No Note In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid. It can be shown that in the second sample test there is no way to make all bids equal. Submitted Solution: ``` n = int(input()) a = [] for x in input().split(): bid = int(x) while bid % 2 == 0: bid /= 2 while bid % 3 == 0: bid /= 3 a.append(bid) val = a[1] jackpot = True for i in range(2, n): if a[i] != val: jackpot = False break if jackpot: print ("YES") else: print ("NO") ```
instruction
0
55,205
19
110,410
No
output
1
55,205
19
110,411
Provide tags and a correct Python 3 solution for this coding contest problem. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.
instruction
0
55,302
19
110,604
Tags: bitmasks, brute force, dfs and similar, expression parsing, implementation Correct Solution: ``` def func(b0, b1, op): if op == 0: return b0 ^ b1 if op == 1: return b0 | b1 if op == 2: return b0 & b1 n, m = [int(x) for x in input().split()] cs = {} ops = ['XOR', 'OR', 'AND'] fs = [] for j in range(n): name, value = input().split(" := ") value = value.split() if len(value) == 1: s = value[0] cs[name] = s else: lt = value[0] rt = value[2] op = ops.index(value[1]) fs.append((name, lt, rt, op)) mins = '' maxs = '' d0 = {'?':0} d1 = {'?':1} for i in range(m): for name, value in cs.items(): d0[name] = int(value[i]) d1[name] = int(value[i]) s0 = 0 s1 = 0 for name, lt, rt, op in fs: b00 = d0[lt] b01 = d0[rt] b10 = d1[lt] b11 = d1[rt] c0 = func(b00, b01, op) c1 = func(b10, b11, op) s0 += (1 if c0 else 0) s1 += (1 if c1 else 0) d0[name] = c0 d1[name] = c1 mins += '1' if s1 < s0 else '0' maxs += '1' if s1 > s0 else '0' print(mins) print(maxs) ```
output
1
55,302
19
110,605
Provide tags and a correct Python 3 solution for this coding contest problem. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.
instruction
0
55,303
19
110,606
Tags: bitmasks, brute force, dfs and similar, expression parsing, implementation Correct Solution: ``` import sys def calc(b0, b1, q): if q == 0: return b0 ^ b1 if q == 1: return b0 | b1 if q == 2: return b0 & b1 n, m = map(int,sys.stdin.readline().split()) arr1 = {} opt = ['XOR', 'OR', 'AND'] arr2 = [] for j in range(n): a, b = list(map(str,sys.stdin.readline().split(" := "))) b = b.split() if len(b) == 1: s = b[0] arr1[a] = s else: c = b[0] d = b[2] q = opt.index(b[1]) arr2.append((a, c, d, q)) mins = '' maxs = '' d0 = {'?':0} d1 = {'?':1} for i in range(m): for a, b in arr1.items(): d0[a] = int(b[i]) d1[a] = int(b[i]) s0 = 0 s1 = 0 for a, c, d, q in arr2: b00 = d0[c] b01 = d0[d] b10 = d1[c] b11 = d1[d] c0 = calc(b00, b01, q) c1 = calc(b10, b11, q) s0 += (1 if c0 else 0) s1 += (1 if c1 else 0) d0[a] = c0 d1[a] = c1 if s1 < s0: mins += "1" else: mins += "0" if s1 > s0: maxs += "1" else: maxs += "0" sys.stdout.write("{0}\n{1}".format(mins,maxs)) ```
output
1
55,303
19
110,607
Provide tags and a correct Python 3 solution for this coding contest problem. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.
instruction
0
55,304
19
110,608
Tags: bitmasks, brute force, dfs and similar, expression parsing, implementation Correct Solution: ``` #from math import * from sys import * from decimal import * def main(): n,k=(int(z) for z in stdin.readline().split()) d=[] nm=[0]*n bt1=[False]*(n+1) bt2=[False]*(n+1) bt2[-1]=True nam=dict() nam["?"]=-1 for i in range(n): fl=0 s=stdin.readline()[:-1].split(" := ") nam[s[0]]=i if len(s[1])<30: for j in s[1]: if j=="A": fl=1 break if j=="X": fl=2 break if j=="O": fl=3 break if fl==0: d.append([nam[s[0]],s[1]]) elif fl==1: d.append([i]+[nam[z] for z in s[1].split(" AND ")]) elif fl==2: d.append([i]+[nam[z] for z in s[1].split(" XOR ")]) else: d.append([i]+[nam[z] for z in s[1].split(" OR ")]) nm[i]=fl mn=[False]*k mx=[False]*k for i in range(k): r1=0 r2=0 for ololo in range(n): eq=d[ololo] #print(bt1,bt2) if nm[ololo]==0: bt1[eq[0]]=bool(int(eq[1][i])) r1+=int(eq[1][i]) bt2[eq[0]]=bool(int(eq[1][i])) r2+=int(eq[1][i]) #print(int(bool(eq[1][i]))) elif nm[ololo]==1: if bt1[eq[1]]==bt1[eq[2]]==True: bt1[eq[0]]=True r1+=1 else: bt1[eq[0]]=False if bt2[eq[1]]==bt2[eq[2]]==True: bt2[eq[0]]=True r2+=1 else: bt2[eq[0]]=False elif nm[ololo]==2: #print(bt1[eq[1]],eq,bt1[eq[2]]) if bt1[eq[1]]!=bt1[eq[2]]: bt1[eq[0]]=True r1+=1 else: bt1[eq[0]]=False #print("wev",int(bt1[eq[0]])) if bt2[eq[1]]!=bt2[eq[2]]: bt2[eq[0]]=True r2+=1 else: bt2[eq[0]]=False #print('wfeaerhbjds',int(bt2[eq[0]])) else: if bt1[eq[1]]!=bt1[eq[2]] or bt1[eq[2]]!=False: bt1[eq[0]]=True r1+=1 else: bt1[eq[0]]=False if bt2[eq[1]]!=bt2[eq[2]] or bt2[eq[2]]!=False: bt2[eq[0]]=True r2+=1 else: bt2[eq[0]]=False #print(r1,r2,mn,mx) if r2>r1: mn[i]=True elif r2<r1: mx[i]=True stdout.write(''.join( (str(int(z)) for z in mx) ) + '\n') stdout.write(''.join( (str(int(z)) for z in mn) ) + '\n') main() ```
output
1
55,304
19
110,609
Provide tags and a correct Python 3 solution for this coding contest problem. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.
instruction
0
55,305
19
110,610
Tags: bitmasks, brute force, dfs and similar, expression parsing, implementation Correct Solution: ``` n, m = map(int, input().split()) v = [('?', '')] temp = [(0, 1)] d = {} d['?'] = 0 mn, mx = '', '' for i in range(n): name, val = input().split(' := ') v.append((name, val.split())) temp.append((-1, -1)) d[name] = i + 1 def eval(expr, bit1, bit2): if expr == 'OR': return bit1 | bit2 elif expr == 'AND': return bit1 and bit2 elif expr == 'XOR': return bit1 ^ bit2 else: raise AttributeError() for i in range(m): for name, expr in v[1:]: j = d[name] if len(expr) == 1: temp[j] = (int(expr[0][i]), int(expr[0][i])) else: bit1, bit2 = temp[d[expr[0]]], temp[d[expr[2]]] temp[j] = (eval(expr[1], bit1[0], bit2[0]), eval(expr[1], bit1[1], bit2[1])) z, o = sum(temp[_][0] for _ in range(1, n + 1)), sum(temp[_][1] for _ in range(1, n + 1)) mn += '1' if o < z else '0' mx += '1' if o > z else '0' print(mn) print(mx) ```
output
1
55,305
19
110,611
Provide tags and a correct Python 3 solution for this coding contest problem. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.
instruction
0
55,306
19
110,612
Tags: bitmasks, brute force, dfs and similar, expression parsing, implementation Correct Solution: ``` f = {'OR': lambda x, y: x | y, 'AND': lambda x, y: x & y, 'XOR': lambda x, y: x ^ y} n, m = map(int, input().split()) u, v = [], [] d = {'?': n} for i in range(n): q, s = input().split(' := ') if ' ' in s: x, t, y = s.split() u += [(i, d[x], f[t], d[y])] else: v += [(i, s)] d[q] = i s = [0] * (n + 1) def g(k, j): s[n] = k for i, q in v: s[i] = q[j] == '1' for i, x, f, y in u: s[i] = f(s[x], s[y]) return sum(s) - k a = b = '' for j in range(m): d = g(1, j) - g(0, j) a += '01'[d < 0] b += '01'[d > 0] print(a, b) ```
output
1
55,306
19
110,613
Provide tags and a correct Python 3 solution for this coding contest problem. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.
instruction
0
55,307
19
110,614
Tags: bitmasks, brute force, dfs and similar, expression parsing, implementation Correct Solution: ``` #from math import * from sys import * from decimal import * n,k=(int(z) for z in stdin.readline().split()) d=[] #bt1=dict() #bt2=dict() nm=[0]*n bt1=[False]*(n+1) bt2=[False]*(n+1) bt2[-1]=True nam=dict() nam["?"]=-1 #gd=set() #agd=set() #xogd=set() for i in range(n): fl=0 s=stdin.readline()[:-1].split(" := ") nam[s[0]]=i if len(s[1])<30: for j in s[1]: if j=="A": fl=1 break if j=="X": fl=2 break if j=="O": fl=3 break if fl==0: d.append([nam[s[0]],s[1]]) elif fl==1: d.append([i]+[nam[z] for z in s[1].split(" AND ")]) elif fl==2: d.append([i]+[nam[z] for z in s[1].split(" XOR ")]) else: d.append([i]+[nam[z] for z in s[1].split(" OR ")]) nm[i]=fl mn=[False]*k mx=[False]*k for i in range(k): r1=0 r2=0 for ololo in range(n): eq=d[ololo] #print(bt1,bt2) if nm[ololo]==0: bt1[eq[0]]=bool(int(eq[1][i])) r1+=int(eq[1][i]) bt2[eq[0]]=bool(int(eq[1][i])) r2+=int(eq[1][i]) #print(int(bool(eq[1][i]))) elif nm[ololo]==1: if bt1[eq[1]]==bt1[eq[2]]==True: bt1[eq[0]]=True r1+=1 else: bt1[eq[0]]=False if bt2[eq[1]]==bt2[eq[2]]==True: bt2[eq[0]]=True r2+=1 else: bt2[eq[0]]=False elif nm[ololo]==2: #print(bt1[eq[1]],eq,bt1[eq[2]]) if bt1[eq[1]]!=bt1[eq[2]]: bt1[eq[0]]=True r1+=1 else: bt1[eq[0]]=False #print("wev",int(bt1[eq[0]])) if bt2[eq[1]]!=bt2[eq[2]]: bt2[eq[0]]=True r2+=1 else: bt2[eq[0]]=False #print('wfeaerhbjds',int(bt2[eq[0]])) else: if bt1[eq[1]]!=bt1[eq[2]] or bt1[eq[2]]!=False: bt1[eq[0]]=True r1+=1 else: bt1[eq[0]]=False if bt2[eq[1]]!=bt2[eq[2]] or bt2[eq[2]]!=False: bt2[eq[0]]=True r2+=1 else: bt2[eq[0]]=False #print(r1,r2,mn,mx) if r2>r1: mn[i]=True elif r2<r1: mx[i]=True stdout.write(''.join( (str(int(z)) for z in mx) ) + '\n') stdout.write(''.join( (str(int(z)) for z in mn) ) + '\n') ```
output
1
55,307
19
110,615
Provide tags and a correct Python 3 solution for this coding contest problem. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.
instruction
0
55,308
19
110,616
Tags: bitmasks, brute force, dfs and similar, expression parsing, implementation Correct Solution: ``` n, m = map(int, input().split()) vars = {} def mxor(a, b): if a == b: return '0' elif (a == '0' and b == '1') or (a == '1' and b =='0'): return '1' elif (a == '0' and b == 'x') or (a == 'x' and b == '0'): return 'x' elif (a == '0' and b == '!') or (a == '!' and b == '0'): return '!' elif (a == '1' and b == 'x') or (a == 'x' and b == '1'): return '!' elif (a == '1' and b == '!') or (a == '!' and b == '1'): return 'x' elif (a == 'x' and b == '!') or (a == '!' and b == 'x'): return '1' def mand(a, b): if a == b: return a elif (a == '0' or b =='0'): return '0' elif (a == '1' and b == 'x') or (a == 'x' and b == '1'): return 'x' elif (a == '1' and b == '!') or (a == '!' and b == '1'): return '!' elif (a == 'x' and b == '!') or (a == '!' and b == 'x'): return '0' def mor(a, b): if a == b: return a if a == '1' or b == '1': return '1' elif a == '0': return b elif b == '0': return a elif (a == 'x' and b == '!') or (a == '!' and b == 'x'): return '1' def calc(a, op, b): global m global vars a = (['x']*m if a == '?' else vars[a]) b = (['x']*m if b == '?' else vars[b]) if op == 'XOR': op = mxor elif op == 'AND': op = mand else: # op == 'OR': op = mor return ''.join([op(x, y) for x, y in zip(a, b)]) for _ in range(n): i = input().split() if len(i) == 3: vars[i[0]] = i[2] else: vars[i[0]] = calc(*i[2:]) r = [0] * m for i in range(m): for v in vars.values(): if v[i] == 'x': r[i] += 1 elif v[i] == '!': r[i] -= 1 mmin = ['0'] * m mmax = ['0'] * m for i in range(m): if r[i] < 0: mmin[i] = '1' for i in range(m): if r[i] > 0: mmax[i] = '1' print(''.join(mmin)) print(''.join(mmax)) ```
output
1
55,308
19
110,617
Provide tags and a correct Python 3 solution for this coding contest problem. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.
instruction
0
55,309
19
110,618
Tags: bitmasks, brute force, dfs and similar, expression parsing, implementation Correct Solution: ``` def sumBin(a, b, varMap): lhs = varMap[a] rhs = varMap[b] return bin(int(lhs, 2) + int(rhs, 2))[2:] def andBin(a, b, varMap): lhs = varMap[a] rhs = varMap[b] return bin(int(lhs, 2) & int(rhs, 2))[2:] def orBin(a, b, varMap): lhs = varMap[a] rhs = varMap[b] return bin(int(lhs, 2) | int(rhs, 2))[2:] def xorBin(a, b, varMap): lhs = varMap[a] rhs = varMap[b] return bin(int(lhs, 2) ^ int(rhs, 2))[2:] mapOper = {'AND': andBin, 'OR': orBin, 'XOR' : xorBin} n, m = map(int, input().split()) minMap = {"?": "0", "": "0"} maxMap = {"?": "1"*m, "": "0"} minSum = "0" maxSum = "0" for _ in range(n): name, _, expr = input().split(' ', 2) if len(expr.split(' ')) == 1: minMap[name] = expr maxMap[name] = expr else: lhs, oper, rhs = expr.split() minMap[name] = mapOper[oper](lhs, rhs, minMap).zfill(m) maxMap[name] = mapOper[oper](lhs, rhs, maxMap).zfill(m) minSum = sumBin("", name, minMap) maxSum = sumBin("", name, maxMap) def countOnes(i, varMap): ones = 0 for name, num in varMap.items(): if name != "?" and name != "": ones += num[i] == "1" return ones minRes = "" maxRes = "" for i in range(m): zeroOnes = countOnes(i, minMap) oneOnes = countOnes(i, maxMap) if zeroOnes > oneOnes: maxRes += "0" minRes += "1" elif zeroOnes < oneOnes: maxRes += "1" minRes += "0" else: maxRes += "0" minRes += "0" print (minRes) print (maxRes) ```
output
1
55,309
19
110,619
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. Submitted Solution: ``` f = {'OR': lambda x, y: x | y, 'AND': lambda x, y: x & y, 'XOR': lambda x, y: x ^ y} n, m = map(int, input().split()) p, u, v = [], [], [] l = {'?': n} for i in range(n): q, s = input().split(' := ') if ' ' in s: x, t, y = s.split() p += [(l[x], f[t], l[y])] u += [i] else: p += [int(s, 2)] v += [i] l[q] = i s = [0] * (n + 1) def g(k, l): s[n] = k for i in v: s[i] = (p[i] & l) > 0 for i in u: x, f, y = p[i] s[i] = f(s[x], s[y]) return sum(s) - k a = b = '' for j in range(m): l = 1 << m - j - 1 x, y = g(0, l), g(1, l) a += '01'[y < x] b += '01'[y > x] print(a, b) ```
instruction
0
55,310
19
110,620
Yes
output
1
55,310
19
110,621
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. Submitted Solution: ``` def OP(i,j,op): if op == "AND": return i & j if op == "OR": return i | j if op == "XOR": return i ^ j return 0 def totbit(i,test): ans = 0 for j in range(0,len(ops)): a = ops[j][0] b = ops[j][1] op = ops[j][2] if a == "?": x = test else: if a in M: x = int(M[a][i]) else: x = OL[OD[a]] if b == "?": y = test else: if b in M: y = int(M[b][i]) else: y = OL[OD[b]] ans += OP(x,y,op) OL[j] = OP(x,y,op) return ans ops = [] [n,m] = list(map(int , input().split())) M = dict() OL = [] OD = dict() for i in range(0,n): inp = input().split() a = inp[0] if len(inp) == 3: b = inp[2] M[a] = b else: a = inp[2] b = inp[4] op = inp[3] OD[inp[0]] = len(OL) OL.append(0) ops.append([a,b,op]) mi = "" ma = "" for i in range(0,m): b0 = totbit(i,0) b1 = totbit(i,1) if b0 >= b1: ma += "0" else: ma += "1" if b0 <= b1: mi += "0" else: mi += "1" print(mi) print(ma) ```
instruction
0
55,311
19
110,622
Yes
output
1
55,311
19
110,623
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. Submitted Solution: ``` import re def res(op, l, r): if op == 'A': return l == '1' and r == '1' if op == 'X': return l != r return l == '1' or r == '1' def getzx(cc, i, b, default): try: return cc[i][b] except: return default n, m = map(int, input().split()) pattern = re.compile(r' ([A-Z])[A-Z]+ ') constants = {} commands = [] for _ in range(n): var, r = input().split(' := ', 1) try: q, op, w = pattern.split(r, 1) except: constants[var] = r continue if q in constants and w in constants: constants[var] = ['1' if res(op, z, x) else '0' for z, x in zip(q, w)] else: commands.append((op, q, w, var)) ansmin = '' ansmax = '' for b in range(m): sum0 = 0 sum1 = 0 cc = constants.copy() for op, q, w, var in commands: z = getzx(cc, q, b, '0') x = getzx(cc, w, b, '0') cc[var] = res(op, z, x) sum0 += cc[var] cc = constants.copy() for op, q, w, var in commands: z = getzx(cc, q, b, '1') x = getzx(cc, w, b, '1') cc[var] = res(op, z, x) sum1 += cc[var] ansmin += '0' if sum0 <= sum1 else '1' ansmax += '0' if sum1 <= sum0 else '1' print(ansmin) print(ansmax) ```
instruction
0
55,312
19
110,624
No
output
1
55,312
19
110,625
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. Submitted Solution: ``` def res(op, l, r): if op == 'a': return l == '1' and r == '1' if op == 'x': return l != r return l == '1' or r == '1' n, m = map(int, input().split()) constants = {} commands = [] for _ in range(n): var, r = input().split(' := ', 1) if '?' not in r: constants[var] = r else: if 'AND' in r: q, w = r.split(' AND ', 1) op = 'a' elif 'XOR' in r: q, w = r.split(' XOR ', 1) op = 'x' else: q, w = r.split(' OR ', 1) op = 'o' if q in constants and w in constants: constants[var] = ''.join('1' if res(op, z, x) else '0' for z, x in zip(q, w)) else: commands.append((op, q, w, var)) ansmin = '' ansmax = '' for b in range(m): sum0 = 0 sum1 = 0 default = '0' cc = constants.copy() for op, q, w, var in commands: z = default if q == '?' else cc[q][b] x = default if w == '?' else cc[w][b] cc[var] = res(op, z, x) sum0 += cc[var] default = '1' cc = constants.copy() for op, q, w, var in commands: z = default if q == '?' else cc[q][b] x = default if w == '?' else cc[w][b] cc[var] = res(op, z, x) sum1 += cc[var] ansmin += '0' if sum0 <= sum1 else '1' ansmax += '0' if sum1 <= sum0 else '1' print(ansmin) print(ansmax) ```
instruction
0
55,313
19
110,626
No
output
1
55,313
19
110,627
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. Submitted Solution: ``` #!/usr/bin/env python3 from copy import deepcopy AND = 0 OR = 1 XOR = 2 INT = 3 N, M = list(map(int, input().split())) variables_dict = {} variables = [] op1 = [] op2 = [] op = [] for i in range(N): l = input().split() variables.append(l[0]) variables_dict[l[0]] = i op1.append(l[2]) ope = INT if len(l) == 5: if l[3] == 'AND': ope = AND elif l[3] == 'OR': ope = OR elif l[3] == 'XOR': ope = XOR op.append(ope) op2.append(l[4]) else: op.append(ope) op2.append(None) # => bit[m] = 0, bit[m] = 1 def calc_and(op1, op2): if op1 == '?' and op2 == '?': return (0, 1) elif op1 == '?': return (0, 1) if op2 == '1' else (0, 0) elif op2 == '?': return (0, 1) if op1 == '1' else (0, 0) else: return (1, 1) if op1 == '1' and op2 == '1' else (0, 0) def calc_or(op1, op2): if op1 == '?' and op2 == '?': return (0, 1) elif op1 == '?': return (1, 1) if op2 == '1' else (0, 1) elif op2 == '?': return (1, 1) if op1 == '1' else (0, 1) else: return (1, 1) if op1 == '1' or op2 == '1' else (1, 1) def calc_xor(op1, op2): if op1 == '?' and op2 == '?': return (0, 0) elif op1 == '?': return (1, 0) if op2 == '1' else (0, 1) elif op2 == '?': return (1, 0) if op1 == '1' else (0, 1) else: return (0, 0) if op1 == op2 else (1, 1) def get_value(op, values, m): if op in values: return values[op] elif op is not None: return op[m] if len(op) > 1 else op return None ans_max = "" ans_min = "" for m in range(M): one = 0 zero = 0 values_zero = {} values_one = {} for j in range(N): val = variables[j] ao, bo = get_value(op1[j], values_one, m), get_value( op2[j], values_one, m) az, bz = get_value(op1[j], values_zero, m), get_value( op2[j], values_zero, m) if op[j] == INT: if op1[j] == '?': values_one[val] = '1' values_zero[val] = '0' else: if op1[0] == '0' or op1[0] == '1': values_one[val] = op1[0][m] values_zero[val] = op1[0][m] else: values_one[val] = ao values_zero[val] = az elif op[j] == AND: _, o = calc_and(ao, bo) z, _ = calc_and(az, bz) one += o zero += z values_one[val] = '1' if o == 1 else '0' values_zero[val] = '1' if z == 1 else '0' elif op[j] == OR: _, o = calc_or(ao, bo) z, _ = calc_or(az, bz) one += o zero += z values_one[val] = '1' if o == 1 else '0' values_zero[val] = '1' if z == 1 else '0' elif op[j] == XOR: _, o = calc_xor(ao, bo) z, _ = calc_xor(az, bz) one += o zero += z values_one[val] = '1' if o == 1 else '0' values_zero[val] = '1' if z == 1 else '0' #print(zero, one, values_zero, values_one) ans_min += ('0' if zero <= one else '1') ans_max += ('0' if zero >= one else '1') print(ans_min) print(ans_max) ```
instruction
0
55,314
19
110,628
No
output
1
55,314
19
110,629
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly m bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Output 011 100 Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Output 0 0 Note In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. Submitted Solution: ``` import sys def calc(b0, b1, q): if q == 0: return b0 ^ b1 if q == 1: return b0 | b1 if q == 2: return b0 & b1 n, m = map(int,sys.stdin.readline().split()) arr1 = {} opt = ['XOR', 'OR', 'AND'] arr2 = [] for j in range(n): a, b = list(map(str,sys.stdin.readline().split(" := "))) b = b.split() if len(b) == 1: s = b[0] arr1[a] = s else: c = b[0] d = b[2] q = opt.index(b[1]) arr2.append((a, c, d, q)) mins = '' maxs = '' d0 = {'?':0} d1 = {'?':1} for i in range(m): for a, b in arr1.items(): d0[a] = int(b[i]) d1[a] = int(b[i]) s0 = 0 s1 = 0 for a, c, d, q in arr2: b00 = d0[c] b01 = d0[d] b10 = d1[c] b11 = d1[d] c0 = calc(b00, b01, q) c1 = calc(b10, b11, q) s0 += (1 if c0 else 0) s1 += (1 if c1 else 0) d0[a] = c0 d1[a] = c1 if s1 < s0: mins += "1" maxs += "0" else: mins += "0" maxs += "1" print("{0}\n{1}".format(mins,maxs)) ```
instruction
0
55,315
19
110,630
No
output
1
55,315
19
110,631
Provide tags and a correct Python 3 solution for this coding contest problem. As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter. <image> Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time. Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game? You have to determine the winner of the game for all initial positions of the marbles. Input The first line of input contains two integers n and m (2 ≀ n ≀ 100, <image>). The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≀ v, u ≀ n, v β‰  u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic. Output Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise. Examples Input 4 4 1 2 b 1 3 a 2 4 c 3 4 b Output BAAA ABAA BBBA BBBB Input 5 8 5 3 h 1 2 c 3 1 c 3 2 r 5 1 r 4 3 z 5 4 r 5 2 h Output BABBB BBBBB AABBB AAABA AAAAB Note Here's the graph in the first sample test case: <image> Here's the graph in the second sample test case: <image>
instruction
0
55,356
19
110,712
Tags: dfs and similar, dp, games, graphs Correct Solution: ``` # python3 from functools import lru_cache def readline(): return list(map(int, input().split())) def main(): n, m = readline() edges = [list() for __ in range(n)] for __ in range(m): tokens = input().split() begin, end = map(int, tokens[:2]) weight = ord(tokens[2]) edges[begin - 1].append((end - 1, weight)) @lru_cache(maxsize=None) def first_wins(first, second, lower=0): for (nxt, w) in edges[first]: if w >= lower: if not first_wins(second, nxt, w): return True return False for i in range(n): print("".join("BA"[first_wins(i, j)] for j in range(n))) main() ```
output
1
55,356
19
110,713
Provide tags and a correct Python 3 solution for this coding contest problem. As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter. <image> Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time. Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game? You have to determine the winner of the game for all initial positions of the marbles. Input The first line of input contains two integers n and m (2 ≀ n ≀ 100, <image>). The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≀ v, u ≀ n, v β‰  u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic. Output Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise. Examples Input 4 4 1 2 b 1 3 a 2 4 c 3 4 b Output BAAA ABAA BBBA BBBB Input 5 8 5 3 h 1 2 c 3 1 c 3 2 r 5 1 r 4 3 z 5 4 r 5 2 h Output BABBB BBBBB AABBB AAABA AAAAB Note Here's the graph in the first sample test case: <image> Here's the graph in the second sample test case: <image>
instruction
0
55,357
19
110,714
Tags: dfs and similar, dp, games, graphs Correct Solution: ``` # int(input()) # [int(i) for i in input().split()] import sys sys.setrecursionlimit(20000) def go(v,w,last): if game[v][w][last] >= 0: return(game[v][w][last]) flag = 0 move = 0 for p in edges_out[v]: if p[1] >= last: move = 1 if not go(w,p[0],p[1]): flag = 1 break if not move or not flag: game[v][w][last] = 0 return(0) else: game[v][w][last] = 1 return(1) n,m = [int(i) for i in input().split()] edges_in = [] edges_out = [] for i in range(n): edges_in.append([]) edges_out.append([]) for i in range(m): s1,s2,s3 = input().split() v = int(s1)-1 w = int(s2)-1 weight = ord(s3[0]) - ord('a') + 1 edges_out[v].append((w,weight)) edges_in[w].append((v,weight)) game = [] for i in range(n): tmp1 = [] for j in range(n): tmp2 = [] for c in range(27): tmp2.append(-1) tmp1.append(tmp2) game.append(tmp1) ##for v in range(n): ## for w in range(n): ## for last in range(27): ## go(v,w,last) for v in range(n): s = '' for w in range(n): if go(v,w,0): s = s + 'A' else: s = s + 'B' print(s) # Made By Mostafa_Khaled ```
output
1
55,357
19
110,715
Provide tags and a correct Python 3 solution for this coding contest problem. As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter. <image> Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time. Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game? You have to determine the winner of the game for all initial positions of the marbles. Input The first line of input contains two integers n and m (2 ≀ n ≀ 100, <image>). The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≀ v, u ≀ n, v β‰  u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic. Output Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise. Examples Input 4 4 1 2 b 1 3 a 2 4 c 3 4 b Output BAAA ABAA BBBA BBBB Input 5 8 5 3 h 1 2 c 3 1 c 3 2 r 5 1 r 4 3 z 5 4 r 5 2 h Output BABBB BBBBB AABBB AAABA AAAAB Note Here's the graph in the first sample test case: <image> Here's the graph in the second sample test case: <image>
instruction
0
55,358
19
110,716
Tags: dfs and similar, dp, games, graphs Correct Solution: ``` # int(input()) # [int(i) for i in input().split()] import sys sys.setrecursionlimit(20000) def go(v,w,last): if game[v][w][last] >= 0: return(game[v][w][last]) flag = 0 move = 0 for p in edges_out[v]: if p[1] >= last: move = 1 if not go(w,p[0],p[1]): flag = 1 break if not move or not flag: game[v][w][last] = 0 return(0) else: game[v][w][last] = 1 return(1) n,m = [int(i) for i in input().split()] edges_in = [] edges_out = [] for i in range(n): edges_in.append([]) edges_out.append([]) for i in range(m): s1,s2,s3 = input().split() v = int(s1)-1 w = int(s2)-1 weight = ord(s3[0]) - ord('a') + 1 edges_out[v].append((w,weight)) edges_in[w].append((v,weight)) game = [] for i in range(n): tmp1 = [] for j in range(n): tmp2 = [] for c in range(27): tmp2.append(-1) tmp1.append(tmp2) game.append(tmp1) ##for v in range(n): ## for w in range(n): ## for last in range(27): ## go(v,w,last) for v in range(n): s = '' for w in range(n): if go(v,w,0): s = s + 'A' else: s = s + 'B' print(s) ```
output
1
55,358
19
110,717
Provide tags and a correct Python 3 solution for this coding contest problem. As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter. <image> Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time. Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game? You have to determine the winner of the game for all initial positions of the marbles. Input The first line of input contains two integers n and m (2 ≀ n ≀ 100, <image>). The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≀ v, u ≀ n, v β‰  u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic. Output Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise. Examples Input 4 4 1 2 b 1 3 a 2 4 c 3 4 b Output BAAA ABAA BBBA BBBB Input 5 8 5 3 h 1 2 c 3 1 c 3 2 r 5 1 r 4 3 z 5 4 r 5 2 h Output BABBB BBBBB AABBB AAABA AAAAB Note Here's the graph in the first sample test case: <image> Here's the graph in the second sample test case: <image>
instruction
0
55,359
19
110,718
Tags: dfs and similar, dp, games, graphs Correct Solution: ``` import sys import bisect as bi import math from collections import defaultdict as dd input=sys.stdin.readline sys.setrecursionlimit(10**7) def solve(u,v,k,adj,dp): if(dp[u][v][k]!=-1):return dp[u][v][k] for child in adj[u]: dest,charstored=child[0],child[1] if(charstored<k):continue if(solve(v,dest,charstored,adj,dp)): dp[u][v][k]=0;return 0 dp[u][v][k]=1;return 1 for _ in range(1): n,m=map(int,input().split()) adj=[[] for i in range(n+2)] for i in range(m): a,b,c=map(str,input().split()) adj[int(a)]+=[(int(b),ord(c)-ord('a'))] dp=[[[-1]*30 for i in range(n+2)] for j in range(n+2)] for i in range(n): for j in range(n): s= 'B' if solve(i+1,j+1,0,adj,dp) else 'A' print(s,end="") print() ```
output
1
55,359
19
110,719
Provide tags and a correct Python 3 solution for this coding contest problem. As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter. <image> Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time. Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game? You have to determine the winner of the game for all initial positions of the marbles. Input The first line of input contains two integers n and m (2 ≀ n ≀ 100, <image>). The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≀ v, u ≀ n, v β‰  u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic. Output Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise. Examples Input 4 4 1 2 b 1 3 a 2 4 c 3 4 b Output BAAA ABAA BBBA BBBB Input 5 8 5 3 h 1 2 c 3 1 c 3 2 r 5 1 r 4 3 z 5 4 r 5 2 h Output BABBB BBBBB AABBB AAABA AAAAB Note Here's the graph in the first sample test case: <image> Here's the graph in the second sample test case: <image>
instruction
0
55,360
19
110,720
Tags: dfs and similar, dp, games, graphs Correct Solution: ``` import sys import bisect as bi import math from collections import defaultdict as dd input=sys.stdin.readline sys.setrecursionlimit(10**7) def solve(u,v,k,adj,dp): if(dp[u][v][k]!=-1):return dp[u][v][k] for child in adj[u]: dest,charstored=child[0],child[1] if(charstored<k):continue #agr wo cond fail kr rha h toh me uske doosre child pr call krunga if(solve(v,dest,charstored,adj,dp)): #agr cond true h toh me 2nd player ko execute hone ke liye bhj rha dp[u][v][k]=0;return 0 #aur agr wo true aati h mtlb wo haar gya mtlb second player jeet gya dp[u][v][k]=1;return 1 #jb first player ko chlne ki jagah nhi milti h toh first player haar gya for _ in range(1): n,m=map(int,input().split()) adj=[[] for i in range(n+2)] for i in range(m): a,b,c=map(str,input().split()) adj[int(a)]+=[(int(b),ord(c)-ord('a'))] dp=[[[-1]*30 for i in range(n+2)] for j in range(n+2)] for i in range(n): for j in range(n): s= 'B' if solve(i+1,j+1,0,adj,dp) else 'A' #mtlb kya MAX haar kr aa rha h kya print(s,end="") print() ```
output
1
55,360
19
110,721
Provide tags and a correct Python 3 solution for this coding contest problem. As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter. <image> Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time. Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game? You have to determine the winner of the game for all initial positions of the marbles. Input The first line of input contains two integers n and m (2 ≀ n ≀ 100, <image>). The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≀ v, u ≀ n, v β‰  u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic. Output Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise. Examples Input 4 4 1 2 b 1 3 a 2 4 c 3 4 b Output BAAA ABAA BBBA BBBB Input 5 8 5 3 h 1 2 c 3 1 c 3 2 r 5 1 r 4 3 z 5 4 r 5 2 h Output BABBB BBBBB AABBB AAABA AAAAB Note Here's the graph in the first sample test case: <image> Here's the graph in the second sample test case: <image>
instruction
0
55,361
19
110,722
Tags: dfs and similar, dp, games, graphs Correct Solution: ``` import sys # sys.setrecursionlimit(10**5) n,m=map(int,input().split()) g={i:[] for i in range(1,n+1)} dp=[[[-1]*26 for _ in range(n+1)] for i in range(n+1)] def rec(i,j,ch): if dp[i][j][ord(ch)-ord('a')]!=-1: return dp[i][j][ord(ch)-ord('a')] for x in g[i]: if ord(x[1])>=ord(ch): v=rec(j,x[0],x[1]) if not v: dp[i][j][ord(ch)-ord('a')]=1 return 1 dp[i][j][ord(ch)-ord('a')]=0 return 0 for _ in range(m): line=input().split() a=int(line[0]) b=int(line[1]) c=line[2] g[a].append([b,c]) for i in range(1,n+1): for j in range(1,n+1): print('A',end="") if rec(i,j,'a') else print('B',end="") print() ```
output
1
55,361
19
110,723