text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Tags: constructive algorithms, math, number theory Correct Solution: ``` p = [True] * 230 i = 2 while i*i <= len(p): if p[i]: for j in range(i+i, len(p), i): p[j] = False i += 1 p = [i for i in range(2, len(p)) if p[i]] n = int(input()) if n == 2: print(-1) else: x = 1 for i in range(n): x *= p[i] for i in range(n): print(x // p[i]) ```
96,600
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Tags: constructive algorithms, math, number theory Correct Solution: ``` def isPrime(n): if n == 1: return False for i in range(2, n): if n%i == 0: return False return True primes = [] for i in range(2, 1000): if isPrime(i): primes.append(i) if len(primes) == 50: break n = int(input()) if n == 2: print(-1) exit() prod = 1 for i in range(n): prod *= primes[i] for i in range(n): print(prod//primes[i]) ```
96,601
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Tags: constructive algorithms, math, number theory Correct Solution: ``` st = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397] #print(len(st)) n = int(input()) arr = [] x = 1 for i in range(n-1): arr.append(2*st[i+1]) x = x*st[i+1] arr.append(x*st[n+1]) if n>2: for x in arr: print(x) else: print(-1) #print(len(str(arr[len(arr)-1]))) ```
96,602
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Tags: constructive algorithms, math, number theory Correct Solution: ``` #!/usr/bin/env python3 primes = [] def sieve(n): isPrime = [True for i in range(n+1)] primes.append(2) for i in range(4, n + 1, 2): isPrime[i] = False for i in range(3, n + 1, 2): if (isPrime[i]): primes.append(i) j = i while i*j <= n: isPrime[i*j] = False j += 1 n = int(input()) if n == 2: print(-1) else: sieve(250) sup = 1 for i in range(n): sup *= primes[i] for i in range(n): print(sup // primes[i]) ```
96,603
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Tags: constructive algorithms, math, number theory Correct Solution: ``` def main(): n = int(input()) if n == 2: print(-1) return primes = [] for i in range(2, 10000): ok = True for j in range(2, i): if j * j > i: break; if i % j == 0: ok = False break if ok: primes.append(i) if len(primes) == n: break for i in range(n): x = 1 for j in range(n): if i == j: continue x *= primes[j] print(x) main() ```
96,604
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Tags: constructive algorithms, math, number theory Correct Solution: ``` def main(): SIZE = 100000 sieve = [True]*SIZE p = list() for it1 in range(2,SIZE): if sieve[it1]: for it2 in range(it1*it1,SIZE,it1): sieve[it2] = False p.append(it1) n = int(input()) if n==2: print(-1) exit(0) v = [1]*n pib = 0 for it1 in range(n): for it2 in range(it1+1,n): if pib==n+2: pib = 0 v[it1] *= p[pib] v[it2] *= p[pib] pib += 1 print('\n'.join(str(it) for it in v)) if __name__=="__main__": main() ```
96,605
Provide tags and a correct Python 3 solution for this coding contest problem. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Tags: constructive algorithms, math, number theory Correct Solution: ``` a=[] for i in range(2,1000): pr=1 for j in range(2,i): if(i%j==0): pr=0 if pr == 1: a.append(i) def gcd(x,y): if(y==0): return x return gcd(y,x%y) cur=1 n=int(input()) if(n==2): print(-1) exit() for i in range(n): #print(a[i]) cur=cur*a[i] b=[] for i in range(n): print(cur//a[i]) #b.append(cur/a[i]) ```
96,606
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Submitted Solution: ``` primes = [0] * 55 def generatePrimes(n): p = [True] * (n + 1) p[0] = p[1] = False i = 2 while (i * i <= n): if (p[i]): j = i * i while (j <= n): p[j] = False j = j + i i = i + 1 i = 2 idx = 0 while (i <= n): if (p[i]): primes[idx] = i idx = idx + 1 i = i + 1 n = int(input().strip()) if (n == 2): print(-1) quit() generatePrimes(250) primeIdx = 0 a = [1] * 55 for i in range(0, n): for j in range(0, n): if (i != j): a[j] = a[j] * primes[primeIdx] primeIdx = primeIdx + 1 for i in range(0, n): print(a[i]) ``` Yes
96,607
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Submitted Solution: ``` n = int(input()) if(n == 2): print(-1) else: print(99) print(55) k = 0 for i in range(0, n - 2): print(2*2*15*(k + 1)) k += 1 ``` Yes
96,608
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Submitted Solution: ``` def isPrime(n): i = 2 while (i * i <= n): if (n % i == 0): return 0 i += 1 return n > 1 m = int(input()) if (m == 2): print(-1) exit(0) i = 2 primes = [] while(len(primes) <= m + 1): if (isPrime(i)): primes.append(i); i += 1 p = 1 for j in range(0, m - 1): print(primes[j] * primes[m]); p *= primes[j] print(p * primes[m - 1]) ``` Yes
96,609
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Submitted Solution: ``` a= int(input()) if(a == 2): print(-1) exit(0) v = [] for i in range(2,1000): if(i < 4): v.append(i) continue p = 0 for z in range(2,i): if(i != z and i%z == 0): p = 1 break if(p != 0): continue v.append(i) c = 1 for i in range(a): c *= v[i] k = [] for i in range(a): print(int(c//v[i])) ``` Yes
96,610
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Submitted Solution: ``` isprime = [1 for i in range(2000)] isprime[0] = 0 isprime[1] = 0 for i in range(2,2000,1): if(isprime[i]): j = i*i while j < 2000: isprime[j] = 0 j += i prime = [] for i in range(2,2000,1): if(isprime[i]): prime.append(i) n = int(input()) for i in range(n): val = 1 for j in range(n): if i==j : continue val *= prime[j] print(val, end=' ') print('') ``` No
96,611
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Submitted Solution: ``` p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973, 10007, 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093, 10099, 10103, 10111, 10133, 10139, 10141, 10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211, 10223, 10243, 10247, 10253, 10259, 10267, 10271, 10273, 10289, 10301, 10303, 10313, 10321, 10331, 10333, 10337, 10343, 10357, 10369, 10391, 10399, 10427, 10429, 10433, 10453, 10457, 10459, 10463, 10477, 10487, 10499, 10501, 10513, 10529, 10531, 10559, 10567, 10589, 10597, 10601, 10607, 10613, 10627, 10631, 10639, 10651, 10657, 10663, 10667, 10687, 10691, 10709, 10711, 10723, 10729, 10733, 10739, 10753, 10771, 10781, 10789, 10799, 10831, 10837, 10847, 10853, 10859, 10861, 10867, 10883, 10889, 10891, 10903, 10909, 10937, 10939, 10949, 10957, 10973, 10979, 10987, 10993, 11003, 11027, 11047, 11057, 11059, 11069, 11071, 11083, 11087, 11093, 11113, 11117, 11119, 11131, 11149, 11159, 11161, 11171, 11173, 11177, 11197, 11213, 11239, 11243, 11251, 11257, 11261, 11273, 11279, 11287, 11299, 11311, 11317, 11321, 11329, 11351, 11353, 11369, 11383, 11393, 11399, 11411, 11423, 11437, 11443, 11447, 11467, 11471, 11483, 11489, 11491, 11497, 11503, 11519, 11527, 11549, 11551, 11579, 11587, 11593, 11597, 11617, 11621, 11633, 11657, 11677, 11681, 11689, 11699, 11701, 11717, 11719, 11731, 11743, 11777, 11779, 11783, 11789, 11801, 11807, 11813, 11821, 11827, 11831, 11833, 11839, 11863, 11867, 11887, 11897, 11903, 11909, 11923, 11927, 11933, 11939, 11941, 11953, 11959, 11969, 11971, 11981, 11987, 12007, 12011, 12037, 12041, 12043, 12049, 12071, 12073, 12097, 12101, 12107, 12109, 12113, 12119, 12143, 12149, 12157, 12161, 12163, 12197, 12203, 12211, 12227, 12239, 12241, 12251, 12253, 12263, 12269, 12277, 12281, 12289, 12301, 12323, 12329, 12343, 12347, 12373, 12377, 12379, 12391, 12401, 12409, 12413, 12421, 12433, 12437, 12451, 12457, 12473, 12479, 12487, 12491, 12497, 12503, 12511, 12517, 12527, 12539, 12541, 12547, 12553, 12569, 12577, 12583, 12589, 12601, 12611, 12613, 12619, 12637, 12641, 12647, 12653, 12659, 12671, 12689, 12697, 12703, 12713, 12721, 12739, 12743, 12757, 12763, 12781, 12791, 12799, 12809, 12821, 12823, 12829, 12841, 12853, 12889, 12893, 12899, 12907, 12911, 12917, 12919, 12923, 12941, 12953, 12959, 12967, 12973, 12979, 12983, 13001, 13003, 13007, 13009, 13033, 13037, 13043, 13049, 13063, 13093, 13099, 13103, 13109, 13121, 13127, 13147, 13151, 13159, 13163, 13171, 13177, 13183, 13187, 13217, 13219, 13229, 13241, 13249, 13259, 13267, 13291, 13297, 13309, 13313, 13327, 13331, 13337, 13339, 13367, 13381, 13397, 13399, 13411, 13417, 13421, 13441, 13451, 13457, 13463, 13469, 13477, 13487, 13499, 13513, 13523, 13537, 13553, 13567, 13577, 13591, 13597, 13613, 13619, 13627, 13633, 13649, 13669, 13679, 13681, 13687, 13691, 13693, 13697, 13709, 13711, 13721, 13723, 13729, 13751, 13757, 13759, 13763, 13781, 13789, 13799, 13807, 13829, 13831, 13841, 13859, 13873, 13877, 13879, 13883, 13901, 13903, 13907, 13913, 13921, 13931, 13933, 13963, 13967, 13997, 13999, 14009, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083, 14087, 14107, 14143, 14149, 14153, 14159, 14173, 14177, 14197, 14207, 14221, 14243, 14249, 14251, 14281, 14293, 14303, 14321, 14323, 14327, 14341, 14347, 14369, 14387, 14389, 14401, 14407, 14411, 14419, 14423, 14431, 14437, 14447, 14449, 14461, 14479, 14489, 14503, 14519, 14533, 14537, 14543, 14549, 14551, 14557, 14561, 14563, 14591, 14593, 14621, 14627, 14629, 14633, 14639, 14653, 14657, 14669, 14683, 14699, 14713, 14717, 14723, 14731, 14737, 14741, 14747, 14753, 14759, 14767, 14771, 14779, 14783, 14797, 14813, 14821, 14827, 14831, 14843, 14851, 14867, 14869, 14879, 14887, 14891, 14897, 14923, 14929, 14939, 14947, 14951, 14957, 14969, 14983] n = int(input()) if n == 2: print(-1) exit(0) a = [2] * n a[n - 1] = 14983 last = 1 for i in range(1, n): c = a.count(a[i]) if c != 1: a[i] *= p[last] last += 1 for i in a: print(i) ``` No
96,612
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Submitted Solution: ``` import sys from math import gcd,sqrt,ceil,log2 from collections import defaultdict,Counter,deque from bisect import bisect_left,bisect_right import math sys.setrecursionlimit(2*10**5+10) import heapq from itertools import permutations # input=sys.stdin.readline # def print(x): # sys.stdout.write(str(x)+"\n") # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 aa='abcdefghijklmnopqrstuvwxyz' 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") # import sys # import io, os # input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def get_sum(bit,i): s = 0 i+=1 while i>0: s+=bit[i] i-=i&(-i) return s def update(bit,n,i,v): i+=1 while i<=n: bit[i]+=v i+=i&(-i) def modInverse(b,m): g = math.gcd(b, m) if (g != 1): return -1 else: return pow(b, m - 2, m) def primeFactors(n): sa = [] # sa.add(n) while n % 2 == 0: sa.append(2) n = n // 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: sa.append(i) n = n // i # sa.add(n) if n > 2: sa.append(n) return sa def seive(n): pri = [True]*(n+1) p = 2 while p*p<=n: if pri[p] == True: for i in range(p*p,n+1,p): pri[i] = False p+=1 return pri def check_prim(n): if n<0: return False for i in range(2,int(sqrt(n))+1): if n%i == 0: return False return True def getZarr(string, z): n = len(string) # [L,R] make a window which matches # with prefix of s l, r, k = 0, 0, 0 for i in range(1, n): # if i>R nothing matches so we will calculate. # Z[i] using naive way. if i > r: l, r = i, i # R-L = 0 in starting, so it will start # checking from 0'th index. For example, # for "ababab" and i = 1, the value of R # remains 0 and Z[i] becomes 0. For string # "aaaaaa" and i = 1, Z[i] and R become 5 while r < n and string[r - l] == string[r]: r += 1 z[i] = r - l r -= 1 else: # k = i-L so k corresponds to number which # matches in [L,R] interval. k = i - l # if Z[k] is less than remaining interval # then Z[i] will be equal to Z[k]. # For example, str = "ababab", i = 3, R = 5 # and L = 2 if z[k] < r - i + 1: z[i] = z[k] # For example str = "aaaaaa" and i = 2, # R is 5, L is 0 else: # else start from R and check manually l = i while r < n and string[r - l] == string[r]: r += 1 z[i] = r - l r -= 1 def search(text, pattern): # Create concatenated string "P$T" concat = pattern + "$" + text l = len(concat) z = [0] * l getZarr(concat, z) ha = [] for i in range(l): if z[i] == len(pattern): ha.append(i - len(pattern) - 1) return ha # n,k = map(int,input().split()) # l = list(map(int,input().split())) # # n = int(input()) # l = list(map(int,input().split())) # # hash = defaultdict(list) # la = [] # # for i in range(n): # la.append([l[i],i+1]) # # la.sort(key = lambda x: (x[0],-x[1])) # ans = [] # r = n # flag = 0 # lo = [] # ha = [i for i in range(n,0,-1)] # yo = [] # for a,b in la: # # if a == 1: # ans.append([r,b]) # # hash[(1,1)].append([b,r]) # lo.append((r,b)) # ha.pop(0) # yo.append([r,b]) # r-=1 # # elif a == 2: # # print(yo,lo) # # print(hash[1,1]) # if lo == []: # flag = 1 # break # c,d = lo.pop(0) # yo.pop(0) # if b>=d: # flag = 1 # break # ans.append([c,b]) # yo.append([c,b]) # # # # elif a == 3: # # if yo == []: # flag = 1 # break # c,d = yo.pop(0) # if b>=d: # flag = 1 # break # if ha == []: # flag = 1 # break # # ka = ha.pop(0) # # ans.append([ka,b]) # ans.append([ka,d]) # yo.append([ka,b]) # # if flag: # print(-1) # else: # print(len(ans)) # for a,b in ans: # print(a,b) def mergeIntervals(arr): # Sorting based on the increasing order # of the start intervals arr.sort(key = lambda x: x[0]) # array to hold the merged intervals m = [] s = -10000 max = -100000 for i in range(len(arr)): a = arr[i] if a[0] > max: if i != 0: m.append([s,max]) max = a[1] s = a[0] else: if a[1] >= max: max = a[1] #'max' value gives the last point of # that particular interval # 's' gives the starting point of that interval # 'm' array contains the list of all merged intervals if max != -100000 and [s, max] not in m: m.append([s, max]) return m class SortedList: def __init__(self, iterable=[], _load=200): """Initialize sorted list instance.""" values = sorted(iterable) self._len = _len = len(values) self._load = _load self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)] self._list_lens = [len(_list) for _list in _lists] self._mins = [_list[0] for _list in _lists] self._fen_tree = [] self._rebuild = True def _fen_build(self): """Build a fenwick tree instance.""" self._fen_tree[:] = self._list_lens _fen_tree = self._fen_tree for i in range(len(_fen_tree)): if i | i + 1 < len(_fen_tree): _fen_tree[i | i + 1] += _fen_tree[i] self._rebuild = False def _fen_update(self, index, value): """Update `fen_tree[index] += value`.""" if not self._rebuild: _fen_tree = self._fen_tree while index < len(_fen_tree): _fen_tree[index] += value index |= index + 1 def _fen_query(self, end): """Return `sum(_fen_tree[:end])`.""" if self._rebuild: self._fen_build() _fen_tree = self._fen_tree x = 0 while end: x += _fen_tree[end - 1] end &= end - 1 return x def _fen_findkth(self, k): """Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`).""" _list_lens = self._list_lens if k < _list_lens[0]: return 0, k if k >= self._len - _list_lens[-1]: return len(_list_lens) - 1, k + _list_lens[-1] - self._len if self._rebuild: self._fen_build() _fen_tree = self._fen_tree idx = -1 for d in reversed(range(len(_fen_tree).bit_length())): right_idx = idx + (1 << d) if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]: idx = right_idx k -= _fen_tree[idx] return idx + 1, k def _delete(self, pos, idx): """Delete value at the given `(pos, idx)`.""" _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len -= 1 self._fen_update(pos, -1) del _lists[pos][idx] _list_lens[pos] -= 1 if _list_lens[pos]: _mins[pos] = _lists[pos][0] else: del _lists[pos] del _list_lens[pos] del _mins[pos] self._rebuild = True def _loc_left(self, value): """Return an index pair that corresponds to the first position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins lo, pos = -1, len(_lists) - 1 while lo + 1 < pos: mi = (lo + pos) >> 1 if value <= _mins[mi]: pos = mi else: lo = mi if pos and value <= _lists[pos - 1][-1]: pos -= 1 _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value <= _list[mi]: idx = mi else: lo = mi return pos, idx def _loc_right(self, value): """Return an index pair that corresponds to the last position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins pos, hi = 0, len(_lists) while pos + 1 < hi: mi = (pos + hi) >> 1 if value < _mins[mi]: hi = mi else: pos = mi _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value < _list[mi]: idx = mi else: lo = mi return pos, idx def add(self, value): """Add `value` to sorted list.""" _load = self._load _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len += 1 if _lists: pos, idx = self._loc_right(value) self._fen_update(pos, 1) _list = _lists[pos] _list.insert(idx, value) _list_lens[pos] += 1 _mins[pos] = _list[0] if _load + _load < len(_list): _lists.insert(pos + 1, _list[_load:]) _list_lens.insert(pos + 1, len(_list) - _load) _mins.insert(pos + 1, _list[_load]) _list_lens[pos] = _load del _list[_load:] self._rebuild = True else: _lists.append([value]) _mins.append(value) _list_lens.append(1) self._rebuild = True def discard(self, value): """Remove `value` from sorted list if it is a member.""" _lists = self._lists if _lists: pos, idx = self._loc_right(value) if idx and _lists[pos][idx - 1] == value: self._delete(pos, idx - 1) def remove(self, value): """Remove `value` from sorted list; `value` must be a member.""" _len = self._len self.discard(value) if _len == self._len: raise ValueError('{0!r} not in list'.format(value)) def pop(self, index=-1): """Remove and return value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) value = self._lists[pos][idx] self._delete(pos, idx) return value def bisect_left(self, value): """Return the first index to insert `value` in the sorted list.""" pos, idx = self._loc_left(value) return self._fen_query(pos) + idx def bisect_right(self, value): """Return the last index to insert `value` in the sorted list.""" pos, idx = self._loc_right(value) return self._fen_query(pos) + idx def count(self, value): """Return number of occurrences of `value` in the sorted list.""" return self.bisect_right(value) - self.bisect_left(value) def __len__(self): """Return the size of the sorted list.""" return self._len def __getitem__(self, index): """Lookup value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) return self._lists[pos][idx] def __delitem__(self, index): """Remove value at `index` from sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) self._delete(pos, idx) def __contains__(self, value): """Return true if `value` is an element of the sorted list.""" _lists = self._lists if _lists: pos, idx = self._loc_left(value) return idx < len(_lists[pos]) and _lists[pos][idx] == value return False def __iter__(self): """Return an iterator over the sorted list.""" return (value for _list in self._lists for value in _list) def __reversed__(self): """Return a reverse iterator over the sorted list.""" return (value for _list in reversed(self._lists) for value in reversed(_list)) def __repr__(self): """Return string representation of sorted list.""" return 'SortedList({0})'.format(list(self)) def ncr(n, r, p): num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p def sol(n): seti = set() for i in range(1,int(sqrt(n))+1): if n%i == 0: seti.add(n//i) seti.add(i) return seti def lcm(a,b): return (a*b)//gcd(a,b) # # n,p = map(int,input().split()) # # s = input() # # if n <=2: # if n == 1: # pass # if n == 2: # pass # i = n-1 # idx = -1 # while i>=0: # z = ord(s[i])-96 # k = chr(z+1+96) # flag = 1 # if i-1>=0: # if s[i-1]!=k: # flag+=1 # else: # flag+=1 # if i-2>=0: # if s[i-2]!=k: # flag+=1 # else: # flag+=1 # if flag == 2: # idx = i # s[i] = k # break # if idx == -1: # print('NO') # exit() # for i in range(idx+1,n): # if # def moore_voting(l): count1 = 0 count2 = 0 first = 10**18 second = 10**18 n = len(l) for i in range(n): if l[i] == first: count1+=1 elif l[i] == second: count2+=1 elif count1 == 0: count1+=1 first = l[i] elif count2 == 0: count2+=1 second = l[i] else: count1-=1 count2-=1 for i in range(n): if l[i] == first: count1+=1 elif l[i] == second: count2+=1 if count1>n//3: return first if count2>n//3: return second return -1 def find_parent(u,parent): if u!=parent[u]: parent[u] = find_parent(parent[u],parent) return parent[u] def dis_union(n): par = [i for i in range(n+1)] rank = [1]*(n+1) k = int(input()) for i in range(k): a,b = map(int,input().split()) z1,z2 = find_parent(a,par),find_parent(b,par) if z1!=z2: par[z1] = z2 rank[z2]+=rank[z1] def dijkstra(n,tot,hash): hea = [[0,n]] dis = [10**18]*(tot+1) dis[n] = 0 boo = defaultdict(bool) check = defaultdict(int) while hea: a,b = heapq.heappop(hea) if boo[b]: continue boo[b] = True for i,w in hash[b]: if b == 1: c = 0 if (1,i,w) in nodes: c = nodes[(1,i,w)] del nodes[(1,i,w)] if dis[b]+w<dis[i]: dis[i] = dis[b]+w check[i] = c elif dis[b]+w == dis[i] and c == 0: dis[i] = dis[b]+w check[i] = c else: if dis[b]+w<=dis[i]: dis[i] = dis[b]+w check[i] = check[b] heapq.heappush(hea,[dis[i],i]) return check def power(x,y,p): res = 1 x = x%p if x == 0: return 0 while y>0: if (y&1) == 1: res*=x x = x*x y = y>>1 return res # # # # t = int(input()) # # for _ in range(t): # # n,m = map(int,input().split()) # l = [] # for i in range(n): # la = list(map(int,input().split())) # l.append(la) # seti = set() # for i in range(n): # for j in range(m): # flag = 0 # if i-1>=0 and l[i][j] == l[i-1][j]: # flag = 1 # if j-1>=0 and l[i][j-1] == l[i][j]: # flag = 1 # if flag: # seti.add((i,j)) # l[i][j]+=1 # # # for i in range(n-1,-1,-1): # for j in range(m-1,-1,-1): # flag = 0 # if i+1<n and l[i][j] == l[i+1][j] and (i,j) not in seti: # flag = 1 # if j+1<m and l[i][j] == l[i][j+1] and (i,j) not in seti: # flag = 1 # # # if flag == 1: # l[i][j]+=1 # # # # # for i in l: # print(*i) # n = int(input()) # print(gcd(55,11115)) ans = [] i = 1 while len(ans)!=n-1: if i+1 == 11: i+=1 continue ans.append((i+1)*(11)) i+=1 k = 1 for i in range(2,100): if i!=11 and check_prim(i): k*=i ans.append(k) if n == 2: print(11) print(k) exit() for i in ans: print(i) ``` No
96,613
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct. Help the friends to choose the suitable numbers a1, ..., an. Input The first line contains an integer n (2 ≤ n ≤ 50). Output If there is no answer, print "-1" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: * For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 * GCD(a1, a2, ..., an) = 1 * For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj 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). Examples Input 3 Output 99 55 11115 Input 4 Output 385 360 792 8360 Submitted Solution: ``` p = [True]*(10000000+5) primes = [] def criba(N): p[0] = p[1] = False i = 2; while(i*i<=N): if p[i]: for j in range(i*i,N,i): p[j] = False; i = i+1 for i in range(2,N): if p[i]: primes.append(i); criba(1000000+5) n = int(input()) if n==2: print("-1") else: a = 1 for i in range(0,n): a = a*primes[i] for i in range(0,n): print(int(a/primes[i])) ``` No
96,614
Provide tags and a correct Python 3 solution for this coding contest problem. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Tags: implementation Correct Solution: ``` test= int(input()) # while test> 0: count= input().split().count("0") if count==1 and test==1: print("NO") elif count==0 and test==1 : print("YES") elif count==1 : print("YES") else: print("NO") ```
96,615
Provide tags and a correct Python 3 solution for this coding contest problem. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Tags: implementation Correct Solution: ``` if __name__ == "__main__": n, x = int(input()), [int(i) for i in input().split()] print('NO' if (n == 1 and x[0] == 0) or (n > 1 and sum(x) != n-1) else 'YES') ```
96,616
Provide tags and a correct Python 3 solution for this coding contest problem. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Tags: implementation Correct Solution: ``` n=int(input()) li=list(map(int,input().split())) flag=0 if n==1 and li[0]==0: flag=1 elif n==1 and li[0]==1: flag=0 else: k=li.count(1) if n-k>1 or n-k==0: flag=1 if flag==1: print("NO") else: print("YES") ```
96,617
Provide tags and a correct Python 3 solution for this coding contest problem. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Tags: implementation Correct Solution: ``` # n = 1 # list_ = [1, 0, 1] # list_ = [1, 0, 0] # list_ = [1, 1, 0] # list_ = [1, 0, 1] # list_ = [0] # list_ = [1, 0, 0] n = int(input()) list_ = [int(x) for x in input().split()] # n = len(list_) count = 0 if (n == 1 and list_[0] == 1): print("YES") elif (n == 1 and list_[0] == 0): print("NO") else: for i in range(n): if (list_[i] == 0): count = count + 1 if (count == 1): print("YES") else: print("NO") ### no but say yes ```
96,618
Provide tags and a correct Python 3 solution for this coding contest problem. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Tags: implementation Correct Solution: ``` n=int(input()) l=list(map(int,input().split())) if l.count(0)==1 and len(l)>1 or len(l)==1 and l[0]==1: print("YES") else: print("NO") ```
96,619
Provide tags and a correct Python 3 solution for this coding contest problem. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Tags: implementation Correct Solution: ``` # Name : Sachdev Hitesh # College : GLSICA user=int(input()) s=list(input().split(" ")) c=0 if user==1 and int(s[0])==1: print("YES") elif user==1 and int(s[0])==0: print("NO") else: for i in s: if int(i)==0: c+=1 if c==1: print("YES") else: print("NO") ```
96,620
Provide tags and a correct Python 3 solution for this coding contest problem. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Tags: implementation Correct Solution: ``` if __name__ == "__main__": n, x = int(input()), [int(i) for i in input().split()] if n == 1: print('YES' if x[0] == 1 else 'NO') elif sum(x) == n - 1: print('YES') else: print('NO') ```
96,621
Provide tags and a correct Python 3 solution for this coding contest problem. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Tags: implementation Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) if (n == 1): if (a[0] == 1): print ("YES") else: print ("NO") exit() count = 0 result = 1 for i in range(n): if a[i] == 0: count += 1 if count == 1: print("YES") else: print("NO") ```
96,622
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Submitted Solution: ``` n=int(input()) s=input().split() t=False if n==1: if s[0]=='0': print('NO') exit() else: print('YES') exit() else: for i in range(n): if s[i]=='0': if t: print('NO') exit() else: t=True if not t: print('NO') else: print('YES') ``` Yes
96,623
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Submitted Solution: ``` def main(): n = int(input()) arr = list(map(int, input().split())) total = sum(arr) if n == 1 and total == 1: print("YES") elif n >= 2 and total == n - 1: print("YES") else: print("NO") if __name__ == "__main__": main() ``` Yes
96,624
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Submitted Solution: ``` n = int(input()) x = input().count('0') print('YES' if x==(n!=1) else 'NO') ``` Yes
96,625
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) if (n == 1 and a != [1]) or (n != 1 and a.count(1) != n - 1): print('NO') else: print('YES') ``` Yes
96,626
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Submitted Solution: ``` n = input() x = input() x = x.split() if 1000>=int(n)>=1: if int(n)== len(x): if int(n)== 1 and x[0]=="0": print("NO") elif x.count("0") <=1: print("YES") else: print("NO") else: print("NO") else: print("NO") ``` No
96,627
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Submitted Solution: ``` n = int(input()) A = list(map(int, input().split())) if(n == 1): if(A[0] == 1): print("YES") else: print("NO") else: if(A[0] == 1 and A[-1]==1): print("YES") else: print("NO") ``` No
96,628
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Submitted Solution: ``` n=int(input()) s=input() if n==1 and s=='1': print('YES') elif s.count('0')<=1: print('YES') elif s.count('0')==0 and s.count('1')>1: print('NO') else: print('NO') ``` No
96,629
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Determine if it is fastened in a right way. Input The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1. Output In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO". Examples Input 3 1 0 1 Output YES Input 3 1 0 0 Output NO Submitted Solution: ``` n=int(input()) l=sum(map(int,input().split())) if n==1 or n==l+1: print("YES") else: print("NO") ``` No
96,630
Provide tags and a correct Python 3 solution for this coding contest problem. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Tags: implementation Correct Solution: ``` n,c=map(int, input().split()) t=[int(t) for t in input().split()] r=1 for i in range(n-1): if t[i+1]-t[i]<=c: r+=1 else: r=1 print(r) ```
96,631
Provide tags and a correct Python 3 solution for this coding contest problem. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Tags: implementation Correct Solution: ``` import sys from collections import defaultdict, Counter import string def main(): n, c = [int(i) for i in input().split()] t = [int(i) for i in input().split()] words = 1 for i, j in zip(t[:-1], t[1:]): if j - i > c: words = 1 else: words += 1 print(words) if __name__ == '__main__': main() ```
96,632
Provide tags and a correct Python 3 solution for this coding contest problem. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Tags: implementation Correct Solution: ``` #Codeforce 716A n,c = (int(v) for v in input().split()) list1=[int(u) for u in input().split()] list1=list1[::-1] ans=1 for i in range(1,n): if list1[i-1] - list1[i] <= c: ans += 1 else: break print(ans) ```
96,633
Provide tags and a correct Python 3 solution for this coding contest problem. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Tags: implementation Correct Solution: ``` n,c=map(int,input().split()) s=list(map(int,input().split())) ans=1 for i in range(1,n): if s[i]-s[i-1]>c: ans=1 else: ans+=1 print(ans) ```
96,634
Provide tags and a correct Python 3 solution for this coding contest problem. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Tags: implementation Correct Solution: ``` n,c = [int(x) for x in input().split()] t = list(map(int,input().split())) l = [t[0]] for i in range(1,n): if t[i]-t[i-1]<=c: l.append(t[i]) else: l.clear() l.append(t[i]) print(len(l)) ```
96,635
Provide tags and a correct Python 3 solution for this coding contest problem. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Tags: implementation Correct Solution: ``` n, c = map(int, input().split(" ")) t = list(map(int, input().split(" "))) words = 1 for i in range(1, n): if t[i] - t[i - 1] <= c: words += 1 else: words = 1 print(words) ```
96,636
Provide tags and a correct Python 3 solution for this coding contest problem. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Tags: implementation Correct Solution: ``` n , c = map(int , input().split()) x = [int(x) for x in input().split()] cnt = 0 for i in range(len(x) - 1): if x[i + 1] - x[i] <= c: cnt += 1 else: cnt = 0 print(cnt + 1) ```
96,637
Provide tags and a correct Python 3 solution for this coding contest problem. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Tags: implementation Correct Solution: ``` n, c = map(int, input().split()) l = list(map(int, input().split())) ans = 1 for i in range(1, n): if l[i] - l[i - 1] <= c: ans += 1 else: ans = 1 print(ans) ```
96,638
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Submitted Solution: ``` n,c=map(int,input().split()) sum=1 words=list(map(int,input().split())) for k in range(1,n): if words[k]-words[k-1]<=c: sum+=1 else: sum=1 print(sum) ``` Yes
96,639
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Submitted Solution: ``` import sys n,c = map(int,input().split()) arr = list(map(int,input().split())) cnt = 1 for i in range(n-1,-1,-1): if i > 0 and arr[i] - arr[i-1] > c: break elif i > 0 and arr[i] - arr[i-1] <= c: cnt += 1 print(cnt) ``` Yes
96,640
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Submitted Solution: ``` # ✪ H4WK3yE乡 # Mayank Chaudhary # I can :) # ABES EC , Ghaziabad # ///==========Libraries, Constants and Functions=============/// import sys from bisect import bisect_left,bisect_right,insort from collections import deque,Counter from math import gcd,sqrt,factorial,ceil,log10 from itertools import permutations from heapq import heappush,heappop,heapify inf = float("inf") mod = 1000000007 mini=1000000007 def max_subarray(array): # <------- Extended Kadane's algo which gives maximum sum sub-array but also the starting and ending indexes max_so_far = max_ending_here = array[0] start_index = 0 end_index = 0 for i in range(1, len(array) -1): temp_start_index = temp_end_index = None if array[i] > (max_ending_here + array[i]): temp_start_index = temp_end_index = i max_ending_here = array[i] else: temp_end_index = i max_ending_here = max_ending_here + array[i] if max_so_far < max_ending_here: max_so_far = max_ending_here if temp_start_index != None: start_index = temp_start_index end_index = i print(max_so_far, start_index, end_index) def ncr(n,r): # < ------ To calculate nCr mod p value using Fermat Little under modulo m d=10**9+7 num=fact(n) den=(fact(r)*fact(n-r))%d den=pow(den,d-2,d) return (num*den)%d def sieve(n): # <----- sieve of eratosthenes for prime no. prime=[True for i in range(n+1)] lst=[] p=2 while p*p<=n: if prime[p]: for i in range(p*p,n+1,p): prime[i]=False p=p+1 for i in range(2,n+1): if prime[i]: lst.append(i) return lst def binary(number): # <----- calculate the no. of 1's in binary representation of number result=0 while number: result=result+1 number=number&(number-1) return result def calculate_factors(n): #<---- most efficient method to calculate no. of factors of number hh = [1] * (n + 1); p = 2; while((p * p) < n): if (hh[p] == 1): for i in range((p * 2), n, p): hh[i] = 0; p += 1; total = 1; for p in range(2, n + 1): if (hh[p] == 1): count = 0; if (n % p == 0): while (n % p == 0): n = int(n / p); count += 1; total *= (count + 1); return total; def prime_factors(n): #<------------ to find prime factors of a no. i = 2 factors = set() while i * i <= n: if n % i: i += 1 else: factors.add(n//i) n=n//i factors.add(i) if n > 1: factors.add(n) return (factors) def isPrime(n): #<-----------check whether a no. is prime or not if n==2 or n==3: return True if n%2==0 or n<2: return False for i in range(3,int(n**0.5)+1,2): # only odd numbers if n%i==0: return False return True def solve(n): if n==1: return (1) p=2;result=[] while (p*p)<=n: if n%p==0: result.append(p);result.append(n//p) p=p+1 return result def atMostSum(arr, n, k): _sum = 0 cnt = 0 maxcnt = 0 for i in range(n): # If adding current element doesn't # Cross limit add it to current window if ((_sum + arr[i]) <= k): _sum += arr[i] cnt += 1 # Else, remove first element of current # window and add the current element elif(sum != 0): _sum = _sum - arr[i - cnt] + arr[i] # keep track of max length. maxcnt = max(cnt, maxcnt) return maxcnt def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() # ///==========MAIN=============/// n,c=get_ints() count=1 Arr=get_array() for i in range(1,n): if Arr[i]-Arr[i-1]<=c: count+=1 else: count=1 print(count) ``` Yes
96,641
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Submitted Solution: ``` n, c = map(int, input().split()) t = list(map(int, input().split())) time = 0 number_of_words = 0 for action in t: if action - time > c: number_of_words = 1 else: number_of_words += 1 time = action print(number_of_words) ``` Yes
96,642
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Submitted Solution: ``` n , c = map(int, input().split()) a = list(map(int,input().split())) ans , it1, tmp = 1, 0, 1 for it2 in range(1, n): if a[it2]-a[it1] > c : ans = max(ans, tmp) tmp = 1 it1 , tmp = it1+1, tmp+1 print(ans) ``` No
96,643
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Submitted Solution: ``` n, c =[int(i) for i in input().split()] t = [int(i) for i in input().split()] ans = 0 for i in range(c): if t[i] - t[i-1] <= c: ans += 2 else: ans = 1 print(ans) ``` No
96,644
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Submitted Solution: ``` a,b=map(int,input().split()) x=list(map(int,input().split())) count=1; for i in range (a): if (x[i]-x[i-1])<= b: count=count+1 else: count=1 print(count) ``` No
96,645
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word. Output Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn. Examples Input 6 5 1 3 8 14 19 20 Output 3 Input 6 1 1 3 5 7 9 10 Output 2 Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1. Submitted Solution: ``` n,m = map(int,input().split()) a = [int(s) for s in input().split()] count = 0 for i in range(n - 1): if abs(a[i] - a[i + 1]) > m : count = 1 else: count += 1 print(count) ``` No
96,646
Provide tags and a correct Python 3 solution for this coding contest problem. Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them. As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one. Input The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k. Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree. Output Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7). Examples Input 2 0 1 2 Output 1 Input 2 1 1 2 Output 3 Input 4 1 1 2 2 3 3 4 Output 9 Input 7 2 1 2 2 3 1 4 4 5 1 6 6 7 Output 91 Note In the first sample, Ostap has to paint both vertices black. In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both. In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}. Tags: dp, trees Correct Solution: ``` def main(): n, k = map(int, input().split()) cnt = [[[0] * 21 for _ in (0, 1)] for _ in range(n + 1)] edges, mod = [[] for _ in range(n + 1)], 1000000007 for _ in range(n - 1): u, v = map(int, input().split()) edges[u].append(v) edges[v].append(u) def dfs(u, f): cnt[u][0][0] = cnt[u][1][k] = 1 for v in edges[u]: if v != f: dfs(v, u) tmp0, tmp1 = [0] * 21, [0] * 21 for i in range(k + 1): for j in range(k + 1): if i != k: tmp0[j if i < j else i + 1] += cnt[u][0][j] * cnt[v][0][i] if i < j: tmp1[j] += cnt[u][1][j] * cnt[v][0][i] elif i != k: tmp0[i + 1] += cnt[u][1][j] * cnt[v][0][i] if i > j: tmp1[i - 1] += cnt[u][0][j] * cnt[v][1][i] else: tmp0[j] += cnt[u][0][j] * cnt[v][1][i] tmp1[max(i - 1, j)] += cnt[u][1][j] * cnt[v][1][i] for i in range(21): tmp0[i] %= mod tmp1[i] %= mod cnt[u][0] = tmp0 cnt[u][1] = tmp1 dfs(1, 1) print(sum(cnt[1][1][j] for j in range(k + 1)) % mod) if __name__ == '__main__': main() ```
96,647
Provide tags and a correct Python 3 solution for this coding contest problem. Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them. As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one. Input The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k. Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree. Output Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7). Examples Input 2 0 1 2 Output 1 Input 2 1 1 2 Output 3 Input 4 1 1 2 2 3 3 4 Output 9 Input 7 2 1 2 2 3 1 4 4 5 1 6 6 7 Output 91 Note In the first sample, Ostap has to paint both vertices black. In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both. In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}. Tags: dp, trees Correct Solution: ``` import sys #sys.stdin=open("data.txt") #sys.stdout=open("data.txt","w") input=sys.stdin.readline n,k=map(int,input().split()) g=[[] for _ in range(n+1)] vis=[0]*(n+1) for _ in range(n-1): u,v=map(int,input().split()) g[u].append(v) g[v].append(u) def getans(u,k): vis[u]=1 # first k -> need k # then cover exact # last k -> covers k above totalv=[0]*(2*k+1) totalv[k-1]=1 carry=1 # total when this node is black for v in g[u]: if vis[v]: continue getv=getans(v,k) carry=(carry*sum(getv))%1000000007 out2=[0]*(2*k+1) #print("before",totalv) for i in range(1,2*k+1): for j in range(2*k+1): if j+i>=2*k: out2[max(i-1,j)]+=getv[i]*totalv[j] else: out2[min(i-1,j)]+=getv[i]*totalv[j] for i in range(2*k+1): totalv[i]=out2[i]%1000000007 #print("after ",totalv,carry) totalv[2*k]+=carry #print(u,totalv) return totalv if k==0: print(1) else: temp=getans(1,k) print(sum(temp[k:])%1000000007) ```
96,648
Provide tags and a correct Python 3 solution for this coding contest problem. Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them. As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one. Input The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k. Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree. Output Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7). Examples Input 2 0 1 2 Output 1 Input 2 1 1 2 Output 3 Input 4 1 1 2 2 3 3 4 Output 9 Input 7 2 1 2 2 3 1 4 4 5 1 6 6 7 Output 91 Note In the first sample, Ostap has to paint both vertices black. In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both. In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}. Tags: dp, trees Correct Solution: ``` n,m = map(int,input().split()) edge_to = [0 for i in range(n*2+1)] edge_next = [0 for i in range(n*2+1)] son = [0 for i in range(n+1)] def add_edge(u,v): global edge_next,edge_to,son,tot; tot += 1; edge_to[tot] = v; edge_next[tot] = son[u]; son[u] = tot; tot = 0; MOD = 10**9+7 dp = [[0]*(m*2+2) for i in range(n+1)] def dp_tree(x,fa): global edge_next,edge_to,m,son,dp; dp[x][0]= 1; dp[x][m+1] = 1; f = [0 for i in range(m*2+2)] i = son[x] while (i): f = [0 for w in range(m*2+2)] to = edge_to[i] i = edge_next[i] if (to == fa): continue; dp_tree(to,x) for j in range(2*m+2): for k in range(2*m+1): if (j+k <= 2*m): f[min(j,k+1)] += (dp[x][j]*dp[to][k])%MOD; f[min(j,k+1)] %= MOD; else : f[max(j,k+1)] += (dp[x][j]*dp[to][k])%MOD; f[max(j,k+1)] %= MOD; dp[x] = f for i in range(n-1): u,v = map(int,input().split()) add_edge(u,v); add_edge(v,u); dp_tree(1,-1) res = 0 for i in range(m+1): res += dp[1][i]; res %= MOD; print(res) ```
96,649
Provide tags and a correct Python 3 solution for this coding contest problem. Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them. As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one. Input The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k. Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree. Output Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7). Examples Input 2 0 1 2 Output 1 Input 2 1 1 2 Output 3 Input 4 1 1 2 2 3 3 4 Output 9 Input 7 2 1 2 2 3 1 4 4 5 1 6 6 7 Output 91 Note In the first sample, Ostap has to paint both vertices black. In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both. In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}. Tags: dp, trees Correct Solution: ``` import sys #sys.stdin=open("data.txt") #sys.stdout=open("data.txt","w") input=sys.stdin.readline n,k=map(int,input().split()) g=[[] for _ in range(n+1)] vis=[0]*(n+1) for _ in range(n-1): u,v=map(int,input().split()) g[u].append(v) g[v].append(u) def getans(u,k): vis[u]=1 # first k -> need k # then cover exact # last k -> covers k above totalv=[0]*(2*k+1) totalv[k-1]=1 carry=1 # total when this node is black for v in g[u]: if vis[v]: continue getv=getans(v,k) carry=(carry*sum(getv))%1000000007 out2=[0]*(2*k+1) #print("before",totalv) for i in range(1,2*k+1): for j in range(2*k+1): if j+i>=2*k: out2[max(i-1,j)]+=getv[i]*totalv[j] else: out2[min(i-1,j)]+=getv[i]*totalv[j] for i in range(2*k+1): totalv[i]=out2[i]%1000000007 #print("after ",totalv,carry) totalv[2*k]+=carry #print(u,totalv) return totalv if k==0: print(1) else: temp=getans(1,k) print(sum(temp[k:])%1000000007) # Made By Mostafa_Khaled ```
96,650
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them. As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one. Input The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k. Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree. Output Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7). Examples Input 2 0 1 2 Output 1 Input 2 1 1 2 Output 3 Input 4 1 1 2 2 3 3 4 Output 9 Input 7 2 1 2 2 3 1 4 4 5 1 6 6 7 Output 91 Note In the first sample, Ostap has to paint both vertices black. In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both. In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}. Submitted Solution: ``` def main(): n, k = map(int, input().split()) cnt = [[[0] * 21 for _ in (0, 1)] for _ in range(n + 1)] edges, mod = [[] for _ in range(n + 1)], 10000007 for _ in range(n - 1): u, v = map(int, input().split()) edges[u].append(v) edges[v].append(u) def dfs(u, f): cnt[u][0][0] = cnt[u][1][k] = 1 for v in edges[u]: if v != f: dfs(v, u) tmp0, tmp1 = [0] * 21, [0] * 21 for i in range(k + 1): for j in range(k + 1): if i != k: tmp0[j if i < j else i + 1] += cnt[u][0][j] * cnt[v][0][i] if i < j: tmp1[j] += cnt[u][1][j] * cnt[v][0][i] elif i != k: tmp0[i + 1] += cnt[u][1][j] * cnt[v][0][i] if i > j: tmp1[i - 1] += cnt[u][0][j] * cnt[v][1][i] else: tmp0[j] += cnt[u][0][j] * cnt[v][1][i] tmp1[max(i - 1, j)] += cnt[u][1][j] * cnt[v][1][i] for i in range(21): tmp0[i] %= mod tmp1[i] %= mod cnt[u][0] = tmp0 cnt[u][1] = tmp1 dfs(1, 1) print(sum(cnt[1][1][j] for j in range(k + 1)) % mod) if __name__ == '__main__': main() ``` No
96,651
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them. As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one. Input The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k. Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree. Output Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7). Examples Input 2 0 1 2 Output 1 Input 2 1 1 2 Output 3 Input 4 1 1 2 2 3 3 4 Output 9 Input 7 2 1 2 2 3 1 4 4 5 1 6 6 7 Output 91 Note In the first sample, Ostap has to paint both vertices black. In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both. In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}. Submitted Solution: ``` def main(): n, k = map(int, input().split()) cnt = [[[0] * 21 for _ in (0, 1)] for _ in range(n + 1)] edges, mod = [[] for _ in range(n + 1)], 100000007 for _ in range(n - 1): u, v = map(int, input().split()) edges[u].append(v) edges[v].append(u) def dfs(u, f): cnt[u][0][0] = cnt[u][1][k] = 1 for v in edges[u]: if v != f: dfs(v, u) tmp0, tmp1 = [0] * 21, [0] * 21 for i in range(k + 1): for j in range(k + 1): if i != k: tmp0[j if i < j else i + 1] += cnt[u][0][j] * cnt[v][0][i] if i < j: tmp1[j] += cnt[u][1][j] * cnt[v][0][i] elif i != k: tmp0[i + 1] += cnt[u][1][j] * cnt[v][0][i] if i > j: tmp1[i - 1] += cnt[u][0][j] * cnt[v][1][i] else: tmp0[j] += cnt[u][0][j] * cnt[v][1][i] tmp1[max(i - 1, j)] += cnt[u][1][j] * cnt[v][1][i] for i in range(21): tmp0[i] %= mod tmp1[i] %= mod cnt[u][0] = tmp0 cnt[u][1] = tmp1 dfs(1, 1) print(sum(cnt[1][1][j] for j in range(k + 1)) % mod) if __name__ == '__main__': main() ``` No
96,652
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them. As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one. Input The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k. Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree. Output Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7). Examples Input 2 0 1 2 Output 1 Input 2 1 1 2 Output 3 Input 4 1 1 2 2 3 3 4 Output 9 Input 7 2 1 2 2 3 1 4 4 5 1 6 6 7 Output 91 Note In the first sample, Ostap has to paint both vertices black. In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both. In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}. Submitted Solution: ``` import sys #sys.stdin=open("data.txt") #sys.stdout=open("data.txt","w") input=sys.stdin.readline n,k=map(int,input().split()) g=[[] for _ in range(n+1)] vis=[0]*(n+1) for _ in range(n-1): u,v=map(int,input().split()) g[u].append(v) g[v].append(u) def getans(u,k): vis[u]=1 # first k -> need k # then cover exact # last k -> covers k above totalv=[0]*(2*k+1) totalv[k-1]=1 carry=1 # total when this node is black for v in g[u]: if vis[v]: continue getv=getans(v,k) carry=(carry*sum(getv))%1000000007 out2=[0]*(2*k+1) #print("before",totalv) for i in range(1,2*k+1): for j in range(2*k+1): if j+i>=2*k-1: out2[max(i-1,j)]+=getv[i]*totalv[j] else: out2[min(i-1,j)]+=getv[i]*totalv[j] for i in range(2*k+1): totalv[i]=out2[i]%1000000007 #print("after ",totalv,carry) totalv[2*k]+=carry #print(u,totalv) return totalv if k==0: print(1) else: temp=getans(1,k) print(sum(temp[k:])%1000000007) ``` No
96,653
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Tags: constructive algorithms, dfs and similar Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) k=0 s=0 v=[False]*n for i in range (n): if v[i]: continue k+=1 while not v[i]: v[i]=True i=a[i]-1 if k==1: k=0 else: s+=k if sum(b)%2==0: s+=1 print(s) ```
96,654
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Tags: constructive algorithms, dfs and similar Correct Solution: ``` from collections import deque def read(type=int): return type(input()) def read_arr(type=int): return [type(token) for token in input().split()] def num_cycles(P): V = [False] * len(P) i = 0 for u in P: if not V[u]: while not V[u]: V[u] = True u = P[u] i += 1 return i def runB(): n = read() P = read_arr() P = [a-1 for a in P] B = read_arr() extra = (sum(B) + 1) % 2 cycles = num_cycles(P) ans = extra + (cycles if cycles > 1 else 0) print(ans) runB() ```
96,655
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Tags: constructive algorithms, dfs and similar Correct Solution: ``` n = int(input()) p = map(int, input().split()) p = [x - 1 for x in p] b = map(int, input().split()) s = int(sum(b)) c = 0 done = set() for i in range(n): if i not in done: c += 1 j = i while j not in done: done.add(j) j = p[j] modif = 0 if c > 1: modif = c res = modif + (s + 1) % 2 print(res) ```
96,656
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Tags: constructive algorithms, dfs and similar Correct Solution: ``` read = lambda: map(int, input().split()) n = int(input()) p = list(read()) b = list(read()) ans = (b.count(1) + 1) % 2 was = [0] * n cnt = 0 for i in range(n): if not was[i]: cnt += 1 v = i while not was[v]: was[v] = 1 v = p[v] - 1 if cnt > 1: ans += cnt print(ans) # Made By Mostafa_Khaled ```
96,657
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Tags: constructive algorithms, dfs and similar Correct Solution: ``` if __name__ == '__main__': n, = map(int, input().split()) p = list(map(lambda x: int(x)-1, input().split())) swaps = sum(map(int, input().split())) res = 1 - (swaps % 2) visited = [False for _ in range(n)] cycles = 0 for i in range(n): if visited[i]: continue visited[i] = True j = p[i] while j != i: visited[j] = True j = p[j] cycles += 1 if cycles > 1: res += cycles print(res) ```
96,658
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Tags: constructive algorithms, dfs and similar Correct Solution: ``` n = int(input()) p = list(map(int, input().split())) b = list(map(int, input().split())) visited = [False] * n loops = 0 for i in range(n): if visited[i]: continue loops += 1 while not visited[i]: visited[i] = True i = p[i] - 1 dp = 0 if loops == 1 else loops db = (sum(b) + 1) % 2 print(dp + db) ```
96,659
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Tags: constructive algorithms, dfs and similar Correct Solution: ``` n = int(input()) p = list(map(int, input().split())) b = list(map(int, input().split())) cnt_cyc = 0 vis = [0] * n for i in range(n): if vis[i] == 0: vis[i] = 1 nxt = p[i]-1 while vis[nxt] == 0: vis[nxt] = 1 nxt = p[nxt]-1 cnt_cyc += 1 res = (b.count(1)+1) % 2 print(res if cnt_cyc == 1 else res + cnt_cyc) ```
96,660
Provide tags and a correct Python 3 solution for this coding contest problem. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Tags: constructive algorithms, dfs and similar Correct Solution: ``` import sys def main(): n = int(sys.stdin.readline()) p = list(map(int,sys.stdin.readline().split())) b = list(map(int,sys.stdin.readline().split())) for i in range(n): p[i]=p[i]-1 cur = 0 res = 0 while cur < n: if p[cur] == -1: cur+=1 continue v = p[cur] cc = cur while v!=-1: p[cc] = -1 cc = v v = p[cc] res+=1 cur+=1 if res ==1: res = 0 if sum(b) %2 == 0: res+=1 print(res) main() ```
96,661
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` #!/usr/bin/env python3 from sys import stdin, stdout def rint(): return map(int, stdin.readline().split()) #lines = stdin.readlines() n = int(input()) p = list(rint()) b = list(rint()) p = [ i-1 for i in p] v = [0]*n loop = 0 for i in range(n): if v[i]: continue loop += 1 while v[i] == 0: v[i] = 1 i = p[i] ans = 0 if loop != 1: ans += loop if sum(b)%2 == 0: ans += 1 print(ans) ``` Yes
96,662
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` n = int(input()) perm = list(map(int, input().split())) zeros = list(map(int, input().split())) answer = 0 if sum(zeros) % 2 == 0: answer += 1 visited = [False] * n cycles = 0 for i in range(n): if not visited[i]: cycles += 1 j = i while not visited[j]: visited[j] = True j = perm[j] - 1 answer += 0 if cycles == 1 else cycles print(answer) ``` Yes
96,663
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` n = int(input()) p = list(map(int, input().split())) b = list(map(int, input().split())) count_cycles = 0 visited = [False] * n for i in range(n): if not visited[i]: visited[i] = True nxt = p[i]-1 while not visited[nxt]: visited[nxt] = True nxt = p[nxt]-1 count_cycles += 1 if count_cycles == 1: print((b.count(1)+1) % 2) else: print(count_cycles + (b.count(1)+1) % 2) ``` Yes
96,664
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` n = int(input()) p = list(map(int, input().split())) b = list(map(int, input().split())) s = sum(b[i] for i in range(n)) if s % 2 == 0: ans = 1 else: ans = 0 visited = [0] * n ptr = 0 start = 1 visited[0] = 1 q = 1 c = 1 while q < n: start = p[start - 1] if visited[start - 1] == 1: c += 1 while ptr < n and visited[ptr] == 1: ptr += 1 start = ptr + 1 else: visited[start - 1] = 1 q += 1 if c == 1: print(ans) else: print(ans + c) ``` Yes
96,665
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` class Graph: def __init__(self,n): self.nodes=n self.edge_mapping=[None]*(self.nodes+1) def add_edge(self,src,dest): if self.edge_mapping[src]==None: self.edge_mapping[src]=[dest] else: self.edge_mapping[src].append(dest) def dfs_inner(self,node,mark_list): mark_list[node]=1 ret=1 for n in self.edge_mapping[node]: if mark_list[n]==0: ret+=self.dfs_inner(n,mark_list) return ret def dfs(self,start_node): mark_list=[0]*(self.nodes+1) return self.dfs_inner(start_node,mark_list) def printg(self): for z in self.edge_mapping: print(z) if __name__ == '__main__': n=int(input()) g=Graph(n) p_list=list(map(int,input().split())) s_list=list(map(int,input().split())) for x in range(len(p_list)): g.add_edge(p_list[x],x+1) ans=0 trav=g.dfs(1) ans+=min(trav,n-trav) s=0 for x in s_list: s+=x print(ans) if s%2==0: print(ans+1) else: print(ans) ``` No
96,666
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` class Graph: def __init__(self,n): self.nodes=n self.edge_mapping=[None]*(self.nodes+1) def add_edge(self,src,dest): if self.edge_mapping[src]==None: self.edge_mapping[src]=[dest] else: self.edge_mapping[src].append(dest) def dfs_inner(self,node,mark_list): mark_list[node]=1 ret=1 for n in self.edge_mapping[node]: if mark_list[n]==0: ret+=self.dfs_inner(n,mark_list) return ret def dfs(self,start_node): mark_list=[0]*(self.nodes+1) return self.dfs_inner(start_node,mark_list) def printg(self): for z in self.edge_mapping: print(z) if __name__ == '__main__': n=int(input()) g=Graph(n) p_list=list(map(int,input().split())) s_list=list(map(int,input().split())) for x in range(len(p_list)): g.add_edge(p_list[x],x+1) ans=0 trav=g.dfs(1) if trav==1: ans+=n else: ans+=min(trav,n-trav) s=0 for x in s_list: s+=x # print(ans) if s%2==0: print(ans+1) else: print(ans) ``` No
96,667
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` n = int(input()) p = list(map(int, input().split())) b = list(map(int, input().split())) visited = [False] * n loops = 0 for i in range(n): if visited[i]: continue loops += 1 while not visited[i]: visited[i] = True i = p[i] - 1 dp = 0 if loops == 1 else loops db = int(not any(b)) print(dp + db) ``` No
96,668
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p and a sequence b1, b2, ..., bn, consisting of zeros and ones. Each second Pavel move skewer on position i to position pi, and if bi equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements. It can be shown that some suitable pair of permutation p and sequence b exists for any n. Input The first line contain the integer n (1 ≤ n ≤ 2·105) — the number of skewers. The second line contains a sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence b1, b2, ..., bn consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Output Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements. Examples Input 4 4 3 2 1 0 1 1 1 Output 2 Input 3 2 3 1 0 0 0 Output 1 Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of b to 1. Submitted Solution: ``` import sys def check(b, n, v): for i in range(n): if b[i] !=v: return False return True def main(): n = int(sys.stdin.readline()) p = list(map(int,sys.stdin.readline().split())) b = list(map(int,sys.stdin.readline().split())) for i in range(n): p[i]=p[i]-1 cur = 0 res = 0 while cur < n: if p[cur] == -1: cur+=1 continue v = p[cur] cc = cur while v!=-1: p[cc] = -1 cc = v v = p[cc] res+=1 cur+=1 if res ==1: res = 0 if check(b,n,0): res+=1 if check(b,n,1): res+=1 print(res) main() ``` No
96,669
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem. We have a forest (acyclic undirected graph) with n vertices and m edges. There are q queries we should answer. In each query two vertices v and u are given. Let V be the set of vertices in the connected component of the graph that contains v, and U be the set of vertices in the connected component of the graph that contains u. Let's add an edge between some vertex <image> and some vertex in <image> and compute the value d of the resulting component. If the resulting component is a tree, the value d is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of d, if we choose vertices a and b from the sets uniformly at random? Can you help Pasha to solve this problem? The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices. Note that queries don't add edges to the initial forest. Input The first line contains three integers n, m and q(1 ≤ n, m, q ≤ 105) — the number of vertices, the number of edges in the graph and the number of queries. Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n), that means there is an edge between vertices ui and vi. It is guaranteed that the given graph is a forest. Each of the next q lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the vertices given in the i-th query. Output For each query print the expected value of d as described in the problem statement. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Let's assume that your answer is a, and the jury's answer is b. The checker program will consider your answer correct, if <image>. Examples Input 3 1 2 1 3 3 1 2 3 Output -1 2.0000000000 Input 5 2 3 2 4 4 3 4 2 4 1 2 5 Output -1 2.6666666667 2.6666666667 Note In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2. In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1 - 2 or 1 - 3 the diameter is 3, and for added edge 1 - 4 the diameter is 2. Thus, the answer is <image>. Submitted Solution: ``` l,r,p,m=0,0,1,10**9+7 for i in input(): if i=='a': l += 1 p = p*2%m else: r = (r + p - 1)%m print(r) ``` No
96,670
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem. We have a forest (acyclic undirected graph) with n vertices and m edges. There are q queries we should answer. In each query two vertices v and u are given. Let V be the set of vertices in the connected component of the graph that contains v, and U be the set of vertices in the connected component of the graph that contains u. Let's add an edge between some vertex <image> and some vertex in <image> and compute the value d of the resulting component. If the resulting component is a tree, the value d is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of d, if we choose vertices a and b from the sets uniformly at random? Can you help Pasha to solve this problem? The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices. Note that queries don't add edges to the initial forest. Input The first line contains three integers n, m and q(1 ≤ n, m, q ≤ 105) — the number of vertices, the number of edges in the graph and the number of queries. Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n), that means there is an edge between vertices ui and vi. It is guaranteed that the given graph is a forest. Each of the next q lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the vertices given in the i-th query. Output For each query print the expected value of d as described in the problem statement. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Let's assume that your answer is a, and the jury's answer is b. The checker program will consider your answer correct, if <image>. Examples Input 3 1 2 1 3 3 1 2 3 Output -1 2.0000000000 Input 5 2 3 2 4 4 3 4 2 4 1 2 5 Output -1 2.6666666667 2.6666666667 Note In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2. In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1 - 2 or 1 - 3 the diameter is 3, and for added edge 1 - 4 the diameter is 2. Thus, the answer is <image>. Submitted Solution: ``` from math import inf n, m, q = [int(x) for x in input().split(' ')] distance = [[inf for _ in range(n)] for _ in range(n)] connected = [[0 for _ in range(n)] for _ in range(n)] in_question = [[-1 for _ in range(n)] for _ in range(n)] answers = [] max_distance = -1 num_max_distance = 0 for i in range(m): a,b = [int(x)-1 for x in input().split(' ')] distance[a][b] = 1 distance[b][a] = 1 connected[a][b] = 1 connected[b][a] = 1 for i in range(n): distance[a][i] = min(distance[a][i], distance[b][i] + 1) distance[i][b] = min(distance[i][b], distance[i][a] + 1) distance[i][a] = min(distance[i][a], distance[i][b] + 1) distance[b][i] = min(distance[b][i], distance[a][i] + 1) distance[a][a] = 0 distance[b][b] = 0 for i in range(n): distance[i][i] = 0 def copy_mat(X): a = [[c for c in row] for row in X] return a for i in range(q): a,b = [int(x)-1 for x in input().split(' ')] if distance[a][b] < inf or distance[b][a] < inf: answers.append(-1) else: distances = [] for i in range(n): if distance[a][i] < inf: for j in range(n): if distance[b][j] < inf: new_dist = sum([ max([d for d in distance[i] if d < inf]), max([d for d in distance[j] if d < inf]), 1 ]) distances.append(new_dist) answers.append(sum(distances)/len(distances)) for a in answers: print(a) ``` No
96,671
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem. We have a forest (acyclic undirected graph) with n vertices and m edges. There are q queries we should answer. In each query two vertices v and u are given. Let V be the set of vertices in the connected component of the graph that contains v, and U be the set of vertices in the connected component of the graph that contains u. Let's add an edge between some vertex <image> and some vertex in <image> and compute the value d of the resulting component. If the resulting component is a tree, the value d is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of d, if we choose vertices a and b from the sets uniformly at random? Can you help Pasha to solve this problem? The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices. Note that queries don't add edges to the initial forest. Input The first line contains three integers n, m and q(1 ≤ n, m, q ≤ 105) — the number of vertices, the number of edges in the graph and the number of queries. Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n), that means there is an edge between vertices ui and vi. It is guaranteed that the given graph is a forest. Each of the next q lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the vertices given in the i-th query. Output For each query print the expected value of d as described in the problem statement. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Let's assume that your answer is a, and the jury's answer is b. The checker program will consider your answer correct, if <image>. Examples Input 3 1 2 1 3 3 1 2 3 Output -1 2.0000000000 Input 5 2 3 2 4 4 3 4 2 4 1 2 5 Output -1 2.6666666667 2.6666666667 Note In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2. In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1 - 2 or 1 - 3 the diameter is 3, and for added edge 1 - 4 the diameter is 2. Thus, the answer is <image>. Submitted Solution: ``` from math import inf n, m, q = [int(x) for x in input().split(' ')] distance = [[inf for _ in range(n)] for _ in range(n)] in_question = [[-1 for _ in range(n)] for _ in range(n)] answers = [] max_distance = -1 num_max_distance = 0 for i in range(m): a,b = [int(x)-1 for x in input().split(' ')] distance[a][b] = 1 distance[b][a] = 1 for i in range(n): distance[a][i] = min(distance[a][i], distance[b][i] + 1) distance[i][b] = min(distance[i][b], distance[i][a] + 1) distance[i][a] = min(distance[i][a], distance[i][b] + 1) distance[b][i] = min(distance[b][i], distance[a][i] + 1) distance[a][a] = inf distance[b][b] = inf for i in range(n): for j in range(n): if distance[i][j] < inf: if distance[i][j] > max_distance: max_distance = distance[i][j] num_max_distance = 0 for i in range(n): for j in range(n): if distance[i][j] < inf: if distance[i][j] == max_distance: num_max_distance += 1 break accessible_nodes = 0 for i in range(n): if any([distance[i][j] < inf for j in range(n)]): accessible_nodes += 1 for i in range(q): a,b = [int(x)-1 for x in input().split(' ')] in_question[a][b] = 1 if distance[a][b] < inf or distance[b][a] < inf: answers.append(-1) else: answers.append( (num_max_distance*(max_distance+1) + (accessible_nodes-num_max_distance)*max_distance)/accessible_nodes ) for a in answers: print(a) # for row in distance: # print(', '.join([str(x).zfill(3) for x in row])) ``` No
96,672
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem. We have a forest (acyclic undirected graph) with n vertices and m edges. There are q queries we should answer. In each query two vertices v and u are given. Let V be the set of vertices in the connected component of the graph that contains v, and U be the set of vertices in the connected component of the graph that contains u. Let's add an edge between some vertex <image> and some vertex in <image> and compute the value d of the resulting component. If the resulting component is a tree, the value d is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of d, if we choose vertices a and b from the sets uniformly at random? Can you help Pasha to solve this problem? The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices. Note that queries don't add edges to the initial forest. Input The first line contains three integers n, m and q(1 ≤ n, m, q ≤ 105) — the number of vertices, the number of edges in the graph and the number of queries. Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n), that means there is an edge between vertices ui and vi. It is guaranteed that the given graph is a forest. Each of the next q lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the vertices given in the i-th query. Output For each query print the expected value of d as described in the problem statement. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Let's assume that your answer is a, and the jury's answer is b. The checker program will consider your answer correct, if <image>. Examples Input 3 1 2 1 3 3 1 2 3 Output -1 2.0000000000 Input 5 2 3 2 4 4 3 4 2 4 1 2 5 Output -1 2.6666666667 2.6666666667 Note In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2. In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1 - 2 or 1 - 3 the diameter is 3, and for added edge 1 - 4 the diameter is 2. Thus, the answer is <image>. Submitted Solution: ``` from math import inf n, m, q = [int(x) for x in input().split(' ')] distance = [[inf for _ in range(n)] for _ in range(n)] connected = [[0 for _ in range(n)] for _ in range(n)] in_question = [[-1 for _ in range(n)] for _ in range(n)] answers = [] max_distance = -1 num_max_distance = 0 for i in range(m): a,b = [int(x)-1 for x in input().split(' ')] distance[a][b] = 1 distance[b][a] = 1 connected[a][b] = 1 connected[b][a] = 1 for i in range(n): distance[a][i] = min(distance[a][i], distance[b][i] + 1) distance[i][b] = min(distance[i][b], distance[i][a] + 1) distance[i][a] = min(distance[i][a], distance[i][b] + 1) distance[b][i] = min(distance[b][i], distance[a][i] + 1) distance[a][a] = inf distance[b][b] = inf for i in range(n): for j in range(n): if distance[i][j] < inf: if distance[i][j] > max_distance: max_distance = distance[i][j] num_max_distance = 0 for i in range(n): for j in range(n): if distance[i][j] < inf: if distance[i][j] == max_distance: num_max_distance += 1 break accessible_nodes = 0 for i in range(n): if any([distance[i][j] < inf for j in range(n)]): accessible_nodes += 1 for i in range(q): a,b = [int(x)-1 for x in input().split(' ')] in_question[a][b] = 1 if distance[a][b] < inf or distance[b][a] < inf: answers.append(-1) else: answers.append( (num_max_distance*(max_distance+1) + (accessible_nodes-num_max_distance)*max_distance)/accessible_nodes ) for d in connected: print(','.join(['1' if e == 1 else '0' for e in d])) for a in answers: print(a) # for row in distance: # print(', '.join([str(x).zfill(3) for x in row])) ``` No
96,673
Provide tags and a correct Python 3 solution for this coding contest problem. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Tags: brute force, math Correct Solution: ``` n = int(input()) def digit_sum(n): dgsum = 0 while n > 0: dgsum += n%10 n = n//10 return dgsum xs = [] for sd in range(1, 100): if sd > n: continue if digit_sum(n-sd) == sd: xs.append(n-sd) if len(xs) == 0: print(0) else: print(len(xs)) xs.sort() print("\n".join(map(str, xs))) ```
96,674
Provide tags and a correct Python 3 solution for this coding contest problem. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Tags: brute force, math Correct Solution: ``` def sum(x): l=str(x) i=0 for j in range(len(l)): i=i+int(l[j]) return i x=int(input('')) q=10*(len(str(x))) q=max(x-q,0) l=[] for i in range(q,x): if ((i+sum(i))==x): l.append(i) print(len(l)) for i in range(len(l)): print(l[i]) ```
96,675
Provide tags and a correct Python 3 solution for this coding contest problem. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Tags: brute force, math Correct Solution: ``` n = int(input()) a = [] for i in range(n, n-100, -1): temp = i ans = temp while(temp>0): ans += temp%10 temp = temp//10 if ans == n: a.insert(0,i) print(len(a)) for x in a: print(x) ```
96,676
Provide tags and a correct Python 3 solution for this coding contest problem. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Tags: brute force, math Correct Solution: ``` n=int(input()) output=[] for i in range(max(n-100,0),n): listi=list(map(int,str(i))) if(i+sum(listi)==n): output.append(i) print(len(output)) for i in range(len(output)): print(output[i]) ```
96,677
Provide tags and a correct Python 3 solution for this coding contest problem. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Tags: brute force, math Correct Solution: ``` n=int(input()) result,res=0,[] for i in range(1,min(90,n)): x,summa=n-i,0 for j,y in enumerate(str(x)): summa+=int(y) if x+summa==n:result+=1;res.append(x) print(result) res.reverse() for i,x in enumerate(res):print(x) ```
96,678
Provide tags and a correct Python 3 solution for this coding contest problem. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Tags: brute force, math Correct Solution: ``` def som(x): ans = 0 while x: ans += x%10 x = x//10 return ans x = int(input().strip()) n = x-1 ans = [] while n > max(0,x-101): if som(n)+n == x: ans.append(n) n = n-1 print(len(ans)) print('\n'.join(list(map(str, sorted(ans))))) ```
96,679
Provide tags and a correct Python 3 solution for this coding contest problem. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Tags: brute force, math Correct Solution: ``` n=int(input()) x=0 L=[] if n>100: for i in range(n-81,n): s=i%10 for j in range(1,10): s+=((i//(10**j))%10) if i+s==n: x+=1 L.append(i) elif n<101: for i in range(1,n): s=i%10+i//10 if i+s==n: x+=1 L.append(i) if L==[]: print(0) else: print(x) for i in L: print(i) ```
96,680
Provide tags and a correct Python 3 solution for this coding contest problem. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Tags: brute force, math Correct Solution: ``` class CodeforcesTask875ASolution: def __init__(self): self.result = '' self.n = 0 def read_input(self): self.n = int(input()) def process_task(self): sols = [] for x in range(100): res = self.n - x if res > 0: well = res + sum([int(j) for j in str(res)]) if well == self.n: sols.append(res) sols.sort() self.result = "{0}\n{1}".format(len(sols), "\n".join([str(x) for x in sols])) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask875ASolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ```
96,681
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Submitted Solution: ``` from sys import stdin, stdout n = int(stdin.readline()) ans = [] for i in range(max(1, n - 1000), n + 1000): if sum(map(int, list(str(i)))) + i == n: ans.append(i) stdout.write(str(len(ans)) + '\n') ans.sort() stdout.write('\n'.join(list(map(str, ans)))) ``` Yes
96,682
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Submitted Solution: ``` #lst = list(map(int, input().strip().split(' '))) #n,k = map(int, input().strip().split(' ')) n=int(input()) s=str(n) l=len(s) if n<11: if n%2==0: print(1) print(n//2) else: print(0) elif n<18 and n%2==0: print(1) print(n//2) elif n==11: print(1) print(10) elif n==13: print(1) print(11) elif n==15: print(1) print(12) elif n==17: print(1) print(13) else: x=n-l*9 i=x f=0 c=0 lst=[] s1=0 while(i<n): s1=0 sx=str(i) for j in range(len(sx)): s1+=int(sx[j]) if s1+i==n: f=1 c+=1 lst.append(i) i+=1 if f==0: print(0) else: print(c) for j in range(c): print(lst[j],end=" ") ``` Yes
96,683
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Submitted Solution: ``` def main(): n = int(input()) solver(n) def solver(n): k = 0 nums = [] for x in range(n - 9 * digitCount(n) - 9, n): if x + sumOfDigits(x) == n: k += 1 nums.append(x) print(k) for x in nums: print(x) def digitCount(n): count = 0 while n > 0: n //= 10 count += 1 return count def sumOfDigits(n): total = 0 while n > 0: total += n % 10 n //= 10 return total #solver(21) main() ``` Yes
96,684
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Submitted Solution: ``` def sumDigits(x) : return sum(list(map(int, str(x)))) n = int(input()) counter = 0 theNumbers = [] digits = len(str(n)) for i in range (max( n - digits *9, 0), n) : if i+sumDigits(i)==n: theNumbers.append(i) counter += 1 print (counter) for j in range(len(theNumbers)): print(theNumbers[j]) ``` Yes
96,685
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Submitted Solution: ``` ''' http://codeforces.com/problemset/problem/875/A Работает, но долго на значении 100000001 ''' value = int(input()) def display(lst): for item in sorted(lst): print(item) def sum_a_string(integer): # print(integer) return sum([int(d) for d in str(integer)]) def make_all_nine(lenght): return '9' * lenght def d_max(value): svalue = str(value) value_len = len(svalue) first = svalue[0] if value_len == 1: return value if first == '1': return sum_a_string(int(make_all_nine(value_len - 1))) nines_count = value_len - 1 first = str(int(first) - 1) return sum_a_string(int(first + make_all_nine(nines_count))) def itter(value): count = 0 possible_digits = [] if value == 1: print(count) display(possible_digits) return for num in range(value - d_max(value), value - 1): result = num + sum_a_string(num) if result == value: count += 1 possible_digits.append(num) print(count) display(possible_digits) itter(value) ``` No
96,686
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Submitted Solution: ``` n = int(input()) ans = [] for s in range(1, min(n + 1, 99)): x = n - s sm = sum(int(i) for i in str(x)) if sm == s: ans.append(x) print(len(ans)) for i in ans: print(i, end=' ') ``` No
96,687
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Submitted Solution: ``` n = int(input()) a = 1 while(a<n): j = 0 s = str(a) c = a while j<len(s): c = c + int(s[j]) j = j + 1 if(c==n): break a = a + 1 print(a) ``` No
96,688
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system. Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova. Input The first line contains integer n (1 ≤ n ≤ 109). Output In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order. Examples Input 21 Output 1 15 Input 20 Output 0 Note In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21. In the second test case there are no such x. Submitted Solution: ``` def sumOfdig(x): ans = 0 while x: ans += x%10 x //= 10 return ans n = int(input().strip()) x = n-1 ans = [] while x > max(0, n-101): if sumOfdig(x) + x == n: ans.append(str(x)) x -= 1 print(len(ans)) print('\n'.join(ans)) ``` No
96,689
Provide tags and a correct Python 3 solution for this coding contest problem. Petya has a string of length n consisting of small and large English letters and digits. He performs m operations. Each operation is described with two integers l and r and a character c: Petya removes from the string all characters c on positions between l and r, inclusive. It's obvious that the length of the string remains the same or decreases after each operation. Find how the string will look like after Petya performs all m operations. Input The first string contains two integers n and m (1 ≤ n, m ≤ 2·105) — the length of the string and the number of operations. The second line contains the string of length n, consisting of small and large English letters and digits. Positions in the string are enumerated from 1. Each of the next m lines contains two integers l and r (1 ≤ l ≤ r), followed by a character c, which is a small or large English letter or a digit. This line describes one operation. It is guaranteed that r doesn't exceed the length of the string s before current operation. Output Print the string Petya will obtain after performing all m operations. If the strings becomes empty after all operations, print an empty line. Examples Input 4 2 abac 1 3 a 2 2 c Output b Input 3 2 A0z 1 3 0 1 1 z Output Az Input 10 4 agtFrgF4aF 2 5 g 4 9 F 1 5 4 1 7 a Output tFrg4 Input 9 5 aAAaBBccD 1 4 a 5 6 c 2 3 B 4 4 D 2 3 A Output AB Note In the first example during the first operation both letters 'a' are removed, so the string becomes "bc". During the second operation the letter 'c' (on the second position) is removed, and the string becomes "b". In the second example during the first operation Petya removes '0' from the second position. After that the string becomes "Az". During the second operations the string doesn't change. Tags: data structures, strings Correct Solution: ``` #!/usr/bin/env python import os import sys from io import BytesIO, IOBase class FenwickTree: def __init__(self, x): """transform list into BIT""" self.bit = x for i in range(len(x)): j = i | (i + 1) if j < len(x): x[j] += x[i] def update(self, idx, x): """updates bit[idx] += x""" while idx < len(self.bit): self.bit[idx] += x idx |= idx + 1 def query(self, end): """calc sum(bit[:end])""" x = 0 while end: x += self.bit[end - 1] end &= end - 1 return x def findkth(self, k): """Find largest idx such that sum(bit[:idx]) <= k""" idx = -1 for d in reversed(range(len(self.bit).bit_length())): right_idx = idx + (1 << d) if right_idx < len(self.bit) and k >= self.bit[right_idx]: idx = right_idx k -= self.bit[idx] return idx + 1 def lb(a,x): l,r=-1,len(a) while(r-l>1): m=(l+r)//2 if(a[m]<x): l=m else: r=m return r def main(): n,m=map(int,input().split()) # print (n,m) s=input() a = [ [] for _ in range(256)] b = FenwickTree([1]*len(s)) for i,x in enumerate(s): a[ord(x)].append(i+1) # print(s) # print(a) for _ in range(m): l,r,c=input().split() l,r=map(int,[l,r]) # print("k",l,b.findkth(l-1)) l,r=b.findkth(l),b.findkth(r) # print(l,r,c) c=ord(c) l=lb(a[c],l) r=lb(a[c],r+1) for j in range(l,r): # print("rm",a[c][j]) b.update(a[c][j],-1) n-=1 a[c] = a[c][:l]+a[c][r:] b = [ x for x in map(b.findkth,range(1,n+1))] # print(b) print("".join(s[x-1] for x in b)) # 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() ```
96,690
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya has a string of length n consisting of small and large English letters and digits. He performs m operations. Each operation is described with two integers l and r and a character c: Petya removes from the string all characters c on positions between l and r, inclusive. It's obvious that the length of the string remains the same or decreases after each operation. Find how the string will look like after Petya performs all m operations. Input The first string contains two integers n and m (1 ≤ n, m ≤ 2·105) — the length of the string and the number of operations. The second line contains the string of length n, consisting of small and large English letters and digits. Positions in the string are enumerated from 1. Each of the next m lines contains two integers l and r (1 ≤ l ≤ r), followed by a character c, which is a small or large English letter or a digit. This line describes one operation. It is guaranteed that r doesn't exceed the length of the string s before current operation. Output Print the string Petya will obtain after performing all m operations. If the strings becomes empty after all operations, print an empty line. Examples Input 4 2 abac 1 3 a 2 2 c Output b Input 3 2 A0z 1 3 0 1 1 z Output Az Input 10 4 agtFrgF4aF 2 5 g 4 9 F 1 5 4 1 7 a Output tFrg4 Input 9 5 aAAaBBccD 1 4 a 5 6 c 2 3 B 4 4 D 2 3 A Output AB Note In the first example during the first operation both letters 'a' are removed, so the string becomes "bc". During the second operation the letter 'c' (on the second position) is removed, and the string becomes "b". In the second example during the first operation Petya removes '0' from the second position. After that the string becomes "Az". During the second operations the string doesn't change. Submitted Solution: ``` nm=input().split() lnm=list(map(int,nm)) n=lnm[0] m=lnm[1] strin = list(input()) for i in range(m): fn = input().split() l=int(fn[0])-1 r=int(fn[1]) c=fn[2] copy=strin[l:r] while c in copy: copy.remove(c) if r < len(strin) and l >0: strin=strin[:l]+copy+strin[l+len(copy)+1:] elif r==len(strin) and l>0: strin=strin[:l]+copy elif r<len(strin) and l == 0: strin = copy +strin[l+len(copy)+1:] else: strin=copy print(''.join(strin)) ``` No
96,691
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya has a string of length n consisting of small and large English letters and digits. He performs m operations. Each operation is described with two integers l and r and a character c: Petya removes from the string all characters c on positions between l and r, inclusive. It's obvious that the length of the string remains the same or decreases after each operation. Find how the string will look like after Petya performs all m operations. Input The first string contains two integers n and m (1 ≤ n, m ≤ 2·105) — the length of the string and the number of operations. The second line contains the string of length n, consisting of small and large English letters and digits. Positions in the string are enumerated from 1. Each of the next m lines contains two integers l and r (1 ≤ l ≤ r), followed by a character c, which is a small or large English letter or a digit. This line describes one operation. It is guaranteed that r doesn't exceed the length of the string s before current operation. Output Print the string Petya will obtain after performing all m operations. If the strings becomes empty after all operations, print an empty line. Examples Input 4 2 abac 1 3 a 2 2 c Output b Input 3 2 A0z 1 3 0 1 1 z Output Az Input 10 4 agtFrgF4aF 2 5 g 4 9 F 1 5 4 1 7 a Output tFrg4 Input 9 5 aAAaBBccD 1 4 a 5 6 c 2 3 B 4 4 D 2 3 A Output AB Note In the first example during the first operation both letters 'a' are removed, so the string becomes "bc". During the second operation the letter 'c' (on the second position) is removed, and the string becomes "b". In the second example during the first operation Petya removes '0' from the second position. After that the string becomes "Az". During the second operations the string doesn't change. Submitted Solution: ``` def pro(s, oper): k = oper.split() x = int(k[0]) y = int(k[1]) ss = k[2] s2 = list(s) if x == 1: if y == n: while (ss in s2)==True: s2.remove(ss) return ''.join(s2) else: s21 = s2[:y] s22 = s2[y:] while (ss in s21)==True: s21.remove(ss) return ''.join(s21+s22) else: if y == n: s21 = s2[x-1:] s22 = s2[:x-1] while (ss in s21)==True: s21.remove(ss) return ''.join(s21+s22) else: s21 = s2[x-1:y] s22 = s2[:x-1] s23 = s2[y:] while (ss in s21)==True: s21.remove(ss) return ''.join(s22+s21+s23) s = input() k = list(map(int, s.split())) n = k[0] m = k[1] origin = input() list1 = [] for i in range(0, m): list1.append(input()) for i in list1: origin = pro(origin, i) if len(origin) == 0: print() else: print(origin) ``` No
96,692
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya has a string of length n consisting of small and large English letters and digits. He performs m operations. Each operation is described with two integers l and r and a character c: Petya removes from the string all characters c on positions between l and r, inclusive. It's obvious that the length of the string remains the same or decreases after each operation. Find how the string will look like after Petya performs all m operations. Input The first string contains two integers n and m (1 ≤ n, m ≤ 2·105) — the length of the string and the number of operations. The second line contains the string of length n, consisting of small and large English letters and digits. Positions in the string are enumerated from 1. Each of the next m lines contains two integers l and r (1 ≤ l ≤ r), followed by a character c, which is a small or large English letter or a digit. This line describes one operation. It is guaranteed that r doesn't exceed the length of the string s before current operation. Output Print the string Petya will obtain after performing all m operations. If the strings becomes empty after all operations, print an empty line. Examples Input 4 2 abac 1 3 a 2 2 c Output b Input 3 2 A0z 1 3 0 1 1 z Output Az Input 10 4 agtFrgF4aF 2 5 g 4 9 F 1 5 4 1 7 a Output tFrg4 Input 9 5 aAAaBBccD 1 4 a 5 6 c 2 3 B 4 4 D 2 3 A Output AB Note In the first example during the first operation both letters 'a' are removed, so the string becomes "bc". During the second operation the letter 'c' (on the second position) is removed, and the string becomes "b". In the second example during the first operation Petya removes '0' from the second position. After that the string becomes "Az". During the second operations the string doesn't change. Submitted Solution: ``` nm=input().split() lnm=list(map(int,nm)) n=lnm[0] m=lnm[1] strin = list(input()) for i in range(m): fn = input().split() l=int(fn[0])-1 r=int(fn[1]) c=fn[2] while c in strin[l:r]: strin.remove(c) print(''.join(strin)) ``` No
96,693
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya has a string of length n consisting of small and large English letters and digits. He performs m operations. Each operation is described with two integers l and r and a character c: Petya removes from the string all characters c on positions between l and r, inclusive. It's obvious that the length of the string remains the same or decreases after each operation. Find how the string will look like after Petya performs all m operations. Input The first string contains two integers n and m (1 ≤ n, m ≤ 2·105) — the length of the string and the number of operations. The second line contains the string of length n, consisting of small and large English letters and digits. Positions in the string are enumerated from 1. Each of the next m lines contains two integers l and r (1 ≤ l ≤ r), followed by a character c, which is a small or large English letter or a digit. This line describes one operation. It is guaranteed that r doesn't exceed the length of the string s before current operation. Output Print the string Petya will obtain after performing all m operations. If the strings becomes empty after all operations, print an empty line. Examples Input 4 2 abac 1 3 a 2 2 c Output b Input 3 2 A0z 1 3 0 1 1 z Output Az Input 10 4 agtFrgF4aF 2 5 g 4 9 F 1 5 4 1 7 a Output tFrg4 Input 9 5 aAAaBBccD 1 4 a 5 6 c 2 3 B 4 4 D 2 3 A Output AB Note In the first example during the first operation both letters 'a' are removed, so the string becomes "bc". During the second operation the letter 'c' (on the second position) is removed, and the string becomes "b". In the second example during the first operation Petya removes '0' from the second position. After that the string becomes "Az". During the second operations the string doesn't change. Submitted Solution: ``` stline = input() stline = stline.split() stline = list(map(int, stline)) seclinestring = input() newstring = "" lstofm = [] n = stline[0] # length of the string m = stline[1] # number of the operations for i in range(0, m): lstofm.append(input()) for row in range(0, m): l = int(lstofm[row][0])-1 r = int(lstofm[row][2]) a = lstofm[row][4] newstring = seclinestring[:l] substring = seclinestring[l:r] for index in range(0,len(substring)): if substring[index] != a: newstring = newstring + substring[index] newstring = newstring + seclinestring[r:] seclinestring = newstring print(newstring) ``` No
96,694
Provide tags and a correct Python 3 solution for this coding contest problem. Recently n students from city S moved to city P to attend a programming camp. They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea. i-th student comes to the end of the queue at the beginning of li-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of ri-th second student i still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea. For each student determine the second he will use the teapot and get his tea (if he actually gets it). Input The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 1000). Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the number of students. Then n lines follow. Each line contains two integer li, ri (1 ≤ li ≤ ri ≤ 5000) — the second i-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea. It is guaranteed that for every <image> condition li - 1 ≤ li holds. The sum of n over all test cases doesn't exceed 1000. Note that in hacks you have to set t = 1. Output For each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea. Example Input 2 2 1 3 1 4 3 1 5 1 1 2 3 Output 1 2 1 0 2 Note The example contains 2 tests: 1. During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second. 2. During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea. Tags: implementation Correct Solution: ``` for _ in range(int(input())): n=int(input()) queue=[] for i in range(n): l,r=map(int,input().split()) queue.append([l,r]) ans=[] curr=0 for i in range(n): if curr<=queue[i][1]: ans.append(max(queue[i][0],curr)) curr=max(queue[i][0],curr)+1 else: ans.append(0) print(*ans) ```
96,695
Provide tags and a correct Python 3 solution for this coding contest problem. Recently n students from city S moved to city P to attend a programming camp. They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea. i-th student comes to the end of the queue at the beginning of li-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of ri-th second student i still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea. For each student determine the second he will use the teapot and get his tea (if he actually gets it). Input The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 1000). Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the number of students. Then n lines follow. Each line contains two integer li, ri (1 ≤ li ≤ ri ≤ 5000) — the second i-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea. It is guaranteed that for every <image> condition li - 1 ≤ li holds. The sum of n over all test cases doesn't exceed 1000. Note that in hacks you have to set t = 1. Output For each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea. Example Input 2 2 1 3 1 4 3 1 5 1 1 2 3 Output 1 2 1 0 2 Note The example contains 2 tests: 1. During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second. 2. During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea. Tags: implementation Correct Solution: ``` a=int(input()) x=[] for i in range(a): n=int(input()) z=[] y=[] for j in range(n): l,k=map(int,input().split()) y=[l,k] z.append(y) x.append(z) v=[] for i in range(a): t=0 b=x[i] w=[] for j in b: if t<=j[0]: t=j[0]+1 w.append(j[0]) elif j[0]<t<=j[1]: w.append(t) t=t+1 else: w.append(0) v.append(w) for i in v: e="" for j in i: d=str(j) e=e+d+" " print(e) ```
96,696
Provide tags and a correct Python 3 solution for this coding contest problem. Recently n students from city S moved to city P to attend a programming camp. They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea. i-th student comes to the end of the queue at the beginning of li-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of ri-th second student i still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea. For each student determine the second he will use the teapot and get his tea (if he actually gets it). Input The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 1000). Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the number of students. Then n lines follow. Each line contains two integer li, ri (1 ≤ li ≤ ri ≤ 5000) — the second i-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea. It is guaranteed that for every <image> condition li - 1 ≤ li holds. The sum of n over all test cases doesn't exceed 1000. Note that in hacks you have to set t = 1. Output For each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea. Example Input 2 2 1 3 1 4 3 1 5 1 1 2 3 Output 1 2 1 0 2 Note The example contains 2 tests: 1. During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second. 2. During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea. Tags: implementation Correct Solution: ``` t = int(input()) for _ in range(t): n = int(input()) last = 0 ans = [] for i in range(n): start, end = map(int,input().split()) if last < end: last = max(last+1,start) ans.append(last) else: ans.append(0) print(*ans) ```
96,697
Provide tags and a correct Python 3 solution for this coding contest problem. Recently n students from city S moved to city P to attend a programming camp. They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea. i-th student comes to the end of the queue at the beginning of li-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of ri-th second student i still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea. For each student determine the second he will use the teapot and get his tea (if he actually gets it). Input The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 1000). Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the number of students. Then n lines follow. Each line contains two integer li, ri (1 ≤ li ≤ ri ≤ 5000) — the second i-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea. It is guaranteed that for every <image> condition li - 1 ≤ li holds. The sum of n over all test cases doesn't exceed 1000. Note that in hacks you have to set t = 1. Output For each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea. Example Input 2 2 1 3 1 4 3 1 5 1 1 2 3 Output 1 2 1 0 2 Note The example contains 2 tests: 1. During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second. 2. During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea. Tags: implementation Correct Solution: ``` t = int(input()) for i in range(t): n = int(input()) a, ans = [], [] for j in range(n): x = list(map(int, input().split())) a.append(x) #print(a) cur_sec = a[0][0] for p in range(n): if a[p][0] <= cur_sec <= a[p][1]: ans.append(cur_sec) cur_sec += 1 elif a[p][0] >= cur_sec: ans.append(a[p][0]) cur_sec = a[p][0] + 1 else: ans.append(0) #cur_sec += 1 print(*ans) ```
96,698
Provide tags and a correct Python 3 solution for this coding contest problem. Recently n students from city S moved to city P to attend a programming camp. They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea. i-th student comes to the end of the queue at the beginning of li-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of ri-th second student i still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea. For each student determine the second he will use the teapot and get his tea (if he actually gets it). Input The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 1000). Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the number of students. Then n lines follow. Each line contains two integer li, ri (1 ≤ li ≤ ri ≤ 5000) — the second i-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea. It is guaranteed that for every <image> condition li - 1 ≤ li holds. The sum of n over all test cases doesn't exceed 1000. Note that in hacks you have to set t = 1. Output For each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea. Example Input 2 2 1 3 1 4 3 1 5 1 1 2 3 Output 1 2 1 0 2 Note The example contains 2 tests: 1. During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second. 2. During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea. Tags: implementation Correct Solution: ``` for _ in range(int(input())): n = int(input()) ar = [] for i in range(n): x, y = map(int, input().split()) ar.append([x, i, y]) ar.sort() ans = [0 for i in range(n)] cur = 1 for i in range(n): cur = max(cur, ar[i][0]) if(cur <= ar[i][2]): ans[ar[i][1]] = cur cur += 1 else: ans[ar[i][1]] = 0 print(*ans) ```
96,699