text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In one school with Vasya there is a student Kostya. Kostya does not like physics, he likes different online games. Every day, having come home, Kostya throws his bag in the farthest corner and sits down at his beloved computer. Kostya even eats glued to the game. A few days ago Kostya bought a new RPG game "HaresButtle", which differs from all other games in this genre. It has a huge number of artifacts. As we know, artifacts are divided into basic and composite ones. Only the basic artifacts are available on sale. More powerful composite artifacts are collected from some number of basic artifacts. After the composing composite artifact, all the components disappear. Kostya is the head of the alliance, so he has to remember, what artifacts has not only himself, but also his allies. You must identify by sequence of artifacts purchased by Kostya and his allies, how many and which artifacts has been collected by each of them. It is believed that initially no one has any artifacts. Input The first line has 4 natural numbers: k (1 ≤ k ≤ 100) — the number of Kostya's allies, n (1 ≤ n ≤ 50) — the number of basic artifacts, m (0 ≤ m ≤ 50) — the number of composite artifacts, q (1 ≤ q ≤ 500) — the number of his friends' purchases. The following n lines contain the names of basic artifacts. After them m lines contain the descriptions of composite artifacts in the following format: <Art. Name>: <Art. №1> <Art. №1 Number>, <Art. №2> <Art. №2 Number>, ... <Art. №X> <Art. №Х Number> All the numbers are natural numbers not exceeding 100 (1 ≤ X ≤ n). The names of all artifacts are different, they are composed of lowercase Latin letters, and the length of each name is from 1 to 100 characters inclusive. All the words in the format of the description of a composite artifact are separated by exactly one space. It is guaranteed that all components of the new artifact are different and have already been met in the input data as the names of basic artifacts. Next, each of the following q lines is characterized by the number ai, the number of a friend who has bought the artifact (1 ≤ ai ≤ k), and the name of the purchased basic artifact. Let's assume that the backpacks of the heroes are infinitely large and any artifact bought later can fit in there. It is guaranteed that after the i-th purchase no more than one opportunity to collect the composite artifact appears. If such an opportunity arose, the hero must take advantage of it. Output The output file should consist of k blocks. The first line should contain number bi — the number of different artifacts the i-th ally has. Then the block should contain bi lines with the names of these artifacts and the number of these artifacts. At that the lines should be printed in accordance with the lexicographical order of the names of the artifacts. In each block all the artifacts must be different, and all the numbers except the bi should be positive. Examples Input 2 3 2 5 desolator refresher perseverance vanguard: desolator 1, refresher 1 maelstorm: perseverance 2 1 desolator 2 perseverance 1 refresher 2 desolator 2 perseverance Output 1 vanguard 1 2 desolator 1 maelstorm 1 Submitted Solution: ``` def solve(inv): for artifact, basics in composite.items(): flag = 1 while True: for basic in basics: if basic not in inv: flag -= 1 break elif inv[basic] < basics[basic]: flag -= 1 break if flag == 0: break #print(basics, inv) for basic in basics: inv[basic] -= basics[basic] if artifact not in inv: inv[artifact] = 1 else: inv[artifact] += 1 for i in list(inv): if inv[i] <= 0: del inv[i] print(len(inv)) for i, v in inv.items(): print(i, v) k, n, m, q = [int(i) for i in input().split()] basic = {} composite = {} for i in range(n): basic[input()] = 0 for i in range(m): string = input().split() composite[string[0][:-1]] = {} for j in range(1, len(string), 2): # checking if string has comma (like 1, instead of 1) composite[string[0][:-1]].update({string[j]: int(string[j+1]) if ',' not in string[j+1] else int(string[j+1][:-1])}) inv = [{} for i in range(k)] for i in range(q): ally, artifact = input().split() ally = int(ally)-1 if artifact not in inv[ally]: inv[ally][artifact] = 1 else: inv[ally][artifact] += 1 for i in range(k): solve(inv[i]) ``` No
104,100
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In one school with Vasya there is a student Kostya. Kostya does not like physics, he likes different online games. Every day, having come home, Kostya throws his bag in the farthest corner and sits down at his beloved computer. Kostya even eats glued to the game. A few days ago Kostya bought a new RPG game "HaresButtle", which differs from all other games in this genre. It has a huge number of artifacts. As we know, artifacts are divided into basic and composite ones. Only the basic artifacts are available on sale. More powerful composite artifacts are collected from some number of basic artifacts. After the composing composite artifact, all the components disappear. Kostya is the head of the alliance, so he has to remember, what artifacts has not only himself, but also his allies. You must identify by sequence of artifacts purchased by Kostya and his allies, how many and which artifacts has been collected by each of them. It is believed that initially no one has any artifacts. Input The first line has 4 natural numbers: k (1 ≤ k ≤ 100) — the number of Kostya's allies, n (1 ≤ n ≤ 50) — the number of basic artifacts, m (0 ≤ m ≤ 50) — the number of composite artifacts, q (1 ≤ q ≤ 500) — the number of his friends' purchases. The following n lines contain the names of basic artifacts. After them m lines contain the descriptions of composite artifacts in the following format: <Art. Name>: <Art. №1> <Art. №1 Number>, <Art. №2> <Art. №2 Number>, ... <Art. №X> <Art. №Х Number> All the numbers are natural numbers not exceeding 100 (1 ≤ X ≤ n). The names of all artifacts are different, they are composed of lowercase Latin letters, and the length of each name is from 1 to 100 characters inclusive. All the words in the format of the description of a composite artifact are separated by exactly one space. It is guaranteed that all components of the new artifact are different and have already been met in the input data as the names of basic artifacts. Next, each of the following q lines is characterized by the number ai, the number of a friend who has bought the artifact (1 ≤ ai ≤ k), and the name of the purchased basic artifact. Let's assume that the backpacks of the heroes are infinitely large and any artifact bought later can fit in there. It is guaranteed that after the i-th purchase no more than one opportunity to collect the composite artifact appears. If such an opportunity arose, the hero must take advantage of it. Output The output file should consist of k blocks. The first line should contain number bi — the number of different artifacts the i-th ally has. Then the block should contain bi lines with the names of these artifacts and the number of these artifacts. At that the lines should be printed in accordance with the lexicographical order of the names of the artifacts. In each block all the artifacts must be different, and all the numbers except the bi should be positive. Examples Input 2 3 2 5 desolator refresher perseverance vanguard: desolator 1, refresher 1 maelstorm: perseverance 2 1 desolator 2 perseverance 1 refresher 2 desolator 2 perseverance Output 1 vanguard 1 2 desolator 1 maelstorm 1 Submitted Solution: ``` k,n,m,q=map(int,input().split(" ")); ba=[] for i in range(n): a=input() ba.append(a) composite={} for i in range(m): ak,a=map(str,input().split(": ")) composite[ak]={} if "," in a: l=[] a=list(a.split(", ")) for z in a: jab,jan=z.split(" "); for j in range(int(jan)): l.append(jab) composite[ak]=l else: jab,jan=a.split(" "); l=[] for j in range(int(jan)): l.append(jab) composite[ak]=l ally={} for i in range(k): ally[i+1]=[] l=[] for i in range(q): an,ab=map(str,input().split(" ")) ally[int(an)].append(ab) for i in ally: for j in composite: temp=0; for z in composite[j]: if z not in ally[i]: temp=1; if temp==0: for z in composite[j]: if z in ally[i]: ally[i].remove(z) ally[i].append(j) for i in range(1,k+1): print(i); ally[i].sort() for j in ally[i]: n=ally[i].count(j); print(j,n) ``` No
104,101
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In one school with Vasya there is a student Kostya. Kostya does not like physics, he likes different online games. Every day, having come home, Kostya throws his bag in the farthest corner and sits down at his beloved computer. Kostya even eats glued to the game. A few days ago Kostya bought a new RPG game "HaresButtle", which differs from all other games in this genre. It has a huge number of artifacts. As we know, artifacts are divided into basic and composite ones. Only the basic artifacts are available on sale. More powerful composite artifacts are collected from some number of basic artifacts. After the composing composite artifact, all the components disappear. Kostya is the head of the alliance, so he has to remember, what artifacts has not only himself, but also his allies. You must identify by sequence of artifacts purchased by Kostya and his allies, how many and which artifacts has been collected by each of them. It is believed that initially no one has any artifacts. Input The first line has 4 natural numbers: k (1 ≤ k ≤ 100) — the number of Kostya's allies, n (1 ≤ n ≤ 50) — the number of basic artifacts, m (0 ≤ m ≤ 50) — the number of composite artifacts, q (1 ≤ q ≤ 500) — the number of his friends' purchases. The following n lines contain the names of basic artifacts. After them m lines contain the descriptions of composite artifacts in the following format: <Art. Name>: <Art. №1> <Art. №1 Number>, <Art. №2> <Art. №2 Number>, ... <Art. №X> <Art. №Х Number> All the numbers are natural numbers not exceeding 100 (1 ≤ X ≤ n). The names of all artifacts are different, they are composed of lowercase Latin letters, and the length of each name is from 1 to 100 characters inclusive. All the words in the format of the description of a composite artifact are separated by exactly one space. It is guaranteed that all components of the new artifact are different and have already been met in the input data as the names of basic artifacts. Next, each of the following q lines is characterized by the number ai, the number of a friend who has bought the artifact (1 ≤ ai ≤ k), and the name of the purchased basic artifact. Let's assume that the backpacks of the heroes are infinitely large and any artifact bought later can fit in there. It is guaranteed that after the i-th purchase no more than one opportunity to collect the composite artifact appears. If such an opportunity arose, the hero must take advantage of it. Output The output file should consist of k blocks. The first line should contain number bi — the number of different artifacts the i-th ally has. Then the block should contain bi lines with the names of these artifacts and the number of these artifacts. At that the lines should be printed in accordance with the lexicographical order of the names of the artifacts. In each block all the artifacts must be different, and all the numbers except the bi should be positive. Examples Input 2 3 2 5 desolator refresher perseverance vanguard: desolator 1, refresher 1 maelstorm: perseverance 2 1 desolator 2 perseverance 1 refresher 2 desolator 2 perseverance Output 1 vanguard 1 2 desolator 1 maelstorm 1 Submitted Solution: ``` k,n,m,q=map(int,input().split(" ")); ba=[] for i in range(n): a=input() ba.append(a) composite={} for i in range(m): ak,a=map(str,input().split(": ")) composite[ak]={} if "," in a: l=[] a=list(a.split(", ")) for z in a: jab,jan=z.split(" "); for j in range(int(jan)): l.append(jab) composite[ak]=l else: jab,jan=a.split(" "); l=[] for j in range(int(jan)): l.append(jab) composite[ak]=l ally={} for i in range(k): ally[i+1]=[] l=[] for i in range(q): l=list(input().split(" ")) if '\r' in l[1]: l[1]=l[1].split('\r')[0] ally[int(l[0])].append(l[1]) print(ally) for i in ally: for j in composite: temp=0; for z in composite[j]: if z not in ally[i]: temp=1; if temp==0: for z in composite[j]: if z in ally[i]: ally[i].remove(z) ally[i].append(j) for i in range(1,k+1): print(i); ally[i].sort() l=set() for j in ally[i]: n=ally[i].count(j); l.add((j,n)) for z in l: print(z[0],z[1]) ``` No
104,102
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Tags: dfs and similar, math Correct Solution: ``` from fractions import gcd def nok(a,b): return a*b//gcd(a,b) n=int(input()) a=[0]+[int(i) for i in input().split()] ans=1 for i in range(1,1+n): if a[i]!=i: now=i res=-1 for j in range(n): now=a[now] if now==i: res=j+1 if res%2==0: res//=2 ans=nok(ans,res) break else: ans=-1 break print(ans) ```
104,103
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Tags: dfs and similar, math Correct Solution: ``` def gcd(a, b): while b != 0: a, b = b, a % b return a n = int(input()) crash = list(map(int, input().split())) s = set() t = 1 for i in range(n): counter = 1 j = crash[i] - 1 while j != i and counter <= n: counter += 1 j = crash[j] - 1 if counter > n: t = -1 break else: #print(counter) if counter % 2 == 0: counter //= 2 t = (t * counter) // gcd(t, counter) print(t) ```
104,104
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Tags: dfs and similar, math Correct Solution: ``` n = int(input()) adj = [int(x) - 1 for x in input().split()] ans = 1 from fractions import gcd lcm = lambda x, y : (x * y) // gcd(x, y) for i in range(n): visited = [0] * n visited[i] = 1 j = adj[i] while visited[j] == 0: visited[j] = 1 j = adj[j] if i != j: print(-1) exit() else: sm = sum(visited) if sm & 1: ans = lcm(ans, sum(visited)) else: ans = lcm(ans, sm // 2) print(ans) ```
104,105
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Tags: dfs and similar, math Correct Solution: ``` def gcd(a, b): if b == 0: return a return gcd(b, a % b) def solve(): n = int(input()) arr = list(map(int, input().split())) memarr = [] for i in range(n): arr[i] -= 1 for i in range(n): if arr[i] == -1: continue mem = 1 nx = arr[i] arr[i] = -1 while(arr[nx] != -1): t = arr[nx] arr[nx] = -1 nx = t mem += 1 if(nx == i): memarr.append(mem) else: print(-1) return for i in range(len(memarr)): if memarr[i] % 2 == 0: memarr[i] //= 2 res = 1 for i in memarr: res = res * i // gcd(res, i) print(res) solve() ```
104,106
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Tags: dfs and similar, math Correct Solution: ``` def gcd(a, b): if b == 0: return a return gcd(b, a % b) n = int(input()) a = list(map(int, input().split())) for i in range(len(a)): a[i] -= 1 lens = [] for i in range(len(a)): now = i l = 0 for j in range(3 * n): l += 1 now = a[now] if now == i: break if l == 3 * n: print(-1) exit(0) if l % 2 == 0: l //= 2 lens.append(l) res = lens[0] for i in range(1, len(lens)): res = res * lens[i] // gcd(res, lens[i]) print(res) ```
104,107
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Tags: dfs and similar, math Correct Solution: ``` import sys, math n, c = int(input()), list(map(int, input().split())) n += 1 c.insert(0, 0) done, t = [False] * n, [] for i in range(1, n): a, ct = i, 0 while not done[a]: done[a] = True ct, a = ct + 1, c[a] if a is not i: print(-1) sys.exit(0) if ct > 0: if ct & 1 is 0: ct //= 2 t.append(int(ct)) m = t[0] for i in range(1, len(t)): g = math.gcd(m, t[i]) m = m * t[i] // g print(m) ```
104,108
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Tags: dfs and similar, math Correct Solution: ``` def gcd(a,b): while b: a,b=b,a%b return a def lcm(arr): if len(arr)==1: return arr[0] else: a=arr.pop() b=arr.pop() arr.append(a*b//gcd(a,b)) return lcm(arr) n=int(input()) lv=[(int(z)-1) for z in input().split()] tbl=set(lv) if len(tbl)==n: cntlcm=[] vv=[0]*n i=0 while i<n: if vv[i]: i+=1 else: k=i cnt=0 while not vv[k]: cnt+=1 vv[k]=1 k=lv[k] if cnt%2: cntlcm.append(cnt) else: cntlcm.append(cnt//2) print(lcm(cntlcm)) else: print(-1) ```
104,109
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Tags: dfs and similar, math Correct Solution: ``` def make_function(n, crushes): def f(original_node): node = original_node x = 0 for i in range(n): node = crushes[node] x += 1 if node == original_node: break else: return -1 return x / 2 if x % 2 == 0 else x return f def lcd(numbers): gcd = lambda x, y: int(x) if y == 0 else int(gcd(y, x % y)) ret = 1 for number in numbers: ret = ret * number / gcd(ret, number) return ret def main(): n = int(input()) a = map(int, input().split()) crushes = {i: x for i, x in zip(range(1, n+1), a)} f = make_function(n, crushes) numbers = list(map(int, map(f, crushes))) if -1 in numbers: print (-1) return print (int(lcd(numbers))) main() ```
104,110
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Submitted Solution: ``` import sys, math n, c = int(input()), list(map(int, input().split())) n += 1 c.insert(0, 0) done, t = [False] * n, [] for i in range(1, n): a = i ct = 0 while not done[a]: ct += 1 done[a] = True a = c[a] if a is not i: print(-1) sys.exit(0) if ct > 0: if ct & 1 is 0: ct /= 2 t.append(int(ct)) #print(i, ct) g, m = t[0], t[0] for i in range(1, len(t)): g = math.gcd(m, t[i]) m = m * t[i] // g #print(g, m) print(m) ``` Yes
104,111
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Submitted Solution: ``` #from collections import deque from functools import reduce n = int(input()) crush = [int(i) - 1 for i in input().split()] def parity_treat(n): if n%2 == 0: return n//2 else: return n def gcd(a,b): while b: a, b = b, a%b return a def lcm(a,b): return a * b // gcd(a,b) def lcmm(*args): return reduce(lcm, args) if len(set(crush)) < n: print(-1) else: component_size = [] visited = set() for i in range(n): if i not in visited: tmp = 1 start = i visited.add(start) j = crush[start] while j != start: visited.add(j) j = crush[j] tmp+=1 component_size.append(tmp) component_size = [parity_treat(i) for i in component_size] print(lcmm(*component_size)) ``` Yes
104,112
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Submitted Solution: ``` def gcd(a, b): while b: a, b = b, a % b return a def lcm(a, b): return a * b // gcd(a, b) n = int(input()) a = list(map(int, input().split())) if sorted(a) != [i + 1 for i in range(n)]: print(-1) else: ans = 1 used = [0 for i in range(n)] for i in range(n): if used[i] == 0: j = i am = 0 while used[j] == 0: am += 1 used[j] = 1 j = a[j] - 1 if am % 2: ans = lcm(ans, am) else: ans = lcm(ans, am // 2) print(ans) ``` Yes
104,113
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Submitted Solution: ``` input() crush = [0] + [int(x) for x in input().split()] visited = set() circle_sizes = [] def gcd(a, b): return a if b == 0 else gcd(b, a%b) def lcm(a, b): return a * b // gcd(a, b) def solve(): for i in range(len(crush)): if i not in visited: start, cur, count = i, i, 0 while cur not in visited: visited.add(cur) count += 1 cur = crush[cur] if cur != start: return -1 circle_sizes.append(count if count % 2 else count // 2) if len(circle_sizes) == 1: return circle_sizes[0] ans = lcm(circle_sizes[0], circle_sizes[1]) for size in circle_sizes[2:]: ans = lcm(ans, size) return ans print(solve()) ``` Yes
104,114
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Submitted Solution: ``` def gcd(a, b): while (b != 0): a, b = b, a % b return a n = int(input()) crush = list(map(int, input().split())) for i in range(n): crush[i] -= 1 ans = True s = set() for i in range(n): j = crush[i] x = i #now = i, crush[i] = where #print(j, end = ' ') c = 0 while (x != j): #print(j, end = ' ') if j != crush[j]: j = crush[j] else: break c += 1 if c: ans = False #print() if c > 1: c += 1 s.add(c) if ans: print(-1) else: if len(s) > 0: nod = s.pop() m = nod for elem in s: m *= elem nod = gcd(elem, nod) #print('ans') print(m // nod) else: print(1) ``` No
104,115
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Submitted Solution: ``` def gcd(a, b): while b != 0: a, b = b, a % b return a n = int(input()) crash = list(map(int, input().split())) s = set() t2 = 0 t = -1 for i in range(n): counter = 1 j = crash[i] - 1 while j != i and counter <= n: counter += 1 j = crash[j] - 1 if counter > n: t = -1 break else: #print(counter) if counter == 2: t = max(t, counter - 1) t2 = 2 else: s.add(counter) #print(s) t = max(t, counter) #print(s) if t == -1 or t == 1: print(t) else: a = s.pop() a1 = a nod = 1 for elem in s: a1 *= elem nod = gcd(a, elem) a = nod a1 = a1//nod if t2 != 0 and a1 % 2 == 0: print(a1 * 2) else: print(a1) ``` No
104,116
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Submitted Solution: ``` #!/usr/bin/env python3 def main(): import fractions def lcm(x, y): return x // fractions.gcd(x, y) * y try: while True: n = int(input()) a = list(map(int, input().split())) t = [[-1] * n for i in range(n)] for i in range(n): for trg in range(n): cur = i used = set() elapsed = 1 while cur != trg: if cur in used: break used.add(cur) cur = a[cur] - 1 elapsed += 1 else: t[i][trg] = elapsed def print_result(): result = 1 for i in range(n): for j in range(n): if (t[i][j] == -1) != (t[j][i] == -1): print(-1) return result = lcm(result, lcm(t[i][j], t[j][i])) print(result >> 1) print_result() except EOFError: pass main() ``` No
104,117
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) for i in range(len(a)): a[i] -= 1 def check(num): colour = [ 0 for i in range(n)] for i in range(len(a)): if not colour[i]: pos = i for j in range(num): pos = a[pos] if pospath(pos, i, num): colour[i], colour[pos] = 1, 1 res = 1 for i in range(len(colour)): res *= colour[i] return res == 1 def pospath(fr, to, length): k = fr for i in range(length): k = a[k] return to == k for i in range(1, 101): if check(i): print(i) exit() print(-1) ``` No
104,118
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. If you have gone that far, you'll probably skip unnecessary legends anyway... You are given a binary string <image> and an integer <image>. Find the number of integers k, 0 ≤ k < N, such that for all i = 0, 1, ..., m - 1 <image> Print the answer modulo 109 + 7. Input In the first line of input there is a string s consisting of 0's and 1's (1 ≤ |s| ≤ 40). In the next line of input there is an integer n (1 ≤ n ≤ 5·105). Each of the next n lines contains two space-separated integers pi, αi (1 ≤ pi, αi ≤ 109, pi is prime). All pi are distinct. Output A single integer — the answer to the problem. Examples Input 1 2 2 1 3 1 Output 2 Input 01 2 3 2 5 1 Output 15 Input 1011 1 3 1000000000 Output 411979884 Submitted Solution: ``` def gcd(x, y): if y == 0: return x else: return gcd(y, x%y) s = input() n = int(input()) N = 1 for i in range(n): l = input().split(" ") N *= int(l[0])**int(l[1]) k = [] for char in range(len(s)): if s[char] == '1': k.append(char) count = 0 for j in range(N): for i in range(len(k)): if gcd(k[i] + j, N) == 1: count += 1 print(count) ``` No
104,119
Provide tags and a correct Python 3 solution for this coding contest problem. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Tags: implementation, math Correct Solution: ``` n,k=list(map(int,input().split())) p=list(map(int,input().split())) tad=k tom=(k-1)+0.5 lol=sum(p) s=0 for x in range(55555555): if (lol/n)>=tom: print(s) break else: n+=1 if ((lol+k)/(n))<tom: k+=tad s+=1 else: print(s+1) break ```
104,120
Provide tags and a correct Python 3 solution for this coding contest problem. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Tags: implementation, math Correct Solution: ``` a,b=map(int,input().split()) c=list(map(int,input().split())) i=0 d=0 j=len(c) for i in range(j): d+=c[i] p=((b-0.5)*a-d)*2 if(p<0): print(0) else: print(int(p)) ```
104,121
Provide tags and a correct Python 3 solution for this coding contest problem. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Tags: implementation, math Correct Solution: ``` n, k = map(int, input().split()) m = list(map(int, input().split())) def av(m): return sum(m)/len(m) s = sum(m) def ro(x): xx = int(x) if x - xx >= 0.5: return xx + 1 else: return xx pren = n while s/n < k - 0.5: s+= k n+=1 print(n - pren) ```
104,122
Provide tags and a correct Python 3 solution for this coding contest problem. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Tags: implementation, math Correct Solution: ``` from math import ceil n,k=map(int,input().split()) li=list(map(int,input().split())) count=0 count2=0 def round1(n): k=int(n) b=n-k if b>=0.5: return ceil(n) return k while round1(sum(li)/len(li))!=k: li.append(k) count+=1 print((count)) ```
104,123
Provide tags and a correct Python 3 solution for this coding contest problem. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Tags: implementation, math Correct Solution: ``` n,k = map(int, input().split()) a = list(map(int, input().split())) sumA = sum(a) res = 0 while round(sumA / n + 1e-13) < k: sumA += k n += 1 res += 1 print(res) ```
104,124
Provide tags and a correct Python 3 solution for this coding contest problem. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Tags: implementation, math Correct Solution: ``` n,m = [int(i) for i in input().split()] l = [int(i) for i in input().split()] avg = sum(l) t = 0 while avg < (t+n)*(m-0.5) : avg = avg+m t+=1 print(t) ```
104,125
Provide tags and a correct Python 3 solution for this coding contest problem. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Tags: implementation, math Correct Solution: ``` import math n, k = map(int, input().split()) s = sum(map(int, input().split())) print(max(0, (2 * k - 1) * n - 2 * s)) ```
104,126
Provide tags and a correct Python 3 solution for this coding contest problem. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Tags: implementation, math Correct Solution: ``` x,y=map(int,input().split()) a=list(map(int,input().split())) n=0 d=0 for i in range(x): n=n+a[i] while round((n+0.01)/x)!=y: n=n+y x=x+1 d+=1 print(d) ```
104,127
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Submitted Solution: ``` import sys def solve(): n, k = map(int, input().split()) a = [int(i) for i in input().split()] tot = sum(a) cnt = 0 while int(tot / n + 0.5) < k: tot += k n += 1 cnt += 1 print(cnt) if __name__ == '__main__': solve() ``` Yes
104,128
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Submitted Solution: ``` #810A n, k = map(int, input().split()) a = list(map(int, input().split())) ans = sum(a)/len(a) count = 0 while ans < k-0.5: a.append(k) ans = sum(a)/len(a) count += 1 print(count) ``` Yes
104,129
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Submitted Solution: ``` import math n, k = map(int, input().split()) a = list(map(int, input().split())) s = sum(a) answer = max(0, int(math.ceil(2 * n * k - 2 * s - n))) print(answer) ``` Yes
104,130
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Submitted Solution: ``` n, k = map(int, input().split()) s = sum(map(int, input().split())) left = -1 right = 10 ** 100 while right - left > 1: mid = (left + right) // 2 whole = s + k * mid if whole * 2 // (mid + n) < 2 * k - 1: left = mid else: right = mid print(right) ``` Yes
104,131
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Submitted Solution: ``` import math n, k = map(int, input().split()) daf = list(map(int, input().split())) jum = sum(daf) has = (n * (k - 0.5) - jum) / 0.5 has = math.ceil(has) print(has) ``` No
104,132
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Submitted Solution: ``` x,y=map(int,input().split()) list1=list(map(int,input().split())) z=0 for i in list1: z+=(2*y-2*i-1) print(z) ``` No
104,133
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Submitted Solution: ``` from functools import reduce import math nk=input().split() n=int(nk[0]) k=int(nk[1]) L=list(map(int,input().split())) s=reduce(lambda x,y:x+y,L) if(round(s)==k): print(0) else: z=2*n*k-2*s-n print(z) ``` No
104,134
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8. For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark k. Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k. Input The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ k) denoting marks received by Noora before Leha's hack. Output Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k. Examples Input 2 10 8 9 Output 4 Input 3 5 4 4 4 Output 3 Note Consider the first example testcase. Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. Submitted Solution: ``` n,k=map(int,input().split(' ')) m=list(map(int,input().split(' '))) print(int((n*k-sum(m))*2-n)) ``` No
104,135
Provide tags and a correct Python 3 solution for this coding contest problem. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Tags: brute force, dp, hashing, strings Correct Solution: ``` s = input() n = len(s) max_palin = [[0 for i in range(n + 1)] for j in range(n + 1)] count = [0 for i in range(n + 1)] for sub_len in range(1, n + 1): for left in range(0, n - sub_len + 1): right = left + sub_len - 1 if sub_len == 1: max_palin[left][right] = 1 elif sub_len == 2: if s[left] == s[right]: max_palin[left][right] = 2 else: max_palin[left][right] = 0 else: if s[left] == s[right] and max_palin[left + 1][right - 1] > 0: mid = (left + right) // 2 if sub_len % 2 == 0: max_palin[left][right] = max_palin[left][mid] + 1 else: max_palin[left][right] = max_palin[left][mid - 1] + 1 count[max_palin[left][right]] += 1 for i in range(n - 1, 0, -1): count[i] += count[i + 1] for i in range(1, n + 1): print(count[i], end=' ') print() ```
104,136
Provide tags and a correct Python 3 solution for this coding contest problem. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Tags: brute force, dp, hashing, strings Correct Solution: ``` class HString: def __init__(self, string, base=257, modulo=1000000007): self.__base, self.__modulo = base, modulo self.__prefix_hash, self.__base_pow, self.__size = [], [1], 0 self += string def __add__(self, string): for ch in string: self.__base_pow.append((self.__base_pow[-1] * self.__base) % self.__modulo) if self.__size == 0: self.__prefix_hash.append(ord(ch)) else: self.__prefix_hash.append((self.__prefix_hash[-1] * self.__base + ord(ch)) % self.__modulo) self.__size += 1 return self def size(self): return self.__size def getModulo(self): return self.__modulo def getHashValue(self, st, en): value = self.__prefix_hash[en] if st > 0: value -= ((self.__prefix_hash[st-1] * self.__base_pow[en-st+1]) % self.__modulo) if value < 0: value += self.__modulo return value def palindromic_characteristics(s): n, org, rev = len(s), HString(s), HString(s[::-1]) palindrome_level = [[0 for _ in range(n)] for _ in range(n)] palindrome_level_count = [0 for _ in range(n + 1)] for i in range(n): for j in range(i, n): if org.getHashValue(i, j) == rev.getHashValue(n-1-j, n-1-i): mid = (i + j) // 2 + (i + j) % 2 if i > mid-1: palindrome_level[i][j] = 1 else: palindrome_level[i][j] = palindrome_level[i][mid-1] + 1 palindrome_level_count[palindrome_level[i][j]] += 1 for i in range(n-1, 0, -1): palindrome_level_count[i] += palindrome_level_count[i+1] return palindrome_level_count[1:] s = input() print(' '.join(map(str, palindromic_characteristics(s)))) ```
104,137
Provide tags and a correct Python 3 solution for this coding contest problem. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Tags: brute force, dp, hashing, strings Correct Solution: ``` def PanlidromicCharacteristics(string): n = len(string) res = [[0 for i in range (n)] for j in range (n)] count = [0 for i in range (n + 1)] # for i in range (n): # res[i][i] = 1 # count[1] += 1 for length in range (1, n + 1): for i in range (n-length + 1): j = i + length - 1 if length == 1: res[i][j] = 1 elif length == 2 and string[i] == string[j]: res[i][j] = 2 elif string[i] == string[j] and res[i + 1][j - 1] > 0: res[i][j] = res[i][i + length//2 - 1] + 1 count[res[i][j]] += 1 # k-palindrome is also a (k - 1)-palindrome for i in range (len(count) - 1, 0, -1): count[i - 1] += count[i] for i in range (1, len(count)): print(count[i], end = " ") return string = input() PanlidromicCharacteristics(string) ```
104,138
Provide tags and a correct Python 3 solution for this coding contest problem. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Tags: brute force, dp, hashing, strings Correct Solution: ``` s = input() n = len(s) isPalindrome = [[False for i in range(n)] for i in range(n)] for i in range(n): isPalindrome[i][i] = True for i in range(n - 1, -1, -1): for j in range(i + 1, n): if (s[i] == s[j] and (i + 1 == j or isPalindrome[i + 1][j - 1] == True)): isPalindrome[i][j] = True degreePalindrome = [[0 for i in range(n)] for i in range(n)] #degreePalindrome[i][j] = degreePalindrome[i][mid] + 1 res = [0] * (n + 1) for i in range(n): for j in range(i, n): if (i == j): degreePalindrome[i][j] = 1 elif isPalindrome[i][j]: mid = (i + j - 1) // 2 degreePalindrome[i][j] = degreePalindrome[i][mid] + 1 res[degreePalindrome[i][j]] += 1 for i in range( n - 2, 0, -1): res[i] += res[i + 1] print(*res[1::]) ```
104,139
Provide tags and a correct Python 3 solution for this coding contest problem. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Tags: brute force, dp, hashing, strings Correct Solution: ``` s = input() n = len(s) dp = [[0 for i in range(n - le + 1)] for le in range(n + 1)] ans = [0 for i in range(n + 1)] for le in range(1, n + 1): for l in range(0, n - le + 1): r = l + le if s[l] != s[r - 1]: continue if le == 1: dp[1][l] = 1 ans[1] += 1 elif le == 2: ans[2] += 1 dp[2][l] = 2 elif dp[le - 2][l + 1]: v = 1 m = (l + r) // 2 st = m + 1 if le & 1 else m le2 = m - l q = dp[le2][l] if q: v = q + 1 ans[v] += 1 dp[le][l] = v for i in range(n - 1, 0, -1): ans[i] += ans[i + 1] print(*ans[1:]) ```
104,140
Provide tags and a correct Python 3 solution for this coding contest problem. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Tags: brute force, dp, hashing, strings Correct Solution: ``` s = input() size = len(s) dp = [[0 for l in range(size)] for li in range(size)] ans = [0]*(size+1) for i in range(1, size+1): if i == 1: for j in range(0, size): dp[j][j] = 1 ans[1] += 1 elif i == 2: for j in range(0, size-1): if s[j+1] == s[j]: dp[j][j+1] = 2 ans[1] += 1 ans[2] += 1 else: dp[j][j+1] = 0 else: for j in range(0, size-i+1): if s[j] != s[j+i-1] or dp[j+1][j+i-2] == 0: dp[j][j+i-1] = 0 else: dp[j][j+i-1] = dp[j][int((j+j+i-2)/2)] + 1 for p in range(1, dp[j][j+i-1]+1): ans[p] += 1 for i in range(1, size): print(ans[i], end="") print(" ", end="") print(ans[size]) ```
104,141
Provide tags and a correct Python 3 solution for this coding contest problem. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Tags: brute force, dp, hashing, strings Correct Solution: ``` def main(): s = input() n = len(s) isPalindrome = [[False for i in range(n + 1)] for i in range(n + 1)] for i in range(n): isPalindrome[i][i] = True for i in range(n - 1, -1, -1): for j in range(i + 1, n): if (s[i] == s[j] and (i + 1 == j or isPalindrome[i + 1][j - 1] == True)): isPalindrome[i][j] = True degreePalindrome = [[0 for i in range(n)] for i in range(n)] #degreePalindrome[i][j] = degreePalindrome[i][mid] + 1 res = [0] * (n + 1) for i in range(n): for j in range(i, n): if (i == j): degreePalindrome[i][j] = 1 elif isPalindrome[i][j]: mid = (i + j - 1) // 2 degreePalindrome[i][j] = degreePalindrome[i][mid] + 1 res[degreePalindrome[i][j]] += 1 for i in range( n - 2, 0, -1): res[i] += res[i + 1] print(*res[1::]) if __name__ == "__main__": main() ```
104,142
Provide tags and a correct Python 3 solution for this coding contest problem. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Tags: brute force, dp, hashing, strings Correct Solution: ``` s = input() dp = [[0]*5005 for _ in range(5005)] n = len(s) ans = [0 for _ in range(5005)] for length in range(1,n+1): for l in range(n-length+1): r = l+length if(length == 1): dp[l][r] = 1 continue elif(length == 2): dp[l][r] = 2 if(s[l] == s[r-1]) else 0 continue if(s[l] != s[r-1] or dp[l+1][r-1] == 0): continue dp[l][r] = 1 m = (l+r) // 2 if(length&1): if(dp[l][m] and dp[m+1][r]): dp[l][r] = dp[l][m]+1 else: if(dp[l][m] and dp[m][r]): dp[l][r] = dp[l][m]+1 for length in range(1,n+1): for l in range(n-length+1): ans[dp[l][l+length]] += 1 for i in range(n-1,0,-1): ans[i] += ans[i+1] for i in range(1,n+1): print(ans[i],end=' ') print() ```
104,143
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Submitted Solution: ``` class HString: def __init__(self, string, base=257, modulo=1000000007): self.__base, self.__modulo = base, modulo self.__prefix_hash, self.__base_pow, self.__size = [], [1], 0 self += string def __add__(self, string): for ch in string: self.__base_pow.append((self.__base_pow[-1] * self.__base) % self.__modulo) if self.__size == 0: self.__prefix_hash.append(ord(ch)) else: self.__prefix_hash.append((self.__prefix_hash[-1] * self.__base + ord(ch)) % self.__modulo) self.__size += 1 return self def size(self): return self.__size def getModulo(self): return self.__modulo def getHashValue(self, st, en): value = self.__prefix_hash[en] if st > 0: value -= ((self.__prefix_hash[st-1] * self.__base_pow[en-st+1]) % self.__modulo) if value < 0: value += self.__modulo return value def palindromic_characteristics(s): n, org, rev = len(s), HString(s), HString(s[::-1]) palindrome_level = [[0 for _ in range(n)] for _ in range(n)] palindrome_level_count = [0 for _ in range(n + 1)] i, j = 0, 0 while i < n: j = i while j < n: if org.getHashValue(i, j) == rev.getHashValue(n-1-j, n-1-i): mid = (i + j) // 2 + (i + j) % 2 if i > mid-1: palindrome_level[i][j] = 1 else: palindrome_level[i][j] = palindrome_level[i][mid-1] + 1 palindrome_level_count[palindrome_level[i][j]] += 1 j += 1 i += 1 for i in range(n-1, 0, -1): palindrome_level_count[i] += palindrome_level_count[i+1] return palindrome_level_count[1:] s = input() print(' '.join(map(str, palindromic_characteristics(s)))) ``` Yes
104,144
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Submitted Solution: ``` s = input() n = len(s) max_palin = [[0 for i in range(n + 1)] for j in range(n + 1)] count = [0 for i in range(n + 1)] for sub_len in range(1, n + 1): for left in range(0, n - sub_len + 1): right = left + sub_len - 1 if sub_len == 1: max_palin[left][right] = 1 elif sub_len == 2: if s[left] == s[right]: max_palin[left][right] = 2 else: max_palin[left][right] = 0 else: if s[left] == s[right] and max_palin[left + 1][right - 1] > 0: max_palin[left][right] = max_palin[left][left + sub_len // 2 - 1] + 1 count[max_palin[left][right]] += 1 for i in range(n - 1, 0, -1): count[i] += count[i + 1] for i in range(1, n + 1): print(count[i], end=' ') print() ``` Yes
104,145
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Submitted Solution: ``` def main(): s = input() n = len(s) dp = [[0 for i in range(n + 1)] for j in range(n + 1)] count = [0 for i in range(n + 1)] for sub_len in range(1, n + 1): for left in range(0, n - sub_len + 1): right = left + sub_len - 1 if sub_len == 1: dp[left][right] = 1 elif sub_len == 2: if s[left] == s[right]: dp[left][right] = 2 else: if s[left] == s[right] and dp[left + 1][right - 1] > 0: dp[left][right] = dp[left][left + sub_len // 2 - 1] + 1 count[dp[left][right]] += 1 for i in range(n - 1, 0, -1): count[i] += count[i + 1] for i in range(1, n + 1): print(count[i], end=' ') print() if __name__ == "__main__": main() ``` Yes
104,146
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Submitted Solution: ``` P = 311 # we use two mods to reduce the chance of collision MOD1 = int(1e9) + 7 MOD2 = int(1e9) + 9 def main(): s = input() n = len(s) # Pre-compute power_1 = [0 for i in range(n + 1)] power_2 = [0 for i in range(n + 1)] mod_inv_1 = [0 for i in range(n + 1)] mod_inv_2 = [0 for i in range(n + 1)] power_1[0] = 1 power_2[0] = 1 mod_inv_1[0] = 1 mod_inv_2[0] = 1 for i in range(1, n + 1): power_1[i] = power_1[i - 1] * P % MOD1 power_2[i] = power_2[i - 1] * P % MOD1 mod_inv_1[i] = bin_exp(power_1[i], MOD1 - 2, MOD1) mod_inv_2[i] = bin_exp(power_2[i], MOD2 - 2, MOD2) # Compute hash values hash_1 = 0 hash_2 = 0 forward_hash_1 = [0 for i in range(n + 1)] forward_hash_2 = [0 for i in range(n + 1)] for i in range(1, n + 1): hash_1 += ord(s[i - 1]) * power_1[i] hash_2 += ord(s[i - 1]) * power_2[i] hash_1 %= MOD1 hash_2 %= MOD2 forward_hash_1[i] = hash_1 forward_hash_2[i] = hash_2 hash_1 = 0 hash_2 = 0 backward_hash_1 = [0 for i in range(n + 1)] backward_hash_2 = [0 for i in range(n + 1)] for i in range(1, n + 1): hash_1 += ord(s[n - i]) * power_1[i] hash_2 += ord(s[n - i]) * power_2[i] hash_1 %= MOD1 hash_2 %= MOD2 backward_hash_1[i] = hash_1 backward_hash_2[i] = hash_2 dp = [[0 for i in range(n + 1)] for j in range(n + 1)] count = [0 for i in range(n + 1)] for sub_len in range(1, n + 1): for left in range(0, n - sub_len + 1): right = left + sub_len - 1 if sub_len == 1: dp[left][right] = 1 elif sub_len == 2: if s[left] == s[right]: dp[left][right] = 2 else: if s[left] == s[right] and dp[left + 1][right - 1] > 0: dp[left][right] = dp[left][left + sub_len // 2 - 1] + 1 count[dp[left][right]] += 1 for i in range(n - 1, 0, -1): count[i] += count[i + 1] for i in range(1, n + 1): print(count[i], end=' ') print() def bin_exp(a, x, mod): res = 1 while x > 0: if x & 1: res *= a res %= mod a *= a a %= mod x >>= 1 return res def get_forward_hash(forward_hash, mod_inv, left, right, mod): return (forward_hash[right + 1] - forward_hash[left] + mod) * mod_inv[left] % mod def get_backward_hash(backward_hash, mod_inv, n, left, right): r_left = n - left - 1 r_right = n - right - 1 return (backward_hash[r_left + 1] - backward_hash[r_right] + mod) * mod_inv[r_right] % mod if __name__ == "__main__": main() ``` Yes
104,147
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Submitted Solution: ``` def palh(s): if s[:(len(s)//2)]==s[-(len(s)//2):]: return palh(s[:(len(s)//2)])+1 else: return 0 S=input() N=len(S) R=[0 for i in range(N+1)] for l in range(0, N): for r in range(l+1, N): for i in range(0, palh(S[l:r])+1): R[i]+=1 print(*R[:-1]) ``` No
104,148
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is k-palindrome (k > 1) if and only if: 1. Its left half equals to its right half. 2. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋ denotes the length of string t divided by 2, rounded down. Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. Input The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters. Output Print |s| integers — palindromic characteristics of string s. Examples Input abba Output 6 1 0 0 Input abacaba Output 12 4 1 0 0 0 0 Note In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here. Submitted Solution: ``` s = input() size = len(s) dp = [[0 for l in range(size)] for l in range(size)] ans = [0]*(size+1) for i in range(1, size+1): if i == 1: for j in range(0, size): dp[j][j] = 1 ans[1] += 1 elif i == 2: for j in range(0, size-1): if s[j+1] == s[j]: dp[j][j+1] = 2 ans[1] += 1 ans[2] += 1 else: dp[j][j+1] = 0 else: for j in range(0, size-i+1): if s[j] != s[j+i-1] or dp[j+1][j+i-2] == 0: dp[j][j+i-1] = 0 else: dp[j][j+i-1] = dp[j][int((j+j+i-2)/2)] + 1 ans[dp[j][j+i-1]] += 1 for i in range(1, size): print(ans[i], end="") print(" ", end="") print(ans[size]) ``` No
104,149
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In this problem you will have to deal with a very special network. The network consists of two parts: part A and part B. Each part consists of n vertices; i-th vertex of part A is denoted as Ai, and i-th vertex of part B is denoted as Bi. For each index i (1 ≤ i < n) there is a directed edge from vertex Ai to vertex Ai + 1, and from Bi to Bi + 1, respectively. Capacities of these edges are given in the input. Also there might be several directed edges going from part A to part B (but never from B to A). You have to calculate the [maximum flow value](https://en.wikipedia.org/wiki/Maximum_flow_problem) from A1 to Bn in this network. Capacities of edges connecting Ai to Ai + 1 might sometimes change, and you also have to maintain the maximum flow value after these changes. Apart from that, the network is fixed (there are no changes in part B, no changes of edges going from A to B, and no edge insertions or deletions). Take a look at the example and the notes to understand the structure of the network better. Input The first line contains three integer numbers n, m and q (2 ≤ n, m ≤ 2·105, 0 ≤ q ≤ 2·105) — the number of vertices in each part, the number of edges going from A to B and the number of changes, respectively. Then n - 1 lines follow, i-th line contains two integers xi and yi denoting that the edge from Ai to Ai + 1 has capacity xi and the edge from Bi to Bi + 1 has capacity yi (1 ≤ xi, yi ≤ 109). Then m lines follow, describing the edges from A to B. Each line contains three integers x, y and z denoting an edge from Ax to By with capacity z (1 ≤ x, y ≤ n, 1 ≤ z ≤ 109). There might be multiple edges from Ax to By. And then q lines follow, describing a sequence of changes to the network. i-th line contains two integers vi and wi, denoting that the capacity of the edge from Avi to Avi + 1 is set to wi (1 ≤ vi < n, 1 ≤ wi ≤ 109). Output Firstly, print the maximum flow value in the original network. Then print q integers, i-th of them must be equal to the maximum flow value after i-th change. Example Input 4 3 2 1 2 3 4 5 6 2 2 7 1 4 8 4 3 9 1 100 2 100 Output 9 14 14 Note This is the original network in the example: <image> Submitted Solution: ``` print("9\n14\n14") ``` No
104,150
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In this problem you will have to deal with a very special network. The network consists of two parts: part A and part B. Each part consists of n vertices; i-th vertex of part A is denoted as Ai, and i-th vertex of part B is denoted as Bi. For each index i (1 ≤ i < n) there is a directed edge from vertex Ai to vertex Ai + 1, and from Bi to Bi + 1, respectively. Capacities of these edges are given in the input. Also there might be several directed edges going from part A to part B (but never from B to A). You have to calculate the [maximum flow value](https://en.wikipedia.org/wiki/Maximum_flow_problem) from A1 to Bn in this network. Capacities of edges connecting Ai to Ai + 1 might sometimes change, and you also have to maintain the maximum flow value after these changes. Apart from that, the network is fixed (there are no changes in part B, no changes of edges going from A to B, and no edge insertions or deletions). Take a look at the example and the notes to understand the structure of the network better. Input The first line contains three integer numbers n, m and q (2 ≤ n, m ≤ 2·105, 0 ≤ q ≤ 2·105) — the number of vertices in each part, the number of edges going from A to B and the number of changes, respectively. Then n - 1 lines follow, i-th line contains two integers xi and yi denoting that the edge from Ai to Ai + 1 has capacity xi and the edge from Bi to Bi + 1 has capacity yi (1 ≤ xi, yi ≤ 109). Then m lines follow, describing the edges from A to B. Each line contains three integers x, y and z denoting an edge from Ax to By with capacity z (1 ≤ x, y ≤ n, 1 ≤ z ≤ 109). There might be multiple edges from Ax to By. And then q lines follow, describing a sequence of changes to the network. i-th line contains two integers vi and wi, denoting that the capacity of the edge from Avi to Avi + 1 is set to wi (1 ≤ vi < n, 1 ≤ wi ≤ 109). Output Firstly, print the maximum flow value in the original network. Then print q integers, i-th of them must be equal to the maximum flow value after i-th change. Example Input 4 3 2 1 2 3 4 5 6 2 2 7 1 4 8 4 3 9 1 100 2 100 Output 9 14 14 Note This is the original network in the example: <image> Submitted Solution: ``` aList = [123, 'xyz', 'zara', 'abc']; print("Length of aList: ",len(aList)) while aList: print(aList.pop()) print("Length of aList: ",len(aList)) ``` No
104,151
Provide a correct Python 3 solution for this coding contest problem. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. "Correct Solution: ``` n = int(input()) a = [int(x) for x in input().strip().split()] a = sorted(a) d = [] for i in range(len(a) - 1): d.append(a[i + 1] - a[i]) def gcd(a, b): if(b == 0): return a else: return gcd(b, a % b) g = d[0] for e in d: g = gcd(g, e) cnt = len(a) print( int( ( a[len(a) - 1] - a[0]) / g + 1 - cnt)) ```
104,152
Provide a correct Python 3 solution for this coding contest problem. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. "Correct Solution: ``` def nod(n, m): if(m==0): return n; if(n>m): return nod(m,n%m); else: return nod(n,m%n); n=input(); x = sorted(list(map(int, input().split()))) m = min(x) nnod=0 for i in range(len(x)): if(i>1): if(i==2): nnod=nod(x[i]-x[i-1], x[i-1]-x[i-2]) else: nnod=nod(nnod, x[i]-x[i-1]) res=0 for i in range(len(x)): if(i>0): res+=(x[i]-x[i-1])/nnod - 1; print(int(res)) ```
104,153
Provide a correct Python 3 solution for this coding contest problem. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. "Correct Solution: ``` from fractions import gcd from functools import reduce n = int(input()) x = list(map(int, input().split())) x.sort() difs = [] for i in range(len(x)-1): difs.append(x[i+1]-x[i]) dif = reduce(gcd, difs) mmin = x[0] mmax = x[-1] x = set(x) cnt = (mmax-mmin+dif)//dif-n print(cnt) ```
104,154
Provide a correct Python 3 solution for this coding contest problem. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. "Correct Solution: ``` from fractions import gcd n=int(input()) a=input().split() for i in range(0, n): a[i]=int(a[i]) a.sort() dists=[] for i in range(1, n): dists.append(a[i]-a[i-1]) s=dists[0] gdc=dists[0] for i in range(1,len(dists)): gdc=gcd(dists[i],gdc) s+=dists[i] print(s//gdc-n+1) ```
104,155
Provide a correct Python 3 solution for this coding contest problem. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. "Correct Solution: ``` def nod(a, b): while a!=0 and b!=0: if a > b: a = a % b else: b = b % a return (a+b) n = int(input()) a = list(map(int, input().split())) a.sort() d = nod(a[1] - a[0], a[2] - a[1]) for i in range(2, n - 1): d = nod(a[i + 1] - a[i], d) count = 0 for i in range(n - 1): count += ((a[i + 1] - a[i]) // d - 1) print(count) ```
104,156
Provide a correct Python 3 solution for this coding contest problem. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. "Correct Solution: ``` from fractions import gcd n = int(input()) a = list(map(int, input().split())) a.sort() for i in range(1, len(a)): a[i-1] = a[i] - a[i-1] a.pop() g = a[0] s = 0 for i in range(len(a)): s += a[i] g = gcd(g, a[i]) s = s // g - len(a) print(s) ```
104,157
Provide a correct Python 3 solution for this coding contest problem. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. "Correct Solution: ``` import sys # For getting input from input.txt file #sys.stdin = open('input.txt', 'r') # Printing the Output to output.txt file #sys.stdout = open('output.txt', 'w') def gcd(a, b): while a != 0 and b != 0: if a > b: a = a % b else: b = b % a return max(a, b) n = int(input()) s = input() num = list(map(int, s.split(' '))) num.sort() slen = [] for i in range(1, n): slen.append(num[i] - num[i - 1]) slen.sort() minlen = slen[0] maxlen = slen[-1] for i in range(0, n - 1): if minlen == 1: break elif slen[i] % minlen != 0: minlen = gcd(minlen, slen[i]) print( int( (num[-1] - num[0]) / minlen - n + 1) ) ```
104,158
Provide a correct Python 3 solution for this coding contest problem. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. "Correct Solution: ``` def gcd(a, b): while a != 0 and b != 0: if b > a: b = b % a else: a = a % b if a == 0: return b else: return a n = int(input()) a = input() a = a.split() for i in range(len(a)): a[i] = int(a[i]) a = sorted(a) b = a[1] - a[0] for i in range(0, len(a) - 1): b = gcd(b, a[i + 1] - a[i]) cnt = (a[len(a) - 1] - a[0]) // b + 1 print(cnt - n) ```
104,159
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. Submitted Solution: ``` import sys import math from collections import defaultdict,deque n=int(sys.stdin.readline()) arr=list(map(int,sys.stdin.readline().split())) arr.sort() x=arr[1]-arr[0] for i in range(n-1): x=math.gcd(x,arr[i+1]-arr[i]) ans=0 for i in range(n-1): dis=arr[i+1]-arr[i] y=dis//x-1 ans+=y print(ans) ``` Yes
104,160
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. Submitted Solution: ``` n = int(input()) m = list(map(int, input().split())) m.sort() min_dif = m[1] - m[0] def nod(a, b): if b == 0: return a else: return nod(b, a %b) for i in range(1, n): min_dif = nod(min_dif, m[i] - m[i-1]) ans = 0 for i in range(1, n): if m[i] - m[i-1] > min_dif: ans += (m[i] - m[i-1]) / min_dif - 1 print(int(ans)) ``` Yes
104,161
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. Submitted Solution: ``` import math n,a,q,w=int(input()),sorted(map(int,input().split())),0,0 for i in range(1,n):q=math.gcd(q,a[i]-a[i-1]) for i in range(1,n):w+=(a[i]-a[i-1])//q-1 print(w) ``` Yes
104,162
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. Submitted Solution: ``` def nod(a, b): if (b == 0): return a; else: return nod(b, a%b); def main(): n = int(input()); arr = [int(i) for i in input().split()] arr.sort() no = 0; for i in range(1, n): no = nod(arr[i] - arr[i-1], no) ans = 0; for i in range(1, n): ans += -1 + (arr[i] - arr[i-1]) // no print(ans) main() ``` Yes
104,163
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. Submitted Solution: ``` n = int(input()) x = list(map(int, input().split())) x.sort() dif = 1e10 for i in range(len(x)-1): dif = min(dif, x[i+1]-x[i]) mmin = x[0] mmax = x[-1] x = set(x) cnt = 0 for i in range(mmin, mmax+100, dif): if i <= mmax and i not in x: cnt += 1 print(cnt) ``` No
104,164
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. Submitted Solution: ``` import sys # For getting input from input.txt file #sys.stdin = open('input.txt', 'r') # Printing the Output to output.txt file #sys.stdout = open('output.txt', 'w') def gcd(a, b): while a != 0 and b != 0: if a > b: a = a % b else: b = b % a return max(a, b) n = int(input()) s = input() num = list(map(int, s.split(' '))) num.sort() slen = [] for i in range(1, n): slen.append(num[i] - num[i - 1]) slen.sort() minlen = slen[0] maxlen = slen[-1] for i in range(0, n - 1): if minlen == 1: break elif slen[i] % minlen != 0: minlen = gcd(minlen, slen[i]) print((num[-1] - num[0]) / minlen - n + 1) ``` No
104,165
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. Submitted Solution: ``` def gcd(a, b): while b != 0: tmp = b b = a % b a = tmp return a n = int(input()) v = list(map(int, input().split())) minrast = abs(v[1] - v[0]) for i in range(1, n): minrast = min(minrast, abs(v[i] - v[i-1])) f = 1 tmp = 0 cur = v[0] for i in range(1, n): tmprast = abs(v[i] - cur) if (tmprast % minrast == 0): tmp += tmprast // minrast - 1 else: f = 0 break cur = v[i] if f == 1: ans = tmp print(ans) else: print(abs(v[n-1] - v[0]) - n + 1) ``` No
104,166
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Input The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. Output Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. Examples Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 Note In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. Submitted Solution: ``` a = input() a = int(a) x = sorted(list(map(int, input().split()))) mn = 100000000000 cnt = 0 i = 0 for i in range(a): if cnt > 0: if x[i] - x[i - 1] < mn: mn = x[i] - x[i - 1] cnt = cnt + 1 ans = 0 cnt = 0; i = 0 for i in range(a): if cnt > 0: ans = ans + ((x[i] - x[i - 1]) // mn - 1) cnt = cnt + 1 print(ans) ``` No
104,167
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Suppose you have two strings s and t, and their length is equal. You may perform the following operation any number of times: choose two different characters c1 and c2, and replace every occurence of c1 in both strings with c2. Let's denote the distance between strings s and t as the minimum number of operations required to make these strings equal. For example, if s is abcd and t is ddcb, the distance between them is 2 — we may replace every occurence of a with b, so s becomes bbcd, and then we may replace every occurence of b with d, so both strings become ddcd. You are given two strings S and T. For every substring of S consisting of |T| characters you have to determine the distance between this substring and T. Input The first line contains the string S, and the second — the string T (1 ≤ |T| ≤ |S| ≤ 125000). Both strings consist of lowercase Latin letters from a to f. Output Print |S| - |T| + 1 integers. The i-th of these integers must be equal to the distance between the substring of S beginning at i-th index with length |T| and the string T. Example Input abcdefa ddcb Output 2 3 3 3 Submitted Solution: ``` class UnionFind: def __init__(self, n): self.parent = [-1] * n self.cnt = n def root(self, x): if self.parent[x] < 0: return x else: self.parent[x] = self.root(self.parent[x]) return self.parent[x] def merge(self, x, y): x = self.root(x) y = self.root(y) if x == y: return if self.parent[x] > self.parent[y]: x, y = y, x self.parent[x] += self.parent[y] self.parent[y] = x self.cnt -= 1 def is_same(self, x, y): return self.root(x) == self.root(y) def get_cnt(self): return self.cnt s = [ord(char) - 97 for char in input()] t = [ord(char) - 97 for char in input()][::-1] s_ = [0] * 6 t_ = [0] * 6 st = [[0] * 6 for _ in range(6)] for i, val in enumerate(s): s_[val] |= 1 << i for i, val in enumerate(t): t_[val] |= 1 << i for ind_s in range(6): for ind_t in range(6): if ind_s == ind_t: continue st[ind_s][ind_t] = bin(s_[ind_s] * t_[ind_t])[2:][::-1] + "0" * (len(s) + len(t) + 100) length = len(t) - 1 ans = [] for i in range(len(s) - len(t) + 1): shift = length + i uf = UnionFind(6) for ind_s in range(6): for ind_t in range(6): if ind_s == ind_t: continue if st[ind_s][ind_t][shift] == "1": uf.merge(ind_s, ind_t) ans.append(6 - uf.get_cnt()) print(*ans) ``` No
104,168
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Suppose you have two strings s and t, and their length is equal. You may perform the following operation any number of times: choose two different characters c1 and c2, and replace every occurence of c1 in both strings with c2. Let's denote the distance between strings s and t as the minimum number of operations required to make these strings equal. For example, if s is abcd and t is ddcb, the distance between them is 2 — we may replace every occurence of a with b, so s becomes bbcd, and then we may replace every occurence of b with d, so both strings become ddcd. You are given two strings S and T. For every substring of S consisting of |T| characters you have to determine the distance between this substring and T. Input The first line contains the string S, and the second — the string T (1 ≤ |T| ≤ |S| ≤ 125000). Both strings consist of lowercase Latin letters from a to f. Output Print |S| - |T| + 1 integers. The i-th of these integers must be equal to the distance between the substring of S beginning at i-th index with length |T| and the string T. Example Input abcdefa ddcb Output 2 3 3 3 Submitted Solution: ``` import sys input = sys.stdin.readline class UnionFind: def __init__(self, n): self.parent = [-1] * n self.cnt = n def root(self, x): if self.parent[x] < 0: return x else: self.parent[x] = self.root(self.parent[x]) return self.parent[x] def merge(self, x, y): x = self.root(x) y = self.root(y) if x == y: return if self.parent[x] > self.parent[y]: x, y = y, x self.parent[x] += self.parent[y] self.parent[y] = x self.cnt -= 1 def is_same(self, x, y): return self.root(x) == self.root(y) def get_cnt(self): return self.cnt s = [ord(char) - 97 for char in input()[:-1]] t = [ord(char) - 97 for char in input()[:-1]][::-1] s_ = [0] * 6 t_ = [0] * 6 st = [[0] * 6 for i in range(6)] for i, val in enumerate(s): s_[val] |= 1 << i for i, val in enumerate(t): t_[val] |= 1 << i for ind_s in range(6): for ind_t in range(6): if ind_s == ind_t: continue st[ind_s][ind_t] = bin(s_[ind_s] * t_[ind_t])[2:][::-1] + "0" * (len(s) + len(t)) length = len(t) - 1 ans = [] for i in range(len(s) - len(t) + 1): shift = length + i uf = UnionFind(6) for ind_s in range(6): for ind_t in range(6): if ind_s == ind_t: continue if st[ind_s][ind_t][shift] == "1": uf.merge(ind_s, ind_t) ans.append(6 - uf.get_cnt()) print(*ans) ``` No
104,169
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Suppose you have two strings s and t, and their length is equal. You may perform the following operation any number of times: choose two different characters c1 and c2, and replace every occurence of c1 in both strings with c2. Let's denote the distance between strings s and t as the minimum number of operations required to make these strings equal. For example, if s is abcd and t is ddcb, the distance between them is 2 — we may replace every occurence of a with b, so s becomes bbcd, and then we may replace every occurence of b with d, so both strings become ddcd. You are given two strings S and T. For every substring of S consisting of |T| characters you have to determine the distance between this substring and T. Input The first line contains the string S, and the second — the string T (1 ≤ |T| ≤ |S| ≤ 125000). Both strings consist of lowercase Latin letters from a to f. Output Print |S| - |T| + 1 integers. The i-th of these integers must be equal to the distance between the substring of S beginning at i-th index with length |T| and the string T. Example Input abcdefa ddcb Output 2 3 3 3 Submitted Solution: ``` class UnionFind: def __init__(self, n): self.parent = [-1] * n self.cnt = n def root(self, x): if self.parent[x] < 0: return x else: self.parent[x] = self.root(self.parent[x]) return self.parent[x] def merge(self, x, y): x = self.root(x) y = self.root(y) if x == y: return if self.parent[x] > self.parent[y]: x, y = y, x self.parent[x] += self.parent[y] self.parent[y] = x self.cnt -= 1 def is_same(self, x, y): return self.root(x) == self.root(y) def get_cnt(self): return self.cnt s = [ord(char) - 97 for char in input()] t = [ord(char) - 97 for char in input()][::-1] s_ = [0] * 6 t_ = [0] * 6 st = [[0] * 6 for _ in range(6)] for i, val in enumerate(s): s_[val] |= 1 << i for i, val in enumerate(t): t_[val] |= 1 << i for ind_s in range(6): for ind_t in range(6): if ind_s == ind_t: continue st[ind_s][ind_t] = bin(s_[ind_s] * t_[ind_t])[2:][::-1] + "0" * (len(s) + len(t) + 100) length = len(t) - 1 ans = [] for i in range(len(s) - len(t) + 1): shift = length + i uf = UnionFind(6) for ind_s in range(6): for ind_t in range(6): if ind_s == ind_t: continue if st[ind_s][ind_t][shift] == "1": uf.merge(ind_s, ind_t) ans.append(6 - uf.get_cnt()) if len(s) > 100: print(len(s)) print(len(t)) print(*ans) ``` No
104,170
Provide tags and a correct Python 3 solution for this coding contest problem. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Tags: sortings Correct Solution: ``` n=int(input()) d=dict() for i in range(n): a,b=list(map(int,input().split())) try: d[a]=max(d[a],b) except: d.update({a:b}) m=int(input()) for i in range(m): a,b=list(map(int,input().split())) try: d[a]=max(d[a],b) except: d.update({a:b}) ans=0 for i in d: ans+=d[i] print(ans) ```
104,171
Provide tags and a correct Python 3 solution for this coding contest problem. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Tags: sortings Correct Solution: ``` n = int(input()) c1 = {} set1 = set() for _ in range(n): t1,t2 = map(int, input().split()) c1[t1] = t2 set1.add(t1) m = int(input()) for __ in range(m): t3,t4 = map(int, input().split()) if t3 in set1: c1[t3] = max(c1[t3],t4) else: c1[t3] = t4 res = 0 for i in c1: res+=c1[i] print(res) ```
104,172
Provide tags and a correct Python 3 solution for this coding contest problem. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Tags: sortings Correct Solution: ``` a_m = {} ret = 0 n = int(input()) for i in range(n): a, v = list(map(int, input().strip().split())) a_m[a] = v ret += v m = int(input()) for j in range(m): b, v = list(map(int, input().split())) if b in a_m: if v > a_m[b]: ret += v-a_m[b] else: ret += v print(ret) ```
104,173
Provide tags and a correct Python 3 solution for this coding contest problem. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Tags: sortings Correct Solution: ``` def main(): dict = {} for j in range(2): n = int(input()) for i in range(n): x, y = map(int, input().split()) if x in dict: dict[x] = max(dict[x], y) else: dict[x] = y print(sum(dict.values())) if __name__ == "__main__": main() ```
104,174
Provide tags and a correct Python 3 solution for this coding contest problem. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Tags: sortings Correct Solution: ``` from array import array database = {} result = 0 n = int(input()) i = 0 while i < n: i += 1 [a, x] = [int(x) for x in input().split()] result += x database[a] = x m = int(input()) i = 0 while i < m: i += 1 [b, y] = [int(x) for x in input().split()] x = database.get(b, 0) if x < y: result += y - x print(result) ```
104,175
Provide tags and a correct Python 3 solution for this coding contest problem. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Tags: sortings Correct Solution: ``` n = int(input()) a = {} for _ in range(n): aa, xx = [int(v) for v in input().split()] a[aa] = xx m = int(input()) b = {} for _ in range(m): bb, yy = [int(v) for v in input().split()] b[bb] = yy sa = set(a.keys()) sb = set(b.keys()) both = sa & sb print( sum(max(a[v], b[v]) for v in both) + sum(a[v] for v in sa - both) + sum(b[v] for v in sb - both) ) ```
104,176
Provide tags and a correct Python 3 solution for this coding contest problem. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Tags: sortings Correct Solution: ``` n1 = int(input()) item_pair = {} for i in range(0, n1): item_price = input().split() item = item_price[0] price = item_price[1] item_pair[item] = int(price) n2 = int(input()) for i in range(0, n2): item_price = input().split() item = item_price[0] price = int(item_price[1]) if item in item_pair: n1_price = item_pair[item] if(price > n1_price): item_pair[item] = price else: item_pair[item] = price sum = 0 for value in item_pair.values(): sum += value print(sum) ```
104,177
Provide tags and a correct Python 3 solution for this coding contest problem. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Tags: sortings Correct Solution: ``` n = int(input()) chemForces = dict() allSum = 0 for i in range(n): newChem = input().split() i,k = int(newChem[0]), int(newChem[1]) allSum += k chemForces.update({i:k}) m = int(input()) topChemist = dict() for j in range(m): newTop = input().split() j, t = int(newTop[0]), int(newTop[1]) allSum += t topChemist.update({j : t}) intersection = set.intersection(set(chemForces), set(topChemist)) for i in intersection: chemF = chemForces.get(i) topC = topChemist.get(i) if chemF <= topC: allSum -= chemF else: allSum -= topC print(allSum) ```
104,178
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Submitted Solution: ``` from collections import defaultdict n=int(input()) count=defaultdict(int) price=defaultdict(int) profit=int profit=0 for i in range(n): A=input().split() k=int(A[0]) l=int(A[1]) count[k]=1 price[k]=l m=int(input()) for i in range(m): A=input().split() k=int(A[0]) l=int(A[1]) if l>price[k]: count[k]==-1 price[k]=l for i in price.keys(): profit=profit+price[i] print(profit) ``` Yes
104,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Submitted Solution: ``` MyDict = {} X = int(input()) for i in range(X): Temp = list(map(int, input().split())) MyDict[Temp[0]] = Temp[1] X = int(input()) for i in range(X): Temp = list(map(int, input().split())) MyDict[Temp[0]] = max(Temp[1], MyDict[Temp[0]]) if Temp[0] in MyDict.keys() else Temp[1] print(sum(MyDict.values())) # UB_CodeForces # Advice: Every person have some powers that he might not know yet, # try to find your powers # Location: Under the shadow of God # Caption: Loving my life ``` Yes
104,180
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Submitted Solution: ``` n = int(input()) chemicals = {} for i in range(n): line = [int(el) for el in input().split()] chemicals.update({line[0]: [line[1], 0]}) m = int(input()) for i in range(m): line = [int(el) for el in input().split()] if line[0] not in chemicals.keys(): chemicals.update({line[0]: [0, line[1]]}) else: chemicals[line[0]][1] = line[1] max_sum = 0 for chemical in chemicals.keys(): max_sum += max(chemicals[chemical]) print(max_sum) ``` Yes
104,181
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Submitted Solution: ``` from collections import defaultdict d=defaultdict(int) n=int(input()) for i in range(n): a,b=map(int,input().split()) d[a]=b m=int(input()) for i in range(m): a,b=map(int,input().split()) if d[a]<b: d[a]=b print(sum(d.values())) ``` Yes
104,182
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Submitted Solution: ``` a1=[]; a2=[]; b1=[]; b2=[]; count=0 for i in range (int(input())): a,b = input().split() a,b=int(a),int(b) count+=b a1.append(a) a2.append(b) print (a1,a2) for i in range(int(input())): a,b = input().split() a,b=int(a),int(b) count+=b b1.append(a) b2.append(b) c = set(a1) & set(b1) c = list(c) for i in range(len(c)): a=a1.index(c[i]) b=b1.index(c[i]) if a2[a]>b2[b]: count-=b2[b] else: count-=a2[a] print (count) ``` No
104,183
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Submitted Solution: ``` n=int(input()) i1={} i2={} sum=0 for i in range(n): l,k=map(int,input().split()) i1[l]=k m=int(input()) for j in range(m): l,k=map(int,input().split()) i2[l]=k for key in i1 : if key in i2 : if i1[key]>i2[key]: sum +=i1[key] else : sum +=i2[key] i2[key]=0 i1[key]=0 else : sum +=i1[key] for key2 in i2 : sum += i2[key] print (sum) ``` No
104,184
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Submitted Solution: ``` _ = int(input()) company={} for i in range(_): x,y = map(int,input().split()) company[str(x)]=y for i in range(int(input())): x,y=map(int,input().split()) print(company.keys()) if str(x) in company.keys(): if company[str(x)]<y: company[str(x)]=y else: company[str(x)]=y ans =0 for k,v in company.items(): ans+=v print(company) print(ans) ``` No
104,185
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies. In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies. All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, …, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company. The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, …, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set. In other words, the first company can present any subset of elements from \\{a_1, a_2, …, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, …, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets. Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. Input The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of elements discovered by ChemForces. The i-th of the next n lines contains two integers a_i and x_i (1 ≤ a_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct. The next line contains a single integer m (1 ≤ m ≤ 10^5) — the number of chemicals invented by TopChemist. The j-th of the next m lines contains two integers b_j and y_j, (1 ≤ b_j ≤ 10^9, 1 ≤ y_j ≤ 10^9) — the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct. Output Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. Examples Input 3 1 2 7 2 3 10 4 1 4 2 4 3 4 4 4 Output 24 Input 1 1000000000 239 3 14 15 92 65 35 89 Output 408 Note In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24. In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. Submitted Solution: ``` n = int(input()) l,d1,d2,ans = [],{},{},0 for i in range(n): a,b = map(int,input().split()) d1[a] = b l.append(a) ans += b m = int(input()) for i in range(m): a,b = map(int,input().split()) d2[a] = b ans += b for i in range(len(l)): if i in d1.keys(): if i in d2.keys(): j = min(d1[i],d2[i]) ans = ans - j print(ans) ``` No
104,186
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 "Correct Solution: ``` N, Q, *Query = [int(_) for _ in open(0).read().split()] D = Query[::2] X = Query[1::2] class LazySegmentTree(): def __init__(self, array, f, g, h, ti, ei): """ Parameters ---------- array : list to construct segment tree from f : func binary operation of the monoid T x T -> T T is dat g : func binary operation of the monoid T x E -> T T is dat, E is laz h : func binary operation of the monoid E x E -> T E is laz ti : T identity element of T ei : E identity element of E """ self.f = f self.g = g self.h = h self.ti = ti self.ei = ei self.height = height = len(array).bit_length() self.n = n = 2**height self.dat = dat = [ti] * n + array + [ti] * (n - len(array)) self.laz = [ei] * (2 * n) for i in range(n - 1, 0, -1): # build dat[i] = f(dat[i << 1], dat[i << 1 | 1]) def reflect(self, k): dat = self.dat ei = self.ei laz = self.laz g = self.g return self.dat[k] if laz[k] is ei else g(dat[k], laz[k]) def evaluate(self, k): laz = self.laz ei = self.ei reflect = self.reflect dat = self.dat h = self.h if laz[k] is ei: return laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]) laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]) dat[k] = reflect(k) laz[k] = ei def thrust(self, k): height = self.height evaluate = self.evaluate for i in range(height, 0, -1): evaluate(k >> i) def recalc(self, k): dat = self.dat reflect = self.reflect f = self.f while k: k >>= 1 dat[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1)) def update(self, a, b, x): # set value at position [a, b) (0-indexed) thrust = self.thrust n = self.n h = self.h laz = self.laz recalc = self.recalc a += n b += n - 1 l = a r = b + 1 thrust(a) thrust(b) while l < r: if l & 1: laz[l] = h(laz[l], x) l += 1 if r & 1: r -= 1 laz[r] = h(laz[r], x) l >>= 1 r >>= 1 recalc(a) recalc(b) def set_val(self, a, x): n = self.n thrust = self.thrust dat = self.dat laz = self.laz recalc = self.recalc ei = self.ei a += n thrust(a) dat[a] = x laz[a] = ei recalc(a) def query(self, a, b): # result on interval [a, b) (0-indexed) f = self.f ti = self.ti n = self.n thrust = self.thrust reflect = self.reflect a += n b += n - 1 thrust(a) thrust(b) l = a r = b + 1 vl = vr = ti while l < r: if l & 1: vl = f(vl, reflect(l)) l += 1 if r & 1: r -= 1 vr = f(reflect(r), vr) l >>= 1 r >>= 1 return f(vl, vr) #RSQ and RUQ array = [N] * (N + 1) f = lambda a, b: a if b == 0 else b g = lambda a, b: min(a, b) h = lambda a, b: min(a, b) ti = 0 ei = float('inf') lst1 = LazySegmentTree(array=array, ti=ti, ei=ei, f=f, g=g, h=h) lst2 = LazySegmentTree(array=array, ti=ti, ei=ei, f=f, g=g, h=h) ans = 0 for d, x in zip(D, X): if d == 1: y = lst1.query(x, x + 1) ans += y - 2 lst2.update(1, y, x) else: y = lst2.query(x, x + 1) ans += y - 2 lst1.update(1, y, x) ans = (N - 2)**2 - ans print(ans) ```
104,187
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 "Correct Solution: ``` n,q = map(int,input().split()) yoko = [-n] yoko2 = [-n] yokol = 1 tate = [-n] tate2 = [-n] tatel = 1 last = [n,n] import bisect ans = (n-2)**2 #print(ans) for _ in range(q): a,c = map(int,input().split()) if a == 1: b = bisect.bisect_left(yoko,-c) if b == yokol: yoko.append(-c) yoko2.append(-last[0]) yokol += 1 last[1] = c ans -= last[0]-2 else: ans -= -yoko2[b] - 2 if a == 2: b = bisect.bisect_left(tate,-c) if b == tatel: tate.append(-c) tate2.append(-last[1]) tatel += 1 last[0] = c ans -= last[1]-2 else: ans -= -tate2[b] - 2 #print(yoko) #print(yoko2) #print(tate) #print(tate2) print(ans) ```
104,188
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 "Correct Solution: ``` import sys def input(): return sys.stdin.readline().strip() def mapint(): return map(int, input().split()) sys.setrecursionlimit(10**9) N, Q = mapint() verti = [N-2]*(N-1) horiz = [N-2]*(N-1) left, top = N-2, N-2 ans = (N-2)**2 last_top = (N-2, N-2) last_left = (N-2, N-2) for _ in range(Q): c, x = mapint() x -= 1 if c==1: if x<=left: ans -= top left = x-1 for i in range(left, last_left[0]+1): horiz[i] = last_top[1] last_left = (left, x-1) else: ans -= horiz[x-1] if c==2: if x<=top: ans -= left top = x-1 for i in range(top, last_top[0]+1): verti[i] = last_left[1] last_top = (top, x-1) else: ans -= verti[x-1] print(ans) ```
104,189
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 "Correct Solution: ``` class SegTree: X_unit = 1 << 30 X_f = min def __init__(self, N): self.N = N self.X = [self.X_unit] * (N + N) def build(self, seq): for i, x in enumerate(seq, self.N): self.X[i] = x for i in range(self.N - 1, 0, -1): self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1]) def set_val(self, i, x): i += self.N self.X[i] = x while i > 1: i >>= 1 self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1]) def fold(self, L, R): L += self.N R += self.N vL = self.X_unit vR = self.X_unit while L < R: if L & 1: vL = self.X_f(vL, self.X[L]) L += 1 if R & 1: R -= 1 vR = self.X_f(self.X[R], vR) L >>= 1 R >>= 1 return self.X_f(vL, vR) N, Q=map(int,input().split()) S1=SegTree(N) S2=SegTree(N) S1.build([N]*N) S2.build([N]*N) ans=(N-2)**2 for i in range(Q): j, x=map(int,input().split()) if j==1: l=S2.fold(x-1,N) ans-=l-2 S1.set_val(l-1,min(x,S1.X[l-1+N])) if j==2: l=S1.fold(x-1,N) ans-=l-2 S2.set_val(l-1,min(x,S2.X[l-1+N])) print(ans) ```
104,190
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 "Correct Solution: ``` import sys def solve(): input = sys.stdin.readline N, Q = map(int, input().split()) total = (N - 2) ** 2 rb = N db = N D = [N] * (N + 1) R = [N] * (N + 1) for _ in range(Q): a, b = map(int, input().split()) if a == 1: #横向き if b < db: total -= (rb - 2) for i in range(b, db): R[i] = rb db = b else: total -= (R[b] - 2) else: #縦向き if b < rb: total -= (db - 2) for i in range(b, rb): D[i] = db rb = b else: total -= (D[b] - 2) print(total) return 0 if __name__ == "__main__": solve() ```
104,191
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 "Correct Solution: ``` # coding: utf-8 import sys # from operator import itemgetter sysread = sys.stdin.buffer.readline read = sys.stdin.buffer.read printout = sys.stdout.write sprint = sys.stdout.flush # from heapq import heappop, heappush # from collections import defaultdict sys.setrecursionlimit(10 ** 7) import math # from itertools import product, accumulate, combinations, product # import bisect # import numpy as np # from copy import deepcopy #from collections import deque # from decimal import Decimal # from numba import jit INF = 1 << 50 EPS = 1e-8 mod = 998244353 def intread(): return int(sysread()) def mapline(t=int): return map(t, sysread().split()) def mapread(t=int): return map(t, read().split()) class segtree: '''init_val : 1-indexed (init_val[0] = self.bin[1])''' def __init__(self, n, init = 0, init_val=None): self.n = n self.init = init self.k = math.ceil(math.log2(n)) self.add_val = 1 << self.k self.bins = [self.init] * (1 << (self.k + 1)) if init_val != None: self.update_set(init_val) self.caliculate() def __getitem__(self, idx): # return idx-value return self.bins[idx + self.add_val] def update_set(self, vals): for idx, i in enumerate(range(self.add_val, self.add_val * 2)): if len(vals) > idx: self.bins[i] = vals[idx] else:continue def compare(self, l, r): return min(l ,r) def caliculate(self): k = self.k while k: for i in range(1<<k, 1<<(k+1)): if not i%2: self.bins[i//2] = self.compare(self.bins[i], self.bins[i+1]) else:continue k -= 1 def update(self, idx, val, by=True): '''idx : 0-started index''' k = (1<<self.k) + idx if by: self.bins[k] += val else: self.bins[k] = val while k>1: self.bins[k // 2] = self.compare(self.bins[k // 2 * 2], self.bins[k // 2 * 2 + 1]) k = k//2 def eval(self, l, r): if l == r: return self.bins[l + self.add_val] ret = self.init l = (1 << self.k) + l r = (1 << self.k) + r #print(l, r) while True: #print(l, r) if r - l == 1: ret = self.compare(ret, self.bins[l]) ret = self.compare(ret, self.bins[r]) break elif l == r: ret = self.compare(ret, self.bins[l]) break else: done = False if l % 2: ret = self.compare(ret, self.bins[l]) l += 1 done = True if not r % 2: ret = self.compare(ret, self.bins[r]) r -= 1 done = True if not done: l = l // 2 r = r // 2 #print(ret) return ret def run(): N, Q = mapline() X = segtree(N+1, init = INF) Y = segtree(N+1, init = INF) sub = 0 for _ in range(Q): q, x = mapline() x -= 1 if q == 1: v = X.eval(x, N-2) if v == INF: sub += N-2 if Y[N-2] > x: Y.update(N-2, x,by = False) else: sub += v-1 if Y[v] > x: Y.update(v, x, by = False) else: v = Y.eval(x, N - 2) if v == INF: sub += N - 2 if X[N - 2] > x: X.update(N - 2, x, by=False) else: sub += v - 1 if X[v] > x: X.update(v, x, by=False) print((N-2) ** 2 - sub) if __name__ == "__main__": run() ```
104,192
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 "Correct Solution: ``` def f_simplified_reversi(): N, Q = [int(i) for i in input().split()] Queries = [[int(i) for i in input().split()] for j in range(Q)] a, b = [N] * N, [N] * N # editorial に準じる black_stone = (N - 2)**2 row, col = N, N for type, x in Queries: if type == 1: if x < col: for i in range(x, col): b[i] = row col = x black_stone -= b[x] - 2 else: if x < row: for i in range(x, row): a[i] = col row = x black_stone -= a[x] - 2 return black_stone print(f_simplified_reversi()) ```
104,193
Provide a correct Python 3 solution for this coding contest problem. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 "Correct Solution: ``` #!python3 import sys iim = lambda: map(int, sys.stdin.readline().rstrip().split()) def resolve(): N, Q = iim() it = map(int, sys.stdin.read().split()) M = N + 1if N & 1 else N NM = 1<<M.bit_length() ans = (N-2)**2 A = [[N-2]*(NM+M) for i in range(2)] def set(a, i, j, x): i += NM; j += NM while i < j: a[i] = min(a[i], x) if i&1: i += 1 a[j] = min(a[j], x) if j&1 == 0: j -= 1 i >>= 1; j >>= 1 a[i] = min(a[i], x) def get(a, i): i += NM ans = a[i] while i > 0: i >>= 1 ans = min(ans, a[i]) return ans for q, i in zip(it, it): q -= 1; i -= 2 val = get(A[q], i) ans -= val set(A[q^1], 0, val, i) print(ans) if __name__ == "__main__": resolve() ```
104,194
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 Submitted Solution: ``` class LazyPropSegmentTree: def __init__(self, N): self.N = N self.LV = (N - 1).bit_length() self.N0 = 2 ** self.LV self.data = [N - 1] * (2 * self.N0) self.lazy = [float('inf')] * (2 * self.N0) # 遅延伝播を行うindexを生成 def gindex(self, l, r): L = (l + self.N0) >> 1; R = (r + self.N0) >> 1 lc = 0 if l & 1 else (L & -L).bit_length() rc = 0 if r & 1 else (R & -R).bit_length() for i in range(self.LV): if rc <= i: yield R if L < R and lc <= i: yield L L >>= 1; R >>= 1 # 遅延伝搬処理 def propagates(self, *ids): for i in reversed(ids): v = self.lazy[i - 1] if v == float('inf'): continue self.lazy[2 * i - 1] = min(self.lazy[2 * i - 1], v) self.lazy[2 * i] = min(self.lazy[2 * i], v) self.data[2 * i - 1] = min(self.data[2 * i - 1], v) self.data[2 * i] = min(self.data[2 * i], v) self.lazy[i - 1] = float('inf') def update(self, l, r, x): *ids, = self.gindex(l, r + 1) self.propagates(*ids) L = self.N0 + l; R = self.N0 + r + 1 while L < R: if R & 1: R -= 1 self.lazy[R - 1] = min(self.lazy[R - 1], x) self.data[R - 1] = min(self.data[R - 1], x) if L & 1: self.lazy[L - 1] = min(self.lazy[L - 1], x) self.data[L - 1] = min(self.data[L - 1], x) L += 1 L >>= 1; R >>= 1 for i in ids: self.data[i - 1] = min(self.data[2 * i - 1], self.data[2 * i]) def point_query(self, k): *ids, = self.gindex(k, k + 1) self.propagates(*ids) return self.data[k + self.N0 - 1] N, Q = map(int, input().split()) query = [tuple(map(int, input().split())) for i in range(Q)] row = LazyPropSegmentTree(N+1) column = LazyPropSegmentTree(N+1) ans = (N-2) ** 2 for t, x in query: if t == 1: n = row.point_query(x) # print(n) ans -= n - 2 column.update(2, n, x) else: n = column.point_query(x) # print(n) ans -= n - 2 row.update(2, n, x) print(ans) ``` Yes
104,195
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 Submitted Solution: ``` from bisect import bisect_left, bisect_right n,q = map(int, input().split()) hori_cnt = [n-2]*(n+2) # 2から ql = [] for _ in range(q): c,x = map(int, input().split()) ql.append((c,x)) tate_min = n yoko_min = n yoko_kyokai = [] ans1 = 0 for c,x in ql: if c == 1: if x < tate_min: hori_cnt[yoko_min] = x-2 yoko_kyokai.append(yoko_min*(-1)) tate_min = x else: # print(c,x,'---------') if x < yoko_min: yoko_min = x # print(yoko_kyokai) ind = bisect_left(yoko_kyokai, x*(-1))-1 # print(x,ind) if ind == -1: if x < yoko_min: ans1 += (tate_min-2) else: ans1 += (n-2) # print(tate_min-2,'++') else: val = yoko_kyokai[ind] ans1 += hori_cnt[val*(-1)] # print(hori_cnt[val*(-1)],'++s') tate_cnt = [n-2]*(n+2) # 2から yoko_min = n tate_min = n tate_kyokai = [] ans2 = 0 for c,x in ql: if c == 2: if x < yoko_min: tate_cnt[tate_min] = x-2 tate_kyokai.append(tate_min*(-1)) yoko_min =x else: # print(c,x,'---------') if x < tate_min: tate_min = x ind = bisect_left(tate_kyokai, x*(-1))-1 # print(tate_kyokai,'aaa') if ind == -1: if x < tate_min: ans2 += (yoko_min-2) else: ans2 += (n-2) # print(yoko_min-2,'+++') else: val = tate_kyokai[ind] ans2 += tate_cnt[val*(-1)] # print(tate_cnt[val*(-1)],'+++s') print((n-2)*(n-2)-ans1-ans2) ``` Yes
104,196
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 Submitted Solution: ``` #!usr/bin/env python3 from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 from math import log2, ceil class SegmentTree: def __init__(self, n, default): self.n = n tn = 2 ** ceil(log2(n)) self.a = [default] * (tn * 2) self.tn = tn def find(self, s, t): return self.__find(1, 0, self.tn - 1, s, t) def __find(self, c, l, r, s, t): if self.a[c] == -1: return self.a[c // 2] if s <= l and r <= t: return self.a[c] mid = (l + r) // 2 if t <= mid: return self.__find(c * 2, l, mid, s, t) elif s > mid: return self.__find(c * 2 + 1, mid + 1, r, s, t) else: return min( self.__find(c * 2, l, mid, s, mid), self.__find(c * 2 + 1, mid + 1, r, mid + 1, t)) def update(self, s, t, x): self.__update(1, 0, self.tn - 1, s, t, x) def __update(self, c, l, r, s, t, x, f=None): if f is None and self.a[c] == -1: f = self.a[c // 2] if l == s and r == t: return self.__set(c, x) mid = (l + r) // 2 if t <= mid: rv, f = self.__get_child(c, c * 2 + 1, f) u = min(self.__update(c * 2, l, mid, s, t, x, f), rv) elif s > mid: lv, f = self.__get_child(c, c * 2, f) u = min(lv, self.__update(c * 2 + 1, mid + 1, r, s, t, x, f)) else: u = min( self.__update(c * 2, l, mid, s, mid, x, f), self.__update(c * 2 + 1, mid + 1, r, mid + 1, t, x, f)) if f is not None: u = min(f, u) self.a[c] = u return u def __set(self, c, x): self.a[c] = x if c < self.tn: self.a[c * 2] = self.a[c * 2 + 1] = -1 return x def __get_child(self, c, child, f): if f is not None: return self.__set(child, f), f v = self.a[child] if v == -1: f = self.a[c] v = self.__set(child, f) return v, f def solve(): n,q = LI() ans = (n-2)*(n-2) right = SegmentTree(n+1,n) down = SegmentTree(n+1,n) for _ in range(q): t,x = LI() if t == 1: d = down.find(x,x) ans -= d-2 k = right.find(0,0) if k > x: right.update(0,d,x) else: r = right.find(x,x) ans -= r-2 k = down.find(0,0) if k > x: down.update(0,r,x) print(ans) return #Solve if __name__ == "__main__": solve() ``` Yes
104,197
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 Submitted Solution: ``` from functools import reduce from fractions import gcd import math import bisect import itertools import sys input = sys.stdin.readline INF = float("inf") # 処理内容 def main(): N, Q = map(int, input().split()) h = N w = N a = [N]*N b = [N]*N ans = (N-2)**2 for _ in range(Q): q, x = map(int, input().split()) if q == 1: if x < w: for i in range(x, w): b[i] = h w = x ans -= b[x] - 2 elif q == 2: if x < h: for i in range(x, h): a[i] = w h = x ans -= a[x] - 2 print(ans) if __name__ == '__main__': main() ``` Yes
104,198
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left. Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it. Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it. Q queries are given. We ask you to process them in order. There are two kinds of queries. Their input format and description are as follows: * `1 x`: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone. * `2 x`: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone. How many black stones are there on the grid after processing all Q queries? Constraints * 3 \leq N \leq 2\times 10^5 * 0 \leq Q \leq \min(2N-4,2\times 10^5) * 2 \leq x \leq N-1 * Queries are pairwise distinct. Input Input is given from Standard Input in the following format: N Q Query_1 \vdots Query_Q Output Print how many black stones there are on the grid after processing all Q queries. Examples Input 5 5 1 3 2 3 1 4 2 2 1 2 Output 1 Input 200000 0 Output 39999200004 Input 176527 15 1 81279 2 22308 2 133061 1 80744 2 44603 1 170938 2 139754 2 15220 1 172794 1 159290 2 156968 1 56426 2 77429 1 97459 2 71282 Output 31159505795 Submitted Solution: ``` N, Q = map(int, input().split()) ans = (N-2)*(N-2) R = [N]*(N+1) W = [N]*(N+1) for i in range(Q): a, b = map(int, input().split()) if a == 1: ans -= max(0, R[b] - 2) if b <= W[1]: for j in range(1, R[b]+1): W[j] = b R[b] = 0 else: ans -= max(0, W[b] - 2) if b <= R[1]: for j in range(1, W[b] + 1): R[j] = b W[b] = 0 print(ans) ``` No
104,199