text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahubina is tired of so many complicated languages, so she decided to invent a new, simple language. She already made a dictionary consisting of n 3-words. A 3-word is a sequence of exactly 3 lowercase letters of the first 24 letters of the English alphabet (a to x). She decided that some of the letters are vowels, and all the others are consonants. The whole language is based on a simple rule: any word that contains at least one vowel is correct. Iahubina forgot which letters are the vowels, and wants to find some possible correct sets of vowels. She asks Iahub questions. In each question, she will give Iahub a set of letters considered vowels (in this question). For each question she wants to know how many words of the dictionary are correct, considering the given set of vowels. Iahubina wants to know the xor of the squared answers to all the possible questions. There are 224 different questions, they are all subsets of the set of the first 24 letters of the English alphabet. Help Iahub find that number. Input The first line contains one integer, n (1 ≀ n ≀ 104). Each of the next n lines contains a 3-word consisting of 3 lowercase letters. There will be no two identical 3-words. Output Print one number, the xor of the squared answers to the queries. Examples Input 5 abc aaa ada bcd def Output 0 Submitted Solution: ``` print(0) ``` No
85,300
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahubina is tired of so many complicated languages, so she decided to invent a new, simple language. She already made a dictionary consisting of n 3-words. A 3-word is a sequence of exactly 3 lowercase letters of the first 24 letters of the English alphabet (a to x). She decided that some of the letters are vowels, and all the others are consonants. The whole language is based on a simple rule: any word that contains at least one vowel is correct. Iahubina forgot which letters are the vowels, and wants to find some possible correct sets of vowels. She asks Iahub questions. In each question, she will give Iahub a set of letters considered vowels (in this question). For each question she wants to know how many words of the dictionary are correct, considering the given set of vowels. Iahubina wants to know the xor of the squared answers to all the possible questions. There are 224 different questions, they are all subsets of the set of the first 24 letters of the English alphabet. Help Iahub find that number. Input The first line contains one integer, n (1 ≀ n ≀ 104). Each of the next n lines contains a 3-word consisting of 3 lowercase letters. There will be no two identical 3-words. Output Print one number, the xor of the squared answers to the queries. Examples Input 5 abc aaa ada bcd def Output 0 Submitted Solution: ``` import collections, functools M = 10 n = int(input()) f = collections.defaultdict(int) for _ in range(n): f[functools.reduce(lambda x, y: x | y, [1<<(ord(x)-ord('a')) for x in input()])] += 1 ans = 0 for i in range(M): for mask in range(1<<M): if(mask & (1<<i)): f[mask] += f[mask^(1<<i)]; for mask in range(1<<M): ans ^= (n-f[mask])*(n-f[mask]) print(ans) ``` No
85,301
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahubina is tired of so many complicated languages, so she decided to invent a new, simple language. She already made a dictionary consisting of n 3-words. A 3-word is a sequence of exactly 3 lowercase letters of the first 24 letters of the English alphabet (a to x). She decided that some of the letters are vowels, and all the others are consonants. The whole language is based on a simple rule: any word that contains at least one vowel is correct. Iahubina forgot which letters are the vowels, and wants to find some possible correct sets of vowels. She asks Iahub questions. In each question, she will give Iahub a set of letters considered vowels (in this question). For each question she wants to know how many words of the dictionary are correct, considering the given set of vowels. Iahubina wants to know the xor of the squared answers to all the possible questions. There are 224 different questions, they are all subsets of the set of the first 24 letters of the English alphabet. Help Iahub find that number. Input The first line contains one integer, n (1 ≀ n ≀ 104). Each of the next n lines contains a 3-word consisting of 3 lowercase letters. There will be no two identical 3-words. Output Print one number, the xor of the squared answers to the queries. Examples Input 5 abc aaa ada bcd def Output 0 Submitted Solution: ``` def main(): n=raw_input("enter string: ") print("answer is,n") main() ``` No
85,302
Provide tags and a correct Python 3 solution for this coding contest problem. Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 β€” the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end. Input The first line contains sequence of characters without spaces s1s2... sn (1 ≀ n ≀ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square. Output Print a single integer β€” the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7). Examples Input ?01??? Output 4 Input ? Output 2 Input **12 Output 0 Input 1 Output 0 Note In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10. Tags: dp, implementation Correct Solution: ``` m = 1000000007 a, b, c = 1, 0, 1 for s in input(): if s == '*': a, b, c = 0, b + c, 0 elif s == '?': a, b, c = (a + b) % m, (b + c) % m, (a + b) % m elif s == '0': a, b, c = a, 0, 0 elif s == '1': a, b, c = b, 0, a else: a, b, c = 0, 0, b print((a + b) % m) ```
85,303
Provide tags and a correct Python 3 solution for this coding contest problem. Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 β€” the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end. Input The first line contains sequence of characters without spaces s1s2... sn (1 ≀ n ≀ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square. Output Print a single integer β€” the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7). Examples Input ?01??? Output 4 Input ? Output 2 Input **12 Output 0 Input 1 Output 0 Note In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10. Tags: dp, implementation Correct Solution: ``` Mod=1000000007 s=input() n=len(s) a,b,c,d=1,0,0,0 for i in range(0,n): if s[i]=='*': t=0,a+b+d,0,0 elif s[i]=='?': t=a+b+c,a+b+d,0,0 elif s[i]=='0': t=0,0,a+c,0 elif s[i]=='1': t=0,0,b,a+c else: t=0,0,0,b+d a,b,c,d=map(lambda a:a%Mod,t) print((a+b+c)%Mod) ```
85,304
Provide tags and a correct Python 3 solution for this coding contest problem. Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 β€” the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end. Input The first line contains sequence of characters without spaces s1s2... sn (1 ≀ n ≀ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square. Output Print a single integer β€” the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7). Examples Input ?01??? Output 4 Input ? Output 2 Input **12 Output 0 Input 1 Output 0 Note In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10. Tags: dp, implementation Correct Solution: ``` Mod=1000000007 s=input() n=len(s) a,b,c,d=1,0,0,0 for i in range(0,n): if s[i]=='*': a,b,c,d=0,(a+b+d)%Mod,0,0 elif s[i]=='?': a,b,c,d=(a+b+c)%Mod,(a+b+d)%Mod,0,0 elif s[i]=='0': a,b,c,d=0,0,(a+c)%Mod,0 elif s[i]=='1': a,b,c,d=0,0,b,(a+c)%Mod else: a,b,c,d=0,0,0,(b+d)%Mod print((a+b+c)%Mod) ```
85,305
Provide tags and a correct Python 3 solution for this coding contest problem. Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 β€” the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end. Input The first line contains sequence of characters without spaces s1s2... sn (1 ≀ n ≀ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square. Output Print a single integer β€” the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7). Examples Input ?01??? Output 4 Input ? Output 2 Input **12 Output 0 Input 1 Output 0 Note In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10. Tags: dp, implementation Correct Solution: ``` # python txdy! mod=1000000007 a,b,c=1,0,1 for s in input(): if s=='*':a,b,c=0,b+c,0 elif s=='?':a,b,c=(a+b)%mod,(b+c)%mod,(a+b)%mod elif s=='0':a,b,c=a,0,0 elif s=='1':a,b,c=b,0,a else: a,b,c=0,0,b print((a+b)%mod) ```
85,306
Provide tags and a correct Python 3 solution for this coding contest problem. Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 β€” the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end. Input The first line contains sequence of characters without spaces s1s2... sn (1 ≀ n ≀ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square. Output Print a single integer β€” the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7). Examples Input ?01??? Output 4 Input ? Output 2 Input **12 Output 0 Input 1 Output 0 Note In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10. Tags: dp, implementation Correct Solution: ``` s=input() n=len(s) a,b,c,d=1,0,0,0 for i in range(0,n): if s[i]=='*': a,b,c,d=0,(a+b+d)% 1000000007,0,0 elif s[i]=='?': a,b,c,d=(a+b+c)% 1000000007,(a+b+d)% 1000000007,0,0 elif s[i]=='0': a,b,c,d=0,0,(a+c)% 1000000007,0 elif s[i]=='1': a,b,c,d=0,0,b,(a+c)% 1000000007 else: a,b,c,d=0,0,0,(b+d)% 1000000007 print((a+b+c)% 1000000007) ```
85,307
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 β€” the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end. Input The first line contains sequence of characters without spaces s1s2... sn (1 ≀ n ≀ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square. Output Print a single integer β€” the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7). Examples Input ?01??? Output 4 Input ? Output 2 Input **12 Output 0 Input 1 Output 0 Note In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10. Submitted Solution: ``` Mod=1000000007 s=input() n=len(s) a,b,c,d=1,0,0,0 for i in range(0,n): if s[i]=='?': a,b,c,d=(a+b+c)%Mod,(a+b+d)%Mod,0,0 elif s[i]=='0': a,b,c,d=0,0,(a+c)%Mod,0 elif s[i]=='1': a,b,c,d=0,0,b,(a+c)%Mod else: a,b,c,d=0,0,0,(b+d)%Mod print(a+b+c) ``` No
85,308
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 β€” the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end. Input The first line contains sequence of characters without spaces s1s2... sn (1 ≀ n ≀ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square. Output Print a single integer β€” the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7). Examples Input ?01??? Output 4 Input ? Output 2 Input **12 Output 0 Input 1 Output 0 Note In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10. Submitted Solution: ``` # python txdy! m=1000000007 a,b,c=1,0,1 for s in input(): if s=='*':a,b,c=0,b+c,0 elif s=='?':a,b,c=(a+b)%m,(b+c)%m,(a+b)%m elif s=='0':a,b,c=a,0,0 elif s=='1':a,b,c=b,0,a else: a,b,c=0,0,b print(a+b) ``` No
85,309
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 β€” the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end. Input The first line contains sequence of characters without spaces s1s2... sn (1 ≀ n ≀ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square. Output Print a single integer β€” the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7). Examples Input ?01??? Output 4 Input ? Output 2 Input **12 Output 0 Input 1 Output 0 Note In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10. Submitted Solution: ``` Mod=1000000007 s=input() n=len(s) a,b,c,d=1,0,0,0 for i in range(0,n): if s[i]=='*': a,b,c,d=0,(a+b+d)%Mod,0,0 elif s[i]=='?': a,b,c,d=(a+b+c)%Mod,(a+b+d)%Mod,0,0 elif s[i]=='0': a,b,c,d=0,0,(a+c)%Mod,0 elif s[i]=='1': a,b,c,d=0,0,b,(a+c)%Mod else: a,b,c,d=0,0,0,(b+d)%Mod print(a+b+c) ``` No
85,310
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 β€” the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end. Input The first line contains sequence of characters without spaces s1s2... sn (1 ≀ n ≀ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square. Output Print a single integer β€” the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7). Examples Input ?01??? Output 4 Input ? Output 2 Input **12 Output 0 Input 1 Output 0 Note In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10. Submitted Solution: ``` m = 1000000007 a, b, c = 1, 0, 1 for s in input(): if s == '*': a, b, c = 0, b + c, 0 elif s == '?': a, b, c = (a + b) % m, (b + c) % m, b % m elif s == '0': a, b, c = a, 0, 0 elif s == '1': a, b, c = b, 0, a else: a, b, c = 0, 0, b print((a + b) % m) ``` No
85,311
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Tags: implementation Correct Solution: ``` import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) i = 1 aSorted = sorted(a) while i < n and a[i] >= a[i-1]: i += 1 a = a[i:] + a[:i] for j in range(n): if aSorted[j] != a[j]: print(-1) break else: print((n-i) % n) ```
85,312
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Tags: implementation Correct Solution: ``` n = int(input()) s = list(map(int, input().split())) s1 = sorted(s) if s == s1: print(0) else: for i in range(n - 1): if s[i + 1] < s[i]: s = s[i + 1:] + s[:i + 1] if s == s1: print(n - (i + 1)) else: print(-1) break ```
85,313
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Tags: implementation Correct Solution: ``` import sys n=int(input()) l1=list(map(int,input().split())) l3=l1.copy() l3.sort() for i in range(1,n): if l1[i-1]>l1[i]: l2=l1[i:]+l1[:i] if l3==l2: print(n-i) sys.exit() else : print(-1) sys.exit() print(0) ```
85,314
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Tags: implementation Correct Solution: ``` n = int(input()) import sys def get_ints(): return list(map(int, sys.stdin.readline().strip().split())) from collections import deque arr = get_ints() dicts = {} flag = 0 maxnum = max(arr) items = deque(arr) flag = 0 def checkdecreasing(newarr): #print(newarr) prev = -1 for i in newarr: if i < prev: return False prev = i return True ansflag = 0 count = 0 for i in range(n ): if items[-1] == maxnum: flag = 1 curans = checkdecreasing(items) if curans: ansflag = 1 break items.rotate(1) count += 1 if ansflag: print(count) else: print(-1) ```
85,315
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Tags: implementation Correct Solution: ``` """ Author: Sagar Pandey """ # ---------------------------------------------------Import Libraries--------------------------------------------------- import sys import time import os from math import sqrt, log, log2, ceil, log10, gcd, floor, pow, sin, cos, tan, pi, inf, factorial from copy import copy, deepcopy from sys import exit, stdin, stdout from collections import Counter, defaultdict, deque from itertools import permutations import heapq from bisect import bisect_left as bl # If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect # If the element is already present in the list, # the right most position where element has to be inserted is r # ---------------------------------------------------Global Variables--------------------------------------------------- # sys.setrecursionlimit(100000000) mod = 1000000007 # ---------------------------------------------------Helper Functions--------------------------------------------------- iinp = lambda: int(sys.stdin.readline()) inp = lambda: sys.stdin.readline().strip() strl = lambda: list(inp().strip().split(" ")) intl = lambda: list(map(int, inp().split(" "))) mint = lambda: map(int, inp().split()) flol = lambda: list(map(float, inp().split(" "))) flush = lambda: stdout.flush() def permute(nums): def fun(arr, nums, cur, v): if len(cur) == len(nums): arr.append(cur.copy()) i = 0 while i < len(nums): if v[i]: i += 1 continue else: cur.append(nums[i]) v[i] = 1 fun(arr, nums, cur, v) cur.pop() v[i] = 0 i += 1 # while i<len(nums) and nums[i]==nums[i-1]:i+=1 # Uncomment for unique permutations return arr res = [] nums.sort() v = [0] * len(nums) return fun(res, nums, [], v) def subsets(res, index, arr, cur): res.append(cur.copy()) for i in range(index, len(arr)): cur.append(arr[i]) subsets(res, i + 1, arr, cur) cur.pop() return res def sieve(N): root = int(sqrt(N)) primes = [1] * (N + 1) primes[0], primes[1] = 0, 0 for i in range(2, root + 1): if primes[i]: for j in range(i * i, N + 1, i): primes[j] = 0 return primes def bs(arr, l, r, x): if x < arr[0] or x > arr[len(arr) - 1]: return -1 while l <= r: mid = l + (r - l) // 2 if arr[mid] == x: return mid elif arr[mid] < x: l = mid + 1 else: r = mid - 1 return -1 def isPrime(n): if n <= 1: return False if n <= 3: return True if n % 2 == 0 or n % 3 == 0: return False p = int(sqrt(n)) for i in range(5, p + 1, 6): if n % i == 0 or n % (i + 2) == 0: return False return True # -------------------------------------------------------Functions------------------------------------------------------ def fun(arr, mid, l): n = len(arr) if mid < arr[0] or mid < l - arr[-1]: return False for i in range(n - 1): if not arr[i] + mid >= arr[i + 1] - mid: return False return True def solve(): n=iinp() arr=intl() i=0 while i<n-1 and arr[i+1]>=arr[i]: i+=1 if i==n-1: print(0) return ans=arr[i+1:] ans.extend(arr[:i+1]) # print(ans,i) if sorted(arr)==ans: print(n-i-1) else: print(-1) # -------------------------------------------------------Main Code------------------------------------------------------ start_time = time.time() # for _ in range(iinp()): solve() # print("--- %s seconds ---" % (time.time() - start_time)) ```
85,316
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Tags: implementation Correct Solution: ``` n = int(input()) lst = list(map(int, input().split())) ans = 0 c = 0 for i in range(1, n): if lst[i] < lst[i-1]: ans = n-i c += 1 if (c == 1 and lst[-1] <= lst[0]) or c == 0: print(ans) else: print(-1) ```
85,317
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Tags: implementation Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) b = -1 for i in range(1,n): if a[i-1]>a[i]: b = i break if b==-1: print(0) else: c = a[b:]+a[:b] flag = 0 for i in range(1,n): if c[i]<c[i-1]: flag = 1 break if flag == 1: print(-1) else: print(n-b) ```
85,318
Provide tags and a correct Python 3 solution for this coding contest problem. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Tags: implementation Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): n = I() a = LI() aa = a + a t = [] for i in range(n*2-1): if aa[i] > aa[i+1]: t.append(i) if len(t) < 2: return 0 if len(t) > 2: return -1 return n - t[0] - 1 print(main()) ```
85,319
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` n = input() n = int(n) d = [int(x) for x in input().split(' ')] e = 0 f = 0 for i in range(1, n): if d[i-1] <= d[i]: if f == 0 or ( f == 1 and d[i] <= d[0]): pass else: e = -1 break else: if (f == 0) and (d[0] >= d[i]): f = 1 e = i else: e = -1 break #print(e) if e == -1: print(-1) else: print((n-e)%n) ``` Yes
85,320
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` n = int(input()) ls = list(map(int, input().split())) if ls == sorted(ls): print(0) elif ls == sorted(ls, reverse=True): if n == 2: print(1) else: print(-1) else: counter = 0 b = False for i in range(1, n): if i == n-1 and ls[-1] > ls[0]: counter = -1 break if ls[i] < ls[i-1] and not b: counter += 1 b = True elif not b and ls[i] < ls[i-1]: counter = -1 break elif b: if ls[i] >= ls[i-1]: counter += 1 else: counter = -1 break print(counter) ``` Yes
85,321
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` n=int(input()) l=list(map(int,input().split())) d=0 for i in range(1,n): if l[i]<l[i-1]: d=i break san=l[i:]+l[:i] if d!=0: l.sort() if l==san: print (n-d) else: print (-1) else: print(0) ``` Yes
85,322
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` #from collections import deque as dq n = int(input()) l = list(map(int,input().split())) d=0 r=0 for i in range(n-1): if l[i+1]<l[i]: d=i+1 break l = l[d:]+l[:d] #print(l,d) r = n-d if l==sorted(l): print(r%n) else: print(-1) ``` Yes
85,323
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` # # "main" # # " . . . " # # # Created by Sergey Yaksanov at 29.09.2019 # Copyright Β© 2019 Yakser. All rights reserved. n = int(input()) a = list(map(int,input().split())) sorted_a = sorted(a) k = 0 a0 = a[0] while a != sorted_a and k < n: a.insert(0,a[n-1]) del(a[n-1]) k += 1 if k >= n: k = -1 print(k) ``` No
85,324
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` #author : dokueki import sys INT_MAX = sys.maxsize INT_MIN = -(sys.maxsize)-1 sys.setrecursionlimit(10**5) mod = 1000000007 def IOE(): sys.stdin = open("input.txt", "r") sys.stdout = open("output.txt", "w") def main(): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) x = sorted(a) if x == a: print(0) else: for i in range(int(10e5)+2): a = [a[-1]] + a[:n - 1] if a == x: break print(i+1 if i != int(10e5)+2 else -1) if __name__ == "__main__": try: IOE() except: pass main() ``` No
85,325
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) #print(a) n=len(a) min_index=a.index(min(a)) k=min_index #print(f"This is min index: {min_index}") ans=0 count=0 #print(a[(k+1)%(n)]) #print(a[k%n]) while(count<n-1): if(a[k%n] > a[(k+1)%(n)]): ans=-1 break k=k+1 count=count+1 #print(f"This is the value of ans: {ans}") if(ans!=0): print(-1) else: if(a[0]>a[n-1]): print(n-min_index) else: print(0) ``` No
85,326
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an β†’ an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence? Input The first line contains an integer n (2 ≀ n ≀ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0 Submitted Solution: ``` ## necessary imports import sys input = sys.stdin.readline # import random # from math import log2, log, ceil # swap_array function def swaparr(arr, a,b): temp = arr[a]; arr[a] = arr[b]; arr[b] = temp ## gcd function def gcd(a,b): if a == 0: return b return gcd(b%a, a) ## prime factorization def primefs(n): ## if n == 1 ## calculating primes primes = {} while(n%2 == 0): primes[2] = primes.get(2, 0) + 1 n = n//2 for i in range(3, int(n**0.5)+2, 2): while(n%i == 0): primes[i] = primes.get(i, 0) + 1 n = n//i if n > 2: primes[n] = primes.get(n, 0) + 1 ## prime factoriazation of n is stored in dictionary ## primes and can be accesed. O(sqrt n) return primes ## DISJOINT SET UNINON FUNCTIONS def swap(a,b): temp = a a = b b = temp return a,b # find function def find(x, link): while(x != link[x]): x = link[x] return x # the union function which makes union(x,y) # of two nodes x and y def union(x, y, size, link): x = find(x, link) y = find(y, link) if size[x] < size[y]: x,y = swap(x,y) if x != y: size[x] += size[y] link[y] = x ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #### PRIME FACTORIZATION IN O(log n) using Sieve #### MAXN = int(1e6 + 5) def spf_sieve(): spf[1] = 1; for i in range(2, MAXN): spf[i] = i; for i in range(4, MAXN, 2): spf[i] = 2; for i in range(3, ceil(MAXN ** 0.5), 2): if spf[i] == i: for j in range(i*i, MAXN, i): if spf[j] == j: spf[j] = i; ## function for storing smallest prime factors (spf) in the array ################## un-comment below 2 lines when using factorization ################# # spf = [0 for i in range(MAXN)] # spf_sieve() def factoriazation(x): ret = {}; while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1; x = x//spf[x] return ret ## this function is useful for multiple queries only, o/w use ## primefs function above. complexity O(log n) ## taking integer array input def int_array(): return list(map(int, input().strip().split())) ## taking string array input def str_array(): return input().strip().split(); #defining a couple constants MOD = int(1e9)+7; CMOD = 998244353; INF = float('inf'); NINF = -float('inf'); ################# ---------------- TEMPLATE ENDS HERE ---------------- ################# n = int(input()); a = int_array(); if a == sorted(a, reverse= True): print(n- a.count(max(a))); exit(); new = []; initial = a[0]; while(len(a) > 0 and initial >= a[-1]): new.append(a.pop()); this = new + a; if this == sorted(this): print(len(new)) else: print(-1); ``` No
85,327
Provide tags and a correct Python 3 solution for this coding contest problem. Dreamoon has a string s and a pattern string p. He first removes exactly x characters from s obtaining string s' as a result. Then he calculates <image> that is defined as the maximal number of non-overlapping substrings equal to p that can be found in s'. He wants to make this number as big as possible. More formally, let's define <image> as maximum value of <image> over all s' that can be obtained by removing exactly x characters from s. Dreamoon wants to know <image> for all x from 0 to |s| where |s| denotes the length of string s. Input The first line of the input contains the string s (1 ≀ |s| ≀ 2 000). The second line of the input contains the string p (1 ≀ |p| ≀ 500). Both strings will only consist of lower case English letters. Output Print |s| + 1 space-separated integers in a single line representing the <image> for all x from 0 to |s|. Examples Input aaaaa aa Output 2 2 1 1 0 0 Input axbaxxb ab Output 0 1 1 2 1 1 0 0 Note For the first sample, the corresponding optimal values of s' after removal 0 through |s| = 5 characters from s are {"aaaaa", "aaaa", "aaa", "aa", "a", ""}. For the second sample, possible corresponding optimal values of s' are {"axbaxxb", "abaxxb", "axbab", "abab", "aba", "ab", "a", ""}. Tags: dp, strings Correct Solution: ``` s, p = input(), input() n, m = len(s) + 1, len(p) d = [[0] * n for t in range(n)] for x in range(1, n): i, j = x, m while i and j: j -= s[i - 1] == p[j - 1] i -= 1 if not j: for y in range(i + 1): d[x][y + x - i - m] = d[i][y] + 1 for y in range(x): d[x][y] = max(d[x][y], d[x - 1][y]) print(*d[-1]) ```
85,328
Provide tags and a correct Python 3 solution for this coding contest problem. One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem. Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight. Input The first (and the only) input line contains integer number w (1 ≀ w ≀ 100) β€” the weight of the watermelon bought by the boys. Output Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case. Examples Input 8 Output YES Note For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant β€” two parts of 4 and 4 kilos). Tags: brute force, math Correct Solution: ``` n = int(input()) if n<3 or n%2: print("NO") else: print("YES") ```
85,329
Provide tags and a correct Python 3 solution for this coding contest problem. One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem. Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight. Input The first (and the only) input line contains integer number w (1 ≀ w ≀ 100) β€” the weight of the watermelon bought by the boys. Output Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case. Examples Input 8 Output YES Note For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant β€” two parts of 4 and 4 kilos). Tags: brute force, math Correct Solution: ``` #!/usr/bin/env python def main(): w = int(input()) if w < 4 or w % 2: print("NO") else: print("YES") if __name__ == '__main__': main() ```
85,330
Provide tags and a correct Python 3 solution for this coding contest problem. One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem. Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight. Input The first (and the only) input line contains integer number w (1 ≀ w ≀ 100) β€” the weight of the watermelon bought by the boys. Output Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case. Examples Input 8 Output YES Note For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant β€” two parts of 4 and 4 kilos). Tags: brute force, math Correct Solution: ``` a = int(input()) if a == 2: print("NO") elif a%2 != 1: print("YES") else: print("NO") ```
85,331
Provide tags and a correct Python 3 solution for this coding contest problem. One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem. Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight. Input The first (and the only) input line contains integer number w (1 ≀ w ≀ 100) β€” the weight of the watermelon bought by the boys. Output Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case. Examples Input 8 Output YES Note For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant β€” two parts of 4 and 4 kilos). Tags: brute force, math Correct Solution: ``` r = int(input()) if r % 2 != 0: print("NO") elif r == 2: print("NO") else: print("YES") ```
85,332
Provide tags and a correct Python 3 solution for this coding contest problem. One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem. Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight. Input The first (and the only) input line contains integer number w (1 ≀ w ≀ 100) β€” the weight of the watermelon bought by the boys. Output Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case. Examples Input 8 Output YES Note For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant β€” two parts of 4 and 4 kilos). Tags: brute force, math Correct Solution: ``` def solve(): w = int(input()) if w <= 2 or w % 2 != 0: print("NO") else: print("YES") if __name__ == '__main__': solve() ```
85,333
Provide tags and a correct Python 3 solution for this coding contest problem. One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem. Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight. Input The first (and the only) input line contains integer number w (1 ≀ w ≀ 100) β€” the weight of the watermelon bought by the boys. Output Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case. Examples Input 8 Output YES Note For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant β€” two parts of 4 and 4 kilos). Tags: brute force, math Correct Solution: ``` a = input() a = int(a) if a % 2 == 0 and a != 2: print('YES') else: print('NO') ```
85,334
Provide tags and a correct Python 3 solution for this coding contest problem. One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem. Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight. Input The first (and the only) input line contains integer number w (1 ≀ w ≀ 100) β€” the weight of the watermelon bought by the boys. Output Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case. Examples Input 8 Output YES Note For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant β€” two parts of 4 and 4 kilos). Tags: brute force, math Correct Solution: ``` i = int(input()) b = False for g in range(0,i): if g % 2 == 0 and (i - g) % 2 == 0 and g != 0: print("YES") b = True break if b == False: print("NO") ```
85,335
Provide tags and a correct Python 3 solution for this coding contest problem. One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem. Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight. Input The first (and the only) input line contains integer number w (1 ≀ w ≀ 100) β€” the weight of the watermelon bought by the boys. Output Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case. Examples Input 8 Output YES Note For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant β€” two parts of 4 and 4 kilos). Tags: brute force, math Correct Solution: ``` weight = float(input()) try: #assert weight.is_integer() and (weight <= 100 and weight >= 1) and (weight/2 % 2 == 0) assert weight.is_integer() and (weight <= 100 and weight > 2) and ((weight - 2) % 2 == 0) #If weight is 2 then its NO print("YES") except AssertionError: print("NO") ```
85,336
Provide tags and a correct Python 3 solution for this coding contest problem. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Tags: brute force, dp, greedy, implementation, strings Correct Solution: ``` s = input() pA = -10 pB = -10 al = [] bl = [] for i in range(len(s)): if s[i] == 'A' and pA == -10: pA = i if s[i] == 'B' and pB == -10: pB = i if s[i] == 'A': if (i - pB == 1): bl.append(i) pA = i if s[i] == 'B': if (i - pA == 1): al.append(i) pB = i #print(al, bl) br = False for i in bl: if br: break tS = set(al[:]) if i-1 in tS: tS.remove(i-1) if i+1 in tS: tS.remove(i+1) if len(tS) > 0: br = True break if br: print('YES') else: print('NO') ```
85,337
Provide tags and a correct Python 3 solution for this coding contest problem. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Tags: brute force, dp, greedy, implementation, strings Correct Solution: ``` import re mystr = input() r1 = mystr.find('AB') r2 = mystr.rfind('BA') if r1 != -1 and r2 != -1 and r1 != r2-1 and r2 != r1-1: print('YES') else: r1 = mystr.find('BA') r2 = mystr.rfind('AB') if r1 != -1 and r2 != -1 and r1 != r2-1 and r2 != r1-1: print('YES') else: print('NO') ```
85,338
Provide tags and a correct Python 3 solution for this coding contest problem. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Tags: brute force, dp, greedy, implementation, strings Correct Solution: ``` s = input() ab, ba = [], [] for i in range(len(s) - 1): if s[i:i+2] == 'AB': ab.append(i) elif s[i:i+2] == 'BA': ba.append(i) for u in ab: for v in ba: if abs(u-v) > 1: print('YES') exit() print('NO') ```
85,339
Provide tags and a correct Python 3 solution for this coding contest problem. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Tags: brute force, dp, greedy, implementation, strings Correct Solution: ``` s = input() matches = {'AB': [], 'BA': []} for i in range(len(s) - 1): substring = s[i:i + 2] if substring in matches: matches[substring].append(i) if not matches['AB'] or not matches['BA']: print('NO') elif abs(max(matches['AB']) - min(matches['BA'])) > 1: print('YES') elif abs(min(matches['AB']) - max(matches['BA'])) > 1: print('YES') else: print('NO') ```
85,340
Provide tags and a correct Python 3 solution for this coding contest problem. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Tags: brute force, dp, greedy, implementation, strings Correct Solution: ``` # -*- coding: utf-8 -*- """ Created on Sat Jan 5 14:48:26 2019 @author: piyus """ def foo(string,sub): ans = [] for i in range(len(string)-len(sub)+1): for j in range(len(sub)): if string[i+j]!=sub[j]: break else: ans.append(i) return ans s = input() a1 = foo(s,"AB") a2 = foo(s,"BA") f = 0 for elm in a2: for start in a1: if elm not in range(start,start+2) and (elm+1) not in range(start,start+2): print("YES") f = 1 break if f: break else: print("NO") ```
85,341
Provide tags and a correct Python 3 solution for this coding contest problem. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Tags: brute force, dp, greedy, implementation, strings Correct Solution: ``` s=input() l=len(s) ab=[] ba=[] for i in range(l-1): if s[i:i+2]=="AB": ab.append(i) elif s[i:i+2]=="BA": ba.append(i) if not ab or not ba: print("NO") else: bk=False for i in ab: for j in ba: if abs(i-j)>=2: bk=True break if bk:break if bk: print("YES") else: print("NO") ```
85,342
Provide tags and a correct Python 3 solution for this coding contest problem. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Tags: brute force, dp, greedy, implementation, strings Correct Solution: ``` s = input() abfound = bafound = -1 for i in range(len(s) - 1): if abfound == -1 and s[i] == 'A' and s[i + 1] == 'B': abfound = i elif bafound == -1 and s[i] == 'B' and s[i + 1] == 'A': bafound = i if abfound != -1 and bafound != -1: break if abfound == -1 or bafound == -1: print("NO") exit(0) if abfound == bafound + 1 or bafound == abfound + 1: for i in range(max(bafound + 2, abfound + 2), len(s) - 1): if s[i] == 'A' and s[i + 1] == 'B': newabfound = True print("YES") exit(0) elif s[i] == 'B' and s[i + 1] == 'A': newbafound = True print("YES") exit(0) else: print("YES") exit(0) print("NO") ```
85,343
Provide tags and a correct Python 3 solution for this coding contest problem. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Tags: brute force, dp, greedy, implementation, strings Correct Solution: ``` st = input() s1 = st.replace('AB','0',1) s1 = s1.replace('BA','1',1) s2 = st.replace('BA','1',1) s2 = s2.replace('AB','0',1) if ('0' in s1 and '1' in s1) or ('0' in s2 and '1' in s2): print('YES') else: print('NO') ```
85,344
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Submitted Solution: ``` s=input() a=s.find('AB') b=s.rfind('AB') c=s.find('BA') d=s.rfind('BA') e=min(a,b,c,d) if((abs(a-d)>1 or abs(b-c)>1) and e>=0): print("YES") else: print('NO') ``` Yes
85,345
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Submitted Solution: ``` # -*- coding: utf-8 -*- """ Created on Mon Jun 22 10:52:08 2020 @author: Harshal """ s=input() u=s.find('AB');v=s.find('BA') if (u+1 and s.find('BA',u+2)+1) or (v+1 and s.find('AB',v+2)+1): print("YES") else: print("NO") ``` Yes
85,346
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Submitted Solution: ``` s=input() a= s.count("AB") b=s.count("BA") c=s.count("ABA") d=s.count("BAB") e=c+d if a>0 and b>0 and a+b-e >=2 : print("YES") else: print("NO") ``` Yes
85,347
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Submitted Solution: ``` s = input() m = [] n = [] for i in range (len(s)-1) : if s[i] == "A" and s[i+1] == "B" : m.append(i) if s[i] == "B" and s[i+1] == "A" : n.append(i) if min(len(m),len(n)) == 0 : print("NO") elif max(len(m),len(n)) > 2 : print("YES") elif len(m) == 2 and len(n) == 2 : print("YES") elif len(m) == 1 and len(n) == 2 : if max(m[0]-n[0] , n[0]-m[0]) > 1 or max(m[0]-n[1] , n[1]-m[0]) > 1 : print("YES") else : print("NO") elif len(n) == 1 and len(m) == 2 : if max(n[0]-m[0] , m[0]-n[0]) > 1 or max(n[0]-m[1] , -n[0]+m[1]) > 1 : print("YES") else : print("NO") else : if m[0] - n[0] > 1 or n[0] - m[0] > 1 : print("YES") else : print("NO") ``` Yes
85,348
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Submitted Solution: ``` string= str(input()) answer="NO" indexAB, indexBA = -1,-1 countAB, countBA = 0,0 for i in range(2,len(string)+1): sub=string[i-2:i] if(sub == "AB"): indexAB= i countAB+=1 if(sub == "BA"): indexBA= i countBA+=1 if(countAB == 1 and countBA == 1): if(indexAB+1 != indexBA and indexAB != indexBA+1): answer= "YES" elif(countAB !=0 and countBA !=0): answer="YES" print(answer) ``` No
85,349
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Submitted Solution: ``` inputString = input() indexAB, indexBA = [-1 for i in range(2)], [-1 for i in range(2)] for i in range(len(inputString)-1): if (inputString[i] == 'A' and inputString[i+1] == 'B'): if (indexAB[0] == -1): indexAB[0] = i if (indexAB[1] < i): indexAB[1] = i elif (inputString[i] == 'B' and inputString[i+1] == 'A'): if (indexBA[0] == -1): indexBA[0] = i if (indexBA[1] < i): indexBA[1] = i if ((indexAB[0] == -1) and (indexBA[0] == -1) and ((indexBA[1] - indexAB[0] >= 2) or (indexAB[1] - indexBA[0] >= 2))): print ("YES") else: print ("NO") ``` No
85,350
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Submitted Solution: ``` # with open("input.txt","r") as f: string = input() abFlag = False baFlag = False i = 0 while(i<len(string)-1): if(string[i]=="A" and string[i+1]=="B" and not abFlag): abFlag =True i+=1 elif(string[i]=="B" and string[i+1]=="A" and not baFlag): baFlag = True i+=1 i+=1 if(abFlag and baFlag): break if(abFlag and baFlag): print("YES") else: print("NO") ``` No
85,351
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters. Output Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise. Examples Input ABA Output NO Input BACFAB Output YES Input AXBYBXA Output NO Note In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA". Submitted Solution: ``` s=input() a1=s.count("AB") a2=s.count("BA") a3=s.count("ABA") a4=s.count("BAB") if a1!=0 and a2!=0: if a1>1 or a2>1: print("YES") else: if a3==0 and a4==0: print("YES") else: print("NO") else: print("NO") ``` No
85,352
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Tags: combinatorics, data structures, dp, two pointers Correct Solution: ``` n, m = list(map(int, input().split())) def intmodm(num): return int(num) % m a = list(map(intmodm, input().split())) states = [[-1] * m] # states[0][0] = 0 for index in range(n): states.append(states[-1][:]) num = a[index] for i in range(m): if states[-2][i] != -1 and states[-1][(i + num) % m] == -1: states[-1][(i + num) % m] = index if states[-1][num % m] == -1: states[-1][num % m] = index # print(states) if states[-1][0] != -1: print("YES") break else: print("NO") ```
85,353
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Tags: combinatorics, data structures, dp, two pointers Correct Solution: ``` import os import sys from io import BytesIO, IOBase from collections import Counter import math as mt import inspect, re def varname(p): # prints name of the variable for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]: m = re.search(r'\bvarname\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)', line) if m: return m.group(1) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def gcd(a, b): if a == 0: return b return gcd(b % a, a) def lcm(a, b): return (a * b) / gcd(a, b) mod = int(1e9) + 7 def power(k, n): if n == 0: return 1 if n % 2: return (power(k, n - 1) * k) % mod t = power(k, n // 2) return (t * t) % mod def totalPrimeFactors(n): count = 0 if (n % 2) == 0: count += 1 while (n % 2) == 0: n //= 2 i = 3 while i * i <= n: if (n % i) == 0: count += 1 while (n % i) == 0: n //= i i += 2 if n > 2: count += 1 return count # #MAXN = int(1e7 + 1) # # spf = [0 for i in range(MAXN)] # # # def sieve(): # spf[1] = 1 # for i in range(2, MAXN): # spf[i] = i # for i in range(4, MAXN, 2): # spf[i] = 2 # # for i in range(3, mt.ceil(mt.sqrt(MAXN))): # if (spf[i] == i): # for j in range(i * i, MAXN, i): # if (spf[j] == j): # spf[j] = i # # # def getFactorization(x): # ret = 0 # while (x != 1): # k = spf[x] # ret += 1 # # ret.add(spf[x]) # while x % k == 0: # x //= k # # return ret # Driver code # precalculating Smallest Prime Factor # sieve() def main(): n, m = map(int, input().split()) a = list(map(int, input().split())) k = [0 for i in range(m)] if m<n: print('YES') return dp=[[0 for i in range(m)] for j in range(n)] for i in range(n): dp[i][a[i]%m]=1 if dp[0][0]: print('YES') return for i in range(1, n): for j in range(m): if dp[i-1][j]: dp[i][(a[i]+j)%m]=1 dp[i][j]=1 if dp[i][0]: print('YES') return print('NO') return if __name__ == "__main__": main() ```
85,354
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Tags: combinatorics, data structures, dp, two pointers Correct Solution: ``` numElementos, modulo = input().split() numElementos = int(numElementos) modulo = int(modulo) modulos = [] for i in range(modulo): modulos.append(False) restantePossivel = [] for i in range(modulo + 1): restantePossivel.append(modulos.copy()) ultimoElemento = min(numElementos, modulo) elementos = input().split() for i in range(1, ultimoElemento + 1, 1): elementoI = int(elementos[i - 1]) for restante in range(0, modulo, 1): if restantePossivel[i - 1][restante]: restantePosAdd = (restante + elementoI) % modulo restantePossivel[i][restantePosAdd] = True restantePossivel[i][restante] = True restantePossivel[i][elementoI % modulo] = True if numElementos > modulo or restantePossivel[ultimoElemento][0]: print('YES') else: print('NO') ''' Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Input 4 7 1 2 3 3 Output YES Input 1 47 0 Output YES Input 2 47 1 0 Output YES ''' ```
85,355
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Tags: combinatorics, data structures, dp, two pointers Correct Solution: ``` def modulus(arr, m, n): for i in range(n): arr[i] %= m f = [0] * m s = [0] * m f[arr[0]] = 1 if f[0]: return 1 for i in arr[1:]: for j in range(m): if f[j]: s[j] = 1 elif i < j: if f[j - i]: s[j] = 1 elif i > j: if f[j - i + m]: s[j] = 1 else: s[j] = 1 if s[0]: return 1 f, s = s, f return 0 n, m = map(int, input().split()) arr = list(map(int, input().split())) print("YES") if modulus(arr, m, n) else print("NO") ```
85,356
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Tags: combinatorics, data structures, dp, two pointers Correct Solution: ``` num_elementos, modulo = [ int(x) for x in input().split() ] modulos = [False] * modulo restante_possivel = [ modulos.copy() for _ in range(modulo + 1) ] ultimo_elemento = min(num_elementos, modulo) elementos = input().split() for i in range(1, ultimo_elemento + 1): elemento = int(elementos[i - 1]) for restante in range(0, modulo): if restante_possivel[i - 1][restante]: restante_pos_add = (restante + elemento) % modulo restante_possivel[i][restante_pos_add] = True restante_possivel[i][restante] = True restante_possivel[i][elemento % modulo] = True if num_elementos > modulo or restante_possivel[ultimo_elemento][0]: print('YES') else: print('NO') ```
85,357
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Tags: combinatorics, data structures, dp, two pointers Correct Solution: ``` __author__ = 'dwliv_000' (n,m)=(int(i) for i in input().split()) c=[int(i)%m for i in input().split()] z=[False]*m for j in c: q=z[:] q[j]=True for i in range(m): if(z[i]): q[(i+j)%m]=True z=q[:] if z[0]: print('YES') exit(0) print('NO') ```
85,358
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Tags: combinatorics, data structures, dp, two pointers Correct Solution: ``` def main(): n, m = map(int, input().split()) l = [False] * m l1 = l.copy() for i in map(int, input().split()): i %= m l1[i] = True for j, f in enumerate(l, i - m): if f: l1[j] = True if l1[0]: print("YES") return for j, f in enumerate(l1): if f: l[j] = True print("NO") if __name__ == '__main__': main() ```
85,359
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Tags: combinatorics, data structures, dp, two pointers Correct Solution: ``` (n,m)=map(int,input().split()) if n>m:print("YES") else: t=[0 for i in range(m)] s=input().split() for i in range(len(s)): h=int(s[i])%m v=[0 for i in range(m)] for j in range(m): if t[j]==1:v[(h+j)%m]=1 for j in range(m): if v[j]==1:t[j]=1 t[h]=1 if t[0]==1:print("YES") else:print("NO") ```
85,360
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Submitted Solution: ``` n, m = map(int, input().split()) a = list(map(lambda x: x%m, map(int, input().split()))) h = {} for el in a: h[el] = h.get(el, 0) + 1 for v in h.values(): if v % m == 0: print("YES") exit() possible = set([]) for el in a: new = set([el]) for candidate in possible: new.add((candidate + el)%m) possible |= new if 0 in possible: print("YES") exit() print("NO") ``` Yes
85,361
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Submitted Solution: ``` def main(): n, m = map(int, input().split()) mas = list(map(int, input().split())) remains = set() for i in range(n): for_add = set() for e in remains: if (e + mas[i]) % m == 0: return True else: for_add.add((e + mas[i]) % m) if mas[i] % m == 0: return True else: for_add.add(mas[i]) remains |= for_add if main(): print("YES") else: print("NO") ``` Yes
85,362
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Submitted Solution: ``` n, m = list(map(int, input().split())) lst = list(map(int, input().split())) if n > m: print('YES') else: def rec(ps, sum, cnt): if ps >= n: if sum == 0 and cnt > 0: return 1 else: return 0 # print(lst[ps], sum) if dp[ps][sum] != -1: return dp[ps][sum] dp[ps][sum] = rec(ps+1, (sum + lst[ps])%m, cnt+1) | rec(ps+1, sum, cnt) return dp[ps][sum] dp = [[-1 for _ in range(m+7)] for _ in range(n+7)] if rec(0,0,0) == 1: print('YES') else: print('NO') ``` Yes
85,363
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Submitted Solution: ``` n, m = map(int, input().split()) a = list(map(int, input().split())) rem = set() for i in a: new = {(i + j) % m for j in rem | {0}} rem |= new if 0 in rem: print('YES') exit() print('NO') ``` Yes
85,364
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Submitted Solution: ``` from collections import * n,k=map(int,input().split()) l=list(map(int,input().split())) d=[0]*k for i in l: d[i%k]+=1 if(d[0]>0): print('YES') else: f=0 for i in range(1,k): if(d[i]>0 and d[i]%k==0): f=1 break elif(i!=k-i-1 and d[i]>0 and d[k-1-i]>0): f=1 break if(f==1): print("YES") else: if(k%2==0): if(d[k//2]>1): print("YES") else: print("NO") else: print("NO") ``` No
85,365
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Submitted Solution: ``` def process(a): kr=[] ar = [0 for j in range(m)] k=0 for i in a: r=int(i)%m if r==0: return 'YES' r2=-1 for j in kr: r2=(j+r)%m if r2==0: return 'YES' ar[r2]+=1 if ar[r2] >= m: return 'YES' if kr.count(r2)==0 and r2>-1: kr.append(r2) ar[r]+=1 if ar[r] >= m: return 'YES' if kr.count(r)==0: kr.append(r) return 'NO' s=input() a=s.split(' ') n, m = int(a[0]), int(a[1]) s=input() a=s.split(' ') print(process(a)) ``` No
85,366
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Submitted Solution: ``` from math import sqrt,gcd,ceil,floor,log,factorial from itertools import permutations,combinations from collections import Counter, defaultdict def knapsack(n,m,a): dp = [[0 for j in range(m+1)] for i in range(n+1)] for i in range(n+1): for j in range(m+1): if i==0 and j==0: dp[i][j]=1 continue elif i==0: continue elif j==0: dp[i][j]=1 continue if j>=a[i-1]: dp[i][j] = dp[i-1][j] or dp[i-1][j-a[i-1]] else: dp[i][j] = dp[i-1][j] #print(dp) return dp[n][m] def lcm(a,b): return (a*b)//gcd(a,b) n,m = map(int,input().split()) a = list(map(int,input().split())) mod = list(map(lambda x: x%m,a)) #print(*mod) if 0 in mod: print('YES') else: d=Counter(mod) flag=0 for key in d: if d[key]>=lcm(key,m)//key: print('YES') flag=1 break if flag==0: for i in range(1,m//2+1): if m%2==0 and i==m//2: if d[key]>1: print('YES') flag=1 break else: if d[m-key]>0: print('YES') flag=1 break if flag==0: mod.sort() if knapsack(n,m,mod): print('YES') else: print('NO') ``` No
85,367
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m. Input The first line contains two numbers, n and m (1 ≀ n ≀ 106, 2 ≀ m ≀ 103) β€” the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. Examples Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES Note In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence. Submitted Solution: ``` import sys input = sys.stdin.readline n,m = map(int,input().split()) mark = [0 for i in range(m)] a = list(map(int,input().split())) for x in a: for i in range(m): if(mark[i]==1): mark[(i+x%m)%m]=1 mark[x%m]=1 if(mark[0]>0): print("YES") else: print("NO") ``` No
85,368
Provide tags and a correct Python 3 solution for this coding contest problem. Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5]. Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b. Input The first line contains integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array a. The second line contains n integers ai ( - 109 ≀ ai ≀ 109) β€” the elements of the array a. The third line contains integer m (1 ≀ m ≀ 2000) β€” the number of elements in the array b. The fourth line contains m integers bj ( - 109 ≀ bj ≀ 109) β€” the elements of the array b. Output In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps. The second line should contain the number of swaps k (0 ≀ k ≀ 2). Each of the next k lines should contain two integers xp, yp (1 ≀ xp ≀ n, 1 ≀ yp ≀ m) β€” the index of the element in the array a and the index of the element in the array b in the p-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them. Examples Input 5 5 4 3 2 1 4 1 1 1 1 Output 1 2 1 1 4 2 Input 5 1 2 3 4 5 1 15 Output 0 0 Input 5 1 2 3 4 5 4 1 2 3 4 Output 1 1 3 1 Tags: binary search, two pointers Correct Solution: ``` from bisect import bisect_left n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) sum_a, sum_b = sum(a), sum(b) delta = sum_b - sum_a ans = abs(delta) ans_swap = [] for i in range(n): for j in range(m): if abs((sum_a - a[i] + b[j]) - (sum_b + a[i] - b[j])) < ans: ans = abs((sum_a - a[i] + b[j]) - (sum_b + a[i] - b[j])) ans_swap = [(i+1, j+1)] d = dict() for i in range(m): for j in range(i+1, m): d[b[i]+b[j]] = (i+1, j+1) minf, inf = -10**13, 10**13 val = [minf, minf] + sorted(d.keys()) + [inf, inf] for i in range(n): for j in range(i+1, n): ap = a[i] + a[j] req = (delta + ap*2) >> 1 k = bisect_left(val, req) for k in range(k-1, k+2): if abs(delta + ap*2 - val[k]*2) < ans: ans = abs(delta + ap*2 - val[k]*2) ans_swap = [(i+1, d[val[k]][0]), (j+1, d[val[k]][1])] print(ans) print(len(ans_swap)) for x, y in ans_swap: print(x, y) ```
85,369
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5]. Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b. Input The first line contains integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array a. The second line contains n integers ai ( - 109 ≀ ai ≀ 109) β€” the elements of the array a. The third line contains integer m (1 ≀ m ≀ 2000) β€” the number of elements in the array b. The fourth line contains m integers bj ( - 109 ≀ bj ≀ 109) β€” the elements of the array b. Output In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps. The second line should contain the number of swaps k (0 ≀ k ≀ 2). Each of the next k lines should contain two integers xp, yp (1 ≀ xp ≀ n, 1 ≀ yp ≀ m) β€” the index of the element in the array a and the index of the element in the array b in the p-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them. Examples Input 5 5 4 3 2 1 4 1 1 1 1 Output 1 2 1 1 4 2 Input 5 1 2 3 4 5 1 15 Output 0 0 Input 5 1 2 3 4 5 4 1 2 3 4 Output 1 1 3 1 Submitted Solution: ``` #import bisect def mindf(a,b): k=0 s1=sum(a) s2=sum(b) min=abs(s1-s2) for i in range(len(a)): for j in range(len(b)): s22=s2-b[j]+a[i] s11=s1-a[i]+b[j] df=abs(s22-s11) if df<=min: min=df t1=i t2=j k=1 if k==0: t1=-1 t2=-1 else: t=a[t1] a[t1]=b[t2] b[t2]=t return min,t1,t2,a,b n=int(input()) a=[int(i) for i in input().split()] m=int(input()) b=[int(i) for i in input().split()] w=sum(a)-sum(b) if (sum(a)-sum(b))==0: j=0 min=0 t1=-1 t2=-1 t3=-1 t4=-1 else: min1,t1,t2,a,b=mindf(a,b) if min1==1: min=min1 j=1 t3=-1 t4=-1 elif min1==(w): min=min1 j=0 t1=-1 t2=-1 t3=-1 t4=-1 else: min,t3,t4,a,b=mindf(a,b) '''if min==min1: min=min1 j=1 t3=-1 t4=-1''' #else: j=2 print(min) print(j) if (t1+1)!=0 and (t2+1)!=0: print(t1+1,t2+1) if (t3+1)!=0 and (t4+1)!=0: print(t3+1,t4+1) ``` No
85,370
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5]. Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b. Input The first line contains integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array a. The second line contains n integers ai ( - 109 ≀ ai ≀ 109) β€” the elements of the array a. The third line contains integer m (1 ≀ m ≀ 2000) β€” the number of elements in the array b. The fourth line contains m integers bj ( - 109 ≀ bj ≀ 109) β€” the elements of the array b. Output In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps. The second line should contain the number of swaps k (0 ≀ k ≀ 2). Each of the next k lines should contain two integers xp, yp (1 ≀ xp ≀ n, 1 ≀ yp ≀ m) β€” the index of the element in the array a and the index of the element in the array b in the p-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them. Examples Input 5 5 4 3 2 1 4 1 1 1 1 Output 1 2 1 1 4 2 Input 5 1 2 3 4 5 1 15 Output 0 0 Input 5 1 2 3 4 5 4 1 2 3 4 Output 1 1 3 1 Submitted Solution: ``` import bisect def mindf(a,b): k=0 s1=sum(a) s2=sum(b) min=abs(s1-s2) for i in range(len(a)): for j in range(len(b)): s22=s2-b[j]+a[i] s11=s1-a[i]+b[j] df=abs(s22-s11) if df<=min: min=df t1=i t2=j k=1 if k==0: t1=-1 t2=-1 else: t=a[t1] a[t1]=b[t2] b[t2]=t return min,t1,t2,a,b def mins(a): c=[] if len(a)==1: c=a else: for i in range(len(a)-1): for j in range(i+1,len(a)): c.append(a[i]+a[j]) return c n=int(input()) a=[int(i) for i in input().split()] m=int(input()) b=[int(i) for i in input().split()] w=sum(a)-sum(b) c=mins(a) d=mins(b) if (sum(a)-sum(b))==0: j=0 min=0 t1=-1 t2=-1 t3=-1 t4=-1 else: min1,t1,t2,a,b=mindf(a,b) if min1==1: min=min1 j=1 t3=-1 t4=-1 elif min1==(w): min=min1 j=0 t1=-1 t2=-1 t3=-1 t4=-1 else: if len(c)<=1 or len(d)<=1: min=min1 j=1 t3=-1 t4=-1 else: #min,t3,t4,a,b=mindf(a,b) min=min1 j=2 c.sort() for i in range(len(c)): c[i]=c[i]*2 for i in range(len(d)): x=w+2*(d[i]) if bisect.bisect(c,x)==0: v=c[0] else: v=c[bisect.bisect(c,x)-1] z=abs(x-v) if z<min: min=z if min==min1: j=1 print(min) print(j) if (t1+1)!=0 and (t2+1)!=0: print(t1+1,t2+1) #if (t3+1)!=0 and (t4+1)!=0: #print(t3+1,t4+1) ``` No
85,371
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5]. Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b. Input The first line contains integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array a. The second line contains n integers ai ( - 109 ≀ ai ≀ 109) β€” the elements of the array a. The third line contains integer m (1 ≀ m ≀ 2000) β€” the number of elements in the array b. The fourth line contains m integers bj ( - 109 ≀ bj ≀ 109) β€” the elements of the array b. Output In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps. The second line should contain the number of swaps k (0 ≀ k ≀ 2). Each of the next k lines should contain two integers xp, yp (1 ≀ xp ≀ n, 1 ≀ yp ≀ m) β€” the index of the element in the array a and the index of the element in the array b in the p-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them. Examples Input 5 5 4 3 2 1 4 1 1 1 1 Output 1 2 1 1 4 2 Input 5 1 2 3 4 5 1 15 Output 0 0 Input 5 1 2 3 4 5 4 1 2 3 4 Output 1 1 3 1 Submitted Solution: ``` #import bisect def mindf(a,b): k=0 s1=sum(a) s2=sum(b) min=abs(s1-s2) for i in range(len(a)): for j in range(len(b)): s22=s2-b[j]+a[i] s11=s1-a[i]+b[j] df=abs(s22-s11) if df<min: min=df t1=i t2=j k=1 if k==0: t1=-1 t2=-1 else: t=a[t1] a[t1]=b[t2] b[t2]=t return min,t1,t2,a,b n=int(input()) a=[int(i) for i in input().split()] m=int(input()) b=[int(i) for i in input().split()] if (sum(a)-sum(b))==0: j=0 min=0 t1=-1 t2=-1 t3=-1 t4=-1 else: min1,t1,t2,a,b=mindf(a,b) if min1==1: min=min1 j=1 t3=-1 t4=-1 elif min1==(sum(a)-sum(b)): min=min1 j=0 t3=-1 t4=-1 else: min,t3,t4,a,b=mindf(a,b) if min==min1: min=min1 j=1 t3=-1 t4=-1 else: j=2 print(min) print(j) if (t1+1)!=0 and (t2+1)!=0: print(t1+1,t2+1) if (t3+1)!=0 and (t4+1)!=0: print(t3+1,t4+1) ``` No
85,372
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5]. Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b. Input The first line contains integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array a. The second line contains n integers ai ( - 109 ≀ ai ≀ 109) β€” the elements of the array a. The third line contains integer m (1 ≀ m ≀ 2000) β€” the number of elements in the array b. The fourth line contains m integers bj ( - 109 ≀ bj ≀ 109) β€” the elements of the array b. Output In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps. The second line should contain the number of swaps k (0 ≀ k ≀ 2). Each of the next k lines should contain two integers xp, yp (1 ≀ xp ≀ n, 1 ≀ yp ≀ m) β€” the index of the element in the array a and the index of the element in the array b in the p-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them. Examples Input 5 5 4 3 2 1 4 1 1 1 1 Output 1 2 1 1 4 2 Input 5 1 2 3 4 5 1 15 Output 0 0 Input 5 1 2 3 4 5 4 1 2 3 4 Output 1 1 3 1 Submitted Solution: ``` import bisect def mindf(a,b): k=0 s1=sum(a) s2=sum(b) min=abs(s1-s2) for i in range(len(a)): for j in range(len(b)): s22=s2-b[j]+a[i] s11=s1-a[i]+b[j] df=abs(s22-s11) if df<=min: min=df t1=i t2=j k=1 if k==0: t1=-1 t2=-1 else: t=a[t1] a[t1]=b[t2] b[t2]=t return min,t1,t2,a,b def mins(a): c=[] if len(a)==1: c=a else: for i in range(len(a)-1): for j in range(i+1,len(a)): c.append(a[i]+a[j]) return c n=int(input()) a=[int(i) for i in input().split()] m=int(input()) b=[int(i) for i in input().split()] w=sum(a)-sum(b) c=mins(a) d=mins(b) if (sum(a)-sum(b))==0: j=0 min=0 t1=-1 t2=-1 t3=-1 t4=-1 else: min1,t1,t2,a,b=mindf(a,b) if min1==1: min=min1 j=1 t3=-1 t4=-1 elif min1==(w): min=min1 j=0 t1=-1 t2=-1 t3=-1 t4=-1 else: if len(c)<=1 or len(d)<=1: min=min1 j=1 t3=-1 t4=-1 else: if n<1000: min,t3,t4,a,b=mindf(a,b) t3=-1 t4=-1 min=min1 j=2 c.sort() for i in range(len(c)): c[i]=c[i]*2 for i in range(len(d)): x=w+2*(d[i]) if bisect.bisect(c,x)==0: v=c[0] else: v=c[bisect.bisect(c,x)-1] z=abs(x-v) if z<min: min=z if min==min1: j=1 print(min) print(j) if (t1+1)!=0 and (t2+1)!=0: print(t1+1,t2+1) if (t3+1)!=0 and (t4+1)!=0: print(t3+1,t4+1) ``` No
85,373
Provide tags and a correct Python 3 solution for this coding contest problem. Little Artem is fond of dancing. Most of all dances Artem likes rueda β€” Cuban dance that is danced by pairs of boys and girls forming a circle and dancing together. More detailed, there are n pairs of boys and girls standing in a circle. Initially, boy number 1 dances with a girl number 1, boy number 2 dances with a girl number 2 and so on. Girls are numbered in the clockwise order. During the dance different moves are announced and all pairs perform this moves. While performing moves boys move along the circle, while girls always stay at their initial position. For the purpose of this problem we consider two different types of moves: 1. Value x and some direction are announced, and all boys move x positions in the corresponding direction. 2. Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so one. It's guaranteed that n is even. Your task is to determine the final position of each boy. Input The first line of the input contains two integers n and q (2 ≀ n ≀ 1 000 000, 1 ≀ q ≀ 2 000 000) β€” the number of couples in the rueda and the number of commands to perform, respectively. It's guaranteed that n is even. Next q lines contain the descriptions of the commands. Each command has type as the integer 1 or 2 first. Command of the first type is given as x ( - n ≀ x ≀ n), where 0 ≀ x ≀ n means all boys moves x girls in clockwise direction, while - x means all boys move x positions in counter-clockwise direction. There is no other input for commands of the second type. Output Output n integers, the i-th of them should be equal to the index of boy the i-th girl is dancing with after performing all q moves. Examples Input 6 3 1 2 2 1 2 Output 4 3 6 5 2 1 Input 2 3 1 1 2 1 -2 Output 1 2 Input 4 2 2 1 3 Output 1 4 3 2 Tags: brute force, constructive algorithms, implementation Correct Solution: ``` import io, os, sys input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n, q = map(int, input().split()) pos = [i for i in range(n)] cnt, temp, flag = 0, [0, 0], 0 for _ in range(q): p = list(map(int, input().split())) if p[0] == 1: x = (n + p[1]) % n cnt = cnt + x flag = flag ^ (x % 2) else: if flag != 0: temp[0] = temp[0] - 1 temp[1] = temp[1] + 1 else: temp[0] = temp[0] + 1 temp[1] = temp[1] - 1 flag = flag ^ 1 ans = [0 for i in range(n)] for i in range(n): ans[(pos[i] + cnt + temp[i%2]) % n] = i + 1 sys.stdout.write(" ".join(map(str,ans))) ```
85,374
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Artem is fond of dancing. Most of all dances Artem likes rueda β€” Cuban dance that is danced by pairs of boys and girls forming a circle and dancing together. More detailed, there are n pairs of boys and girls standing in a circle. Initially, boy number 1 dances with a girl number 1, boy number 2 dances with a girl number 2 and so on. Girls are numbered in the clockwise order. During the dance different moves are announced and all pairs perform this moves. While performing moves boys move along the circle, while girls always stay at their initial position. For the purpose of this problem we consider two different types of moves: 1. Value x and some direction are announced, and all boys move x positions in the corresponding direction. 2. Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so one. It's guaranteed that n is even. Your task is to determine the final position of each boy. Input The first line of the input contains two integers n and q (2 ≀ n ≀ 1 000 000, 1 ≀ q ≀ 2 000 000) β€” the number of couples in the rueda and the number of commands to perform, respectively. It's guaranteed that n is even. Next q lines contain the descriptions of the commands. Each command has type as the integer 1 or 2 first. Command of the first type is given as x ( - n ≀ x ≀ n), where 0 ≀ x ≀ n means all boys moves x girls in clockwise direction, while - x means all boys move x positions in counter-clockwise direction. There is no other input for commands of the second type. Output Output n integers, the i-th of them should be equal to the index of boy the i-th girl is dancing with after performing all q moves. Examples Input 6 3 1 2 2 1 2 Output 4 3 6 5 2 1 Input 2 3 1 1 2 1 -2 Output 1 2 Input 4 2 2 1 3 Output 1 4 3 2 Submitted Solution: ``` n, q = map(int, input().split()) pos = [i for i in range(n)] cnt, temp, flag = 0, [0, 0], 0 for _ in range(q): p = list(map(int, input().split())) if p[0] == 1: cnt = cnt + p[1] flag = flag ^ (cnt % 2) else: if flag != 0: temp[0] = temp[0] - 1 temp[1] = temp[1] + 1 else: temp[0] = temp[0] + 1 temp[1] = temp[1] - 1 flag = flag ^ 1 ans = [0 for i in range(n)] for i in range(n): ans[(pos[i] + cnt + temp[i%2]) % n] = i + 1 for i in ans: print(i, end = " ") ``` No
85,375
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Tags: implementation, strings Correct Solution: ``` # your code goes here a=int(input("")) if a<=127 : print("byte") else : if a<=32767 : print("short") else : if a<=2147483647 : print("int") else : if a<=9223372036854775807 : print("long") else : print("BigInteger") ```
85,376
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Tags: implementation, strings Correct Solution: ``` num = int(input()); long = len(str(abs(num))); if(num<0): num = abs(num+1); if(num<=127): print("byte"); else: if(num<=32767):print("short"); else: if( num<=2147483647):print("int"); else: if(num<=9223372036854775807):print("long"); else:print("BigInteger"); ```
85,377
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Tags: implementation, strings Correct Solution: ``` x = int(input()) if(x >= -128 & x <= 127): print ("byte") elif(x >= -32768 & x <= 32767): print("short") elif(x >= -2147483648 & x <= 2147483647): print("int") elif(x >= -9223372036854775808 & x <= 9223372036854775807 ): print("long") else: print("BigInteger") ```
85,378
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Tags: implementation, strings Correct Solution: ``` n = int(input()) if n >= -128 and n <= 127: print("byte") elif n >=-32768 and n <=32767: print("short") elif n >=-2147483648 and n <=2147483647: print("int") elif n >=-9223372036854775808 and n <=9223372036854775807: print("long") else : print("BigInteger") ```
85,379
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Tags: implementation, strings Correct Solution: ``` #easy in python n = int(input()) if n <= 127: print("byte") elif n <= 32767: print("short") elif n <= 2147483647: print("int") elif n <= 9223372036854775807: print("long") else: print("BigInteger") ```
85,380
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Tags: implementation, strings Correct Solution: ``` # Author : nitish420 -------------------------------------------------------------------- import os import sys from io import BytesIO, IOBase # mod=10**9+7 # sys.setrecursionlimit(10**6) # from functools import lru_cache def main(): n=input() if len(n)>20: print('BigInteger') exit() n=int(n) if -128<=n<=127: print("byte") elif -32768<=n<=32767: print("short") elif -2147483648<=n<=2147483647: print('int') elif -9223372036854775808<=n<=9223372036854775807: print('long') else: print('BigInteger') #---------------------------------------------------------------------------------------- # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = 'x' in file.mode or 'r' not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b'\n') + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode('ascii')) self.read = lambda: self.buffer.read().decode('ascii') self.readline = lambda: self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # endregion if __name__ == '__main__': main() ```
85,381
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Tags: implementation, strings Correct Solution: ``` b=127 s=32767 i=2147483647 l=9223372036854775807 inp=int(input()) if(inp<=b): print('byte') elif(inp<=s): print('short') elif(inp<=i): print('int') elif(inp<=l): print('long') else: print('BigInteger') ```
85,382
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Tags: implementation, strings Correct Solution: ``` s = int(input ()) if (s >= -128 ) and (s <= 127): print ("byte") elif (s >= -32768)and(s <= 32767): print ("short") elif (s >= -2147483648)and(s <= 2147483647): print ("int") elif (s >= -9223372036854775808)and(s <= 9223372036854775807): print ("long") else: print ("BigInteger") ```
85,383
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Submitted Solution: ``` i = int(input()) if(-128<=i and i<=127): print("byte") elif(-32768<=i and i<= 32767): print("short") elif(-2147483648<=i and i<=2147483647): print("int") elif(-9223372036854775808<=i and i<=9223372036854775807): print("long") else: print("BigInteger") ``` Yes
85,384
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Submitted Solution: ``` n = int(input()) if(n>= -127 and n<=127): print("byte") elif(n>= -32768 and n<= 32767): print("short") elif(n>= -2147483648 and n<= 2147483647): print("int") elif(n>= -9223372036854775808 and n<= 9223372036854775807): print("long") else: print("BigInteger") ``` Yes
85,385
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Submitted Solution: ``` a = int(input()) if 0<a<=127: print('byte') elif a<32768: print('short') elif a<2147483648: print('int') elif a<9223372036854775808: print('long') else: print('BigInteger') ``` Yes
85,386
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Submitted Solution: ``` s=int(input()) if(s>=-128 and s<=127): print("byte") elif(s>=-32768 and s<=32767): print("short") elif(s>=-2147483648 and s<=2147483647): print("int") elif(s>=-9223372036854775808 and s<=9223372036854775807): print("long") else: print("BigInteger") ``` Yes
85,387
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Submitted Solution: ``` n = int(input()) #heights = list(map(int, input().split())) if n >= -128 and n <= 127: print("byte") elif n >= -32768 and n <= 32767: print("short") elif n >= -2147483648 and n <= 2147483647: print("int") else: print("BigInteger") ``` No
85,388
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Submitted Solution: ``` def main(): n = input() n = int(n) if n >= -127 and n <= 127: print("byte") elif n >= -32768 and n <= 32767 : print("short") elif n >= -2147483648 and n <= 2147483647: print("intger") elif n >= -9223372036854775808 and n <= 9223372036854775807: print("long") else: print("Biginter") main() ``` No
85,389
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Submitted Solution: ``` n=int(input()) if n>=-128 and n<=127: print('byte') if n>=-32768 and n<=32767: print('short') if n>=-2147483648 and n<=2147483647: print('int') if n>=-9223372036854775808 and n<=9223372036854775807: print('long') else: print('BigInteger') ``` No
85,390
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from - 128 to 127 2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above. Examples Input 127 Output byte Input 130 Output short Input 123456789101112131415161718192021222324 Output BigInteger Submitted Solution: ``` # Author : nitish420 -------------------------------------------------------------------- import os import sys from io import BytesIO, IOBase # mod=10**9+7 # sys.setrecursionlimit(10**6) # from functools import lru_cache def main(): n=input() if len(n)>20: print('BigInteger') exit() n=int(n) if -128<=n<=127: print("byte") elif -32768<=n<=32767: print("short") elif -9223372036854775808<=n<=9223372036854775807: print('long') else: print('BigInteger') #---------------------------------------------------------------------------------------- # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = 'x' in file.mode or 'r' not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b'\n') + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode('ascii')) self.read = lambda: self.buffer.read().decode('ascii') self.readline = lambda: self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # endregion if __name__ == '__main__': main() ``` No
85,391
Provide a correct Python 3 solution for this coding contest problem. Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies. On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges). To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her! Input The first line of the input contains a single integer n – the number of vertices in the tree (1 ≀ n ≀ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≀ a < b ≀ n). It is guaranteed that the input represents a tree. Output Print one integer – the number of lifelines in the tree. Examples Input 4 1 2 1 3 1 4 Output 3 Input 5 1 2 2 3 3 4 3 5 Output 4 Note In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. "Correct Solution: ``` # Test ##################################################################################################################### def main(): tree_sLength = int(input()) - 1 tree = {} for i in range(tree_sLength): v1, v2 = map(int, input().split()) tree[v1] = tree.get(v1, ()) + (v2, ) tree[v2] = tree.get(v2, ()) + (v1, ) return nLifeLines(tree) def nLifeLines(tree): return sum(subTree_snLifeLines(len(tree[x])) for x in tree)//2 def subTree_snLifeLines(nBranches): return nBranches*(nBranches - 1) if __name__ == '__main__': print(main()) ```
85,392
Provide a correct Python 3 solution for this coding contest problem. Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies. On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges). To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her! Input The first line of the input contains a single integer n – the number of vertices in the tree (1 ≀ n ≀ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≀ a < b ≀ n). It is guaranteed that the input represents a tree. Output Print one integer – the number of lifelines in the tree. Examples Input 4 1 2 1 3 1 4 Output 3 Input 5 1 2 2 3 3 4 3 5 Output 4 Note In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. "Correct Solution: ``` n = int(input()) t = [0] * n for _ in range(n-1): a, b = input().split(' ') a, b = [int(a), int(b)] t[a-1] += 1 t[b-1] += 1 out = [(e * (e-1)) / 2 for e in t] print(int(sum(out))) ```
85,393
Provide a correct Python 3 solution for this coding contest problem. Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies. On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges). To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her! Input The first line of the input contains a single integer n – the number of vertices in the tree (1 ≀ n ≀ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≀ a < b ≀ n). It is guaranteed that the input represents a tree. Output Print one integer – the number of lifelines in the tree. Examples Input 4 1 2 1 3 1 4 Output 3 Input 5 1 2 2 3 3 4 3 5 Output 4 Note In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. "Correct Solution: ``` I=input d=[0]*10010 for _ in '0'*(int(I())-1):x,y=map(int,I().split());d[x]+=1;d[y]+=1 print(sum(i*(i-1)for i in d)//2) ```
85,394
Provide a correct Python 3 solution for this coding contest problem. Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies. On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges). To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her! Input The first line of the input contains a single integer n – the number of vertices in the tree (1 ≀ n ≀ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≀ a < b ≀ n). It is guaranteed that the input represents a tree. Output Print one integer – the number of lifelines in the tree. Examples Input 4 1 2 1 3 1 4 Output 3 Input 5 1 2 2 3 3 4 3 5 Output 4 Note In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. "Correct Solution: ``` def computeDegrees(n): degrees = [0 for vertex in range(n)] for edge in range(n-1): v1, v2 = map(int, input().split()) degrees[v1-1] += 1 degrees[v2-1] += 1 return degrees def computeNumberOfLength2Paths(degrees, n): return int(sum(d**2 for d in degrees)/2 - n + 1) if __name__ == '__main__': n = int(input()) degrees = computeDegrees(n) print(computeNumberOfLength2Paths(degrees, n)) ```
85,395
Provide a correct Python 3 solution for this coding contest problem. Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies. On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges). To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her! Input The first line of the input contains a single integer n – the number of vertices in the tree (1 ≀ n ≀ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≀ a < b ≀ n). It is guaranteed that the input represents a tree. Output Print one integer – the number of lifelines in the tree. Examples Input 4 1 2 1 3 1 4 Output 3 Input 5 1 2 2 3 3 4 3 5 Output 4 Note In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. "Correct Solution: ``` def main(): n = int(input()) l = [0] * (n + 1) for _ in range(n - 1): a, b = map(int, input().split()) l[a] += 1 l[b] += 1 res = 0 for x in l: res += x * (x - 1) print(res // 2) if __name__ == '__main__': main() ```
85,396
Provide a correct Python 3 solution for this coding contest problem. Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies. On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges). To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her! Input The first line of the input contains a single integer n – the number of vertices in the tree (1 ≀ n ≀ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≀ a < b ≀ n). It is guaranteed that the input represents a tree. Output Print one integer – the number of lifelines in the tree. Examples Input 4 1 2 1 3 1 4 Output 3 Input 5 1 2 2 3 3 4 3 5 Output 4 Note In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. "Correct Solution: ``` n=int(input()) L=[] S=0 for k in range(n): L.append(0) for k in range(n-1): a,b=map(int,input().split()) L[a-1]+=1 L[b-1]+=1 for k in range(n): S+=L[k]*(L[k]-1)/2 print(int(S)) ```
85,397
Provide a correct Python 3 solution for this coding contest problem. Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies. On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges). To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her! Input The first line of the input contains a single integer n – the number of vertices in the tree (1 ≀ n ≀ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≀ a < b ≀ n). It is guaranteed that the input represents a tree. Output Print one integer – the number of lifelines in the tree. Examples Input 4 1 2 1 3 1 4 Output 3 Input 5 1 2 2 3 3 4 3 5 Output 4 Note In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. "Correct Solution: ``` def main(): n = int(input()) l = [-1] * (n + 1) for _ in range(n - 1): a, b = map(int, input().split()) l[a] += 1 l[b] += 1 res = 0 for x in filter(None, l): res += x * (x + 1) print(res // 2) if __name__ == '__main__': main() ```
85,398
Provide a correct Python 3 solution for this coding contest problem. Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies. On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges). To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her! Input The first line of the input contains a single integer n – the number of vertices in the tree (1 ≀ n ≀ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≀ a < b ≀ n). It is guaranteed that the input represents a tree. Output Print one integer – the number of lifelines in the tree. Examples Input 4 1 2 1 3 1 4 Output 3 Input 5 1 2 2 3 3 4 3 5 Output 4 Note In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. "Correct Solution: ``` n = int(input()) d = n * [0] for i in range(n - 1): a, b = map(int, input().split()) d[a - 1] += 1 d[b - 1] += 1 cnt = 0 for i in d: cnt += (i * (i - 1)) // 2 print(cnt) ```
85,399