text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide a correct Python 3 solution for this coding contest problem. Nathan O. Davis has been running an electronic bulletin board system named JAG-channel. He is now having hard time to add a new feature there --- threaded view. Like many other bulletin board systems, JAG-channel is thread-based. Here a thread (also called a topic) refers to a single conversation with a collection of posts. Each post can be an opening post, which initiates a new thread, or a reply to a previous post in an existing thread. Threaded view is a tree-like view that reflects the logical reply structure among the posts: each post forms a node of the tree and contains its replies as its subnodes in the chronological order (i.e. older replies precede newer ones). Note that a post along with its direct and indirect replies forms a subtree as a whole. Let us take an example. Suppose: a user made an opening post with a message `hoge`; another user replied to it with `fuga`; yet another user also replied to the opening post with `piyo`; someone else replied to the second post (i.e. `fuga`”) with `foobar`; and the fifth user replied to the same post with `jagjag`. The tree of this thread would look like: hoge ├─fuga │ ├─foobar │ └─jagjag └─piyo For easier implementation, Nathan is thinking of a simpler format: the depth of each post from the opening post is represented by dots. Each reply gets one more dot than its parent post. The tree of the above thread would then look like: hoge .fuga ..foobar ..jagjag .piyo Your task in this problem is to help Nathan by writing a program that prints a tree in the Nathan's format for the given posts in a single thread. Input Input contains a single dataset in the following format: n k_1 M_1 k_2 M_2 : : k_n M_n The first line contains an integer n (1 ≤ n ≤ 1,000), which is the number of posts in the thread. Then 2n lines follow. Each post is represented by two lines: the first line contains an integer k_i (k_1 = 0, 1 ≤ k_i < i for 2 ≤ i ≤ n) and indicates the i-th post is a reply to the k_i-th post; the second line contains a string M_i and represents the message of the i-th post. k_1 is always 0, which means the first post is not replying to any other post, i.e. it is an opening post. Each message contains 1 to 50 characters, consisting of uppercase, lowercase, and numeric letters. Output Print the given n messages as specified in the problem statement. Sample Input 1 1 0 icpc Output for the Sample Input 1 icpc Sample Input 2 5 0 hoge 1 fuga 1 piyo 2 foobar 2 jagjag Output for the Sample Input 2 hoge .fuga ..foobar ..jagjag .piyo Sample Input 3 8 0 jagjag 1 hogehoge 1 buhihi 2 fugafuga 4 ponyoponyo 5 evaeva 4 nowawa 5 pokemon Output for the Sample Input 3 jagjag .hogehoge ..fugafuga ...ponyoponyo ....evaeva ....pokemon ...nowawa .buhihi Sample Input 4 6 0 nakachan 1 fan 2 yamemasu 3 nennryou2 4 dannyaku4 5 kouzai11 Output for the Sample Input 4 nakachan .fan ..yamemasu ...nennryou2 ....dannyaku4 .....kouzai11 Sample Input 5 34 0 LoveLive 1 honoka 2 borarara 2 sunohare 2 mogyu 1 eri 6 kasikoi 7 kawaii 8 eriichika 1 kotori 10 WR 10 haetekurukotori 10 ichigo 1 umi 14 love 15 arrow 16 shoot 1 rin 18 nyanyanya 1 maki 20 6th 20 star 22 nishikino 1 nozomi 24 spiritual 25 power 1 hanayo 27 darekatasukete 28 chottomattete 1 niko 30 natsuiro 30 nikkonikkoni 30 sekaino 33 YAZAWA Output for the Sample Input 5 LoveLive .honoka ..borarara ..sunohare ..mogyu .eri ..kasikoi ...kawaii ....eriichika .kotori ..WR ..haetekurukotori ..ichigo .umi ..love ...arrow ....shoot .rin ..nyanyanya .maki ..6th ..star ...nishikino .nozomi ..spiritual ...power .hanayo ..darekatasukete ...chottomattete .niko ..natsuiro ..nikkonikkoni ..sekaino ...YAZAWA Sample Input 6 6 0 2ch 1 1ostu 1 2get 1 1otsu 1 1ostu 3 pgr Output for the Sample Input 6 2ch .1ostu .2get ..pgr .1otsu .1ostu Example Input 1 0 icpc Output icpc "Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS():return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): l = [None for i in range(n)] for i in range(n):l[i] = I() return l def LIR(n): l = [None for i in range(n)] for i in range(n):l[i] = LI() return l def SR(n): l = [None for i in range(n)] for i in range(n):l[i] = S() return l def LSR(n): l = [None for i in range(n)] for i in range(n):l[i] = LS() return l sys.setrecursionlimit(1000000) mod = 1000000007 #A def A(): n,h,w = LI() x = LI() t = [0 for i in range(w*n+1)] for i in range(n): if not i%2: t[i*w+x[i]] += 1 t[(i+1)*w+x[i]] -= 1 else: t[i*w-x[i]] += 1 t[(i+1)*w-x[i]] -= 1 for i in range(w*n): t[i+1] += t[i] ans = 0 for i in t[:-1]: if i == 0: ans += h print(ans) return #B def B(): def dfs(d,x): for i in range(d): print(".",end = "") print(lis[x]) for y in v[x]: dfs(d+1,y) n = I() v = [[] for i in range(n)] lis = [None for i in range(n)] for i in range(n): k = I() lis[i] = input() if k > 0: v[k-1].append(i) dfs(0,0) return #C def C(): return #D def D(): return #E def E(): return #F def F(): return #G def G(): return #H def H(): return #I def I_(): return #J def J(): return #Solve if __name__ == "__main__": B() ```
14,100
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nathan O. Davis has been running an electronic bulletin board system named JAG-channel. He is now having hard time to add a new feature there --- threaded view. Like many other bulletin board systems, JAG-channel is thread-based. Here a thread (also called a topic) refers to a single conversation with a collection of posts. Each post can be an opening post, which initiates a new thread, or a reply to a previous post in an existing thread. Threaded view is a tree-like view that reflects the logical reply structure among the posts: each post forms a node of the tree and contains its replies as its subnodes in the chronological order (i.e. older replies precede newer ones). Note that a post along with its direct and indirect replies forms a subtree as a whole. Let us take an example. Suppose: a user made an opening post with a message `hoge`; another user replied to it with `fuga`; yet another user also replied to the opening post with `piyo`; someone else replied to the second post (i.e. `fuga`”) with `foobar`; and the fifth user replied to the same post with `jagjag`. The tree of this thread would look like: hoge ├─fuga │ ├─foobar │ └─jagjag └─piyo For easier implementation, Nathan is thinking of a simpler format: the depth of each post from the opening post is represented by dots. Each reply gets one more dot than its parent post. The tree of the above thread would then look like: hoge .fuga ..foobar ..jagjag .piyo Your task in this problem is to help Nathan by writing a program that prints a tree in the Nathan's format for the given posts in a single thread. Input Input contains a single dataset in the following format: n k_1 M_1 k_2 M_2 : : k_n M_n The first line contains an integer n (1 ≤ n ≤ 1,000), which is the number of posts in the thread. Then 2n lines follow. Each post is represented by two lines: the first line contains an integer k_i (k_1 = 0, 1 ≤ k_i < i for 2 ≤ i ≤ n) and indicates the i-th post is a reply to the k_i-th post; the second line contains a string M_i and represents the message of the i-th post. k_1 is always 0, which means the first post is not replying to any other post, i.e. it is an opening post. Each message contains 1 to 50 characters, consisting of uppercase, lowercase, and numeric letters. Output Print the given n messages as specified in the problem statement. Sample Input 1 1 0 icpc Output for the Sample Input 1 icpc Sample Input 2 5 0 hoge 1 fuga 1 piyo 2 foobar 2 jagjag Output for the Sample Input 2 hoge .fuga ..foobar ..jagjag .piyo Sample Input 3 8 0 jagjag 1 hogehoge 1 buhihi 2 fugafuga 4 ponyoponyo 5 evaeva 4 nowawa 5 pokemon Output for the Sample Input 3 jagjag .hogehoge ..fugafuga ...ponyoponyo ....evaeva ....pokemon ...nowawa .buhihi Sample Input 4 6 0 nakachan 1 fan 2 yamemasu 3 nennryou2 4 dannyaku4 5 kouzai11 Output for the Sample Input 4 nakachan .fan ..yamemasu ...nennryou2 ....dannyaku4 .....kouzai11 Sample Input 5 34 0 LoveLive 1 honoka 2 borarara 2 sunohare 2 mogyu 1 eri 6 kasikoi 7 kawaii 8 eriichika 1 kotori 10 WR 10 haetekurukotori 10 ichigo 1 umi 14 love 15 arrow 16 shoot 1 rin 18 nyanyanya 1 maki 20 6th 20 star 22 nishikino 1 nozomi 24 spiritual 25 power 1 hanayo 27 darekatasukete 28 chottomattete 1 niko 30 natsuiro 30 nikkonikkoni 30 sekaino 33 YAZAWA Output for the Sample Input 5 LoveLive .honoka ..borarara ..sunohare ..mogyu .eri ..kasikoi ...kawaii ....eriichika .kotori ..WR ..haetekurukotori ..ichigo .umi ..love ...arrow ....shoot .rin ..nyanyanya .maki ..6th ..star ...nishikino .nozomi ..spiritual ...power .hanayo ..darekatasukete ...chottomattete .niko ..natsuiro ..nikkonikkoni ..sekaino ...YAZAWA Sample Input 6 6 0 2ch 1 1ostu 1 2get 1 1otsu 1 1ostu 3 pgr Output for the Sample Input 6 2ch .1ostu .2get ..pgr .1otsu .1ostu Example Input 1 0 icpc Output icpc Submitted Solution: ``` N = int(input()) src = [] for i in range(N): k = int(input()) s = input() src.append((s,[])) if i == 0: continue src[k-1][1].append(i) def dfs(i,depth): s,ch = src[i] print('.'*depth + s) for c in ch: dfs(c,depth+1) dfs(0,0) ``` No
14,101
Provide a correct Python 3 solution for this coding contest problem. Problem statement Cards with ranks of $ 2 $ and $ 8 $ are powerful in card game millionaires. Therefore, we call an integer consisting of only the numbers $ 2 $ and $ 8 $ in $ 10 $ decimal notation a good integer. The best integers are listed from the smallest to $ 2, 8, 22, 28, 82, 88, \ cdots $. Let $ n $ be a positive integer. When $ n $ can be expressed in the form of a product of good integers, find the maximum product. If you can't, output $ -1 $. Constraint $ 1 \ leq n \ leq 10 ^ {18} $ sample Sample input 1 1 Sample output 1 -1 Sample input 2 2 Sample output 2 1 Sample input 3 88 Sample output 3 3 It can be expressed as $ 2 \ times 2 \ times 22 $. Sample input 4 100 Sample output 4 -1 Sample input 5 173553147234869248 Sample output 5 11 It can be expressed as $ 2 ^ 6 \ times 28 \ times 2222 ^ 3 \ times 8828 $. input $ n $ output Print the answer on the $ 1 $ line. Example Input 1 Output -1 "Correct Solution: ``` import bisect import sys sys.setrecursionlimit(10000) a=[] def f(a,bin,n): if bin>n:return if bin:a+=[bin] f(a,bin*10+2,n) f(a,bin*10+8,n) def g(n,p): m=-1<<20 x=bisect.bisect_left(a,n) if x!=len(a) and a[x]==n:m=1 if a[p]**2>n:return m if n%a[p]==0:m=g(n//a[p],p)+1 return max(m,g(n,p+1)) n=int(input()) if n&1:print(-1);exit() f(a,0,n) a=sorted(a) b=g(n,0) if b<0:b=-1 print(b) ```
14,102
Provide a correct Python 3 solution for this coding contest problem. Problem statement Cards with ranks of $ 2 $ and $ 8 $ are powerful in card game millionaires. Therefore, we call an integer consisting of only the numbers $ 2 $ and $ 8 $ in $ 10 $ decimal notation a good integer. The best integers are listed from the smallest to $ 2, 8, 22, 28, 82, 88, \ cdots $. Let $ n $ be a positive integer. When $ n $ can be expressed in the form of a product of good integers, find the maximum product. If you can't, output $ -1 $. Constraint $ 1 \ leq n \ leq 10 ^ {18} $ sample Sample input 1 1 Sample output 1 -1 Sample input 2 2 Sample output 2 1 Sample input 3 88 Sample output 3 3 It can be expressed as $ 2 \ times 2 \ times 22 $. Sample input 4 100 Sample output 4 -1 Sample input 5 173553147234869248 Sample output 5 11 It can be expressed as $ 2 ^ 6 \ times 28 \ times 2222 ^ 3 \ times 8828 $. input $ n $ output Print the answer on the $ 1 $ line. Example Input 1 Output -1 "Correct Solution: ``` import bisect import sys sys.setrecursionlimit(10000) a=[] def f(a,bin,n): if bin>n:return if bin:a+=[bin] f(a,bin*10+2,n) f(a,bin*10+8,n) def g(n,p): m=-1<<20 x=bisect.bisect_left(a,n) if x!=len(a) and a[x]==n:m=1 if a[p]**2>n:return m if n%a[p]==0:m=g(n//a[p],p)+1 return max(m,g(n,p+1)) n=int(input()) if n&1:print(-1);exit() f(a,0,n) a=sorted(a)+[10**20] b=g(n,0) if b<0:b=-1 print(b) ```
14,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Problem statement Cards with ranks of $ 2 $ and $ 8 $ are powerful in card game millionaires. Therefore, we call an integer consisting of only the numbers $ 2 $ and $ 8 $ in $ 10 $ decimal notation a good integer. The best integers are listed from the smallest to $ 2, 8, 22, 28, 82, 88, \ cdots $. Let $ n $ be a positive integer. When $ n $ can be expressed in the form of a product of good integers, find the maximum product. If you can't, output $ -1 $. Constraint $ 1 \ leq n \ leq 10 ^ {18} $ sample Sample input 1 1 Sample output 1 -1 Sample input 2 2 Sample output 2 1 Sample input 3 88 Sample output 3 3 It can be expressed as $ 2 \ times 2 \ times 22 $. Sample input 4 100 Sample output 4 -1 Sample input 5 173553147234869248 Sample output 5 11 It can be expressed as $ 2 ^ 6 \ times 28 \ times 2222 ^ 3 \ times 8828 $. input $ n $ output Print the answer on the $ 1 $ line. Example Input 1 Output -1 Submitted Solution: ``` import bisect a=[] def f(a,bin,n): if bin>n:return if bin:a+=[bin] f(a,bin*10+2,n) f(a,bin*10+8,n) def g(n,p): m=-1<<20 x=bisect.bisect_left(a,n) if p>len(a):return m if x!=len(a) and a[x]==n:m=1 if a[p]**2>n:return m if n%a[p]==0:m=g(n//a[p],p)+1 return max(m,g(n,p+1)) n=int(input()) if n&1:print(-1);exit() f(a,0,n) a=sorted(a)+[10**20] b=g(n,0) if b<0:b=-1 print(b) ``` No
14,104
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Problem statement Cards with ranks of $ 2 $ and $ 8 $ are powerful in card game millionaires. Therefore, we call an integer consisting of only the numbers $ 2 $ and $ 8 $ in $ 10 $ decimal notation a good integer. The best integers are listed from the smallest to $ 2, 8, 22, 28, 82, 88, \ cdots $. Let $ n $ be a positive integer. When $ n $ can be expressed in the form of a product of good integers, find the maximum product. If you can't, output $ -1 $. Constraint $ 1 \ leq n \ leq 10 ^ {18} $ sample Sample input 1 1 Sample output 1 -1 Sample input 2 2 Sample output 2 1 Sample input 3 88 Sample output 3 3 It can be expressed as $ 2 \ times 2 \ times 22 $. Sample input 4 100 Sample output 4 -1 Sample input 5 173553147234869248 Sample output 5 11 It can be expressed as $ 2 ^ 6 \ times 28 \ times 2222 ^ 3 \ times 8828 $. input $ n $ output Print the answer on the $ 1 $ line. Example Input 1 Output -1 Submitted Solution: ``` import bisect a=[] def f(a,bin,n): if bin>n:return if bin:a+=[bin] f(a,bin*10+2,n) f(a,bin*10+8,n) def g(n,p): m=-1<<20 x=bisect.bisect_left(a,n) if x!=len(a) and a[x]==n:m=1 if a[p]**2>n:return m if n%a[p]==0:m=g(n//a[p],p)+1 return max(m,g(n,p+1)) n=int(input()) if n&1:print(-1);exit() f(a,0,n) a.sort() b=g(n,0) if b<0:b=-1 print(b) ``` No
14,105
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Problem statement Cards with ranks of $ 2 $ and $ 8 $ are powerful in card game millionaires. Therefore, we call an integer consisting of only the numbers $ 2 $ and $ 8 $ in $ 10 $ decimal notation a good integer. The best integers are listed from the smallest to $ 2, 8, 22, 28, 82, 88, \ cdots $. Let $ n $ be a positive integer. When $ n $ can be expressed in the form of a product of good integers, find the maximum product. If you can't, output $ -1 $. Constraint $ 1 \ leq n \ leq 10 ^ {18} $ sample Sample input 1 1 Sample output 1 -1 Sample input 2 2 Sample output 2 1 Sample input 3 88 Sample output 3 3 It can be expressed as $ 2 \ times 2 \ times 22 $. Sample input 4 100 Sample output 4 -1 Sample input 5 173553147234869248 Sample output 5 11 It can be expressed as $ 2 ^ 6 \ times 28 \ times 2222 ^ 3 \ times 8828 $. input $ n $ output Print the answer on the $ 1 $ line. Example Input 1 Output -1 Submitted Solution: ``` import bisect a=[] def f(n,p): m=-1<<20 x=bisect.bisect_left(a,n) if a[x]==n:m=1 if a[p]**2>n:return m if n%a[p]==0:m=f(n//a[p],p)+1 return max(m,f(n,p+1)) for i in range(1,19): for j in range(1<<i): b='' for k in range(i): if (j//(1<<k))%2:b+='2' else:b+='8' a+=[int(b)] a.sort() n=int(input()) b=f(n,0) if n==1 or b<0:b=-1 print(b) ``` No
14,106
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Problem statement Cards with ranks of $ 2 $ and $ 8 $ are powerful in card game millionaires. Therefore, we call an integer consisting of only the numbers $ 2 $ and $ 8 $ in $ 10 $ decimal notation a good integer. The best integers are listed from the smallest to $ 2, 8, 22, 28, 82, 88, \ cdots $. Let $ n $ be a positive integer. When $ n $ can be expressed in the form of a product of good integers, find the maximum product. If you can't, output $ -1 $. Constraint $ 1 \ leq n \ leq 10 ^ {18} $ sample Sample input 1 1 Sample output 1 -1 Sample input 2 2 Sample output 2 1 Sample input 3 88 Sample output 3 3 It can be expressed as $ 2 \ times 2 \ times 22 $. Sample input 4 100 Sample output 4 -1 Sample input 5 173553147234869248 Sample output 5 11 It can be expressed as $ 2 ^ 6 \ times 28 \ times 2222 ^ 3 \ times 8828 $. input $ n $ output Print the answer on the $ 1 $ line. Example Input 1 Output -1 Submitted Solution: ``` N = int(input()) goods = ["2", "8"] for i in range(9): goods += ["2" + g for g in goods] + ["8" + g for g in goods] goods = list(map(int, goods)) del goods[1] def isgood(n): for c in str(n): if(c != '2' and c != '8'): return False return True def ma(n, mi, nowma): if(n == 1): return 0 if(n % 2 != 0): return -1 mama = int(n**(1 / nowma)) g = 2 i = mi mamama = -1 if(isgood(n)): mamama = 1 while(g < mama + 3): if(n % g == 0): k = n // g newnowma = 2 if(mamama > 2): newnowma = mamama if(newnowma < nowma - 1): newnowma = nowma - 1 tma = ma(k, i, newnowma) t = tma + 1 if tma != -1 else -1 if(t > mamama): mamama = t i += 1 g = goods[i] # print(g) return mamama if(N == 1): print(-1) else: print(ma(N, 0, 2)) ``` No
14,107
Provide a correct Python 3 solution for this coding contest problem. Problem If you bring an empty bottle of $ a $ milk, you can exchange it for a new bottle of $ b $ milk. How many bottles of milk can Mr. Kawabayashi, who initially has a bottle of $ x $ milk, drink? Output the remainder after dividing by $ 1000000007 $. Constraints The input satisfies the following conditions. * $ 1 \ leq a \ leq 10 ^ {15} $ * $ 0 \ leq b \ lt a $ * $ 0 \ leq x \ leq 10 ^ {15} $ Input The input is given in the following format. $ a $ $ b $ $ x $ Three integers $ a $, $ b $, $ x $ are given, separated by spaces. Output Print the answer on one line. Examples Input 3 1 5 Output 7 Input 3 2 5 Output 11 Input 82 69 64 Output 64 Input 316250877917604 316250877917599 681260158257385 Output 62687552 "Correct Solution: ``` a, b, x = map(int, input().split()) print((x+max(0, x-b)//(a-b)*b)%1000000007) ```
14,108
Provide a correct Python 3 solution for this coding contest problem. Problem If you bring an empty bottle of $ a $ milk, you can exchange it for a new bottle of $ b $ milk. How many bottles of milk can Mr. Kawabayashi, who initially has a bottle of $ x $ milk, drink? Output the remainder after dividing by $ 1000000007 $. Constraints The input satisfies the following conditions. * $ 1 \ leq a \ leq 10 ^ {15} $ * $ 0 \ leq b \ lt a $ * $ 0 \ leq x \ leq 10 ^ {15} $ Input The input is given in the following format. $ a $ $ b $ $ x $ Three integers $ a $, $ b $, $ x $ are given, separated by spaces. Output Print the answer on one line. Examples Input 3 1 5 Output 7 Input 3 2 5 Output 11 Input 82 69 64 Output 64 Input 316250877917604 316250877917599 681260158257385 Output 62687552 "Correct Solution: ``` def inpl(): return list(map(int, input().split())) MOD = 10**9 + 7 a, b, x = inpl() if x < a: print(x%MOD) else: e = (x-b)//(a-b) print((x + e*b)%MOD) ```
14,109
Provide a correct Python 3 solution for this coding contest problem. Problem If you bring an empty bottle of $ a $ milk, you can exchange it for a new bottle of $ b $ milk. How many bottles of milk can Mr. Kawabayashi, who initially has a bottle of $ x $ milk, drink? Output the remainder after dividing by $ 1000000007 $. Constraints The input satisfies the following conditions. * $ 1 \ leq a \ leq 10 ^ {15} $ * $ 0 \ leq b \ lt a $ * $ 0 \ leq x \ leq 10 ^ {15} $ Input The input is given in the following format. $ a $ $ b $ $ x $ Three integers $ a $, $ b $, $ x $ are given, separated by spaces. Output Print the answer on one line. Examples Input 3 1 5 Output 7 Input 3 2 5 Output 11 Input 82 69 64 Output 64 Input 316250877917604 316250877917599 681260158257385 Output 62687552 "Correct Solution: ``` a,b,x = [int(i) for i in input().split()] d = a - b n = max(0,(x - b)) // d ans = x + n * b print(ans % 1000000007) ```
14,110
Provide a correct Python 3 solution for this coding contest problem. Problem If you bring an empty bottle of $ a $ milk, you can exchange it for a new bottle of $ b $ milk. How many bottles of milk can Mr. Kawabayashi, who initially has a bottle of $ x $ milk, drink? Output the remainder after dividing by $ 1000000007 $. Constraints The input satisfies the following conditions. * $ 1 \ leq a \ leq 10 ^ {15} $ * $ 0 \ leq b \ lt a $ * $ 0 \ leq x \ leq 10 ^ {15} $ Input The input is given in the following format. $ a $ $ b $ $ x $ Three integers $ a $, $ b $, $ x $ are given, separated by spaces. Output Print the answer on one line. Examples Input 3 1 5 Output 7 Input 3 2 5 Output 11 Input 82 69 64 Output 64 Input 316250877917604 316250877917599 681260158257385 Output 62687552 "Correct Solution: ``` a, b, x = [int(k) for k in input().split()] if x >= a: d = ((x-b) // (a-b))* b + x else: d = x print(d%1000000007) ```
14,111
Provide a correct Python 3 solution for this coding contest problem. Problem If you bring an empty bottle of $ a $ milk, you can exchange it for a new bottle of $ b $ milk. How many bottles of milk can Mr. Kawabayashi, who initially has a bottle of $ x $ milk, drink? Output the remainder after dividing by $ 1000000007 $. Constraints The input satisfies the following conditions. * $ 1 \ leq a \ leq 10 ^ {15} $ * $ 0 \ leq b \ lt a $ * $ 0 \ leq x \ leq 10 ^ {15} $ Input The input is given in the following format. $ a $ $ b $ $ x $ Three integers $ a $, $ b $, $ x $ are given, separated by spaces. Output Print the answer on one line. Examples Input 3 1 5 Output 7 Input 3 2 5 Output 11 Input 82 69 64 Output 64 Input 316250877917604 316250877917599 681260158257385 Output 62687552 "Correct Solution: ``` #!/usr/bin/env python3 a, b, x = map(int, input().split()) print((x + max(x - b, 0) // (a - b) * b) % 1000000007) ```
14,112
Provide a correct Python 3 solution for this coding contest problem. Problem If you bring an empty bottle of $ a $ milk, you can exchange it for a new bottle of $ b $ milk. How many bottles of milk can Mr. Kawabayashi, who initially has a bottle of $ x $ milk, drink? Output the remainder after dividing by $ 1000000007 $. Constraints The input satisfies the following conditions. * $ 1 \ leq a \ leq 10 ^ {15} $ * $ 0 \ leq b \ lt a $ * $ 0 \ leq x \ leq 10 ^ {15} $ Input The input is given in the following format. $ a $ $ b $ $ x $ Three integers $ a $, $ b $, $ x $ are given, separated by spaces. Output Print the answer on one line. Examples Input 3 1 5 Output 7 Input 3 2 5 Output 11 Input 82 69 64 Output 64 Input 316250877917604 316250877917599 681260158257385 Output 62687552 "Correct Solution: ``` #! /usr/bin/env python3 a, b, x = map(int, input().split()) MOD = int(1e9+7) if x < a: res = x % MOD else: k = (x-b) // (a-b) res = (x + b*k) res %= MOD print(res) ```
14,113
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 "Correct Solution: ``` x1,y1,x2,y2=map(int,input().split()) p1,p2=complex(x1,y1),complex(x2,y2) a=p2-p1 dis=(x1-x2)**2+(y1-y2)**2 for i in range(int(input())): p=complex(*map(int,input().split())) b=p-p1 co=(a.real*b.real+a.imag*b.imag)/dis ans=p+2*(co*a-b) print(ans.real,ans.imag) ```
14,114
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 "Correct Solution: ``` import sys input = sys.stdin.readline class Vector(): def __init__(self, x, y): self.x = x self.y = y def __add__(self, vec): return Vector(self.x+vec.x, self.y+vec.y) def __sub__(self, vec): return Vector(self.x-vec.x, self.y-vec.y) def __mul__(self, sc): return Vector(self.x*sc, self.y*sc) def __truediv__(self, sc): return Vector(self.x/sc, self.y/sc) def __iadd__(self, vec): self.x += vec.x self.y += vec.y return self def __isub__(self, vec): self.x -= vec.x self.y -= vec.y return self def __imul__(self, sc): self.x *= sc self.y *= sc return self def __itruediv__(self, sc): self.x /= sc self.y /= sc return self def __str__(self): return '{:.9f} {:.9f}'.format(self.x, self.y) def __eq__(self, vec): return self.x == vec.x and self.y == vec.y def dot(self, vec): return self.x * vec.x + self.y * vec.y def abs(self): return (self.x*self.x + self.y*self.y)**0.5 def ortho(self): return Vector(-self.y, self.x) x1, y1, x2, y2 = map(int, input().split()) v1 = Vector(x1, y1) v2 = Vector(x2, y2) n = v2-v1 n /= n.abs() m = n.ortho() for _ in [0]*int(input()): x, y = map(int, input().split()) v = Vector(x, y) - v1 u = v - m * v.dot(m) * 2 print(v1 + u) ```
14,115
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 "Correct Solution: ``` xp1,yp1,xp2,yp2=map(int,input().split()) q=int(input()) if xp1==xp2: for i in range(q): x0,y0=map(int,input().split()) print('{:.10f}'.format(2*xp1-x0),'{:.10f}'.format(y0)) else: a=float((yp2-yp1)/(xp2-xp1)) for i in range(q): x0,y0=map(int,input().split()) x=a*((y0-yp1)-a*(x0-xp1))/(1+a**2)+x0 y=(a*(a*y0+x0-xp1)+yp1)/(1+a**2) print('{:.10f}'.format(2*x-x0),'{:.10f}'.format(2*y-y0)) ```
14,116
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 "Correct Solution: ``` import sys input = sys.stdin.readline def main(): x1, y1, x2, y2 = map(int, input().split()) q = int(input()) if x2 - x1 == 0:# x = x1 の直線 for i in range(q): xp, yp = map(int, input().split()) print(xp + 2 * (x1 - xp), yp) else: m = (y2 - y1) / (x2 - x1) for i in range(q): xp, yp = map(int, input().split()) yq = (2 * m * (xp - x1) + yp * (m * m - 1) + 2 * y1) / (1 + m * m) xq = xp - m * (yq - yp) print(xq, yq) if __name__ == "__main__": main() ```
14,117
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 "Correct Solution: ``` import math class Point(): def __init__(self, x=None, y=None): self.x = x self.y = y class Line(): def __init__(self, x1, y1, x2, y2): self.p1 = Point(x1, y1) self.p2 = Point(x2, y2) def projection(self, p): a = (self.p2.y - self.p1.y)/(self.p2.x - self.p1.x) if self.p2.x != self.p1.x else float('inf') if a == 0: return (p.x, 2*self.p1.y - p.y) elif a == float('inf'): return (2*self.p1.x - p.x, p.y) else: b = self.p2.y - a*self.p2.x d = abs(-1*a*p.x + p.y - b)/math.sqrt(1 + a*a) ans_y = [p.y + math.sqrt(4*d*d/(a*a+1)), p.y - math.sqrt(4*d*d/(a*a+1))] ans_x = [p.x - a*(_y - p.y) for _y in ans_y] rst_x, rst_y = None, None tmp = abs(-1*a*ans_x[0] + ans_y[0] - b)/math.sqrt(1 + a*a) - d if tmp < abs(-1*a*ans_x[1] + ans_y[1] - b)/math.sqrt(1 + a*a) - d: rst_x, rst_y = ans_x[0], ans_y[0] else: rst_x, rst_y = ans_x[1], ans_y[1] return round(rst_x, 9), round(rst_y, 9) x1, y1, x2, y2 = list(map(int, input().split(' '))) line = Line(x1, y1, x2, y2) q = int(input()) for i in range(q): x, y = list(map(int, input().split(' '))) p = Point(x, y) rst_x, rst_y = line.projection(p) print(rst_x, rst_y) ```
14,118
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 "Correct Solution: ``` # -*- coding: utf-8 -*- import collections import math class Vector2(collections.namedtuple("Vector2", ["x", "y"])): def __add__(self, other): return Vector2(self.x + other.x, self.y + other.y) def __sub__(self, other): return Vector2(self.x - other.x, self.y - other.y) def __mul__(self, scalar): # cross product return Vector2(self.x * scalar, self.y * scalar) def __neg__(self): return Vector2(-self.x, -self.y) def __pos__(self): return Vector2(+self.x, +self.y) def __abs__(self): # norm return math.sqrt(float(self.x * self.x + self.y * self.y)) def dot(self, other): # dot product return self.x * other.x + self.y * other.y def cross(self, other): # cross product return self.x * other.y - self.y * other.x if __name__ == '__main__': a, b, c, d = map(int, input().split()) p1 = Vector2(a, b) p2 = Vector2(c, d) base = p2 - p1 q = int(input()) ans = [] for _ in range(q): e, f = map(int, input().split()) p = Vector2(e, f) hypo = Vector2(e - a, f - b) proj = p1 + base * (hypo.dot(base) / float(base.x**2 + base.y**2)) x = p + (proj - p) * 2 ans.append(x) for x in ans: print(x.x, x.y) ```
14,119
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 "Correct Solution: ``` from sys import stdin class Vector: def __init__(self, x=None, y=None): self.x = x self.y = y def __add__(self, other): return Vector(self.x + other.x, self.y + other.y) def __sub__(self, other): return Vector(self.x - other.x, self.y - other.y) def __mul__(self, k): return Vector(self.x * k, self.y * k) def __gt__(self, other): return self.x > other.x and self.y > other.y def __eq__(self, other): return self.x == other.x and self.y == other.y def dot(self, other): return self.x * other.x + self.y * other.y # usually cross operation return Vector but it returns scalor def cross(self, other): return self.x * other.y - self.y * other.x def norm(self): return self.x * self.x + self.y * self.y def abs(self): return math.sqrt(self.norm()) class Point(Vector): def __init__(self, *args, **kargs): return super().__init__(*args, **kargs) class Segment: def __init__(self, p1=Point(0, 0), p2=Point(1, 1)): self.p1 = p1 self.p2 = p2 def project(s, p): base = s.p2 - s.p1 hypo = p - s.p1 r = hypo.dot(base) / base.norm() return s.p1 + base * r def reflect(s, p): return p + (project(s, p) - p) * 2 def read_and_print_results(s, n): for _ in range(n): line = stdin.readline().strip().split() p = Vector(int(line[0]), int(line[1])) x = reflect(s, p) print('{0:0.10f} {1:0.10f}'.format(x.x, x.y)) x1, y1, x2, y2 = input().split() p1 = Point(int(x1), int(y1)) p2 = Point(int(x2), int(y2)) s = Segment(p1, p2) n = int(input()) read_and_print_results(s, n) ```
14,120
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 "Correct Solution: ``` #!/usr/bin/python3 import array from fractions import Fraction import math import os import sys class Vec(object): def __init__(self, x, y): self.x = x self.y = y super().__init__() def __add__(self, other): return Vec(self.x + other.x, self.y + other.y) def __sub__(self, other): return Vec(self.x - other.x, self.y - other.y) def __mul__(self, scalar): return Vec(self.x * scalar, self.y * scalar) def __rmul__(self, scalar): return Vec(self.x * scalar, self.y * scalar) def __truediv__(self, scalar): return Vec(self.x / scalar, self.y / scalar) def __iadd__(self, other): self.x += other.x self.y += other.y return self def __isub__(self, other): self.x -= other.x self.y -= other.y return self def __imul__(self, scalar): self.x *= scalar self.y *= scalar return self def __idiv__(self, scalar): self.x /= scalar self.y /= scalar return self def __neg__(self): return Vec(-self.x, -self.y) def dot(self, other): return self.x * other.x + self.y * other.y def cross(self, other): return self.x * other.y - self.y * other.x def abs2(self): return self.x * self.x + self.y * self.y def __abs__(self): return math.sqrt(float(self.abs2())) def __str__(self): return '({}, {})'.format(self.x, self.y) def main(): x1, y1, x2, y2 = read_ints() Q = read_int() for _ in range(Q): x, y = read_ints() print(*solve(Vec(x1, y1), Vec(x2, y2), Vec(x, y))) def solve(u, v, a): v -= u a -= u b = Fraction(2 * v.dot(a), v.abs2()) * v - a b += u return float(b.x), float(b.y) ############################################################################### # AUXILIARY FUNCTIONS DEBUG = 'DEBUG' in os.environ def inp(): return sys.stdin.readline().rstrip() def read_int(): return int(inp()) def read_ints(): return [int(e) for e in inp().split()] def dprint(*value, sep=' ', end='\n'): if DEBUG: print(*value, sep=sep, end=end) if __name__ == '__main__': main() ```
14,121
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 Submitted Solution: ``` import math from typing import Union class Point(object): __slots__ = ['x', 'y'] def __init__(self, x, y): self.x = x self.y = y def __add__(self, other): return Point(self.x + other.x, self.y + other.y) def __sub__(self, other): return Point(self.x - other.x, self.y - other.y) def __mul__(self, other: Union[int, float]): return Point(self.x * other, self.y * other) def __repr__(self): return f"({self.x},{self.y})" class Segment(object): __slots__ = ['pt1', 'pt2', 'vector'] def __init__(self, x: Point, y: Point): self.pt1 = x self.pt2 = y self.vector = self.pt2 - self.pt1 def dot(self, other): return self.vector.x * other.vector.x + self.vector.y * other.vector.y def cross(self, other): return self.vector.x * other.vector.y - self.vector.y * other.vector.x def norm(self): return pow(self.vector.x, 2) + pow(self.vector.y, 2) def abs(self): return math.sqrt(self.norm()) def projection(self, pt: Point)-> Point: t = self.dot(Segment(self.pt1, pt)) / pow(self.abs(), 2) return Point(self.pt1.x + t * self.vector.x, self.pt1.y + t * self.vector.y) def reflection(self, pt: Point) -> Point: return self.projection(pt) * 2 - pt def __repr__(self): return f"{self.pt1},{self.pt2},{self.vector}" def main(): p0_x, p0_y, p1_x, p1_y = map(int, input().split()) seg = Segment(Point(p0_x, p0_y), Point(p1_x, p1_y)) num_query = int(input()) for i in range(num_query): pt_x, pt_y = map(int, input().split()) reflection = seg.reflection(Point(pt_x, pt_y)) print("{:.10f} {:.10f}".format(reflection.x, reflection.y)) return main() ``` Yes
14,122
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 Submitted Solution: ``` from math import sin, cos, atan2 def sgn(x, eps=1e-10): if x < -eps: return -1 if -eps <= x <= eps: return 0 if eps < x: return 1 class Vector(): def __init__(self, x=0.0, y=0.0): self.x = x self.y = y def arg(self): return atan2(self.y, self.x) def norm(self): return (self.x**2 + self.y**2)**0.5 def rotate(self, t): nx = self.x * cos(t) - self.y * sin(t) ny = self.x * sin(t) + self.y * cos(t) return Vector(nx, ny) def counter(self): nx = -self.x ny = -self.y return Vector(nx, ny) def times(self, k): nx = self.x * k ny = self.y * k return Vector(nx, ny) def unit(self): norm = self.norm() nx = self.x / norm ny = self.y / norm return Vector(nx, ny) def normal(self): norm = self.norm() nx = -self.y / norm ny = self.x / norm return Vector(nx, ny) def add(self, other): #Vector, Vector -> Vector nx = self.x + other.x ny = self.y + other.y return Vector(nx, ny) def sub(self, other): nx = self.x - other.x ny = self.y - other.y return Vector(nx, ny) def dot(self, other): #Vector, Vector -> int return self.x * other.x + self.y * other.y def cross(self, other): #Vector, Vector -> int return self.x * other.y - self.y * other.x def __str__(self): return '{:.9f}'.format(self.x) + ' ' + '{:.9f}'.format(self.y) class Line(): def __init__(self, bgn=Vector(), end=Vector()): self.bgn = bgn self.end = end def build(self, a, b, c): #ax + by == 1 assert sgn(a) != 0 or sgn(b) != 0 if sgn(b) == 0: self.bgn = Vector(-c / a, 0.0) self.end = Vector(-c / a, 1.0) else: self.v = Vector(0, -c / b) self.u = Vector(1.0, -(a + b) / b) def vec(self): return self.end.sub(self.bgn) def projection(self, point): v = self.vec() u = point.sub(self.bgn) k = v.dot(u) / v.norm() h = v.unit().times(k) return self.bgn.add(h) def refrection(self, point): proj = self.projection(point) return proj.sub(point).times(2).add(point) xp1, yp1, xp2, yp2 = map(int, input().split()) q = int(input()) p1 = Vector(xp1, yp1) p2 = Vector(xp2, yp2) l = Line(p1, p2) for _ in range(q): x, y = map(int, input().split()) p = Vector(x, y) ref = l.refrection(p) print(ref) ``` Yes
14,123
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 Submitted Solution: ``` x1,y1,x2,y2 = map(float,input().split()) det = x1 - x2 if det != 0: a = (y1 - y2) / det b = (-(y1 * x2) + y2 * x1) / det q = int(input()) x,y = [],[] for i in range(q): x_t,y_t = map(float,input().split()) x.append(x_t) y.append(y_t) if det == 0: for i in range(q): print(-x[i],y[i]) elif y2 == y1: for i in range(q): print(x[i],-y[i]) else: for i in range(q): k = y[i] + 1/a * x[i] s = (k - b)/(a + 1/a) print(s+(s-x[i]),-1/a * (s+(s-x[i])) + k) ``` Yes
14,124
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 Submitted Solution: ``` def project(x,y): base=[x2-x1,y2-y1] hypo=[x-x1,y-y1] r=(base[0]*hypo[0]+base[1]*hypo[1])/(base[0]**2+base[1]**2) return x1+base[0]*r,y1+base[1]*r def reflect(x,y): px,py=project(x,y) return x+(px-x)*2,y+(py-y)*2 x1,y1,x2,y2=map(int,input().split()) q=int(input()) for i in range(q): x,y=map(int,input().split()) print(*reflect(x,y)) ``` Yes
14,125
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 Submitted Solution: ``` # coding=utf-8 from math import sqrt, fsum def inner_product(vect1, vect2): return math.fsum([(v1_el*v2_el) for v1_el, v2_el in zip(vect1, vect2)]) def vector_abs(vect): return sqrt(sum([element**2 for element in vect])) def direction_unit_vector(p_from, p_to): d_vector = [(xt - xf) for xt, xf in zip(p_to, p_from)] d_u_vector = [element/vector_abs(d_vector) for element in d_vector] return d_u_vector def projection(origin, line_from, line_to): direction_unit = direction_unit_vector(line_from, line_to) origin_d_vector = [(org-lf) for org, lf in zip(origin, line_from)] inject_dist = inner_product(direction_unit, origin_d_vector) on_line_vect = [inject_dist*element for element in direction_unit] return [olv_el + lf_el for olv_el, lf_el in zip(on_line_vect, line_from)] def reflection(origin, line_from, line_to): mid_point = projection(origin, line_from, line_to) direction = [2*(mp_el - or_el) for mp_el, or_el in zip(mid_point, origin)] reflected = [(dr_el + or_el) for dr_el, or_el in zip(direction, origin)] return reflected if __name__ == '__main__': xy_list = list(map(int, input().split())) p1_list = xy_list[:2] p2_list = xy_list[2:] Q = int(input()) for i in range(Q): p_list = list(map(int, input().split())) x_list = reflection(p_list, p1_list, p2_list) print(' '.join(map(str, x_list))) ``` No
14,126
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 Submitted Solution: ``` def dot(a, b): return a.real * b.real + a.imag * b.imag def projection(s,p): base=abs(b-a) r=dot(p-a,base)/(base**2) return a+base*r def reflect(p0, p1, p2): a = p1 - p0 b = p2 - p0 v = projection(a, b) u = v - b p3 = p2 + 2 * u return p3 x0,y0,x1,y1 = map(float,input().split()) a = complex(x0,y0) b = complex(x1,y1) n=int(input()) for i in range(n): x,y=map(int,input().split()) c=complex(x,y) p=reflect(a,b,c) print("{:.10} {:.10}".format(p.real, p.imag)) ``` No
14,127
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 Submitted Solution: ``` a, b,c,d = map(float,input().split()) a = complex(a,b) b = complex(c,d) for _ in [0] *int(input()): c = complex(*map(float, input().split())) q = b-a c -= a p = a+q*(c/q).conjugate() print("{:.10} {:.10}".format(p.real, p.imag)) ``` No
14,128
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For given three points p1, p2, p, find the reflection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 -1.0000000000 0.0000000000 -1.0000000000 1.0000000000 -1.0000000000 Input 0 0 3 4 3 2 5 1 4 0 3 Output 4.2400000000 3.3200000000 3.5600000000 2.0800000000 2.8800000000 0.8400000000 Submitted Solution: ``` # coding=utf-8 from math import sqrt def inner_product(vect1, vect2): return sum([(v1_el*v2_el) for v1_el, v2_el in zip(vect1, vect2)]) def vector_abs(vect): return sqrt(sum([element**2 for element in vect])) def direction_unit_vector(p_from, p_to): d_vector = [(xt - xf) for xt, xf in zip(p_to, p_from)] d_u_vector = [element/vector_abs(d_vector) for element in d_vector] return d_u_vector def projection(origin, line_from, line_to): direction_unit = direction_unit_vector(line_from, line_to) origin_d_vector = [(org-lf) for org, lf in zip(origin, line_from)] inject_dist = inner_product(direction_unit, origin_d_vector) on_line_vect = [inject_dist*element for element in direction_unit] return [olv_el + lf_el for olv_el, lf_el in zip(on_line_vect, line_from)] def reflection(origin, line_from, line_to): mid_point = projection(origin, line_from, line_to) direction = [2*(mp_el - or_el) for mp_el, or_el in zip(mid_point, origin)] reflected = [(dr_el + or_el) for dr_el, or_el in zip(direction, origin)] return reflected if __name__ == '__main__': xy_list = list(map(int, input().split())) p1_list = xy_list[:2] p2_list = xy_list[2:] Q = int(input()) for i in range(Q): p_list = list(map(int, input().split())) x_list = reflection(p_list, p1_list, p2_list) print(' '.join(map(str, x_list))) ``` No
14,129
Provide a correct Python 3 solution for this coding contest problem. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 "Correct Solution: ``` class Node: def __init__(self, value): self.value = value self.next = None class List: def __init__(self): self.head = None self.tail = None def insert(self, v): node = Node(v) if self.tail is None: self.head = node self.tail = node else: self.tail.next = node self.tail = node def dump(self): node = self.head while node is not None: yield node.value node = node.next def splice(self, li): if self.head is None: self.head = li.head self.tail = li.tail else: self.tail.next = li.head self.tail = li.tail li.clear() def clear(self): self.head = None self.tail = None def run(): n, q = [int(x) for x in input().split()] ls = [List() for _ in range(n)] for _ in range(q): com = [int(x) for x in input().split()] c = com[0] if c == 0: t, v = com[1:] ls[t].insert(v) elif c == 1: t = com[1] values = [] for v in ls[t].dump(): values.append(str(v)) print(" ".join(values)) elif c == 2: s, t = com[1:] ls[t].splice(ls[s]) else: raise ValueError('invalid command') if __name__ == '__main__': run() ```
14,130
Provide a correct Python 3 solution for this coding contest problem. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 "Correct Solution: ``` import sys input = sys.stdin.readline from collections import deque N,Q = map(int,input().split()) L = [deque([]) for _ in range(N)] for _ in range(Q): q = list(map(int,input().split())) if q[0] == 0: L[q[1]].append(q[2]) elif q[0] == 1: if L[q[1]]: print(' '.join(map(str,L[q[1]]))) else: print() else: if L[q[1]]: if L[q[2]]: if len(L[q[1]]) == 1: L[q[2]].append(L[q[1]][0]) elif len(L[q[2]]) == 1: L[q[1]].appendleft(L[q[2]][0]) L[q[2]] = L[q[1]] else: L[q[2]].extend(L[q[1]]) else: L[q[2]] = L[q[1]] L[q[1]] = deque([]) ```
14,131
Provide a correct Python 3 solution for this coding contest problem. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 "Correct Solution: ``` import collections class Splice(): def __init__(self, num_lists): self.lists = [collections.deque() for i in range(0, num_lists, 1)] def insert(self, t, x): self.lists[t].append(x) return self def dump(self, t): print(' '.join(map(str, self.lists[t]))) def splice(self, s, t): if self.lists[t]: if len(self.lists[s]) == 1: self.lists[t].append(self.lists[s][0]) elif len(self.lists[t]) == 1: self.lists[s].appendleft(self.lists[t][0]) self.lists[t] = self.lists[s] else: self.lists[t].extend(self.lists[s]) else: self.lists[t] = self.lists[s] self.lists[s] = collections.deque() return self num_lists, num_op = map(int, input().split(' ')) lists = Splice(num_lists) for op in range(0, num_op): op = tuple(map(int, input().split(' '))) if op[0] == 0: lists.insert(op[1], op[2]) elif op[0] == 1: lists.dump(op[1]) elif op[0] == 2: lists.splice(op[1], op[2]) ```
14,132
Provide a correct Python 3 solution for this coding contest problem. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 "Correct Solution: ``` import sys from collections import deque,defaultdict A = defaultdict(deque) sys.stdin.readline().split() ans =[] for query in sys.stdin: if query[0] == "0": t,x = query[2:].split() A[t].append(x) elif query[0] == "1": ans.append(" ".join(A[query[2:-1]]) + "\n") else: s,t = query[2:].split() if A[t]: if len(A[s]) == 1: A[t].append(A[s][0]) elif len(A[t]) == 1: A[s].appendleft(A[t][0]) A[t] = A[s] else: A[t].extend(A[s]) else: A[t] = A[s] A[s] = deque() sys.stdout.writelines(ans) ```
14,133
Provide a correct Python 3 solution for this coding contest problem. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 "Correct Solution: ``` def solve(): from collections import deque from sys import stdin f_i = stdin n, q = map(int, f_i.readline().split()) L = [deque() for i in range(n)] ans = [] for op in (line.split() for line in f_i): op_type = op[0] if op_type == '0': L[int(op[1])].append(op[2]) elif op_type == '1': ans.append(' '.join(L[int(op[1])])) else: s = int(op[1]) ls = L[s] t = int(op[2]) lt = L[t] if lt: if len(ls) == 1: lt.append(ls[0]) elif len(lt) == 1: ls.appendleft(lt[0]) L[t] = ls elif ls: lt.extend(ls) else: L[t] = ls L[s] = deque() print('\n'.join(ans)) solve() ```
14,134
Provide a correct Python 3 solution for this coding contest problem. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 "Correct Solution: ``` from collections import deque import sys n, q = map(int, input().split()) # Q = [[] for _ in range(n)] Q = [deque() for _ in range(n)] ans = [] for query in (line.split() for line in sys.stdin): s = int(query[1]) if query[0] == '0': t = int(query[2]) Q[s].append(t) elif query[0] == '1': # ans.append(Q[s]) print(*Q[s]) elif query[0] == '2': t = int(query[2]) if Q[t]: if len(Q[s]) == 1: Q[t].append(Q[s][0]) elif len(Q[t]) == 1: Q[s].appendleft(Q[t][0]) Q[t] = Q[s] else: Q[t].extend(Q[s]) else: Q[t] = Q[s] Q[s] = deque() # for i in ans: # print(i) ```
14,135
Provide a correct Python 3 solution for this coding contest problem. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 "Correct Solution: ``` import sys from collections import deque sys.setrecursionlimit(10**9) n,q = map(int, input().split()) l = [deque() for _ in range(n)] for _ in range(q): query = input().split() if query[0] == "0": # insert idx = int(query[1]) l[idx].append(query[2]) elif query[0] == "1": # dump idx = int(query[1]) print(" ".join(l[idx])) elif query[0] == "2": # splice idx1 = int(query[1]) idx2 = int(query[2]) if len(l[idx1]) > 1: if len(l[idx2]) == 0: l[idx2] = l[idx1] elif len(l[idx2]) == 1: l[idx1].appendleft(l[idx2][0]) l[idx2] = l[idx1] else: l[idx2].extend(l[idx1]) elif len(l[idx1]) == 1: l[idx2].append(l[idx1][0]) l[idx1] = deque() ```
14,136
Provide a correct Python 3 solution for this coding contest problem. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 "Correct Solution: ``` from collections import deque n, q = [int(x) for x in input().split()] L = [ deque() for _ in range(n) ] for _ in range(q): c, t, x = [int(x) for x in (input()+" 0").split()][:3] if c == 0: L[t].append(x) elif c == 1: print( " ".join(map(str,L[t]))) else: s,t = t, x if L[s]: if L[t]: if len(L[s]) == 1: L[t].append(L[s][0]) elif len(L[t]) == 1: L[s].appendleft(L[t][0]) L[t] = L[s] else: L[t].extend(L[s]) else: L[t] = L[s] L[s] = deque() ```
14,137
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 Submitted Solution: ``` from collections import deque def insert(t, x): Llist[t].append(x) def dump(t): print(*Llist[t]) def splice(s, t): if Llist[t]: if len(Llist[t]) ==1: Llist[s].appendleft(Llist[t][0]) Llist[t] = Llist[s] elif len(Llist[s]) == 1: Llist[t].append(Llist[s][0]) else: Llist[t].extend(Llist[s]) else: Llist[t] = Llist[s] Llist[s] = deque() n, q = map(int, input().split()) Llist = [] for i in range(n): Llist.append(deque()) for i in range(q): queryi = list(map(int, input().split())) if queryi[0] == 0: insert(queryi[1], queryi[2]) elif queryi[0] == 1: dump(queryi[1]) else: splice(queryi[1], queryi[2]) ``` Yes
14,138
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 Submitted Solution: ``` from sys import stdin,stdout from collections import defaultdict,deque n, q = map(int,stdin.readline().split()) queries = stdin.readlines() A = defaultdict(deque) ans = [] #count = 0 for query in queries: #print(count) #count += 1 query = query.split() if query[0] == '0': A[int(query[1])].append(query[2]) elif query[0] == '1': ans.append(' '.join(A[int(query[1])]) + '\n') else: s = int(query[1]) t = int(query[2]) if (A[t]): if len(A[s]) == 1: A[t].append(A[s][0]) elif len(A[t]) == 1: A[s].appendleft(A[t][0]) A[t] = A[s] else: A[t].extend(A[s]) else: A[t] = A[s] A[s] = deque() stdout.writelines(ans) ``` Yes
14,139
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 Submitted Solution: ``` from collections import deque import sys sys.setrecursionlimit(10**9) def input(): return sys.stdin.readline().strip() n,q = map(int,input().split()) L = deque(deque() for i in range(n)) for i in range(q): query = input().split() if query[0] == "0": L[int(query[1])].append(query[2]) elif query[0] == "1": print(" ".join(L[int(query[1])])) else: s = int(query[1]) t = int(query[2]) if len(L[s]): if len(L[t]) == 0: L[t] = L[s] elif len(L[t]) == 1: L[s].appendleft(L[t][0]) L[t] = L[s] else: L[t].extend(L[s]) elif len(L[s]) == 1: L[t].append(L[t][0]) L[s] = deque() ``` Yes
14,140
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 Submitted Solution: ``` from collections import deque readline = open(0).readline writelines = open(1, 'w').writelines N, Q = map(int, readline().split()) ans = [] A = [deque() for i in range(N)] def push(t, x): A[t].append(str(x)) def dump(t): ans.append(" ".join(A[t])) ans.append("\n") def splice(s, t): if A[s]: if A[t]: if len(A[s]) == 1: A[t].append(A[s][0]) elif len(A[t]) == 1: A[s].appendleft(A[t][0]) A[t] = A[s] else: A[t].extend(A[s]) else: A[t] = A[s] A[s] = deque() C = [push, dump, splice].__getitem__ for i in range(Q): t, *a=map(int, readline().split()) C(t)(*a) writelines(ans) ``` Yes
14,141
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 Submitted Solution: ``` flag = 0 def lsprint(lists): global flag if lists: if flag: print() print() else: flag = 1 print(lists.pop(0), end = '') for i in lists: print(' %s' %i, end = '') else: pass def splice(s, t): return s + t, [] if __name__ == '__main__': n, q = input().split() n, q = int(n), int(q) lists = [[] for _ in range(n)] for i in range(q): query = input().split() if query[0] == "0": lists[int(query[1])].insert(-1, query[2]) elif query[0] == "1": lsprint(lists[int(query[1])]) else: lists[int(query[1])], lists[int(query[2])] = splice(lists[int(query[1])], lists[int(query[2])]) ``` No
14,142
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 Submitted Solution: ``` # coding=utf-8 if __name__ == '__main__': N, Q = map(int, input().split()) Que = [[] for i in range(N)] for j in range(Q): query = list(map(int, input().split())) if query[0] == 0: Que[query[1]].append(query[2]) elif query[0] == 1: print(' '.join(map(str, Que[query[1]]))) elif query[0] == 2: Que[query[2]].extend(Que[query[1]]) Que[query[1]].clear() ``` No
14,143
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 Submitted Solution: ``` n, q = list(map(int, input().split(' '))) splice = [[] for i in range(n)] for i in range(q): op = list(map(int, input().split(' '))) if op[0] == 0: splice[op[1]].append(op[2]) elif op[0] == 1: print(' '.join(list(map(str, splice[op[1]])))) elif op[0] == 2: splice[op[2]].extend(splice[op[1]]) splice[op[1]] = [] ``` No
14,144
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations. * insert($t$, $x$): Insert an integer $x$ at the end of $L_t$. * dump($t$): Print all elements in $L_t$. * splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty. In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ are empty. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * For a splice operation, $s \ne t$ * For a splice operation, $L_s$ is not empty * The total number of elements printed by dump operations do not exceed 1,000,000 * $-1,000,000,000 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $n \; q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $s$ $t$ where the first digits 0, 1 and 2 represent insert, dump and splice operations respectively. Output For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed. Example Input 3 10 0 0 1 0 0 2 0 0 3 0 1 4 0 1 5 2 1 0 0 2 6 1 0 1 1 1 2 Output 1 2 3 4 5 6 Submitted Solution: ``` def lsprint(lists): if lists: print(lists.pop(0), end = '') for i in lists: print(' %s' %i, end = '') print() print() else: pass def splice(s, t): return s + t, [] if __name__ == '__main__': n, q = input().split() n, q = int(n), int(q) lists = [[] for _ in range(n)] for i in range(q): query = input().split() if query[0] == "0": lists[int(query[1])].insert(-1, query[2]) elif query[0] == "1": lsprint(lists[int(query[1])]) else: lists[int(query[1])], lists[int(query[2])] = splice(lists[int(query[1])], lists[int(query[2])]) ``` No
14,145
Provide tags and a correct Python 3 solution for this coding contest problem. Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of n locations connected by m two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location. Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses. The game will start in location s and end in location t, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from s to t without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as s or as t. Input The first line contains two integers n and m (2 ≤ n ≤ 3 ⋅ 10^5, n - 1 ≤ m ≤ 3 ⋅ 10^5) — the number of locations and passages, respectively. Then m lines follow, each containing two integers x and y (1 ≤ x, y ≤ n, x ≠ y) describing the endpoints of one of the passages. It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location. Output Print one integer — the maximum number of bosses your friend can place, considering all possible choices for s and t. Examples Input 5 5 1 2 2 3 3 1 4 1 5 2 Output 2 Input 4 3 1 2 4 3 3 2 Output 3 Tags: dfs and similar, graphs, trees Correct Solution: ``` import sys from array import array # noqa: F401 def readline(): return sys.stdin.buffer.readline().decode('utf-8') def build_bridge_tree(v_count, edge_count, adj, edge_index): from collections import deque preorder = [0] parent, order, low = [0]+[-1]*v_count, [0]+[-1]*(v_count-1), [0]*v_count stack = [(0, adj[0][::])] pre_i = 1 while stack: v, dests = stack.pop() while dests: dest = dests.pop() if order[dest] == -1: preorder.append(dest) order[dest] = low[dest] = pre_i parent[dest] = v pre_i += 1 stack.extend(((v, dests), (dest, adj[dest][::]))) break is_bridge = array('b', [0]) * edge_count for v in reversed(preorder): for dest, ei in zip(adj[v], edge_index[v]): if dest != parent[v] and low[v] > low[dest]: low[v] = low[dest] if dest != parent[v] and order[v] < low[dest]: is_bridge[ei] = 1 bridge_tree = [[] for _ in range(v_count)] stack = [0] visited = array('b', [1] + [0]*(v_count-1)) while stack: v = stack.pop() dq = deque([v]) while dq: u = dq.popleft() for dest, ei in zip(adj[u], edge_index[u]): if visited[dest]: continue visited[dest] = 1 if is_bridge[ei]: bridge_tree[v].append(dest) bridge_tree[dest].append(v) stack.append(dest) else: dq.append(dest) return bridge_tree def get_dia(adj): from collections import deque n = len(adj) dq = deque([(0, -1)]) while dq: end1, par = dq.popleft() for dest in adj[end1]: if dest != par: dq.append((dest, end1)) prev = [-1]*n prev[end1] = -2 dq = deque([(end1, 0)]) while dq: end2, diameter = dq.popleft() for dest in adj[end2]: if prev[dest] == -1: prev[dest] = end2 dq.append((dest, diameter+1)) return end1, end2, diameter, prev n, m = map(int, readline().split()) adj = [[] for _ in range(n)] eindex = [[] for _ in range(n)] for ei in range(m): u, v = map(int, readline().split()) adj[u-1].append(v-1) adj[v-1].append(u-1) eindex[u-1].append(ei) eindex[v-1].append(ei) btree = build_bridge_tree(n, m, adj, eindex) print(get_dia(btree)[2]) ```
14,146
Provide tags and a correct Python 3 solution for this coding contest problem. Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of n locations connected by m two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location. Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses. The game will start in location s and end in location t, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from s to t without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as s or as t. Input The first line contains two integers n and m (2 ≤ n ≤ 3 ⋅ 10^5, n - 1 ≤ m ≤ 3 ⋅ 10^5) — the number of locations and passages, respectively. Then m lines follow, each containing two integers x and y (1 ≤ x, y ≤ n, x ≠ y) describing the endpoints of one of the passages. It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location. Output Print one integer — the maximum number of bosses your friend can place, considering all possible choices for s and t. Examples Input 5 5 1 2 2 3 3 1 4 1 5 2 Output 2 Input 4 3 1 2 4 3 3 2 Output 3 Tags: dfs and similar, graphs, trees Correct Solution: ``` import sys from array import array # noqa: F401 def readline(): return sys.stdin.buffer.readline().decode('utf-8') def build_bridge_tree(v_count, edge_count, adj, edge_index): from collections import deque preorder = [0] parent, order, low = [0]+[-1]*v_count, [0]+[-1]*(v_count-1), [0]*v_count stack = [0] rem = [len(dests)-1 for dests in adj] pre_i = 1 while stack: v = stack.pop() while rem[v] >= 0: dest = adj[v][rem[v]] rem[v] -= 1 if order[dest] == -1: preorder.append(dest) order[dest] = low[dest] = pre_i parent[dest] = v pre_i += 1 stack.extend((v, dest)) break is_bridge = array('b', [0]) * edge_count for v in reversed(preorder): for dest, ei in zip(adj[v], edge_index[v]): if dest != parent[v] and low[v] > low[dest]: low[v] = low[dest] if dest != parent[v] and order[v] < low[dest]: is_bridge[ei] = 1 bridge_tree = [[] for _ in range(v_count)] stack = [0] visited = array('b', [1] + [0]*(v_count-1)) while stack: v = stack.pop() dq = deque([v]) while dq: u = dq.popleft() for dest, ei in zip(adj[u], edge_index[u]): if visited[dest]: continue visited[dest] = 1 if is_bridge[ei]: bridge_tree[v].append(dest) bridge_tree[dest].append(v) stack.append(dest) else: dq.append(dest) return bridge_tree def get_dia(adj): from collections import deque n = len(adj) dq = deque([(0, -1)]) while dq: end1, par = dq.popleft() for dest in adj[end1]: if dest != par: dq.append((dest, end1)) prev = [-1]*n prev[end1] = -2 dq = deque([(end1, 0)]) while dq: end2, diameter = dq.popleft() for dest in adj[end2]: if prev[dest] == -1: prev[dest] = end2 dq.append((dest, diameter+1)) return end1, end2, diameter, prev n, m = map(int, readline().split()) adj = [[] for _ in range(n)] eindex = [[] for _ in range(n)] for ei in range(m): u, v = map(int, readline().split()) adj[u-1].append(v-1) adj[v-1].append(u-1) eindex[u-1].append(ei) eindex[v-1].append(ei) btree = build_bridge_tree(n, m, adj, eindex) print(get_dia(btree)[2]) ```
14,147
Provide tags and a correct Python 3 solution for this coding contest problem. Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of n locations connected by m two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location. Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses. The game will start in location s and end in location t, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from s to t without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as s or as t. Input The first line contains two integers n and m (2 ≤ n ≤ 3 ⋅ 10^5, n - 1 ≤ m ≤ 3 ⋅ 10^5) — the number of locations and passages, respectively. Then m lines follow, each containing two integers x and y (1 ≤ x, y ≤ n, x ≠ y) describing the endpoints of one of the passages. It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location. Output Print one integer — the maximum number of bosses your friend can place, considering all possible choices for s and t. Examples Input 5 5 1 2 2 3 3 1 4 1 5 2 Output 2 Input 4 3 1 2 4 3 3 2 Output 3 Tags: dfs and similar, graphs, trees Correct Solution: ``` import sys def get_input(): n, m = [int(x) for x in sys.stdin.readline().split(' ')] graph = [[] for _ in range(n + 1)] for _ in range(m): u, v = [int(x) for x in sys.stdin.readline().split(' ')] graph[u].append(v) graph[v].append(u) return graph def dfs_bridges(graph, node): n = len(graph) time = 0 discover_time = [0] * n low = [0] * n pi = [None] * n color = ['white'] * n ecc = [-1] * n ecc_number = 0 bridges = [] stack = [node] discover_stack = [] while stack: current_node = stack[-1] if color[current_node] != 'white': stack.pop() if color[current_node] == 'grey': if pi[current_node] is not None: low[pi[current_node]] = min(low[pi[current_node]], low[current_node]) if low[current_node] == discover_time[current_node]: bridges.append((pi[current_node], current_node)) while discover_stack: top = discover_stack.pop() ecc[top] = ecc_number if top == current_node: break ecc_number += 1 color[current_node] = 'black' continue time += 1 color[current_node] = 'grey' low[current_node] = discover_time[current_node] = time discover_stack.append(current_node) for adj in graph[current_node]: if color[adj] == 'white': pi[adj] = current_node stack.append(adj) elif pi[current_node] != adj: low[current_node] = min(low[current_node], discover_time[adj]) else: while discover_stack: top = discover_stack.pop() ecc[top] = ecc_number ecc_number += 1 return ecc_number, ecc, bridges def build_bridge_tree(ecc_count, ecc, bridges): n = len(graph) bridge_tree = [[] for _ in range(ecc_count)] for u, v in bridges: if ecc[u] != ecc[v]: bridge_tree[ecc[u]].append(ecc[v]) bridge_tree[ecc[v]].append(ecc[u]) return bridge_tree def dfs_tree(tree, node): n = len(tree) visited = [False] * n distances = [0] * n current_distance = 0 stack = [node] while stack: current_node = stack[-1] if visited[current_node]: stack.pop() current_distance -= 1 continue visited[current_node] = True distances[current_node] = current_distance current_distance += 1 for adj in tree[current_node]: if not visited[adj]: stack.append(adj) return distances def diameter(tree): distances = dfs_tree(bridge_tree, 0) node = max(enumerate(distances), key=lambda t: t[1])[0] distances = dfs_tree(bridge_tree, node) return max(distances) if __name__ == "__main__": graph = get_input() ecc_count, ecc, bridges = dfs_bridges(graph, 1) bridge_tree = build_bridge_tree(ecc_count, ecc, bridges) print(diameter(bridge_tree)) ```
14,148
Provide tags and a correct Python 3 solution for this coding contest problem. Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of n locations connected by m two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location. Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses. The game will start in location s and end in location t, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from s to t without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as s or as t. Input The first line contains two integers n and m (2 ≤ n ≤ 3 ⋅ 10^5, n - 1 ≤ m ≤ 3 ⋅ 10^5) — the number of locations and passages, respectively. Then m lines follow, each containing two integers x and y (1 ≤ x, y ≤ n, x ≠ y) describing the endpoints of one of the passages. It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location. Output Print one integer — the maximum number of bosses your friend can place, considering all possible choices for s and t. Examples Input 5 5 1 2 2 3 3 1 4 1 5 2 Output 2 Input 4 3 1 2 4 3 3 2 Output 3 Tags: dfs and similar, graphs, trees Correct Solution: ``` import sys # sys.stind.readline lee datos el doble de # rápido que la funcion por defecto input input = sys.stdin.readline def get_input(): n, m = [int(x) for x in input().split(' ')] graph = [[] for _ in range(n + 1)] for _ in range(m): u, v = [int(x) for x in input().split(' ')] graph[u].append(v) graph[v].append(u) return graph def dfs_bridges(graph, node): n = len(graph) time = 0 discover_time = [0] * n low = [0] * n pi = [None] * n color = ['white'] * n ecc = [-1] * n ecc_number = 0 bridges = [] stack = [node] discover_stack = [] while stack: current_node = stack[-1] if color[current_node] != 'white': stack.pop() if color[current_node] == 'grey': if pi[current_node] is not None: low[pi[current_node]] = min(low[pi[current_node]], low[current_node]) if low[current_node] == discover_time[current_node]: bridges.append((pi[current_node], current_node)) while discover_stack: top = discover_stack.pop() ecc[top] = ecc_number if top == current_node: break ecc_number += 1 color[current_node] = 'black' continue time += 1 color[current_node] = 'grey' low[current_node] = discover_time[current_node] = time discover_stack.append(current_node) for adj in graph[current_node]: if color[adj] == 'white': pi[adj] = current_node stack.append(adj) elif pi[current_node] != adj: low[current_node] = min(low[current_node], discover_time[adj]) else: while discover_stack: top = discover_stack.pop() ecc[top] = ecc_number ecc_number += 1 return ecc_number, ecc, bridges def build_bridge_tree(ecc_count, ecc, bridges): n = len(graph) bridge_tree = [[] for _ in range(ecc_count)] for u, v in bridges: if ecc[u] != ecc[v]: bridge_tree[ecc[u]].append(ecc[v]) bridge_tree[ecc[v]].append(ecc[u]) return bridge_tree def diameter(tree, root): n = len(tree) pi = [None] * n color = ['white'] * n dp1, dp2 = [0] * n, [0] * n stack = [root] while stack: current_node = stack[-1] if color[current_node] != 'white': stack.pop() if color[current_node] == 'grey': parent = pi[current_node] if parent is not None: t = dp1[current_node] + 1 if t > dp1[parent]: t, dp1[parent] = dp1[parent], t if t > dp2[parent]: t, dp2[parent] = dp2[parent], t color[current_node] = 'black' continue color[current_node] = 'grey' for adj in tree[current_node]: if color[adj] == 'white': pi[adj] = current_node stack.append(adj) root = max(range(n), key=lambda x: dp1[x] + dp2[x]) return dp1[root] + dp2[root] def dfs_tree(tree, node): n = len(tree) visited = [False] * n distances = [0] * n current_distance = 0 stack = [node] while stack: current_node = stack[-1] if visited[current_node]: stack.pop() current_distance -= 1 continue visited[current_node] = True distances[current_node] = current_distance current_distance += 1 for adj in tree[current_node]: if not visited[adj]: stack.append(adj) return distances # def diameter(tree): # distances = dfs_tree(bridge_tree, 0) # node = max(enumerate(distances), key=lambda t: t[1])[0] # distances = dfs_tree(bridge_tree, node) # return max(distances) if __name__ == "__main__": graph = get_input() ecc_count, ecc, bridges = dfs_bridges(graph, 1) bridge_tree = build_bridge_tree(ecc_count, ecc, bridges) if ecc_count > 2: root = [x for x, l in enumerate(bridge_tree) if len(l) > 1][0] print(diameter(bridge_tree, root)) else: print(ecc_count - 1) ```
14,149
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of n locations connected by m two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location. Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses. The game will start in location s and end in location t, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from s to t without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as s or as t. Input The first line contains two integers n and m (2 ≤ n ≤ 3 ⋅ 10^5, n - 1 ≤ m ≤ 3 ⋅ 10^5) — the number of locations and passages, respectively. Then m lines follow, each containing two integers x and y (1 ≤ x, y ≤ n, x ≠ y) describing the endpoints of one of the passages. It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location. Output Print one integer — the maximum number of bosses your friend can place, considering all possible choices for s and t. Examples Input 5 5 1 2 2 3 3 1 4 1 5 2 Output 2 Input 4 3 1 2 4 3 3 2 Output 3 Submitted Solution: ``` import sys def main(): p = input().split() n = int(p[0]) #number of locations m = int(p[1]) #number of passages g = graph(n,m) t = g.buildBridgeTree() if n<=5: t.treeDiameter() class graph: n = int() #number of nodes edges= int() #number of edges time = int() conexions = [] #adjacency list bridges=[] #are we using a removable edge or not for city i? discovery = [] #time to discover node i seen = [] #visited or not cc = [] #index of connected components low = [] #low[i]=min(discovery(i),discovery(w)) w:any node discoverable from i pi = [] #index of i's father def __init__(self, n, m): self.n = n self.edges = m for i in range(self.n): self.conexions.append([]) self.discovery.append(sys.float_info.max) self.low.append(sys.float_info.max) self.seen.append(False) self.cc.append(-1) def buildGraph(self): #this method put every edge in the adjacency list for i in range(self.edges): p2=input().split() self.conexions[int(p2[0])-1].append((int(p2[1])-1,False)) self.conexions[int(p2[1])-1].append((int(p2[0])-1,False)) def searchBridges (self): self.time = 0 for i in range(self.n): self.pi.append(-1) self.searchBridges_(0,-1) #we can start in any node 'cause the graph is connected def searchBridges_(self,c,index): self.seen[c]=True #mark as visited self.time+=1 self.discovery[c]=self.low[c]= self.time for i in range(len(self.conexions[c])): pos = self.conexions[c][i][0] if self.seen[pos]==False: self.pi[pos]=c #the node that discovered it self.searchBridges_(pos,i) #search throw its not visited conexions self.low[c]= min([self.low[c],self.low[pos]]) #definition of low elif self.pi[c]!=pos: #It is not the node that discovered it self.low[c]= min([self.low[c],self.discovery[pos]]) if self.pi[c]!=-1 and self.low[c]==self.discovery[c]: #it isn't the root and none of its connections is reacheable earlier than it self.bridges.append((c,self.pi[c])) for i in range(len(self.conexions[c])): if(self.conexions[c][i][0]==self.pi[c]): self.conexions[c][i]=(self.pi[c],True) self.conexions[self.pi[c]][index]=(c,True) break def findCC(self): j = 0 for i in range(self.n): self.pi[i]=-1 self.seen[i]=False for i in range(self.n): if self.seen[i]==False: self.cc[i]=j #assign the index of the new connected component self.findCC_(i,j) #search throw its not visited conexions j+=1 #we finished the search in the connected component def findCC_(self, c, j): self.seen[c]=True #mark as visited for i in range(len(self.conexions[c])): pos = self.conexions[c][i][0] if(self.seen[pos]==False and self.conexions[c][i][1]==False): self.cc[pos]=j #assign the index of the connected component self.pi[pos]=c #the node that discovered it self.findCC_(pos,j) #search throw its not visited conexions def treeDiameter(self): for i in range(self.n): self.seen[i]=False self.pi[i]=-1 self.discovery[0]=0 self.treeDiameter_(0) #search the distance from this node, to the others max = sys.float_info.min maxIndex = 0 for i in range(self.n): if self.discovery[i]>max: max= self.discovery[i] #look for the furthest node maxIndex=i for i in range(self.n): self.seen[i]=False self.pi[i]=-1 self.discovery[maxIndex]=0 self.treeDiameter_(maxIndex) #search the distance from the furthest node, to the others max = sys.float_info.min for i in range(self.n): if self.discovery[i]>max : max= self.discovery[i] #look for the furthest node to preview furthest node, and that is the diameter in a tree print(max) def treeDiameter_(self, c): self.seen[c]=True #mark as visited for i in range(len(self.conexions[c])): pos = self.conexions[c][i][0] if self.seen[pos]==False: self.pi[pos]=c #the node that discovered it self.discovery[pos]= self.discovery[c]+1 #distance to the node who discover it + 1 #we don't have to compare and search for other paths, since it's a tree and there is only one path between two nodes. self.treeDiameter_(pos) #search throw its not visited conexions def buildBridgeTree(self): if self.n>5: self.buildGraph() self.searchBridges() self.findCC() print(2) return self.buildGraph() self.searchBridges() self.findCC() t = graph(len(self.bridges)+1,len(self.bridges)) for i in range(len(self.bridges)): t.conexions[self.cc[self.bridges[i][0]]].append((self.cc[self.bridges[i][1]],True)) t.conexions[self.cc[self.bridges[i][1]]].append((self.cc[self.bridges[i][0]],True)) return t if __name__ == '__main__': main() ``` No
14,150
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of n locations connected by m two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location. Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses. The game will start in location s and end in location t, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from s to t without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as s or as t. Input The first line contains two integers n and m (2 ≤ n ≤ 3 ⋅ 10^5, n - 1 ≤ m ≤ 3 ⋅ 10^5) — the number of locations and passages, respectively. Then m lines follow, each containing two integers x and y (1 ≤ x, y ≤ n, x ≠ y) describing the endpoints of one of the passages. It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location. Output Print one integer — the maximum number of bosses your friend can place, considering all possible choices for s and t. Examples Input 5 5 1 2 2 3 3 1 4 1 5 2 Output 2 Input 4 3 1 2 4 3 3 2 Output 3 Submitted Solution: ``` from sys import stdin,stdout class DFS_General: def __init__(self,edges,n): self.n= n self.pi = [-1 for _ in range(0,n)] self.visit = [False for _ in range(0,n)] self.Ady= edges self.d = [-1 for _ in range(0,n)] self.low = [-1 for _ in range(0,n)] self.compo = [-1 for _ in range(0,n )] self.count = 0 self.time = 0 self.bridges = [] self.queue= [] def DFS_visit_AP(self, u): self.visit[u] = True self.time += 1 self.d[u] = self.time self.low[u] = self.d[u] for v in self.Ady[u]: if not self.visit[v]: self.pi[v]= u self.DFS_visit_AP(v) self.low[u]= min(self.low[v], self.low[u]) elif self.visit[v] and self.pi[v] != u: self.low[u]= min(self.low[u], self.d[v]) self.compo[u]= self.count if self.pi[u] != -1 and self.low[u]== self.d[u]: self.bridges.append((self.pi[u], u)) def DFS_AP(self): for i in range(0,self.n): if not self.visit[i]: self.count += 1 self.DFS_visit_AP(i) def BFS(s,Ady,n): d = [0 for _ in range(0,n)] color = [-1 for _ in range(0,n)] queue = [] queue.append(s) d[s]= 0 color[s] = 0 while queue: u = queue.pop(0) for v in Ady[u]: if color[v] == -1: color[v]= 0 d[v] = d[u] + 1 queue.append(v) return d def Solution(Ady,n): DFS_ = DFS_General(Ady,n) DFS_.DFS_AP() if len(DFS_.bridges) > 0: for i in DFS_.bridges: Ady[i[0]].remove(i[1]) Ady[i[1]].remove(i[0]) DFS_final = DFS_General(Ady,n) DFS_final.DFS_AP() Ady = [[] for _ in range(0,DFS_final.count)] for i in DFS_.bridges: Ady[DFS_final.compo[i[0]]-1].append(DFS_final.compo[i[1]] -1) Ady[DFS_final.compo[i[1]] -1].append(DFS_final.compo[i[0]] -1) d = BFS(0,Ady,len(Ady)) i = d.index(max(d)) d = BFS(i,Ady,len(Ady)) return max(d) else: return 0 n_m = stdin.readline().split() n = int(n_m[0]) m = int(n_m[1]) Ady = [[] for i in range (0,n)] for i in range(m): stri= stdin.readline().split() item = (int(stri[0])-1,int(stri[1])-1) Ady[item[0]].append(item[1]) Ady[item[1]].append(item[0]) stdout.write(str(Solution(Ady,n))) ``` No
14,151
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of n locations connected by m two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location. Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses. The game will start in location s and end in location t, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from s to t without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as s or as t. Input The first line contains two integers n and m (2 ≤ n ≤ 3 ⋅ 10^5, n - 1 ≤ m ≤ 3 ⋅ 10^5) — the number of locations and passages, respectively. Then m lines follow, each containing two integers x and y (1 ≤ x, y ≤ n, x ≠ y) describing the endpoints of one of the passages. It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location. Output Print one integer — the maximum number of bosses your friend can place, considering all possible choices for s and t. Examples Input 5 5 1 2 2 3 3 1 4 1 5 2 Output 2 Input 4 3 1 2 4 3 3 2 Output 3 Submitted Solution: ``` import sys def main(): p = input().split() n = int(p[0]) #number of locations m = int(p[1]) #number of passages g = graph(n,m) g.buildBridgeTree() if m==19999: return t = graph(len(g.bridges)+1,len(g.bridges)) t.conexions= [] t.pi=[] t.discovery=[] t.seen=[] for i in range(len(g.bridges)+1): t.conexions.append([]) t.pi.append(-1) t.discovery.append(sys.float_info.max) t.seen.append(False) for i in range(len(g.bridges)): t.conexions[g.cc[g.bridges[i][0]]].append((g.cc[g.bridges[i][1]],True)) t.conexions[g.cc[g.bridges[i][1]]].append((g.cc[g.bridges[i][0]],True)) t.treeDiameter() class graph: n = int() #number of nodes edges= int() #number of edges time = int() conexions = [] #adjacency list bridges=[] #are we using a removable edge or not for city i? discovery = [] #time to discover node i seen = [] #visited or not cc = [] #index of connected components low = [] #low[i]=min(discovery(i),discovery(w)) w:any node discoverable from i pi = [] #index of i's father def __init__(self, n, m): self.n = n self.edges = m for i in range(self.n): self.conexions.append([]) self.discovery.append(sys.float_info.max) self.low.append(sys.float_info.max) self.seen.append(False) self.cc.append(-1) def buildGraph(self): #this method put every edge in the adjacency list for i in range(self.edges): p2=input().split() self.conexions[int(p2[0])-1].append((int(p2[1])-1,False)) self.conexions[int(p2[1])-1].append((int(p2[0])-1,False)) def searchBridges (self): self.time = 0 for i in range(self.n): self.pi.append(-1) self.searchBridges_(0,-1) #we can start in any node 'cause the graph is connected def searchBridges_(self,c,index): self.seen[c]=True #mark as visited self.time+=1 self.discovery[c]=self.low[c]= self.time try: for i in range(len(self.conexions[c])): pos = self.conexions[c][i][0] if self.seen[pos]==False: self.pi[pos]=c #the node that discovered it self.searchBridges_(pos,i) #search throw its not visited conexions self.low[c]= min([self.low[c],self.low[pos]]) #definition of low elif self.pi[c]!=pos: #It is not the node that discovered it self.low[c]= min([self.low[c],self.discovery[pos]]) except: print(c) if self.pi[c]!=-1 and self.low[c]==self.discovery[c]: #it isn't the root and none of its connections is reacheable earlier than it self.bridges.append((c,self.pi[c])) for i in range(len(self.conexions[c])): if(self.conexions[c][i][0]==self.pi[c]): self.conexions[c][i]=(self.pi[c],True) self.conexions[self.pi[c]][index]=(c,True) def findCC(self): j = 0 for i in range(self.n): self.pi[i]=-1 self.seen[i]=False for i in range(self.n): if self.seen[i]==False: self.cc[i]=j #assign the index of the new connected component self.findCC_(i,j) #search throw its not visited conexions j+=1 #we finished the search in the connected component def findCC_(self, c, j): self.seen[c]=True #mark as visited for i in range(len(self.conexions[c])): pos = self.conexions[c][i][0] if(self.seen[pos]==False and self.conexions[c][i][1]==False): self.cc[pos]=j #assign the index of the connected component self.pi[pos]=c #the node that discovered it self.findCC_(pos,j) #search throw its not visited conexions def treeDiameter(self): for i in range(self.n): self.seen[i]=False self.pi[i]=-1 self.discovery[0]=0 self.treeDiameter_(0) #search the distance from this node, to the others max = -1 maxIndex = 0 for i in range(self.n): if self.discovery[i]>max: max= self.discovery[i] #look for the furthest node maxIndex=i for i in range(self.n): self.seen[i]=False self.pi[i]=-1 self.discovery[maxIndex]=0 self.treeDiameter_(maxIndex) #search the distance from the furthest node, to the others max =-1 for i in range(self.n): if self.discovery[i]>max : max= self.discovery[i] #look for the furthest node to preview furthest node, and that is the diameter in a tree print(max) def treeDiameter_(self, c): self.seen[c]=True #mark as visited for i in range(len(self.conexions[c])): pos = self.conexions[c][i][0] if self.seen[pos]==False: self.pi[pos]=c #the node that discovered it self.discovery[pos]= self.discovery[c]+1 #distance to the node who discover it + 1 #we don't have to compare and search for other paths, since it's a tree and there is only one path between two nodes. self.treeDiameter_(pos) #search throw its not visited conexions def buildBridgeTree(self): self.buildGraph() self.searchBridges() self.findCC() if __name__ == '__main__': main() ``` No
14,152
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of n locations connected by m two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location. Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses. The game will start in location s and end in location t, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from s to t without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as s or as t. Input The first line contains two integers n and m (2 ≤ n ≤ 3 ⋅ 10^5, n - 1 ≤ m ≤ 3 ⋅ 10^5) — the number of locations and passages, respectively. Then m lines follow, each containing two integers x and y (1 ≤ x, y ≤ n, x ≠ y) describing the endpoints of one of the passages. It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location. Output Print one integer — the maximum number of bosses your friend can place, considering all possible choices for s and t. Examples Input 5 5 1 2 2 3 3 1 4 1 5 2 Output 2 Input 4 3 1 2 4 3 3 2 Output 3 Submitted Solution: ``` import sys def main(): p = input().split() n = int(p[0]) #number of locations m = int(p[1]) #number of passages g = graph(n,m) g.buildBridgeTree() t = graph(len(g.bridges)+1,len(g.bridges)) t.conexions= [] t.pi=[] t.discovery=[] t.seen=[] for i in range(len(g.bridges)+1): t.conexions.append([]) t.pi.append(-1) t.discovery.append(sys.float_info.max) t.seen.append(False) for i in range(len(g.bridges)): t.conexions[g.cc[g.bridges[i][0]]].append((g.cc[g.bridges[i][1]],True)) t.conexions[g.cc[g.bridges[i][1]]].append((g.cc[g.bridges[i][0]],True)) t.treeDiameter() class graph: n = int() #number of nodes edges= int() #number of edges time = int() conexions = [] #adjacency list bridges=[] #are we using a removable edge or not for city i? discovery = [] #time to discover node i seen = [] #visited or not cc = [] #index of connected components low = [] #low[i]=min(discovery(i),discovery(w)) w:any node discoverable from i pi = [] #index of i's father def __init__(self, n, m): self.n = n self.edges = m for i in range(self.n): self.conexions.append([]) self.discovery.append(sys.float_info.max) self.low.append(sys.float_info.max) self.seen.append(False) self.cc.append(-1) def buildGraph(self): #this method put every edge in the adjacency list for i in range(self.edges): p2=input().split() self.conexions[int(p2[0])-1].append((int(p2[1])-1,False)) self.conexions[int(p2[1])-1].append((int(p2[0])-1,False)) def searchBridges (self): self.time = 0 for i in range(self.n): self.pi.append(-1) self.searchBridges_(0,-1) #we can start in any node 'cause the graph is connected def searchBridges_(self,c,index): self.seen[c]=True #mark as visited self.time+=1 self.discovery[c]=self.low[c]= self.time for i in range(len(self.conexions[c])): pos = self.conexions[c][i][0] if self.seen[pos]==False: self.pi[pos]=c #the node that discovered it self.searchBridges_(pos,i) #search throw its not visited conexions self.low[c]= min([self.low[c],self.low[pos]]) #definition of low elif self.pi[c]!=pos: #It is not the node that discovered it self.low[c]= min([self.low[c],self.discovery[pos]]) if self.pi[c]!=-1 and self.low[c]==self.discovery[c]: #it isn't the root and none of its connections is reacheable earlier than it self.bridges.append((c,self.pi[c])) for i in range(len(self.conexions[c])): if(self.conexions[c][i][0]==self.pi[c]): self.conexions[c][i]=(self.pi[c],True) self.conexions[self.pi[c]][index]=(c,True) break def findCC(self): j = 0 for i in range(self.n): self.pi[i]=-1 self.seen[i]=False for i in range(self.n): if self.seen[i]==False: self.cc[i]=j #assign the index of the new connected component self.findCC_(i,j) #search throw its not visited conexions j+=1 #we finished the search in the connected component def findCC_(self, c, j): self.seen[c]=True #mark as visited for i in range(len(self.conexions[c])): pos = self.conexions[c][i][0] if(self.seen[pos]==False and self.conexions[c][i][1]==False): self.cc[pos]=j #assign the index of the connected component self.pi[pos]=c #the node that discovered it self.findCC_(pos,j) #search throw its not visited conexions def treeDiameter(self): for i in range(self.n): self.seen[i]=False self.pi[i]=-1 self.discovery[0]=0 self.treeDiameter_(0) #search the distance from this node, to the others max = sys.float_info.min maxIndex = 0 for i in range(self.n): if self.discovery[i]>max: max= self.discovery[i] #look for the furthest node maxIndex=i for i in range(self.n): self.seen[i]=False self.pi[i]=-1 self.discovery[maxIndex]=0 self.treeDiameter_(maxIndex) #search the distance from the furthest node, to the others max = sys.float_info.min for i in range(self.n): if self.discovery[i]>max : max= self.discovery[i] #look for the furthest node to preview furthest node, and that is the diameter in a tree print(max) def treeDiameter_(self, c): self.seen[c]=True #mark as visited for i in range(len(self.conexions[c])): pos = self.conexions[c][i][0] if self.seen[pos]==False: self.pi[pos]=c #the node that discovered it self.discovery[pos]= self.discovery[c]+1 #distance to the node who discover it + 1 #we don't have to compare and search for other paths, since it's a tree and there is only one path between two nodes. self.treeDiameter_(pos) #search throw its not visited conexions def buildBridgeTree(self): self.buildGraph() self.searchBridges() self.findCC() if __name__ == '__main__': main() ``` No
14,153
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Tags: brute force, implementation Correct Solution: ``` n = int(input()) main = [] for i in range(n): arr = list(map(int, input().split())) main.append(arr) outdated = set() for i in range(len(main) - 1): for j in range(i + 1, len(main)): if main[i][0] < main[j][0] and main[i][1] < main[j][1] and main[i][2] < main[j][2]: outdated.add(i) if main[j][0] < main[i][0] and main[j][1] < main[i][1] and main[j][2] < main[i][2]: outdated.add(j) print(min([i for i in range(len(main)) if i not in outdated], key = lambda x: main[x][3]) + 1) ```
14,154
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Tags: brute force, implementation Correct Solution: ``` import sys n = int(sys.stdin.readline().strip()) t = [] for i in range(n): c = [int(x) for x in sys.stdin.readline().strip().split()] c[3] = -c[3] t.append(c) #print(c) #print(t) id = -1 best = 1e9 for i, c in enumerate(t): ok = True for d in t: less = True for j in range(3): if c[j] >= d[j]: less = False break if less: ok = False break #print(i, ok) if ok and -c[3] < best: best = -c[3] id = i + 1 print(id) ```
14,155
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Tags: brute force, implementation Correct Solution: ``` n = int(input()) s = [[int(i) for i in input().split()]+[1] for i in range(n)] speed,ram,hdd=0,0,0 num = 1001 for i in range(n): for j in range(n): if s[i][0]>s[j][0] and s[i][1]>s[j][1] and s[i][2]>s[j][2]: s[j][4]=0 for i in range(n): if s[i][4]: if s[i][3]<num: num = s[i][3] ans = i+1 print(ans) ```
14,156
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Tags: brute force, implementation Correct Solution: ``` a=[] n=int(input()) for i in range(n): s,r,h,c=map(int,input().split()) a.append([s,r,h,c]) ans=-1 for i in range(n): flag=True for j in range(n): if a[i][0]<a[j][0] and a[i][1]<a[j][1] and a[i][2]<a[j][2]: flag=False break if flag: if ans==-1: ans=i else: if a[ans][3]>a[i][3]: ans=i print(ans+1) ```
14,157
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Tags: brute force, implementation Correct Solution: ``` n=int(input()) a=0 b=0 c=0 g=[] v=[0]*n for _ in range(n): l=list(map(int,input().split())) g.append(l) l=[] for i in range(n): f=0 for j in range(n): if(g[i][1]<g[j][1] and g[i][2]<g[j][2] and g[i][0]<g[j][0]): f=1 break if(f==0): l.append((g[i][3],i+1)) l.sort(key=lambda x:(x[0],x[1])) print(l[0][1]) ```
14,158
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Tags: brute force, implementation Correct Solution: ``` n = int(input()) t = [0] * n for i in range(n): t[i] = list(map(int, input().split())) ans = [1] * n for i in range(n): for j in range(n): if all(t[i][k] < t[j][k] for k in range(3)): ans[i] = 0 break k, p = 0, 1001 for i in range(n): if ans[i] and t[i][3] < p: p, k = t[i][3], i print(k + 1) ```
14,159
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Tags: brute force, implementation Correct Solution: ``` def choosing_laptop(n,specs): index = 0 length = n not_outdated = [] while index < length : sub_index = 0 assign = 1 speed , ram , hdd, cost = specs[index] for speed2,ram2,hdd2, cost2 in specs : if speed < speed2 and ram < ram2 and hdd < hdd2 : assign = 0 break if assign == 1 : not_outdated.append(tuple([cost,index+1])) index += 1 not_outdated.sort(key= lambda x : x[0]) return not_outdated[0][1] n = int(input()) specs = [] for x in range(n) : specs.append(list(map(int,input().split()))) print (choosing_laptop(n,specs)) ```
14,160
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Tags: brute force, implementation Correct Solution: ``` from sys import* input= stdin.readline t=int(input()) l=[] cost=[] for i in range(t): a,b,c,d=map(int,input().split()) l.append([a,b,c]) cost.append(d) for i in range(t): for j in range(t): if(i!=j): if(l[i][0]<l[j][0] and l[i][1]<l[j][1] and l[i][2]<l[j][2]): cost[i]=10000 break print(cost.index(min(cost))+1) ```
14,161
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Submitted Solution: ``` n=int(input()) Z=[] for i in range(n): S,R,H,C=map(int,input().split()) Z.append((S,R,H,C)) Y=[] for i in range(n): for j in range(n): if(Z[i][0]<Z[j][0] and Z[i][1]<Z[j][1] and Z[i][2]<Z[j][2]): Y.append(Z[i]) Y=set(Y) minn=100000 ans=0 for i in range(n): item=Z[i] if(item not in Y and item[3]<minn): minn=item[3] ans=i+1 print(ans) ``` Yes
14,162
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Submitted Solution: ``` n=int(input()) A=[] for i in range(n): A+=[list(map(int,input().split()))] cost=10**18 I=0 for i in range(n): ans=True for j in range(n): if(A[i][0]<A[j][0] and A[i][1]<A[j][1] and A[i][2]<A[j][2]): ans=False if(ans): cost=min(cost,A[i][3]) if(cost==A[i][3]): I=i+1 print(I) ``` Yes
14,163
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Submitted Solution: ``` def chck(m,l): p,q,r,s=l for i in m: a,b,e,d=i if a>p and b>q and e>r:return 0 else:return 1 n=int(input());l,c=[],[];m=10**4 for i in range(n): t=list(map(int,input().split())) l.append(t);c.append(t[-1]) for i in range(n): if chck(l,l[i]):m=min(l[i][-1],m) print(c.index(m)+1) ``` Yes
14,164
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Submitted Solution: ``` def readln(): return tuple(map(int, input().split())) n, = readln() ans = 0 best = (0, 0, 0, 0) lst = [readln() + (_ + 1,) for _ in range(n)] lst = [v for v in lst if not [1 for u in lst if v[0] < u[0] and v[1] < u[1] and v[2] < u[2]]] lst.sort(key=lambda x: x[3]) print(lst[0][4]) ``` Yes
14,165
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Submitted Solution: ``` n = int(input()) l = [] mspec = [0 for i in range(4)] index = 0 price = 1000 for i in range(n): spec = list(map(int, input().split())) l.append(spec) if spec[0] >= mspec[0] and spec[1] >= mspec[1] and spec[2] >= mspec[2]: mspec = spec for i in range(n): if (l[i][0] >= mspec[0] or l[i][1] >= mspec[1] or l[i][2] >= mspec[2]) and l[i][ 3 ] <= price: price = l[i][3] index = i + 1 print(index) ``` No
14,166
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Submitted Solution: ``` # ========= /\ /| |====/| # | / \ | | / | # | /____\ | | / | # | / \ | | / | # ========= / \ ===== |/====| # code if __name__ == "__main__": p = [] c = [] r = [] n = int(input()) x = range(n) i = 0 for i in x: a,b,e,d = map(int,input().split()) c.append(d) p.append((a,b,e)) for i in x: for j in x: if p[j] < p[i]: r.append(j) for i in r: c[i] = 1000000 print(c.index(min(c)) + 1) ``` No
14,167
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Submitted Solution: ``` from sys import* input= stdin.readline t=int(input()) speed=[] ram=[] hdd=[] cost=[] res=[] for i in range(t): l=[0]*3 res.append(l) a,b,c,d=map(int,input().split()) speed.append([a,i]) ram.append([b,i]) hdd.append([c,i]) cost.append(d) speed.sort() ram.sort() hdd.sort() for i in range(t): res[speed[i][1]][0]=i res[ram[i][1]][1]=i res[hdd[i][1]][2]=i for i in range(t): if(t-1 not in res[i]): cost[i]=10000 print(cost.index(min(cost))+1) ``` No
14,168
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is choosing a laptop. The shop has n laptops to all tastes. Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties. If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one. There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him. Input The first line contains number n (1 ≤ n ≤ 100). Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, * speed, ram, hdd and cost are integers * 1000 ≤ speed ≤ 4200 is the processor's speed in megahertz * 256 ≤ ram ≤ 4096 the RAM volume in megabytes * 1 ≤ hdd ≤ 500 is the HDD in gigabytes * 100 ≤ cost ≤ 1000 is price in tugriks All laptops have different prices. Output Print a single number — the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data. Examples Input 5 2100 512 150 200 2000 2048 240 350 2300 1024 200 320 2500 2048 80 300 2000 512 180 150 Output 4 Note In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop. Submitted Solution: ``` n = int(input()) lap = [list(map(int,input().split())) for _ in range(n) ] outdated = [] for i in range(n): for j in range(n): if lap[i][0] > lap[j][0] and lap[i][1] > lap[j][1] and lap[i][2] > lap[j][2]: outdated.append(j) q = len(outdated) out = 0 min_c = float("inf") min_i = 0 for i in range(n): if out < q and outdated[out] == i: out += 1 else: if lap[i][3] < min_c: min_c = lap[i][3] min_i = i print(i) ``` No
14,169
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Tags: brute force, implementation, math Correct Solution: ``` import sys def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def input(): return sys.stdin.readline().strip() y,b,r = get_ints() for i in range(r,0,-1): if(r-1<=b and r-2<=y): break else: r=r-1 print(3*r-3) ```
14,170
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Tags: brute force, implementation, math Correct Solution: ``` a = input() a = a.split() x= int(a[0]) y=int(a[1]) z=int(a[2]) m=x n=y-1 o=z-2 print (min(m, n, o)*3+3) ```
14,171
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Tags: brute force, implementation, math Correct Solution: ``` import sys y,b,r = map(int, input().split()) ans = 0 for i in range(1,y+1): if b > i and r > i + 1: ans = max(ans, 3 * i + 3) print(ans) ```
14,172
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Tags: brute force, implementation, math Correct Solution: ``` import math y, b, r = [int(s) for s in input().split(" ")] ans = 0 for i in range(1, y+1): if i+1 > b: break if i+2 > r: break ans = 3*i + 3 print(ans) ```
14,173
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Tags: brute force, implementation, math Correct Solution: ``` a,b,c=map(int,input().split()) if b>=c-1: if a>=c-2: print((c*3)-3) else: print(a*3+3) else: if a>=b-1: print(b*3) else: print(a*3+3) ```
14,174
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Tags: brute force, implementation, math Correct Solution: ``` max_y, max_b, max_r= map(int, input().split(" ")) y = min(max_y, max_b-1, max_r-2) b = y + 1 r = y + 2 print(y+b+r) ```
14,175
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Tags: brute force, implementation, math Correct Solution: ``` aa = [int(x) for x in input().split()] y = aa[0] b = aa[1] r = aa[2] max = 0 if y + 1 <= b and y + 2 <= r: max = y + y + 1 + y + 2 elif b - 1 <= y and b + 1 <= r: max = b - 1 + b + b + 1 else: max = r + r - 1 + r - 2 print(max) ```
14,176
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Tags: brute force, implementation, math Correct Solution: ``` y, b, r = map(int, input().split()) ans = 6 for i in range(1, 100): if y > i and b > i + 1 and r > i + 2: ans += 3 else: break print(ans) ```
14,177
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Submitted Solution: ``` y, b, r = map(int, input().split()) res = min(y + 1, b, r - 1) print(res*3) #y < b > r ``` Yes
14,178
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Submitted Solution: ``` y, b, r = map(int, input().split()) ans = 0 for i in range(1, y+1): if i+1 < b and i+2 < r: ans = max(ans, i + i+1 + i+2) for j in range(1, b+1): if j-1 <= y and j+1 < r: ans = max(ans, j-1 + j + j+1) for k in range(1, r+1): if k-2 <= y and k-1 <= b: ans = max(ans, k + k-1 + k-2) print(ans) ``` Yes
14,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Submitted Solution: ``` y , b, r = map(int, input().split()) k = 0 for i in range(1000): if i <= y and i + 1 <= b and i + 2 <= r: k = i print(3*k + 3) ``` Yes
14,180
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Submitted Solution: ``` y,b,r=map(int,input().split()) x=min(y,b-1,r-2) print(x*3+3) ``` Yes
14,181
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Submitted Solution: ``` y,b,r = [int(x) for x in input().split()] while y>=1 and b>=2 and r>=3: if b>=y+1: b = y+1 if r>=b+1: r = b+1 print(y+b+r) break else: print(r-1+r-2+r) break elif y==b and y==r: print(r+r-1+r-2) break elif b==r: print(r+r-1+r-2) break else: print(b-1+b+b+1) break ``` No
14,182
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Submitted Solution: ``` y,b,r=input().split() y=int(y) b=int(b) r=int(r) if y<b<r: print(y+y+1+y+2) if y>b<r: print(b-1+b+1+b) if y<b>r and (r-1==y or y>r): print(r+r-1+r-2) if y==b==r: print(y+y-1+y-2) if y==b>r: print(y-1+y+y+1) if y==r>b: print(b-1+b+b+1) if y>b==r: print(b-1+b-2+b) if y<b>r and y<r and r-1!=y: print(y+2+y+1+y) if r==y<b: print(y-1+y-2+y) if r==b<y: print(b+b-1+b+1) ``` No
14,183
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Submitted Solution: ``` y ,b ,r = map(int,input().split()) def s(y,b,r): for i in range(1,b+1): if(i >= y and i<r): b = i return b if(y <= r): print(3*(s(y,b,r))) else: y = r-1 print(3*s(y,b,r)) ``` No
14,184
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are decorating a Christmas Tree. Alice wants only 3 types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have y yellow ornaments, b blue ornaments and r red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: * the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and * the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. That is, if they have 8 yellow ornaments, 13 blue ornaments and 9 red ornaments, we can choose 4 yellow, 5 blue and 6 red ornaments (5=4+1 and 6=5+1). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose 7 yellow, 8 blue and 9 red ornaments. If we do it, we will use 7+8+9=24 ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Input The only line contains three integers y, b, r (1 ≤ y ≤ 100, 2 ≤ b ≤ 100, 3 ≤ r ≤ 100) — the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least 6 (1+2+3=6) ornaments. Output Print one number — the maximum number of ornaments that can be used. Examples Input 8 13 9 Output 24 Input 13 3 6 Output 9 Note In the first example, the answer is 7+8+9=24. In the second example, the answer is 2+3+4=9. Submitted Solution: ``` a = [int(x) for x in input().split()] for i in range(a[0]): if((i+1)>=a[1] or (i+2)>=a[2]): break sum=3*(i+1) print(sum) ``` No
14,185
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Tags: brute force, probabilities Correct Solution: ``` s = list() def rec(x): if x > 100000000000: return s.append(x) rec(x * 10 + 4) rec(x * 10 + 7) def f(l1, r1, l2, r2): l1 = max(l1, l2) r1 = min(r1, r2) return max(r1 - l1 + 1, 0) def main(): rec(0) s.sort() args = input().split() pl, pr, vl, vr, k = int(args[0]), int(args[1]), int(args[2]), int(args[3]), int(args[4]) ans = 0 i = 1 while i + k < len(s): l1 = s[i - 1] + 1 r1 = s[i] l2 = s[i + k - 1] r2 = s[i + k] - 1 a = f(l1, r1, vl, vr) * f(l2, r2, pl, pr) b = f(l1, r1, pl, pr) * f(l2, r2, vl, vr) ans += a + b if k == 1 and a > 0 and b > 0: ans -= 1 i += 1 all = (pr - pl + 1) * (vr - vl + 1) print(1.0 * ans / all) if __name__ == '__main__': main() ```
14,186
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Tags: brute force, probabilities Correct Solution: ``` def gen(n, cur): if n == 0: global arr arr.append(cur) else: gen(n - 1, cur + '4') gen(n - 1, cur + '7') def lseg(x1, x2): if x2 < x1: return 0 return x2 - x1 + 1 def inter2(x1, x2, a, b): left = max(x1, a) right = min(x2, b) return (left, right) def inter(x1, x2, a, b): l, r = inter2(x1, x2, a, b) return lseg(l, r) def minus(a, b, c, d): d1 = (-1, c - 1) d2 = (d + 1, 10 ** 10) d1 = inter(a, b, *d1) d2 = inter(a, b, *d2) return d1 + d2 arr = [] for i in range(1, 11): gen(i, '') arr = [0] + sorted(list(map(int, arr))) pl, pr, vl, vr, k = map(int, input().split()) ans = 0 for start in range(1, len(arr) - k + 1): if arr[start + k - 1] > max(vr, pr): break if arr[start] < min(pl, vl): continue goleft = inter(pl, pr, arr[start - 1] + 1, arr[start]) goright = inter(vl, vr, arr[start + k - 1], arr[start + k] - 1) left, right = inter2(pl, pr, arr[start - 1] + 1, arr[start]), \ inter2(vl, vr, arr[start + k - 1], arr[start + k] - 1) ans += goleft * goright left1 = inter2(pl, pr, arr[start + k - 1], arr[start + k] - 1) right1 = inter2(vl, vr, arr[start - 1] + 1, arr[start]) ans += minus(right1[0], right1[1], right[0], right[1]) * lseg(*left1) ans += (lseg(*right1) - minus(right1[0], right1[1], right[0], right[1])) \ * minus(left1[0], left1[1], left[0], left[1]) ans /= (pr - pl + 1) * (vr - vl + 1) print(ans) ```
14,187
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Tags: brute force, probabilities Correct Solution: ``` import itertools as it all_lucky = [] for length in range(1, 10): for comb in it.product(['7', '4'], repeat=length): all_lucky += [int(''.join(comb))] all_lucky.sort() # print(len(all_lucky)) pl, pr, vl, vr, k = map(int, input().split()) result = 0 def inters_len(a, b, c, d): a, b = sorted([a, b]) c, d = sorted([c, d]) return (max([a, c]), min([b, d])) def check_for_intervals(pl, pr, vl, vr): global result for i in range(1, len(all_lucky) - k): le, re = i, i + k - 1 a, b = inters_len(all_lucky[le - 1] + 1, all_lucky[le], pl, pr) left_len = max([0, b - a + 1]) c, d = inters_len(all_lucky[re], all_lucky[re + 1] - 1, vl, vr) right_len = max([0, d - c + 1]) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) if b == c and left_len * right_len > 1e-6: result -= (1 / (pr - pl + 1) / (vr - vl + 1)) / 2 a, b = inters_len(0, all_lucky[0], pl, pr) left_len = max([0, b - a + 1]) c, d = inters_len(all_lucky[k - 1], all_lucky[k] - 1, vl, vr) right_len = max([0, d - c + 1]) #print((left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1))) #print(left_len, right_len) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) if b == c and left_len * right_len > 1e-6: result -= (1 / (pr - pl + 1) / (vr - vl + 1)) / 2 a, b = inters_len(all_lucky[-(k + 1)] + 1, all_lucky[-k], pl, pr) left_len = max([0, b - a + 1]) c, d = inters_len(all_lucky[-1], 10**9, vl, vr) right_len = max([0, d - c + 1]) #print((left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1))) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) if b == c and left_len * right_len > 1e-6: result -= (1 / (pr - pl + 1) / (vr - vl + 1)) / 2 #print(all_lucky[:5]) if k != len(all_lucky): check_for_intervals(pl, pr, vl, vr) check_for_intervals(vl, vr, pl, pr) else: a, b = inters_len(0, all_lucky[0], pl, pr) left_len = max([0, b - a + 1]) c, d = inters_len(all_lucky[-1], 10**9, vl, vr) right_len = max([0, d - c + 1]) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) if b == c and left_len * right_len > 1e-6: result -= (1 / (pr - pl + 1) / (vr - vl + 1)) / 2 a, b = inters_len(0, all_lucky[0], vl, vr) left_len = max([0, b - a + 1]) c, d = inters_len(all_lucky[-1], 10**9, pl, pr) right_len = max([0, d - c + 1]) result += (left_len / (vr - vl + 1)) * (right_len / (pr - pl + 1)) if b == c and left_len * right_len > 1e-6: result -= (1 / (pr - pl + 1) / (vr - vl + 1)) / 2 print(result) ```
14,188
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Tags: brute force, probabilities Correct Solution: ``` def gen_len(l): gen = [] for i in range(2 ** l): k = int(bin(i)[2:].rjust(l, '0').replace('0', '4').replace('1', '7')) if k <= 10 ** 9: gen.append(k) return gen def pairs_with_k_len(a, k): l = 0 r = k - 1 while r < len(a): yield l, r l += 1 r += 1 def get_intersection_length(l1, r1, l2, r2): return max(min(r1, r2) - max(l1, l2) + 1, 0) def main(): gen = set() for i in range(1, 10): for e in gen_len(i): gen.add(e) gen = list(sorted(gen)) pl, pr, vl, vr, k = map(int, input().split()) denominator = (pr - pl + 1) * (vr - vl + 1) p = (pl, pr) v = (vl, vr) count = 0 for l, r in pairs_with_k_len(gen, k): if gen[l] >= min(pl, vl) and gen[r] <= max(pr, vr): l1 = gen[l - 1] if l != 0 else 0 r1 = gen[l] l2 = gen[r] r2 = gen[r + 1] if r != len(gen) - 1 else 10**9 + 1 count += (get_intersection_length(l1 + 1, r1, pl, pr) * get_intersection_length(l2, r2 - 1, vl, vr)) count += (get_intersection_length(l1 + 1, r1, vl, vr) * get_intersection_length(l2, r2 - 1, pl, pr)) if k == 1 and get_intersection_length(vl, vr, pl, pr) != 0: count -= 1 print(f"{(count / denominator):.{12}f}") if __name__ == '__main__': main() ```
14,189
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Tags: brute force, probabilities Correct Solution: ``` import itertools def generate_happy(): happy_digits = '47' happies = [] for num_len in range(1, 10): happies.extend(itertools.product(happy_digits, repeat=num_len)) return [int(''.join(num)) for num in happies] def clamp_segment(start, end, min_b, max_b): if start > end or min_b > max_b: raise ValueError(f"{start} {end} {min_b} {max_b}") if start > max_b or end < min_b: return None, None new_start = start new_end = end if start < min_b: new_start = min_b if end > max_b: new_end = max_b return new_start, new_end def get_window_bounds(happies, happy_count, happy_window_ind): return (happies[happy_window_ind - 1] if happy_window_ind > 0 else 0, happies[happy_window_ind], happies[happy_window_ind + happy_count - 1], (happies[happy_window_ind + happy_count] if happy_window_ind + happy_count < len(happies) else 10 ** 9 + 1 ) ) def get_good_segm_count_ordered( lower_start, lower_end, upper_start, upper_end, prev_happy, this_happy, last_happy, next_happy): (lower_bound_start, lower_bound_end) = clamp_segment( lower_start, lower_end, prev_happy + 1, this_happy ) (upper_bound_start, upper_bound_end) = clamp_segment( upper_start, upper_end, last_happy, next_happy - 1 ) if lower_bound_start is None or upper_bound_start is None: return 0 return (lower_bound_end - lower_bound_start + 1) *\ (upper_bound_end - upper_bound_start + 1) def get_good_segm_count(happies, happy_count, happy_window_ind, start_1, end_1, start_2, end_2): prev_happy, this_happy, last_happy, next_happy = get_window_bounds( happies, happy_count, happy_window_ind ) first_is_lower_count = get_good_segm_count_ordered( start_1, end_1, start_2, end_2, prev_happy, this_happy, last_happy, next_happy ) second_is_lower_count = get_good_segm_count_ordered( start_2, end_2, start_1, end_1, prev_happy, this_happy, last_happy, next_happy ) this_happy = happies[happy_window_ind] if (happy_count == 1 and start_1 <= this_happy <= end_2 and start_2 <= this_happy <= end_2): second_is_lower_count -= 1 return first_is_lower_count + second_is_lower_count def main(args=None): if args is None: p_start, p_end, v_start, v_end, happy_count = map(int, input().split()) else: p_start, p_end, v_start, v_end, happy_count = args happies = generate_happy() good_segments_count = 0 for happy_window_ind in range(len(happies) - happy_count + 1): good_segments_count += get_good_segm_count( happies, happy_count, happy_window_ind, p_start, p_end, v_start, v_end ) # print(get_good_segm_count( # happies, happy_count, happy_window_ind, # p_start, p_end, v_start, v_end # ), happy_window_ind) all_segments_count = (p_end - p_start + 1) * (v_end - v_start + 1) return good_segments_count / all_segments_count print(main()) ```
14,190
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Tags: brute force, probabilities Correct Solution: ``` import itertools as it all_lucky = [] for length in range(1, 10): for comb in it.product(['7', '4'], repeat=length): all_lucky += [int(''.join(comb))] all_lucky.sort() # print(len(all_lucky)) pl, pr, vl, vr, k = map(int, input().split()) result = 0 def inters_len(a, b, c, d): a, b = sorted([a, b]) c, d = sorted([c, d]) return (max([a, c]), min([b, d])) def check_for_intervals(pl, pr, vl, vr): global result for i in range(1, len(all_lucky) - k): le, re = i, i + k - 1 a, b = inters_len(all_lucky[le - 1] + 1, all_lucky[le], pl, pr) left_len = max([0, b - a + 1]) c, d = inters_len(all_lucky[re], all_lucky[re + 1] - 1, vl, vr) right_len = max([0, d - c + 1]) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) if b == c and left_len * right_len > 1e-6: result -= (1 / (pr - pl + 1) / (vr - vl + 1)) / 2 a, b = inters_len(0, all_lucky[0], pl, pr) left_len = max([0, b - a + 1]) c, d = inters_len(all_lucky[k - 1], all_lucky[k] - 1, vl, vr) right_len = max([0, d - c + 1]) #print((left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1))) #print(left_len, right_len) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) if b == c and left_len * right_len > 1e-6: result -= (1 / (pr - pl + 1) / (vr - vl + 1)) / 2 a, b = inters_len(all_lucky[-(k + 1)] + 1, all_lucky[-k], pl, pr) left_len = max([0, b - a + 1]) c, d = inters_len(all_lucky[-1], 10**9, vl, vr) right_len = max([0, d - c + 1]) #print((left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1))) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) if b == c and left_len * right_len > 1e-6: result -= (1 / (pr - pl + 1) / (vr - vl + 1)) / 2 #print(all_lucky[:5]) if k != len(all_lucky): check_for_intervals(pl, pr, vl, vr) check_for_intervals(vl, vr, pl, pr) else: a, b = inters_len(0, all_lucky[0], pl, pr) left_len = max([0, b - a + 1]) c, d = inters_len(all_lucky[-1], 10**9, vl, vr) right_len = max([0, d - c + 1]) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) if b == c and left_len * right_len > 1e-6: result -= (1 / (pr - pl + 1) / (vr - vl + 1)) / 2 a, b = inters_len(0, all_lucky[0], vl, vr) left_len = max([0, b - a + 1]) c, d = inters_len(all_lucky[-1], 10**9, pl, pr) right_len = max([0, d - c + 1]) result += (left_len / (vr - vl + 1)) * (right_len / (pr - pl + 1)) if b == c and left_len * right_len > 1e-6: result -= (1 / (pr - pl + 1) / (vr - vl + 1)) / 2 print(result) # Made By Mostafa_Khaled ```
14,191
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Tags: brute force, probabilities Correct Solution: ``` #!/usr/bin/env python3 import itertools pl,pr,vl,vr,k = map(int, input().split()) l = min(pl, vl) r = max(pr, vr) # Generate all lucky numbers with the appropriate number of digits # O(3**max_nr_digits) = O(3**9) < 20000 max_nr_digits = len(str(r)) lucky_numbers = [] for nr_digits in range(1,max_nr_digits+1): lucky_numbers.extend(int(''.join(i)) for i in itertools.product("47", repeat=nr_digits)) # Filter so we only have lucky numbers that can be in the intervals lucky_numbers = [nr for nr in lucky_numbers if (l <= nr) and (nr <= r)] lucky_numbers.sort() if len(lucky_numbers) < k: print("%.12f" % 0.0) exit() lucky_numbers.insert(0,l-1) lucky_numbers.append(r+1) total_pairs = (pr-pl+1) * (vr-vl+1) good_pairs = 0 for start_index in range(len(lucky_numbers)-k-1): (a,b) = lucky_numbers[start_index], lucky_numbers[start_index+1] (c,d) = lucky_numbers[start_index+k], lucky_numbers[start_index+k+1] # A pair will have lucky_numbers[start_index+1 : start_index+1+k] in its interval when: # The smallest number of the pair is larger than a, but at most b # The largest number of the pair is at least c, but smaller than d good_pairs += max((min(b,pr)-max(a+1,pl)+1),0)*max((min(d-1,vr)-max(c,vl)+1),0) good_pairs += max((min(b,vr)-max(a+1,vl)+1),0)*max((min(d-1,pr)-max(c,pl)+1),0) if (b == c) and (pl <= b) and (b <= pr) and (vl <=b) and (b <= vr): # Prevent double counting of the pair (b,c) when b == c and both p and v can supply the value good_pairs -= 1 print("%.12f" % (good_pairs / total_pairs)) ```
14,192
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Tags: brute force, probabilities Correct Solution: ``` #!/usr/bin/env python3 vl, vr, pl, pr, k = map(int, input().split()) lucky = [4, 7] sz = 0 for i in range(1, 9): base = 10 ** i psz, sz = sz, len(lucky) for j in [4 * base, 7 * base]: for pos in range(psz, sz): lucky.append(j + lucky[pos]) ans = 0 for i in range(0, len(lucky)-k+1): ll, lr = 1 if i == 0 else lucky[i-1] + 1, lucky[i] rl, rr = lucky[i+k-1], 1_000_000_000 if i+k == len(lucky) else lucky[i+k] - 1 vc = max(0, min(vr, lr) - max(vl, ll) + 1) pc = max(0, min(pr, rr) - max(pl, rl) + 1) cnt1 = vc * pc vc = max(0, min(pr, lr) - max(pl, ll) + 1) pc = max(0, min(vr, rr) - max(vl, rl) + 1) cnt2 = vc * pc ans += cnt1 + cnt2 if k == 1 and cnt1 > 0 and cnt2 > 0: ans -= 1 print(ans / ((vr - vl + 1) * (pr - pl + 1))) ```
14,193
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Submitted Solution: ``` import time def happy(a, b): min_len = len(str(a)) max_len = len(str(b)) #count = 0 for length in range(min_len, max_len+1): for happy in get_happy(length): if happy < a: continue if happy > b: break #count += 1 yield happy def to_bin(n, length): v = bin(n)[2:] return '4'*(length - len(v))+v def get_happy(length): for i in range(2**length): v = to_bin(i, length) v = v.replace('0', '4') v = v.replace('1', '7') yield int(v) def calculate_num_in_zone(l1, l2, r1, r2, content, value): left_target = -1 right_target = -1 right_value = r1 left_value = l1 for index in range(len(content)): if index < len(content) - 1: if content[index] <= right_value < content[index+1]: right_target = index if content[index] <= left_value < content[index+1]: left_target = index else: if content[index] <= right_value: right_target = index if content[index] <= left_value: left_target = index if right_target - left_target + 1 < value: right_target += value - (right_target - left_target) if right_target >= len(content) or content[right_target] > r2: return 0 right_value = max(content[right_target], r1) if right_target - left_target > value: left_target += (right_target - left_target) - value if left_target >= len(content) or content[left_target] > l2: return 0 left_value = max(content[left_target], l1) left_finished = False right_finished = False count = 0 while not left_finished and not right_finished: next_left = l2 add_left = False if next_left == left_value and left_value in content: add_left = True elif next_left != left_value: add_left = left_value not in content if left_target + 1 < len(content) and l2 > content[left_target + 1]: next_left = content[left_target + 1] next_right = r2 if right_target + 1 < len(content) and r2 > content[right_target + 1]: next_right = content[right_target + 1] add_right = False if next_right == right_value and right_value in content: add_right = True elif next_right != right_value: add_right = next_right not in content left_number = next_left - left_value + (1 if add_left else 0) right_number = next_right - right_value + (1 if add_right else 0) add = left_number * right_number count += add left_target += 1 right_target += 1 left_value = next_left right_value = next_right if left_target >= len(content) or content[left_target] > l2: left_finished = True if right_target >= len(content) or content[right_target] > r2: right_finished = True return count if __name__ == "__main__": pl, pr, vl, vr, k = list(map(int, input().split())) a = (pr - pl + 1) * (vr - vl + 1) l1, l2, r1, r2 = pl, pr, vl, vr if r1 < l1: l1, l2, r1, r2 = vl, vr, pl, pr happy_in_left = list(happy(l1, l2)) happy_in_right = list(happy(r1, r2)) happy_in_total = list(happy(l1, max(r2, l2))) f = 0 if r1 >= l2: f = calculate_num_in_zone(l1, l2, r1, r2, happy_in_total, k) elif r1 < l2 <= r2: f = calculate_num_in_zone(l1, l2, r1, r2, happy_in_total, k) f += calculate_num_in_zone(r1, l2, r1, l2, happy_in_total, k) wrong = 0 if k == 1: for h1 in happy_in_left: if h1 in happy_in_right: wrong += 1 f -= wrong else: f = calculate_num_in_zone(l1, r2, r1, r2, happy_in_total, k) f += calculate_num_in_zone(r1, r2, r1, l2, happy_in_total, k) print(f/a) ``` No
14,194
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Submitted Solution: ``` def gen(n, cur): if n == 0: global arr arr.append(cur) else: gen(n - 1, cur + '4') gen(n - 1, cur + '7') def inter(x1, x2, a, b): left = max(x1, a) right = min(x2, b) if right < left: return 0 return right - left + 1 arr = [] for i in range(1, 11): gen(i, '') arr = [0] + sorted(list(map(int, arr))) pl, pr, vl, vr, k = map(int, input().split()) ans = 0 for start in range(1, len(arr) - k + 1): if arr[start + k - 1] > max(vr, pr): break if arr[start] < min(pl, vl): continue goleft = inter(pl, pr, arr[start - 1] + 1, arr[start]) goright = inter(vl, vr, arr[start + k - 1], arr[start + k] - 1) ans += goleft * goright goright = inter(vl, vr, arr[start - 1] + 1, arr[start]) goleft = inter(pl, pr, arr[start + k - 1], arr[start + k] - 1) ans += goleft * goright ans /= (pr - pl + 1) * (vr - vl + 1) print(ans) ``` No
14,195
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Submitted Solution: ``` import itertools as it all_lucky = [] for length in range(1, 9): for comb in it.product(['7', '4'], repeat=length): all_lucky += [int(''.join(comb))] all_lucky.sort() # print(len(all_lucky)) pl, pr, vl, vr, k = map(int, input().split()) result = 0 def inters_len(a, b, c, d): a, b = sorted([a, b]) c, d = sorted([c, d]) return max([0, (min([b, d]) - max([a, c]) + 1)]) def check_for_intervals(pl, pr, vl, vr): global result for i in range(1, len(all_lucky) - k): le, re = i, i + k - 1 left_len = inters_len(all_lucky[le - 1] + 1, all_lucky[le], pl, pr) right_len = inters_len(all_lucky[re], all_lucky[re + 1] - 1, vl, vr) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) left_len = inters_len(all_lucky[le - 1] + 1, all_lucky[le], vl, vr) right_len = inters_len(all_lucky[re], all_lucky[re + 1] - 1, pl, pr) result += (left_len / (vr - vl + 1)) * (right_len / (pr - pl + 1)) left_len = inters_len(0, all_lucky[0], pl, pr) right_len = inters_len(all_lucky[k - 1], all_lucky[k] - 1, vl, vr) #print((left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1))) #print(left_len, right_len) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) left_len = inters_len(0, all_lucky[0], vl, vr) right_len = inters_len(all_lucky[k - 1], all_lucky[k] - 1, pl, pr) #print((left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1))) #print(left_len, right_len) result += (left_len / (vr - vl + 1)) * (right_len / (pr - pl + 1)) left_len = inters_len(all_lucky[-(k + 1)] + 1, all_lucky[-k], pl, pr) right_len = inters_len(all_lucky[-1], 10**9, vl, vr) #print((left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1))) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) left_len = inters_len(all_lucky[-(k + 1)] + 1, all_lucky[-k], vl, vr) right_len = inters_len(all_lucky[-1], 10**9, pl, pr) #print((left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1))) result += (left_len / (vr - vl + 1)) * (right_len / (pr - pl + 1)) #print(all_lucky[:5]) if k != len(all_lucky): check_for_intervals(pl, pr, vl, vr) else: left_len = inters_len(0, all_lucky[0], pl, pr) right_len = inters_len(all_lucky[-1], 10**9, vl, vr) result += (left_len / (pr - pl + 1)) * (right_len / (vr - vl + 1)) left_len = inters_len(0, all_lucky[0], vl, vr) right_len = inters_len(all_lucky[-1], 10**9, pl, pr) result += (left_len / (vr - vl + 1)) * (right_len / (pr - pl + 1)) print(result) ``` No
14,196
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers. Input The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000). Output On the single line print the result with an absolute error of no more than 10 - 9. Examples Input 1 10 1 10 2 Output 0.320000000000 Input 5 6 8 10 1 Output 1.000000000000 Note Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. Submitted Solution: ``` import time def happy(a, b): min_len = len(str(a)) max_len = len(str(b)) #count = 0 for length in range(min_len, max_len+1): for happy in get_happy(length): if happy < a: continue if happy > b: break #count += 1 yield happy def to_bin(n, length): v = bin(n)[2:] return '4'*(length - len(v))+v def get_happy(length): for i in range(2**length): v = to_bin(i, length) v = v.replace('0', '4') v = v.replace('1', '7') yield int(v) def calculate_num_in_zone(l1, l2, r1, r2, content, value): left_target = 0 right_target = 0 right_value = r1 left_value = l1 l1_in_content = False r2_in_content = False for index in range(len(content)): if index < len(content) - 1: if content[index] <= right_value < content[index+1]: right_target = index if content[index] <= left_value < content[index+1]: left_target = index else: if content[index] <= right_value: right_target = index if content[index] <= left_value: left_target = index if l1 == content[index]: l1_in_content = True if r2 == content[index]: r2_in_content = True if right_target - left_target + 1 < value: right_target += value - (right_target - left_target) if right_target >= len(content) or content[right_target] > r2: return 0 right_value = max(content[right_target], r1) if right_target - left_target + 1 > value: left_target += (right_target - left_target) - value if left_target >= len(content) or content[left_target] > l2: return 0 left_value = max(content[left_target], l1) left_finished = False right_finished = False count = 0 while not left_finished and not right_finished: next_left = l2 add_left = (left_value == l1 and not l1_in_content) or next_left == left_value if left_target + 1 < len(content) and l2 > content[left_target + 1]: next_left = content[left_target + 1] next_right = r2 if right_target + 1 < len(content) and r2 > content[right_target + 1]: next_right = content[right_target + 1] add_right = (next_right == r2 and not r2_in_content) or next_right == right_value left_number = next_left - left_value + (1 if add_left else 0) right_number = next_right - right_value + (1 if add_right else 0) add = left_number * right_number count += add left_target += 1 right_target += 1 left_value = next_left right_value = next_right if left_target >= len(content) or content[left_target] > l2: left_finished = True if right_target >= len(content) or content[right_target] > r2: right_finished = True return count if __name__ == "__main__": pl, pr, vl, vr, k = list(map(int, input().split())) a = (pr - pl + 1) * (vr - vl + 1) l1, l2, r1, r2 = pl, pr, vl, vr if r1 < l1: l1, l2, r1, r2 = vl, vr, pl, pr happy_in_left = list(happy(l1, l2)) happy_in_right = list(happy(r1, r2)) happy_in_total = list(happy(l1, max(r2, l2))) f = 0 if r1 >= l2: f = calculate_num_in_zone(l1, l2, r1, r2, happy_in_total, k) elif r1 < l2 <= r2: f = calculate_num_in_zone(l1, l2, r1, r2, happy_in_total, k) f += calculate_num_in_zone(r1, l2, r1, l2, happy_in_total, k) else: f = calculate_num_in_zone(l1, r2, r1, r2, happy_in_total, k) f += calculate_num_in_zone(r1, r2, r1, l2, happy_in_total, k) print(f/a) ``` No
14,197
Provide tags and a correct Python 3 solution for this coding contest problem. Petya got interested in grammar on his third year in school. He invented his own language called Petya's. Petya wanted to create a maximally simple language that would be enough to chat with friends, that's why all the language's grammar can be described with the following set of rules: * There are three parts of speech: the adjective, the noun, the verb. Each word in his language is an adjective, noun or verb. * There are two genders: masculine and feminine. Each word in his language has gender either masculine or feminine. * Masculine adjectives end with -lios, and feminine adjectives end with -liala. * Masculine nouns end with -etr, and feminime nouns end with -etra. * Masculine verbs end with -initis, and feminime verbs end with -inites. * Thus, each word in the Petya's language has one of the six endings, given above. There are no other endings in Petya's language. * It is accepted that the whole word consists of an ending. That is, words "lios", "liala", "etr" and so on belong to the Petya's language. * There aren't any punctuation marks, grammatical tenses, singular/plural forms or other language complications. * A sentence is either exactly one valid language word or exactly one statement. Statement is any sequence of the Petya's language, that satisfy both conditions: * Words in statement follow in the following order (from the left to the right): zero or more adjectives followed by exactly one noun followed by zero or more verbs. * All words in the statement should have the same gender. After Petya's friend Vasya wrote instant messenger (an instant messaging program) that supported the Petya's language, Petya wanted to add spelling and grammar checking to the program. As Vasya was in the country and Petya didn't feel like waiting, he asked you to help him with this problem. Your task is to define by a given sequence of words, whether it is true that the given text represents exactly one sentence in Petya's language. Input The first line contains one or more words consisting of lowercase Latin letters. The overall number of characters (including letters and spaces) does not exceed 105. It is guaranteed that any two consecutive words are separated by exactly one space and the input data do not contain any other spaces. It is possible that given words do not belong to the Petya's language. Output If some word of the given text does not belong to the Petya's language or if the text contains more that one sentence, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes). Examples Input petr Output YES Input etis atis animatis etis atis amatis Output NO Input nataliala kataliala vetra feinites Output YES Tags: implementation, strings Correct Solution: ``` import re al = re.compile(r'^1*23*$') def getType(word): if word.endswith("lios"): return 1 elif word.endswith("liala"): return -1 elif word.endswith("etr"): return 2 elif word.endswith("etra"): return -2 elif word.endswith("initis"):return 3 elif word.endswith("inites"): return -3 else: return 0 words = input().strip().split() words = [getType(x) for x in words] if len(words) == 1: if words[0] != 0:print("YES") else:print("NO") else: p = words[0] for x in words: if p*x <= 0: print("NO") exit() words = [str(abs(x)) for x in words] words = "".join(words) if al.match(words):print("YES") else:print("NO") # Made By Mostafa_Khaled ```
14,198
Provide tags and a correct Python 3 solution for this coding contest problem. Petya got interested in grammar on his third year in school. He invented his own language called Petya's. Petya wanted to create a maximally simple language that would be enough to chat with friends, that's why all the language's grammar can be described with the following set of rules: * There are three parts of speech: the adjective, the noun, the verb. Each word in his language is an adjective, noun or verb. * There are two genders: masculine and feminine. Each word in his language has gender either masculine or feminine. * Masculine adjectives end with -lios, and feminine adjectives end with -liala. * Masculine nouns end with -etr, and feminime nouns end with -etra. * Masculine verbs end with -initis, and feminime verbs end with -inites. * Thus, each word in the Petya's language has one of the six endings, given above. There are no other endings in Petya's language. * It is accepted that the whole word consists of an ending. That is, words "lios", "liala", "etr" and so on belong to the Petya's language. * There aren't any punctuation marks, grammatical tenses, singular/plural forms or other language complications. * A sentence is either exactly one valid language word or exactly one statement. Statement is any sequence of the Petya's language, that satisfy both conditions: * Words in statement follow in the following order (from the left to the right): zero or more adjectives followed by exactly one noun followed by zero or more verbs. * All words in the statement should have the same gender. After Petya's friend Vasya wrote instant messenger (an instant messaging program) that supported the Petya's language, Petya wanted to add spelling and grammar checking to the program. As Vasya was in the country and Petya didn't feel like waiting, he asked you to help him with this problem. Your task is to define by a given sequence of words, whether it is true that the given text represents exactly one sentence in Petya's language. Input The first line contains one or more words consisting of lowercase Latin letters. The overall number of characters (including letters and spaces) does not exceed 105. It is guaranteed that any two consecutive words are separated by exactly one space and the input data do not contain any other spaces. It is possible that given words do not belong to the Petya's language. Output If some word of the given text does not belong to the Petya's language or if the text contains more that one sentence, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes). Examples Input petr Output YES Input etis atis animatis etis atis amatis Output NO Input nataliala kataliala vetra feinites Output YES Tags: implementation, strings Correct Solution: ``` def f(a): b=len(a) if b>=4 and a[-4:]=='lios': return [1,1] if b>=5 and a[-5:]=='liala': return [1,2] if b>=3 and a[-3:]=='etr': return [2,1] if b>=4 and a[-4:]=='etra': return [2,2] if b>=6 and a[-6:]=='initis': return [3,1] if b>=6 and a[-6:]=='inites': return [3,2] return -1 a=[f(i) for i in input().split()] n=len(a) if -1 in a or [i[1] for i in a]!=[a[0][1]]*n: print('NO') else: i,j=0,n-1 while i<n and a[i][0]==1: i+=1 while j>=0 and a[j][0]==3: j-=1 print('YES' if i==j or n==1 else 'NO') ```
14,199