text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide tags and a correct Python 3 solution for this coding contest problem. Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible. Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party. Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party. Input The first line of the input contains a single integer n (1 ≀ n ≀ 5000) β€” then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≀ ai ≀ bi ≀ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive. Output Print the maximum number of people that may come to Famil Door's party. Examples Input 4 M 151 307 F 343 352 F 117 145 M 24 128 Output 2 Input 6 M 128 130 F 128 131 F 131 140 F 131 141 M 131 200 M 140 200 Output 4 Note In the first sample, friends 3 and 4 can come on any day in range [117, 128]. In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140. Tags: brute force Correct Solution: ``` __author__ = 'aste' DAYS = 366 def main(): n = int(input()) g_f = [0] * DAYS g_m = [0] * DAYS for i in range(0, n): g, a, b = input().split() a = int(a) b = int(b) for j in range(1, DAYS + 1): if a <= j <= b: if g == 'F': g_f[j - 1] += 1 else: g_m[j - 1] += 1 res = 0 for i in range(0, DAYS): res = max(res, 2*min(g_f[i], g_m[i])) print(res) main() ```
15,500
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible. Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party. Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party. Input The first line of the input contains a single integer n (1 ≀ n ≀ 5000) β€” then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≀ ai ≀ bi ≀ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive. Output Print the maximum number of people that may come to Famil Door's party. Examples Input 4 M 151 307 F 343 352 F 117 145 M 24 128 Output 2 Input 6 M 128 130 F 128 131 F 131 140 F 131 141 M 131 200 M 140 200 Output 4 Note In the first sample, friends 3 and 4 can come on any day in range [117, 128]. In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140. Submitted Solution: ``` n=int(input()) friends=[None]*n*3 from_to=[None]*n*2 for i in range(n): line=input().split() friends[i*3]=line[0] friends[i*3+1]=from_to[i*2]=int(line[1]) friends[i*3+2]=from_to[i*2+1]=int(line[2]) from_to_dist=list(set(from_to)) len_from_to_dist=len(from_to_dist) female_male=[0]*len_from_to_dist*2 pair_counts=[None]*len_from_to_dist for i in range(len_from_to_dist): cur=from_to_dist[i] for j in range(n): if cur>=friends[j*3+1] and cur<=friends[j*3+2]: if friends[j*3]=='F': female_male[i*2]+=1 else: female_male[i*2+1]+=1 pair_counts[i]=min(female_male[i*2],female_male[i*2+1]) print(max(pair_counts)*2) ``` Yes
15,501
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible. Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party. Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party. Input The first line of the input contains a single integer n (1 ≀ n ≀ 5000) β€” then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≀ ai ≀ bi ≀ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive. Output Print the maximum number of people that may come to Famil Door's party. Examples Input 4 M 151 307 F 343 352 F 117 145 M 24 128 Output 2 Input 6 M 128 130 F 128 131 F 131 140 F 131 141 M 131 200 M 140 200 Output 4 Note In the first sample, friends 3 and 4 can come on any day in range [117, 128]. In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140. Submitted Solution: ``` m=[0]*367 f=[0]*367 for i in range(int(input())): a=list(input().split()) if a[0]=='M': for i in range(int(a[1]),int(a[2])+1): m[i]+=1 else: for i in range(int(a[1]),int(a[2])+1): f[i]+=1 x=0 for i in range(367): x=max(x,min(m[i],f[i])) print(2*x) ``` Yes
15,502
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible. Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party. Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party. Input The first line of the input contains a single integer n (1 ≀ n ≀ 5000) β€” then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≀ ai ≀ bi ≀ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive. Output Print the maximum number of people that may come to Famil Door's party. Examples Input 4 M 151 307 F 343 352 F 117 145 M 24 128 Output 2 Input 6 M 128 130 F 128 131 F 131 140 F 131 141 M 131 200 M 140 200 Output 4 Note In the first sample, friends 3 and 4 can come on any day in range [117, 128]. In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140. Submitted Solution: ``` if __name__ == '__main__': n = int(input()) mi = [0] * 366 fi = [0] * 366 for _ in range(n): fm, s, e = map(str, input().split()) if fm == 'M': for i in range(int(s) - 1, int(e)): mi[i] += 1 elif fm == 'F': for i in range(int(s) - 1, int(e)): fi[i] += 1 print(2 * max([min(m, f) for m, f in zip(mi, fi)])) ``` Yes
15,503
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible. Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party. Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party. Input The first line of the input contains a single integer n (1 ≀ n ≀ 5000) β€” then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≀ ai ≀ bi ≀ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive. Output Print the maximum number of people that may come to Famil Door's party. Examples Input 4 M 151 307 F 343 352 F 117 145 M 24 128 Output 2 Input 6 M 128 130 F 128 131 F 131 140 F 131 141 M 131 200 M 140 200 Output 4 Note In the first sample, friends 3 and 4 can come on any day in range [117, 128]. In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140. Submitted Solution: ``` n=int(input()) a,b=[[0,0] for i in range(366)],0 for i in range(n): x,y,z=map(str,input().split()) y,z=int(y),int(z) if x=='M': for i in range(y-1,z):a[i][0]+=1 else: for i in range(y-1,z):a[i][1]+=1 for i in range(366): if min(a[i])>b:b=min(a[i]) print(b*2) ``` Yes
15,504
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible. Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party. Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party. Input The first line of the input contains a single integer n (1 ≀ n ≀ 5000) β€” then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≀ ai ≀ bi ≀ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive. Output Print the maximum number of people that may come to Famil Door's party. Examples Input 4 M 151 307 F 343 352 F 117 145 M 24 128 Output 2 Input 6 M 128 130 F 128 131 F 131 140 F 131 141 M 131 200 M 140 200 Output 4 Note In the first sample, friends 3 and 4 can come on any day in range [117, 128]. In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140. Submitted Solution: ``` class friends: def __init__(self,gender,start,end): self.gender=gender self.start=start self.end=end n=int(input()) obj=[] for _ in range(n): gender,start,end=input().split() obj.append(friends(gender,int(start),int(end))) obj.sort(key= lambda x:x.start) invit=[] count=0 for i in obj: mygen=1 othergen=0 count=1 for j in obj[obj.index(i)+1:n]: if j.start<=i.end: count+=1 if j.gender!=i.gender: othergen+=1 else: mygen+=1 if mygen==othergen: invit.append(2*mygen) if mygen!=othergen: invit.append(2*min(mygen,othergen)) print(max(invit)) ``` No
15,505
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible. Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party. Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party. Input The first line of the input contains a single integer n (1 ≀ n ≀ 5000) β€” then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≀ ai ≀ bi ≀ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive. Output Print the maximum number of people that may come to Famil Door's party. Examples Input 4 M 151 307 F 343 352 F 117 145 M 24 128 Output 2 Input 6 M 128 130 F 128 131 F 131 140 F 131 141 M 131 200 M 140 200 Output 4 Note In the first sample, friends 3 and 4 can come on any day in range [117, 128]. In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140. Submitted Solution: ``` a=int(input()) ma=[] md=[] fa=[] fd=[] for i in range(a): y=input().split() if y[0]=='M': ma.append(int(y[1])) md.append(int(y[2])+1) else: fa.append(int(y[1])) fd.append(int(y[2])+1) ma.sort() md.sort() fa.sort() fd.sort() m=0 f=0 mx=0 mpa=0 fpa=0 mda=0 fda=0 for i in range(1,366): while mpa<len(ma) and ma[mpa]==i: mpa+=1 m+=1 while mda<len(md) and md[mda]==i: mda+=1 m-=1 while fpa<len(fa) and fa[fpa]==i: fpa+=1 f+=1 while fda<len(fd) and fd[fda]==i: fda+=1 f-=1 mx=max(mx, min(f,m)*2) print(mx) ``` No
15,506
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible. Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party. Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party. Input The first line of the input contains a single integer n (1 ≀ n ≀ 5000) β€” then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≀ ai ≀ bi ≀ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive. Output Print the maximum number of people that may come to Famil Door's party. Examples Input 4 M 151 307 F 343 352 F 117 145 M 24 128 Output 2 Input 6 M 128 130 F 128 131 F 131 140 F 131 141 M 131 200 M 140 200 Output 4 Note In the first sample, friends 3 and 4 can come on any day in range [117, 128]. In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140. Submitted Solution: ``` # print("Input n") n = int(input()) # Array to store all the input information arr = [[0 for i in range(n)] for j in range(3)] # Set to hold all the endpoints we need to check endpts = set() # Input all the information for i in range(n): # print("Input the next information") s,a,b = [x for x in input().split()] a = int(a) b = int(b) endpts.add(a) endpts.add(b) arr[0][i] = s arr[1][i] = a arr[2][i] = b # Must add the first and last one to the check! endpts.add(1) endpts.add(366) # Loop over the set of endpoints and brute force the max answer = 0 # for check in endpts: for check in range(367): male = 0 female = 0 total = 0 for i in range(n): sex = arr[0][i] left = arr[1][i] right = arr[2][i] if left <= check and check <= right: # A person who can come on that day total += 1 if sex == "M": male += 1 else: female += 1 if male == female and total > answer: answer = total # print("A better day is day " + str(check)) print(answer) ``` No
15,507
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible. Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party. Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party. Input The first line of the input contains a single integer n (1 ≀ n ≀ 5000) β€” then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≀ ai ≀ bi ≀ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive. Output Print the maximum number of people that may come to Famil Door's party. Examples Input 4 M 151 307 F 343 352 F 117 145 M 24 128 Output 2 Input 6 M 128 130 F 128 131 F 131 140 F 131 141 M 131 200 M 140 200 Output 4 Note In the first sample, friends 3 and 4 can come on any day in range [117, 128]. In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140. Submitted Solution: ``` n = int(input()) males = [0]*368 females = [0]*368 for i in range(n): pol, a, b = input().split() a = int(a) b = int(b) if pol == 'M': for i in range(a,b+1): males[i]+=1 else: for i in range(a,b+1): females[i]+=1 ans = 0 for i in range(368): if (males[i] == females[i]) and (males[i]>ans): ans = males[i] print(ans*2) ``` No
15,508
Provide tags and a correct Python 3 solution for this coding contest problem. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Tags: brute force, implementation Correct Solution: ``` #!/usr/bin/python3 def needs_rep(t, i): if i % 2 == 0: return t[i] >= t[i+1] else: return t[i] <= t[i+1] def would_need_rep(t, i, j1, j2): n = len(t) if i < 0: return False if i >= n-1: return False tj1 = t[j2] tj2 = t[j1] ti = t[i] if i == j1: ti = tj1 if i == j2: ti = tj2 ti1 = t[i+1] if i+1 == j1: ti1 = tj1 if i+1 == j2: ti1 = tj2 if i % 2 == 0: return ti >= ti1 else: return ti <= ti1 def main(): n = int(input()) t = [int(i) for i in input().split()] # rep = [False]*(n-1) rep = [] for i in range(n-1): if needs_rep(t, i): rep.append(i) if(len(rep) > 4): print(0) return # print(rep) # to_try = [rep[0]] # if rep[0] < n-1: # to_try.append(rep[0] + 1) to_try = [rep[0], rep[0] + 1] s = set() for i in to_try: for j in range(n): if i == j: continue if would_need_rep(t, i, i, j): continue if would_need_rep(t, i-1, i, j): continue if would_need_rep(t, j, i, j): continue if would_need_rep(t, j-1, i, j): continue bad = False for r in rep: if would_need_rep(t, r, i, j): bad = True if bad: continue # print(i, j) # print(would_need_rep(t, 2, i, j)) if (i, j) not in s and (j, i) not in s: # print('Adding {}'.format((i, j))) s.add((i, j)) print(len(s)) if __name__ == '__main__': main() ```
15,509
Provide tags and a correct Python 3 solution for this coding contest problem. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Tags: brute force, implementation Correct Solution: ``` n = int(input()) t = list(map(int, input().split())) t = [-1] + t badIdx = [] nice = [] def getBadIdx(): for i in range(1,n): if ((i%2 == 0) and (t[i] <= t[i+1])) or ((i%2 == 1) and (t[i] >= t[i+1])): badIdx.append((i,i+1)) def checkBad(k): if ((k <= (n-1)) and (((k%2 == 0) and (t[k] <= t[k+1])) or ((k%2 == 1) and (t[k] >= t[k+1])))) \ or ((k-1) >= 1 and (((k-1)%2 == 0) and (t[k-1] <= t[k]) or ((k-1)%2 == 1) and (t[k-1] >= t[k]))): return True for (i,j) in badIdx: if ((i%2 == 0) and (t[i] <= t[j])) or ((i%2 == 1) and (t[i] >= t[j])): return True return False def swap(i,j): ith = t[i] t[i] = t[j] t[j] = ith getBadIdx() if len(badIdx) > 4: print(0) else: (i,j) = badIdx[0] #for (i,j) in badIdx: for k in range(1,n+1): if i != k and t[i] != t[k]: swap(i,k) if not(checkBad(k)): nice.append((i,k)) swap(i,k) else: swap(i,k) if j != k and t[j] != t[k]: swap(j,k) if not(checkBad(k)): nice.append((j,k)) swap(j,k) else: swap(j,k) print(len(set([tuple(sorted(t)) for t in nice]))) ```
15,510
Provide tags and a correct Python 3 solution for this coding contest problem. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Tags: brute force, implementation Correct Solution: ``` def main(): n, l = int(input()), list(map(int, input().split())) if not (n & 1): l.append(0) l.append(150001) a, i, j, fails = l[0], 0, 1, set() try: while True: b = l[j] if a >= b: fails.add(i) fails.add(j) if len(fails) > 6: break i += 2 a = l[i] if a >= b: fails.add(i) fails.add(j) if len(fails) > 6: break j += 2 print(0) return except IndexError: tmp, res = [], 0 for b in sorted(fails): tmp.append("><"[b & 1] if b - a == 1 else "and ") tmp.append("l[{:n}]".format(b)) a = b check = compile("".join(tmp[1:]), "<string>", "eval") for i in fails: a = l[i] for j in fails: l[i], l[j] = l[j], a if eval(check): res -= 1 l[j] = l[i] for j in range(0, n, 2): l[i], l[j] = l[j], a if l[j - 1] > a < l[j + 1] and eval(check): res += 2 l[j] = l[i] for j in range(1, n, 2): l[i], l[j] = l[j], a if l[j - 1] < a > l[j + 1] and eval(check): res += 2 l[j] = l[i] l[i] = a print(res // 2) if __name__ == '__main__': main() ```
15,511
Provide tags and a correct Python 3 solution for this coding contest problem. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Tags: brute force, implementation Correct Solution: ``` def get_bit(diff, i): return 1 if ((i%2==1 and diff<=0) or (i%2==0 and diff>=0)) else 0 def swap_(i, j, a): temp = a[i] a[i] = a[j] a[j] = temp def swap(i, j, n, a, mask, S): change = 0 swap_(i, j, a) set_index = set([i, j]) if i<n-1: set_index.add(i+1) if j<n-1: set_index.add(j+1) for index in set_index: if index > 0: diff = a[index] - a[index-1] bit_ = get_bit(diff, index) change += bit_ - mask[index] swap_(i, j, a) if S + change == 0: return 1 return 0 n = int(input()) a = list(map(int, input().split())) diff = [-1] + [x-y for x, y in zip(a[1:], a[:-1])] mask = [get_bit(diff[i], i) for i in range(n)] S = sum(mask) first = -1 for i, x in enumerate(mask): if x == 1: first = i break cnt = 0 for second in range(n): if swap(first, second, n, a, mask, S) == 1: cnt += 1 if first != 0 and swap(first-1, second, n, a, mask, S) == 1: cnt += 1 if first!=0 and swap(first-1, first, n, a, mask, S) == 1: cnt-=1 print(cnt) #9 #1 2 3 4 5 6 7 8 9 #10 #3 2 1 4 1 4 1 4 1 4 #4 #200 150 100 50 #5 #2 8 4 7 7 ```
15,512
Provide tags and a correct Python 3 solution for this coding contest problem. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Tags: brute force, implementation Correct Solution: ``` def main(): n, l = int(input()), list(map(int, input().split())) if not (n & 1): l.append(0) l.append(150001) a, b, fails, tmp, res = 0, 150001, [], [], 0 for i, c in enumerate(l, -1): if i & 1: if a >= b or b <= c: if len(fails) > 5: print(0) return fails.append(i) else: if a <= b or b >= c: if len(fails) > 5: print(0) return fails.append(i) a, b = b, c for b in fails: tmp.append("><"[b & 1] if b - a == 1 else "and ") tmp.append("l[{:n}]".format(b)) a = b check = compile("".join(tmp[1:]), "<string>", "eval") for i in fails: a = l[i] for j in range(n): l[i], l[j] = l[j], a if eval(check) and ((l[j - 1] < l[j] > l[j + 1]) if j & 1 else (l[j - 1] > l[j] < l[j + 1])): res += 1 if j in fails else 2 l[j] = l[i] l[i] = a print(res // 2) if __name__ == '__main__': main() ```
15,513
Provide tags and a correct Python 3 solution for this coding contest problem. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Tags: brute force, implementation Correct Solution: ``` def main(): n, l = int(input()), list(map(int, input().split())) if not (n & 1): l.append(0) l.append(150001) a, b, fails, res = 0, 150001, [], 0 for i, c in enumerate(l, -1): if i & 1: if a >= b or b <= c: if len(fails) > 5: print(0) return fails.append(i) else: if a <= b or b >= c: if len(fails) > 5: print(0) return fails.append(i) a, b = b, c check = compile("and".join( "(l[{:n}]{:s}l[{:n}]{:s}l[{:n}])".format(i - 1, "><"[i & 1], i, "<>"[i & 1], i + 1) for i in fails), "<string>", "eval") for i in fails: a = l[i] for j in range(n): l[i], l[j] = l[j], a if eval(check) and ((l[j - 1] < l[j] > l[j + 1]) if j & 1 else (l[j - 1] > l[j] < l[j + 1])): res += 1 if j in fails else 2 l[j] = l[i] l[i] = a print(res // 2) if __name__ == '__main__': main() ```
15,514
Provide tags and a correct Python 3 solution for this coding contest problem. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Tags: brute force, implementation Correct Solution: ``` def main(): n, l = int(input()), list(map(int, input().split())) if not (n & 1): l.append(0) l.append(150001) i, b, c, fails0, fails1 = 0, 0, 150001, [], [] try: while True: a, b, c = b, c, l[i] if a >= b or b <= c: if len(fails0) + len(fails1) > 5: print(0) return fails1.append(i - 1) i += 1 a, b, c = b, c, l[i] if a <= b or b >= c: if len(fails0) + len(fails1) > 5: print(0) return fails0.append(i - 1) i += 1 except IndexError: fails, res = fails0 + fails1, 0 for i in fails: a = l[i] for j in range(n): f = fails1 if j & 1 else fails0 f.append(j) l[i], l[j] = l[j], a if (all(l[b - 1] > l[b] < l[b + 1] for b in fails0) and all(l[b - 1] < l[b] > l[b + 1] for b in fails1)): res += 1 if j in fails else 2 l[j] = l[i] del f[-1] l[i] = a print(res // 2) if __name__ == '__main__': main() ```
15,515
Provide tags and a correct Python 3 solution for this coding contest problem. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Tags: brute force, implementation Correct Solution: ``` def main(): n, l = int(input()), list(map(int, input().split())) if not (n & 1): l.append(0) l.append(150001) a, b, fails, res = 0, 150001, [], 0 for i, c in enumerate(l, -1): if i & 1: if a >= b or b <= c: if len(fails) > 5: print(0) return fails.append(i) else: if a <= b or b >= c: if len(fails) > 5: print(0) return fails.append(i) a, b = b, c check = compile("and".join( "(l[{:n}]{:s}l[{:n}]{:s}l[{:n}])".format(i - 1, "><"[i & 1], i, "<>"[i & 1], i + 1) for i in fails), "<string>", "eval") for i in fails: a = l[i] for j in range(n): l[i], l[j] = l[j], a if eval(check) and ((l[j - 1] < l[j] > l[j + 1]) if j & 1 else (l[j - 1] > l[j] < l[j + 1])): res += 1 if j in fails else 2 l[j] = l[i] l[i] = a print(res // 2) if __name__ == '__main__': main() # Made By Mostafa_Khaled ```
15,516
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Submitted Solution: ``` n, m = int(input()), 150001 p = [m] + list(map(int, input().split())) + [m * (n & 1)] f = lambda i: p[i] >= p[i + 1] if i & 1 else p[i] <= p[i + 1] g = lambda i, j: sum(f(k) for k in {i, i - 1, j, j - 1}) t = [f(i) for i in range(n + 1)] r, s = 0, sum(t) if s > 4: exit(print(0)) e = {i + 1 for i in range(n) if t[i] or t[i + 1]} for i in e: for j in range(1, n + 1): if (i < j or (i > j and j not in e)) and g(i, j) == s: p[i], p[j] = p[j], p[i] r += g(i, j) == 0 p[i], p[j] = p[j], p[i] print(r) ``` Yes
15,517
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Submitted Solution: ``` def main(): n, l = int(input()), list(map(int, input().split())) if not (n & 1): l.append(0) l.append(150001) a, b, fails, tmp, res = 0, 150001, [], [], 0 for i, c in enumerate(l, -1): if i & 1: if a >= b or b <= c: if len(fails) > 5: print(0) return fails.append(i) else: if a <= b or b >= c: if len(fails) > 5: print(0) return fails.append(i) a, b = b, c for b in fails: tmp.append("><"[b & 1] if b - a == 1 else "and ") tmp.append("l[{:n}]".format(b)) a = b check = compile("".join(tmp[1:]), "<string>", "eval") for i in fails: a = l[i] for j in range(0, n, 2): l[i], l[j] = l[j], a if l[j - 1] > a < l[j + 1] and eval(check): res += 1 if j in fails else 2 l[j] = l[i] for j in range(1, n, 2): l[i], l[j] = l[j], a if l[j - 1] < a > l[j + 1] and eval(check): res += 1 if j in fails else 2 l[j] = l[i] l[i] = a print(res // 2) if __name__ == '__main__': main() ``` Yes
15,518
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Submitted Solution: ``` def main(): n, l = int(input()), list(map(int, input().split())) if not (n & 1): l.append(0) l.append(150001) a, b, fails, res = 0, 150001, [], 0 for i, c in enumerate(l, -1): if i & 1: if a >= b or b <= c: if len(fails) > 5: print(0) return fails.append(i) else: if a <= b or b >= c: if len(fails) > 5: print(0) return fails.append(i) a, b = b, c ff = fails + [0] for i in fails: a = l[i] for j in range(n): l[i], l[j], ff[-1] = l[j], a, j if (all((l[b - 1] < l[b] > l[b + 1]) if b & 1 else (l[b - 1] > l[b] < l[b + 1]) for b in ff)): res += 1 if j in fails else 2 l[j] = l[i] l[i] = a print(res // 2) if __name__ == '__main__': main() ``` Yes
15,519
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Submitted Solution: ``` n = int(input()) t = [int(d) for d in input().split()] m = 0 ind = [0,0] nb = [0,0] res = -1 def mis(i,v): if i==n-1: return False if i==-1: return False if ((v>=t[i+1]) and i%2==0) or ((v<=t[i+1]) and i%2==1): return True return False def miso(i,v): if i==n-1: return False if i==-1: return False if ((t[i]>=v) and i%2==0) or ((t[i]<=v) and i%2==1): return True return False for i in range(n-1): if mis(i,t[i]): #mis if m==2: res = 0 break if nb[m]>0: nb[m] = nb[m]+1 else: ind[m] = i nb[m] = 2 if (i==n-2) and (nb[m]>0): m = m+1 else: #god if m==2: continue if nb[m]>0: m = m+1 def switch(i,j): if i==j-1: t[i],t[j] = t[j],t[i] bol = not (mis(i,t[i]) or miso(i-1,t[i]) or mis(j,t[j]) or miso(j-1,t[j])) t[i],t[j] = t[j],t[i] return bol return not (mis(i,t[j]) or miso(i-1,t[j]) or mis(j,t[i]) or miso(j-1,t[i])) if res==0 or (nb[0]>5) or (nb[1]>5): print(0) elif ((nb[0]==4) or (nb[0]==5)) and (m==2): print(0) elif ((nb[1]==4) or (nb[1]==5)) and (m==2): print(0) else: res = 0 i = ind[0] j = ind[1] if nb[0]==5: #m==1 if switch(i+1,i+3): res = 1 elif nb[0]==4: #m==1 res = int(switch(i+1,i+2))+int(switch(i+1,i+3))+int(switch(i,i+2)) elif m==2: #nb[i] == 2 or 3 if switch(i+1,j+1): res = res+1 if (nb[0]==2) and switch(i,j+1): res = res+1 if (nb[1]==2) and switch(i+1,j): res = res+1 if (nb[0]==2) and (nb[1]==2) and switch(i,j): res = res+1 else: #m==1 for k in range(n): if (k!=i+1) and switch(min(i+1,k),max(i+1,k)): res = res+1 if (nb[0]==2) and (k!=i) and (k!=i+1) and switch(min(i,k),max(i,k)): res = res+1 print(res) ``` Yes
15,520
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Submitted Solution: ``` n = int(input()) a = [int(x) for x in input().split()] def check(x): if x == 0: return a[0] < a[1] if x == len(a)-1: return check(x-1) if x%2 == 0: return (a[x] < a[x+1] and a[x-1] > a[x]) else: return (a[x] > a[x+1] and a[x-1] < a[x]) hits = 0 x = 0 for i in range(len(a)-1): if (i%2) == 0: if a[i] >= a[i+1]: hits += 1 x = i else: if a[i] <= a[i+1]: hits += 1 x = i if hits > 2: print(0) elif hits == 2: print('1') else: ans = 0 d = {} for i in range(len(a)): a[x], a[i] = a[i], a[x] if check(x) and check(i): p,q = sorted((x,i)) if (p,q) not in d: d[(p,q)]=1 ans += 1 a[i], a[x] = a[x], a[i] x += 1 for i in range(len(a)): a[x], a[i] = a[i], a[x] if check(x) and check(i): p,q = sorted((x,i)) if (p,q) not in d: d[(p,q)]=1 ans += 1 a[i], a[x] = a[x], a[i] print(ans) ``` No
15,521
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Submitted Solution: ``` # even, odd def check_even(i): if i == 0: if t[i] <= t[i+1]: return False elif i == n - 1: if t[i] <= t[i-1]: return False elif t[i] <= t[i-1] or t[i] <= t[i+1]: return False return True def check_odd(i): if i == 0: if t[i] >= t[i+1]: return False elif i == n - 1: if t[i] >= t[i-1]: return False elif t[i] >= t[i-1] or t[i] >= t[i+1]: return False return True def check(x=None): for j in not_nice: for k in range(-1, 2): if j + k < 0 or j + k >= n: continue if (j + k + 1) % 2 == 0: if not check_even(j + k): return False else: if not check_odd(j + k): return False if x: if (x + 1) % 2 == 0: if not check_even(x): return False else: if not check_odd(x): return False return True n = int(input()) t = list(map(int, input().split())) not_nice = list() for i in range(n): if (i + 1) % 2 == 0: if not check_even(i): not_nice.append(i) else: if not check_odd(i): not_nice.append(i) if len(not_nice) > 10: print(0) else: ret = set() for x in not_nice: for i in range(n): if (x + 1) % 2 == 0 and t[x] >= t[i]: continue if (x + 1) % 2 == 1 and t[x] <= t[i]: continue t[i], t[x] = t[x], t[i] if check(x): if i < x: ret.add((i, x)) else: ret.add((x, i)) t[i], t[x] = t[x], t[i] print(len(ret)) ``` No
15,522
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Submitted Solution: ``` n = int(input()) t = list(map(int, input().split())) def checkeven(i): if i > 0: if t[i] <= t[i-1]: return False if i < n-1: if t[i] <= t[i+1]: return False return True def checkodd(i): if i > 0: if t[i] >= t[i-1]: return False if i < n-1: if t[i] >= t[i+1]: return False return True def checkswap(i, x, p): if (i < 0 or i >= n or x < 0 or x >= n): return False t[i], t[x] = t[x], t[i] check = True if (i % 2 == 0): check = checkodd(i) else: check = checkeven(i) if not check: t[i], t[x] = t[x], t[i] return check if (x % 2 == 0): check = checkodd(x) else: check = checkeven(x) if not check: t[i], t[x] = t[x], t[i] return check if (p % 2 == 0): check = checkodd(p) else: check = checkeven(p) if not check: t[i], t[x] = t[x], t[i] return check if check: t[i], t[x] = t[x], t[i] return check def checkswapanother(i, x, p, q): if (i < 0 or i >= n or x < 0 or x >= n): return False t[i], t[x] = t[x], t[i] check = True if (i % 2 == 0): check = checkodd(i) else: check = checkeven(i) if not check: t[i], t[x] = t[x], t[i] return check if (x % 2 == 0): check = checkodd(x) else: check = checkeven(x) if not check: t[i], t[x] = t[x], t[i] return check if (p % 2 == 0): check = checkodd(p) else: check = checkeven(p) if not check: t[i], t[x] = t[x], t[i] return check if (q % 2 == 0): check = checkodd(q) else: check = checkeven(q) if not check: t[i], t[x] = t[x], t[i] return check if check: t[i], t[x] = t[x], t[i] return check if (n == 2): print("1") else: index = [-1, -1] j = 0 count = 0 # count no of wrongly placed integers for i in range(1, n, 2): if (t[i] <= t[i-1]): if count == 2: count += 1 break index[j] = i j = j + 1 count = count + 1 elif (i < n-1 and t[i] <= t[i+1]): if count == 2: count += 1 break index[j] = i j = j + 1 count = count + 1 if count >= 3: print("0") if count == 1: ans = 0 for i in range(n): for j in range(-1, 2): x = index[0] + j if (x == i): continue if (i == index[0]) or (i == index[0]-1) or (i == index[0]+1):continue #print(i, x) ch = checkswap(i,x, index[0]) if ch: ans += 1 if checkswap(index[0]-1, index[0], index[0]): ans+=1 if checkswap(index[0]-1, index[0]+1, index[0]): ans+=1 if checkswap(index[0], index[0]+1, index[0]): ans+=1 print(ans) if count == 2: ans = 0 for i in range(-1, 2): for j in range(-1, 2): x = index[0] + i y = index[1] + j if (x == y): continue ch = checkswapanother(x,y,index[0], index[1]) if ch: ans += 1 print(ans) ``` No
15,523
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: * ti < ti + 1 for each odd i < n; * ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. Input The first line of the input contains one integer n (2 ≀ n ≀ 150 000) β€” the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 150 000) β€” the initial sequence. It's guaranteed that the given sequence is not nice. Output Print the number of ways to swap two elements exactly once in order to get a nice sequence. Examples Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 Note In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap t2 = 8 with t4 = 7. 2. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way β€” Limak should swap t1 = 200 with t4 = 50. Submitted Solution: ``` # You lost the game. def tester(L,i,j): e = i % 2 f = j % 2 r = 0 R = L[:] R[i],R[j] = R[j],R[i] if e == 0: if R[i] > R[i+1] and R[i] > R[i-1]: r += 1 else: if R[i] < R[i+1] and R[i] < R[i-1]: r += 1 if f == 0: if R[j] > R[j+1] and R[j] > R[j-1]: r += 1 else: if R[j] < R[j+1] and R[j] < R[j-1]: r += 1 return (r == 2) n = int(input()) L = list(map(int, input().split())) a = 0 F = [] for i in range(1,n): if i % 2 and L[i] <= L[i-1]: a += 1 F = F + [i] if i % 2 == 0 and L[i] >= L[i-1]: a += 1 F = F + [i] L = [10**6]+L+[(-1)**(1-n%2)*10**6] if a > 3: print(0) elif a == 2: z = F[0] b = F[0]+1 c = F[1] d = F[1]+1 r = 0 P = [[z,c],[z,d],[b,c],[b,d]] for i in range(4): r = r + tester(L,P[i][0],P[i][1]) print(r) elif a == 3: z = F[0] b = F[0]+1 c = F[1] d = F[1]+1 e = F[2] f = F[2]+1 if b < c and d < e: print(0) elif c == b and d < e: r = 0 + tester(L,c,f) elif d == e and b < c: r = 0 + tester(L,z,d) elif d == e and b == c: r = tester(L,z,d) + tester(L,b,d) + tester(L,c,f) print(r) else: z = F[0] b = F[0]+1 r = 0 for i in range(1,n+1): if i != z: r = r + tester(L,min(i,z),max(i,z)) for i in range(1,n+1): if i != z and i != b: r = r + tester(L,min(i,b),max(i,b)) print(r) ``` No
15,524
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Tags: constructive algorithms, interactive, math Correct Solution: ``` import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect from time import perf_counter from fractions import Fraction # sys.setrecursionlimit(pow(10, 6)) # sys.stdin = open("input.txt", "r") # sys.stdout = open("output.txt", "w") mod = pow(10, 9) + 7 mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end) def l(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] def prime(number): if number == 2: return True if number & 1: i = 2 while i * i <= number: if number % i == 0: return False i += 1 return True return False arr = [] for i in range(2, 101): if prime(i): arr.append(i) if i * i <= 100: arr.append(i*i) arr.sort() low, high = 0, 24 c = 0 for i in range(20): out(arr[i]) sys.stdout.flush() verdict = data() if verdict.lower() == 'yes': c += 1 if c == 2: out("composite") exit() out("prime") ```
15,525
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Tags: constructive algorithms, interactive, math Correct Solution: ``` import sys def main(): a = [2,3,4,5,7,9,11,13,17,19,23,25,29,31,37,41,43,47,49] factors_count = 0 for i in a: print(i, flush=True) read = sys.stdin.readline().strip() if read == "yes": factors_count += 1 if factors_count > 1: break if factors_count>1: print("composite", flush=True) else: print("prime", flush=True) main() ```
15,526
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Tags: constructive algorithms, interactive, math Correct Solution: ``` import os import sys if os.path.exists('/mnt/c/Users/Square/square/codeforces'): f = iter(open('A.txt').readlines()) def input(): return next(f) input = lambda: sys.stdin.readline().strip() else: input = lambda: sys.stdin.readline().strip() def primes(n): A = [1] * (n+1) A[0] = A[1] = 1 primes = [] lenA = len(A) for i in range(2, lenA): if A[i] == 1: primes.append(i) for j in range(i*2, lenA, i): A[j] = 0 return primes # print(len(primes(50))) def main(): ps = primes(50) ps.extend([i**2 for i in primes(10)]) # print(len(ps)) r = 0 for p in ps: print(p, flush=True) if input() == 'yes': r += 1 if r <= 1: print('prime', flush=True) else: print('composite', flush=True) main() # return 'no' # print(main()) # l, r = 1, 10**6+1 # while l + 1 < r: # cur = (l + r) // 2 # print(cur, flush=True) # res = input() # if res == '>=': # l = cur # else: # r = cur # print('! %d' % l, flush=True) ```
15,527
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Tags: constructive algorithms, interactive, math Correct Solution: ``` import sys input = sys.stdin.readline flush = sys.stdout.flush def query(x): print(x) flush() y = input()[:-1] return 1 if y == "yes" else 0 A = [2, 3, 4, 5, 7, 9, 11, 13, 17, 19, 23, 25, 29, 31, 37, 41, 43, 47, 49] cnt = 0 for a in A: cnt += query(a) print("composite" if cnt >= 2 else "prime") ```
15,528
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Tags: constructive algorithms, interactive, math Correct Solution: ``` #------------------Important Modules------------------# from sys import stdin,stdout from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import * from random import * from itertools import permutations input=stdin.readline prin=stdout.write from random import sample from collections import Counter,deque from fractions import * from math import sqrt,ceil,log2,gcd,cos,pi,floor from copy import deepcopy #dist=[0]*(n) mod=10**9+7 mod2=998244353 def ps(n): cp=0;lk=0;arr={} lk=0;ap=n cc=0 while n%2==0: n=n//2 cc=1 if cc==1: lk+=1 for ps in range(3,ceil(sqrt(n))+1,2): #print(ps) cc=0 while n%ps==0: n=n//ps cc=1 lk+=1 if cc==1 else 0 if n!=1: lk+=1 if lk==1: return False #print(arr) return True #count=0 #dp=[[0 for i in range(m)] for j in range(n)] #[int(x) for x in input().strip().split()] def gcd(x, y): while(y): x, y = y, x % y return x # Driver Code def factorials(n,r): #This calculates ncr mod 10**9+7 slr=n;dpr=r qlr=1;qs=1 mod=10**9+7 for ip in range(n-r+1,n): qlr=(qlr*ip)%mod for ij in range(1,r): qs=(qs*ij)%mod #print(qlr,qs) ans=(qlr*modInverse(qs))%mod return ans def modInverse(b): qr=10**9+7 return pow(b, qr - 2,qr) #=============================================================================================== ### START ITERATE RECURSION ### from types import GeneratorType def iterative(f, stack=[]): def wrapped_func(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) continue stack.pop() if not stack: break to = stack[-1].send(to) return to return wrapped_func def power(arr): listrep = arr subsets = [] for i in range(2**len(listrep)): subset = [] for k in range(len(listrep)): if i & 1<<k: subset.append(listrep[k]) subsets.append(subset) return subsets def pda(n) : list=[];su=0 for i in range(1, int(sqrt(n) + 1)) : if (n % i == 0) : if (n // i == i) : list.append(i) su+=i else : list.append(n//i);list.append(i) su+=i;su+=n//i # The list will be printed in reverse return su def dis(xa,ya,xb,yb): return sqrt((xa-xb)**2+(ya-yb)**2) #### END ITERATE RECURSION #### #=============================================================================================== #----------Input functions--------------------# def ii(): return int(input()) def ilist(): return [int(x) for x in input().strip().split()] def islist(): return list(map(str,input().split().rstrip())) def inp(): return input().strip() def google(test): return "Case #"+str(test)+": "; def overlap(x1,y1,x2,y2): if x2>y1: return y1-x2 if y1>y2: return y2-x2 return y1-x2; ###-------------------------CODE STARTS HERE--------------------------------########### def dist(x1,y1,x2,y2): return sqrt((x1-x2)**2+(y1-y2)**2) def sieve(n): prime = [True for i in range(n + 1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * 2, n + 1, p): prime[i] = False p += 1 prime[0]= False prime[1]= False ans=[] for i in range(1,n+1): if prime[i]: ans.append(i) return ans def prod(arr): n=len(arr) k=1 for j in range(n): k*=arr[j] return k def SumOfDigits(s): su=0 while (s): su+=s%10 s=s//10 return su ######################################################################################### #def valid(sec,hr,min,nano): #t=int(input()) t=1 ans=sieve(50) for i in ans: if i*i<50: ans.append(i*i) ans.sort() for pl in range(t): cc=1 co=0 #print(len(ans)) for query in ans: print(query) stdout.flush() result=inp() if result=="yes": co+=1 if co>=2: cc=0 break an="composite" if cc==0 else "prime" print(an);stdout.flush() ```
15,529
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Tags: constructive algorithms, interactive, math Correct Solution: ``` #Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- a=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,4,9,25,49] n=len(a) count=0 for i in a: print(i) sys.stdout.flush() s=input() if s=="yes": count+=1 if count>=2: print("composite") else: print("prime") ```
15,530
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Tags: constructive algorithms, interactive, math Correct Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * # from fractions import * # from heapq import* from bisect import * from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ALPHA='abcdefghijklmnopqrstuvwxyz' M=10**9+7 EPS=1e-6 def Ceil(a,b): return a//b+int(a%b>0) def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() def primeN(n): prime = [True for i in range(n+1)] prime[0]=False prime[1]=False p=2 while(p*p<=n): if(prime[p]): for i in range(p*p,n+1,p): prime[i]=False p+=1 return [p for p in range(n+1) if(prime[p])] primes=primeN(50) # print(primes) have={} have[2]=[37, 41, 43, 47] have[3]=[23, 29, 31] have[5]=[17, 19] have[7]=[11, 13] found=0 for i in [2,3,5,7]: print(i,flush=True) res=input() if(res=='yes'):found+=1 for p in have[i]: print(p,flush=True) res=input() if(res=='yes'):found+=1 print(i*i,flush=True) res=input() if(res=='yes'):found+=1 if(found>1): print("composite",flush=True) exit() print("prime",flush=True) exit() ```
15,531
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Tags: constructive algorithms, interactive, math Correct Solution: ``` import os import sys from io import BytesIO, IOBase import math from decimal import * from collections import defaultdict getcontext().prec = 25 MOD = pow(10, 9) + 7 BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # n, k = map(int, input().split(" ")) # = list(map(int, input().split(" "))) # for _ in range(int(input())): a = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] ans = [] for i in range(15): print(a[i]) sys.stdout.flush() ans.append(input()) if ans.count("yes") > 1: print("composite") elif ans.count('yes') == 1: yes = 1 ind = 10 for i in range(4): if ans[i] == "yes": ind = a[i] break j = 2 while ind ** j < 101: print(ind ** j) sys.stdout.flush() if input() == "yes": yes += 1 break j+=1 if yes == 1: print('prime') else: print('composite') else: print('prime') ```
15,532
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Submitted Solution: ``` from sys import stdout, stdin PS = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] # PSL50 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] ## len == 15 # PSU50 = [53, 59, 61, 67, 71, 73, 79, 83, 89, 97] ## len == 10 def ask_and_yes( num): print(num) stdout.flush() ## get cpu answer line = stdin.readline().strip() return line == "yes" def solve(): # yes_cnt = 0 first_p = -1 for p in PS: if p >= 53: print("prime") return else: if ask_and_yes( p): first_p = p break ## rewind back quo = int(100/first_p) ## deal with edge cases if first_p >= quo: print( "prime") return ## get the first feasible prime for i in range( len( PS)-1, -1, -1): if PS[ i] < quo: break ## start questioning again for j in range( i, -1, -1): if PS[ j] == first_p: break ## fixed else: if ask_and_yes( PS[ j]): print( "composite") return ## one last time ## fixed if ask_and_yes( first_p*first_p): print( "composite") else: print( "prime") solve() ``` Yes
15,533
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Submitted Solution: ``` import sys def is_prime(n, primes): for p in primes: if n % p == 0: return False else: return True def main(): primes = [2, 3, 5, 7] for i in range(primes[-1] + 2, 49, 2): if is_prime(i, primes): primes.append(i) ndivs = 0 last_divisor = -1 for p in primes[:4]: print(p) sys.stdout.flush() ans = sys.stdin.readline().strip() == "no" ndivs += not ans if not ans: last_divisor = p if ndivs >= 2: break if ndivs > 1: print("composite") elif ndivs == 0: print("prime") else: # ndivs == 1 print(last_divisor * last_divisor) sys.stdout.flush() ans = sys.stdin.readline().strip() == "no" if not ans: print("composite") else: for p in primes[4:]: print(p) sys.stdout.flush() ans = sys.stdin.readline().strip() == "no" if not ans: print("composite") break else: print("prime") sys.stdout.flush() if __name__ == "__main__": main() ``` Yes
15,534
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Submitted Solution: ``` import os import sys if os.path.exists('/mnt/c/Users/Square/square/codeforces'): f = iter(open('A.txt').readlines()) def input(): return next(f) input = lambda: sys.stdin.readline().strip() else: input = lambda: sys.stdin.readline().strip() fprint = lambda *args: print(*args, flush=True) def primes(n): A = [1] * (n+1) A[0] = A[1] = 1 primes = [] lenA = len(A) for i in range(2, lenA): if A[i] == 1: primes.append(i) for j in range(i*2, lenA, i): A[j] = 0 return primes # print(len(primes(50))) def main(): ps = primes(50) ps.extend([i**2 for i in primes(10)]) # print(len(ps)) r = 0 for p in ps: fprint(p) if input() == 'yes': r += 1 if r <= 1: fprint('prime') else: fprint('composite') main() # return 'no' # print(main()) # l, r = 1, 10**6+1 # while l + 1 < r: # cur = (l + r) // 2 # print(cur, flush=True) # res = input() # if res == '>=': # l = cur # else: # r = cur # print('! %d' % l, flush=True) ``` Yes
15,535
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Submitted Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * # from fractions import * # from heapq import* from bisect import * from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ALPHA='abcdefghijklmnopqrstuvwxyz' M=10**9+7 EPS=1e-6 def Ceil(a,b): return a//b+int(a%b>0) def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() def primeN(n): prime = [True for i in range(n+1)] prime[0]=False prime[1]=False p=2 while(p*p<=n): if(prime[p]): for i in range(p*p,n+1,p): prime[i]=False p+=1 return [p for p in range(n+1) if(prime[p])] primes=primeN(50) found=0 for i in primes: print(i,flush=True) res=input() if(res=='yes'):found+=1 if(i>7):continue print(i*i,flush=True) res=input() if(res=='yes'):found+=1 if(found>1): print("composite",flush=True) else: print("prime",flush=True) exit() ``` Yes
15,536
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Submitted Solution: ``` from sys import stdout arr = [] for i in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]: print(i) stdout.flush() arr.append(input()) if arr.count('yes') > 1: print('composite') else: print('prime') ``` No
15,537
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Submitted Solution: ``` import sys from math import sqrt def pl(): l=[] for i in range(2,101): k=[] for j in range(2,int(sqrt(i))+1): if i%j==0: k+=[j] if (k==[]): l+=[i] return l def main(): k=pl() l=[] i=0 while i<20: print(k[i]) sys.stdout.flush() s=input() if s=='yes' and k[i]==2: print(4) sys.stdout.flush() j=input() if j=='yes': l+=[k[i],4] else: l+=[k[i]] i+=2 elif s=='yes' and k[i]==3: print(9) sys.stdout.flush() j=input() if j=='yes': l+=[k[i],9] else: l+=[k[i]] i+=2 elif s=='yes' and k[i]==5: print(25) sys.stdout.flush() j=input() if j=='yes': l+=[k[i],25] else: l+=[k[i]] i+=2 elif s=='yes' and k[i]==7: print(49) sys.stdout.flush() j=input() if j=='yes': l+=[k[i],49] else: l+=[k[i]] i+=2 elif s=='yes': l+=[k[i]] i+=1 elif s=='no': i+=1 continue if len(l)<=1: print('prime') sys.stdout.flush() else: print('composite') sys.stdout.flush if __name__=='__main__': main() ``` No
15,538
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Submitted Solution: ``` import os import sys if os.path.exists('/mnt/c/Users/Square/square/codeforces'): f = iter(open('A.txt').readlines()) def input(): return next(f) input = lambda: sys.stdin.readline().strip() else: input = lambda: sys.stdin.readline().strip() def primes(n): A = [1] * (n+1) A[0] = A[1] = 1 primes = [] lenA = len(A) for i in range(2, lenA): if A[i] == 1: primes.append(i) for j in range(i*2, lenA, i): A[j] = 0 return primes print(len(primes(50))) def main(): ps = primes(100) ps.append([i**2 for i in primes(10)]) r = 0 for p in ps: print(p, flush=True) if input() == 'yes': res += 1 if res <= 1: print('yes', flush=True) else: print('no', flush=True) # return 'no' # print(main()) # l, r = 1, 10**6+1 # while l + 1 < r: # cur = (l + r) // 2 # print(cur, flush=True) # res = input() # if res == '>=': # l = cur # else: # r = cur # print('! %d' % l, flush=True) ``` No
15,539
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an interactive problem. In the output section below you will see the information about flushing the output. Bear Limak thinks of some hidden number β€” an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite. Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite. You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no". For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14. When you are done asking queries, print "prime" or "composite" and terminate your program. You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct. You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). Input After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. Output Up to 20 times you can ask a query β€” print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number β€” one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. Examples Input yes no yes Output 2 80 5 composite Input no yes no no no Output 58 59 78 78 2 prime Note The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process. <image> The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30. <image> 59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). Submitted Solution: ``` primes = (2, 3, 5, 7, 11, 13, 17, 19, 23) count = 0 for p in primes: print(p) if input().strip() == "yes": count += 1 print(p * p) if input().strip() == "yes": count += 1 print("composite" if count >= 2 else "prime") ``` No
15,540
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Tags: data structures, math Correct Solution: ``` n,m=map(int,input().split()) k=n**2 ki=[] cx=set();cy=set() a=[False]*n b=[False]*n x1=y1=-1 d=0 for i in range(m): d=0 x,y=map(int,input().split()) x-=1;y-=1 if a[x]==True and b[y]==True: ki.append(k) continue if a[x]==False: a[x]=True x1+=1 if b[y]==False: b[y]=True y1+=1 k-=(2*n-1-x1-y1) else: k-=(n-1-y1) else: b[y]=True y1+=1 d+=1 k-=(n-1-x1) ki.append(k) print(*ki) ```
15,541
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Tags: data structures, math Correct Solution: ``` x = input() n, m = list(map(int, x.strip().split())) vseh_polj = n**2 vrstice = set() stolpci = set() vrni = [] for _ in range(m): u = input() x, y = list(map(int, u.strip().split())) x, y = x - 1, y - 1 vrstice.add(x) stolpci.add(y) vrni.append(str(vseh_polj - n*len(vrstice) - len(stolpci)*(n - len(vrstice)))) print(' '.join(vrni)) ```
15,542
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Tags: data structures, math Correct Solution: ``` import sys def main(): size, n_rooks = [int(x) for x in sys.stdin.readline().split()] rows_akd = set() cols_akd = set() for line in sys.stdin.readlines(): (x,y) = line.split() rows_akd.add(x) cols_akd.add(y) # print(rows_akd, cols_akd) safe = size ** 2 safe -= len(rows_akd) * size safe -= len(cols_akd) * (size - len(rows_akd)) print(safe, end=' ') print() if __name__ == '__main__': main() ```
15,543
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Tags: data structures, math Correct Solution: ``` I = lambda :map(int, input().split()) n,m=I() R=[True]*100001 C=[True]*100001 c=n r=n for i in range(m): x,y=I() r-=R[x] c-=C[y] C[y]=False R[x]=False print(r*c, end=' ') ```
15,544
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Tags: data structures, math Correct Solution: ``` n, m = map(int, input().split()) rows = set(range(1, n+1)) columns = set(range(1, n+1)) for i in range(m): x, y = map(int, input().split()) rows.discard(x) columns.discard(y) print(len(rows) * len(columns), end=' ') print() ```
15,545
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Tags: data structures, math Correct Solution: ``` base1,base2=map(int,input().split(' ')) row=['F' for i in range(base1)] col=['F' for i in range(base1)] nx=ny=base1 for i in range(base2): xx,yy= map(int,input().split(' ')) if row[xx-1]=='F': nx-=1 if col[yy-1]=='F': ny-=1 row[xx-1]='B' col[yy-1]='B' print (nx*ny) ```
15,546
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Tags: data structures, math Correct Solution: ``` from sys import stdin input=lambda : stdin.readline().strip() from math import ceil,sqrt,factorial INT_MIN=float("-infinity") INT_MAX=float("infinity") n,m=map(int,input().split()) row=set() col=set() su=n*n for i in range(m): a,b=map(int,input().split()) if a not in row: su-=n-len(col) row.add(a) # print(su) if b not in col: su-=n-len(row) col.add(b) print(su,end=' ') ```
15,547
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Tags: data structures, math Correct Solution: ``` n,m = map(int, input().split()) all_x = set() all_y = set() ans = [] for i in range(m): x,y = map(int, input().split()) all_x.add(x) all_y.add(y) ans.append((n - len(all_x)) * (n - len(all_y))) print(*ans) ```
15,548
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Submitted Solution: ``` n, m = map(int, input().split()) v = [0]*(10**5 + 1) k1 = k2 = 0 h = [0]*(10**5 + 1) ans = n * n for i in range(m): x, y = map(int, input().split()) if not v[x] or not h[y]: if v[x]: ans -= n - k1 elif h[y]: ans -= n - k2 else: ans -= 2*n-k1-k2-1 if not v[x]: v[x] = 1 k1 += 1 if not h[y]: h[y] = 1 k2 += 1 print(ans) ``` Yes
15,549
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Submitted Solution: ``` def main(): n, m = map(int, input().split()) row_remains = n col_remains = n row_used = dict() col_used = dict() ans = list() for i in range(m): x, y = map(int, input().split()) if x not in row_used: row_used[x] = True row_remains -= 1 if y not in col_used: col_used[y] = True col_remains -= 1 ans.append(row_remains * col_remains) print(' '.join(str(x) for x in ans)) if __name__ == '__main__': main() ``` Yes
15,550
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Submitted Solution: ``` s,rooks=map(int,input().split()) columns,rows=[0]*s,[0]*s cN,cR=0,0 taken=0 for i in range(rooks): a,b=map(int,input().split()) if columns[a-1]==0: cN+=1 taken+=s-cR columns[a-1]=1 if rows[b-1]==0: cR+=1 taken+=s-cN rows[b-1]=1 print(s**2-taken,end=' ') ``` Yes
15,551
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Submitted Solution: ``` board_size,rooks=map(int,(input().split())) valid_squares=board_size**2 row_set=set() col_set=set() soln=[] for i in range (rooks): row,col=map(int,input().split()) if row not in row_set and col not in col_set: valid_squares-=(2*board_size)-len(row_set)-len(col_set)-1 elif row not in row_set: valid_squares-=board_size-len(col_set) elif col not in col_set: valid_squares-=board_size-len(row_set) row_set.add(row) col_set.add(col) soln.append(valid_squares) print(*soln) ``` Yes
15,552
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Submitted Solution: ``` # -*- coding: utf-8 -*- # Baqir Khan # Software Engineer (Backend) n, m = map(int, input().split()) safe = n * n ans = [] rows = set() cols = set() while m: m -= 1 x, y = map(int, input().split()) if x not in rows and y not in cols: safe -= (n + n - len(rows) - len(cols) - 1) rows.add(x) cols.add(y) if x in rows and y not in cols: safe -= (n - len(rows)) cols.add(x) if x not in rows and y in cols: safe -= (n - len(cols)) rows.add(x) ans.append(safe) print(" ".join([str(a) for a in ans])) ``` No
15,553
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Submitted Solution: ``` n, m = [int(i) for i in input().split()] x_list = [] y_list = [] for i in range(m): p, q = [int(i) for i in input().split()] if i == 0: x, y = n, n c = x * y if p in x_list: print(c - y, end=' ') x -= 1 y_list.append(q) elif q in y_list: print(c - x, end=' ') y -= 1 x_list.append(p) else: print(c - (x + y - 1), end=' ') x_list.append(p) y_list.append(q) x -= 1 y -= 1 ``` No
15,554
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Submitted Solution: ``` n,m=map(int,input().split()) col=set([]) row=set([]) s=n*n l=[] for i in range(m): x,y=map(int,input().split()) if x not in col and y not in row: s-=2*n-1-len(row)-len(col) elif y not in row and x in col: s-=n-len(row) elif x not in col and y in row: s-=n-len(col) l.append(s) col.add(x) row.add(y) print(*l) ``` No
15,555
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has the square chessboard of size n Γ— n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack. You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100 000, 1 ≀ m ≀ min(100 000, n2)) β€” the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≀ xi, yi ≀ n) β€” the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook. Output Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put. Examples Input 3 3 1 1 3 1 2 2 Output 4 2 0 Input 5 2 1 5 5 1 Output 16 9 Input 100000 1 300 400 Output 9999800001 Note On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. <image> Submitted Solution: ``` n, m = list(map(int, input().split())) v = [] h = [] nb = n ** 2 st = n * 2 - 1 for i in range(0, m): y, x = list(map(int, input().split())) y-=1 x-=1 if y not in v and x not in h: nb -= st st -= 2 elif (y not in v and x in h) or (y in v and x not in h): nb -= (st - 1) if n%2==0: st -= 1 else: pass if nb<0: nb=0 print((nb), end=" ") v.append(y) h.append(x) ``` No
15,556
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Tags: greedy Correct Solution: ``` from collections import defaultdict N, M = map(int, input().split()) ori = list(map(int, input().split())) best = N // M c = defaultdict(int) for o in ori: if 1 <= o <= M: c[o] += 1 counter = {} more = set() less = set() for i in range(1, M+1): counter[i] = c[i] - best if i in c else -best if best < c[i]: more.add(i) elif best > c[i]: less.add(i) # print("more") # print(more) # print("less") # print(less) # print(counter) move = 0 for i in range(N): if ori[i] not in counter: if less: k = next(iter(less)) ori[i] = k counter[k] += 1 if counter[k] == 0: less.remove(k) move += 1 elif ori[i] in more: if less: counter[ori[i]] -= 1 if counter[ori[i]] == 0: more.remove(ori[i]) k = next(iter(less)) ori[i] = k counter[k] += 1 if counter[k] == 0: less.remove(k) move += 1 print(str(best) + " " + str(move)) print(" ".join(map(str, ori))) ```
15,557
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Tags: greedy Correct Solution: ``` import copy n, m = map(int, input().split()) a = list(map(int, input().split())) d = copy.copy(a) a.sort() s = n // m b = [0] * m cnt = 0 q = 0 for el in a: if el <= m and b[el - 1] < s: b[el - 1] += 1 q += 1 ans = (n - q - n % m) print(s, ans) a = d q = copy.copy(b) # pos = 0 for el in a: if el <= m and b[el - 1] > 0: b[el - 1] -= 1 print(el, end = ' ') else: while pos < m and q[pos] >= s: pos += 1 if pos < m: q[pos] += 1 print(pos + 1, end = ' ') else: print(el, end = ' ') #print(d) ```
15,558
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Tags: greedy Correct Solution: ``` n,m = (int(x) for x in input().split()) l = [ int(x) for x in input().split() ] count = [0]*(m+1) overcount = 0 dielist = [] changelist = [] maxmin = int(n/m) for ind,i in enumerate(l): if i <= m: count[i] += 1 if count[i] > maxmin: dielist.append(ind) else: dielist.append(ind) changes = 0 for i in range(1,len(count)): c = count[i] if c < maxmin: changes += maxmin - c for j in range(maxmin - c): l[dielist[overcount]] = i overcount += 1 print(maxmin, changes) print(' '.join([str(x) for x in l])) ```
15,559
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Tags: greedy Correct Solution: ``` n,m=[int(item) for item in input().split()] a=[int(item) for item in input().split()] mx=n//m r=n%m c=0 counts=[a.count(i) for i in range(1,m+1)] queue=[] for i in range(1,m+1): queue=[i]*(mx-counts[i-1])+queue for i in range(n): if a[i]>m: if queue: curr=queue.pop() a[i]=curr counts[a[i]-1]+=1 c+=1 else: break for i in range(n): if not queue: break if counts[a[i]-1]>mx: counts[a[i]-1]-=1 a[i]=queue.pop() counts[a[i]-1]+=1 c+=1 print(mx,c) print(" ".join([str(item) for item in a])) ```
15,560
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Tags: greedy Correct Solution: ``` from collections import Counter n, m = map(int, input().split()) nums = list(map(int, input().split())) cnts = dict(Counter(nums)) for i in range(1, m+1): if i not in cnts: cnts[i] = 0 def minner(): return min(cnts.items(), key=lambda x: x[1]) n //= m res = 0 for i, num in enumerate(nums): if num > m or cnts[num] > n: for r in range(1, m+1): if cnts[r] < n: cnts[num] -= 1 nums[i] = r cnts[r] += 1 res += 1 break print(n, res) print(' '.join(map(str, nums))) ```
15,561
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Tags: greedy Correct Solution: ``` n, m = [int(i) for i in input().split()] a = [int(i) for i in input().split()] b = [0] * (m + 1) low = int(n / m) changes = 0 for i in a: if i <= m: b[i] += 1 bi = 1 for i in range(n): done = False while b[bi] >= low: bi += 1 if bi > m: done = True break if done: break if a[i] > m: changes += 1 a[i] = bi b[bi] += 1 else: if b[a[i]] > low: b[a[i]] -= 1 b[bi] += 1 a[i] = bi changes += 1 print(low, changes) print(" ".join([str(i) for i in a])) ```
15,562
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Tags: greedy Correct Solution: ``` n, m = map(int, input().split()) a = list(map(int, input().split())) good = n // m ans = 0 cnt = [0] * m ch = [0] * n for i in range(n): if 1 <= a[i] <= m: cnt[a[i] - 1] += 1 if cnt[a[i] - 1] <= good: ch[i] = 1 for i in range(m): ans += max(0, good - cnt[i]) print(good, ans) j = 0 for i in range(n): if ch[i]: continue while j < m and good <= cnt[j]: j += 1 if j == m: break a[i] = j + 1 cnt[j] += 1 print(*a) ```
15,563
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Tags: greedy Correct Solution: ``` import collections n,m = [int(s) for s in input().split()] a = [int(s) for s in input().split()] ctr = collections.Counter(a) nummin = n//m inses = [] for b in range(1,m+1): if ctr[b] < nummin: inses.extend([b]*(nummin - ctr[b])) num_ins = 0 for i in range(n): b = a[i] if ctr[b] > nummin or b > m: if num_ins >= len(inses): break a[i] = inses[num_ins] ctr[b] -= 1 num_ins += 1 print(nummin, len(inses)) print(' '.join(map(str,a))) ```
15,564
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Submitted Solution: ``` #!/usr/bin/env python3 from collections import Counter n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] tgt = n // m b = Counter(a) rd = sum(b[x] for x in b if x > m) r = 0 for i in range(1, m+1): while rd and b[i] < tgt: for j in range(n): if a[j] > m: b[a[j]] -= 1 b[i] += 1 a[j] = i rd -= 1 r += 1 break while b[i] < tgt: for j in range(n): if b[a[j]] > tgt: b[a[j]] -= 1 b[i] += 1 a[j] = i r += 1 break print(tgt, r) print(" ".join(str(x) for x in a)) ``` Yes
15,565
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Submitted Solution: ``` #python3 # utf-8 from collections import Counter songs_nr, fav_groups_nr = (int(x) for x in input().split()) song_idx___group_idx = [int(x) for x in input().split()] fav_group_idx___songs_nr = {} for fav_group_idx in range(1, fav_groups_nr + 1): fav_group_idx___songs_nr[fav_group_idx] = 0 for group_idx in song_idx___group_idx: if group_idx <= fav_groups_nr: fav_group_idx___songs_nr[group_idx] += 1 min_max_value = songs_nr // fav_groups_nr possib_replacements_nr = 0 for song_idx in range(songs_nr): group_idx = song_idx___group_idx[song_idx] if group_idx <= fav_groups_nr: continue possib_replacements_nr += 1 for fav_group_idx, fav_songs_nr in fav_group_idx___songs_nr.items(): if fav_songs_nr > min_max_value: possib_replacements_nr += fav_songs_nr - min_max_value replacements_nr = 0 while True: curr_group_idx = min(fav_group_idx___songs_nr, key=lambda x: fav_group_idx___songs_nr[x]) if min(fav_group_idx___songs_nr.values()) >= min_max_value: break elif replacements_nr == possib_replacements_nr: break for song_idx in range(songs_nr): group_idx = song_idx___group_idx[song_idx] if group_idx > fav_groups_nr: replacements_nr += 1 song_idx___group_idx[song_idx] = curr_group_idx fav_group_idx___songs_nr[curr_group_idx] += 1 break elif fav_group_idx___songs_nr[group_idx] > min_max_value: replacements_nr += 1 song_idx___group_idx[song_idx] = curr_group_idx fav_group_idx___songs_nr[group_idx] -= 1 fav_group_idx___songs_nr[curr_group_idx] += 1 break print(min_max_value, replacements_nr) print(*song_idx___group_idx) ``` Yes
15,566
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Submitted Solution: ``` n, m = map(int, input().split()) target = n//m A = [int(i) for i in input().split()] B = [0 for _ in range(2005)] R = B[:] ch = 0 for i in A: if i <= m: B[i] += 1 for i in range(2005): R[i] = target - B[i] j = 1 for i in range(n): if A[i] > m: while j <= m and R[j] <= 0: j += 1 if j <= m and R[j] > 0: ch += 1 R[j] -= 1 A[i] = j for i in range(n): if A[i] <= m and R[A[i]] < 0: while j <= m and R[j] <= 0: j += 1 if j <= m and R[j] > 0: R[j] -= 1 R[A[i]] += 1 A[i] = j ch += 1 print(target, ch) print(*A) ``` Yes
15,567
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Submitted Solution: ``` n,m = map(int, input().split()) A = dict() per = list(map(int, input().split())) cou =[0] * m for j in range(n): if per[j] <= m: cou[per[j]-1] +=1 ans = 0 ans2 = n//m s = 0 for j in range(n): num = per[j] while s <= m-1: if cou[s] < ans2: if num > m: per[j] = s+1 cou[s] +=1 if cou[s] >= ans2: s +=1 ans +=1 break else: if num != s+1 and cou[num-1] > ans2: cou[num-1] -= 1 ans +=1 cou[s] +=1 per[j] = s+1 if cou[s] >= ans2: s+=1 break else: break else: s+=1 else: break print(ans2, ans) print(' '.join(map(str, per))) ``` Yes
15,568
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Submitted Solution: ``` def main(): from sys import stdin, stdout i = 0 for line in stdin.readlines(): if (i == 0): n, m, *rest = line.rstrip().split() n, m = int(n), int(m) elif (i == 1): nums = line.rstrip().split() ## list of a_i in str i += 1 counts = {} changes = 0 for i in range(1, m+1): counts[i] = 0 output_playlist = [] i = 0 for num in nums: i += 1 num = int(num) if (num <= m): counts[num] += 1 output_playlist.append(num) elif (num > m): min_num = min(counts, key = counts.get) if (equal_count(counts) and i == len(nums)): output_playlist.append(num) else: output_playlist.append(min_num) counts[min_num] += 1 changes += 1 max_num = max(counts, key = counts.get) min_num = min(counts, key = counts.get) diff = counts[max_num] - counts[min_num] while (diff > 1): counts[max_num] -= 1 counts[min_num] += 1 for i in range(len(output_playlist)): if output_playlist[i] == max_num: output_playlist[i] = min_num changes += 1 break max_num = max(counts, key = counts.get) min_num = min(counts, key = counts.get) diff = counts[max_num] - counts[min_num] stdout.write(str(counts[min_num]) + " " + str(changes) + '\n') stdout.write(to_string(output_playlist)) def to_string(l): s = "" for num in l: s += str(num) + " " return s.rstrip() def equal_count(counts): l = list(counts.values()) count = l[0] for i in l: if (count != i): return False return True if __name__ == "__main__": main() ``` No
15,569
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Submitted Solution: ``` d=input().split() n,m=int(d[0]),int(d[1]) d=input().split() d=[int(x) for x in d] p=0 di={} for i in range(1,1+m): di[i]=0 for i in d: if i<=m: di[i]+=1 for i in range(1,1+m): if di[i]<n//m: p+=abs(di[i]-n//m) print(n//m,p) D=[] for i in range(1,m+1): if di[i]<n//m: D+=[i]*(n//m-di[i]) for i in range(n): if D!=[]: if d[i]>m : d[i]=D.pop(-1) elif di[d[i]]>(n//m): d[i]=D.pop(-1) for i in d: print(i,end=" ") ``` No
15,570
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Submitted Solution: ``` n, m = map(int, input().split()) a = list(map(int, input().split())) d = dict() d2 = dict() a2 = [0] * n counter = 0 s = [] ans = n // m ost = n % m for i in range(m): d2[i + 1] = 0 for i in range(n): if a[i] in d: d[a[i]] += 1 else: d[a[i]] = 1 if a[i] <= m and d[a[i]] <= ans: a2[i] = a[i] d2[a2[i]] += 1 elif a[i] <= m and d[a[i]] <= ans + ost: a2[i] = a[i] d2[a2[i]] += 1 ost -= 1 #print(d2) playlist = sorted(d2.keys()) for elem in playlist: if d2[elem] < ans: s += [elem] * (ans - d2[elem]) if len(s) > 0: s += [s[-1]] * ost #print(a2) #print(s) j = 0 for i in range(n): if a2[i] == 0: counter += 1 a2[i] = s[j] j += 1 print(ans, counter) print(*a2) ``` No
15,571
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible. Find this maximum possible value of the minimum among the bj (1 ≀ j ≀ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. Input The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 2000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109), where ai is the performer of the i-th song. Output In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≀ j ≀ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. Examples Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 Note In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. Submitted Solution: ``` ## necessary imports import sys input = sys.stdin.readline # from math import * # swap_array function def swaparr(arr, a,b): temp = arr[a]; arr[a] = arr[b]; arr[b] = temp ## gcd function def gcd(a,b): if a == 0: return b return gcd(b%a, a) ## nCr function efficient using Binomial Cofficient def nCr(n, k): if(k > n - k): k = n - k res = 1 for i in range(k): res = res * (n - i) res = res / (i + 1) return res ## upper bound function code -- such that e in a[:i] e < x; def upper_bound(a, x, lo=0): hi = len(a) while lo < hi: mid = (lo+hi)//2 if a[mid] < x: lo = mid+1 else: hi = mid return lo ## prime factorization def primefs(n): ## if n == 1 ## calculating primes primes = {} while(n%2 == 0): primes[2] = primes.get(2, 0) + 1 n = n//2 for i in range(3, int(n**0.5)+2, 2): while(n%i == 0): primes[i] = primes.get(i, 0) + 1 n = n//i if n > 2: primes[n] = primes.get(n, 0) + 1 ## prime factoriazation of n is stored in dictionary ## primes and can be accesed. O(sqrt n) return primes ## MODULAR EXPONENTIATION FUNCTION def power(x, y, p): res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 x = (x * x) % p return res ## DISJOINT SET UNINON FUNCTIONS def swap(a,b): temp = a a = b b = temp return a,b # find function with path compression included (recursive) # def find(x, link): # if link[x] == x: # return x # link[x] = find(link[x], link); # return link[x]; # find function with path compression (ITERATIVE) def find(x, link): p = x; while( p != link[p]): p = link[p]; while( x != p): nex = link[x]; link[x] = p; x = nex; return p; # the union function which makes union(x,y) # of two nodes x and y def union(x, y, link, size): x = find(x, link) y = find(y, link) if size[x] < size[y]: x,y = swap(x,y) if x != y: size[x] += size[y] link[y] = x ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #### PRIME FACTORIZATION IN O(log n) using Sieve #### MAXN = int(1e6 + 5) def spf_sieve(): spf[1] = 1; for i in range(2, MAXN): spf[i] = i; for i in range(4, MAXN, 2): spf[i] = 2; for i in range(3, ceil(MAXN ** 0.5), 2): if spf[i] == i: for j in range(i*i, MAXN, i): if spf[j] == j: spf[j] = i; ## function for storing smallest prime factors (spf) in the array ################## un-comment below 2 lines when using factorization ################# # spf = [0 for i in range(MAXN)] # spf_sieve() def factoriazation(x): ret = {}; while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1; x = x//spf[x] return ret ## this function is useful for multiple queries only, o/w use ## primefs function above. complexity O(log n) ## taking integer array input def int_array(): return list(map(int, input().strip().split())) ## taking string array input def str_array(): return input().strip().split(); #defining a couple constants MOD = int(1e9)+7; CMOD = 998244353; INF = float('inf'); NINF = -float('inf'); ################### ---------------- TEMPLATE ENDS HERE ---------------- ################### n, m = int_array(); a = int_array(); ans = n // m; change = 0; ctr = [0]*3000; for i in range(n): if a[i] <= m: ctr[ a[i] ] += 1; for i in range(1, m+1): change += max(0, ans - ctr[i]); for i in range(n): if a[i] > m: for j in range(1, m+1): if ctr[j] < ans: ctr[j] += 1; a[i] = j; break; else: if ctr[ a[i] ] <= m: continue; else: for j in range(1, m+1): if ctr[j] < ans: ctr[j] += 1; a[i] = j; break; print(*(ans, change)); print(*a); ``` No
15,572
Provide tags and a correct Python 3 solution for this coding contest problem. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Tags: implementation Correct Solution: ``` m, n=[int(i) for i in input().split()] started=False failed=False finished=False t=[-5, -5] blank="."*n for i in range(m): s=input() # print(t, finished, started, failed, s) if "X" in s: if finished: failed=True break if not started: started=True first=False second=False for j in range(len(s)): if first: if s[j]!="X": second=True t[1]=j break else: if s[j]=="X": first=True t[0]=j if not second: t[1]=len(s) #print(s, "."*t[0]+"X"*(t[1]-t[0])+"."*(n-t[1])) if s!="."*t[0]+"X"*(t[1]-t[0])+"."*(n-t[1]): failed=True break elif started: finished=True if failed: print("NO") else: print("YES") ```
15,573
Provide tags and a correct Python 3 solution for this coding contest problem. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Tags: implementation Correct Solution: ``` n, m = map(int, input().split()) a = [input() for i in range(n)] minx = m miny = n maxx = -1 maxy = -1 for i in range(n): for j in range(m): if a[i][j] == 'X': if i > maxy: maxy = i if i < miny: miny = i if j > maxx: maxx = j if j < minx: minx = j for i in range(miny, maxy+1): for j in range(minx, maxx+1): if a[i][j] != 'X': print('NO') break if a[i][j] != 'X': break else: print('YES') ```
15,574
Provide tags and a correct Python 3 solution for this coding contest problem. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Tags: implementation Correct Solution: ``` #!/usr/bin/env python3 from sys import stdin,stdout def ri(): return map(int, input().split()) n, m = ri() found = 0 for i in range(n): r = input() if found == 0 and 'X' in r: r0 = r found = 1 continue if found and 'X' in r: if r != r0: print("NO") exit() print("YES") ```
15,575
Provide tags and a correct Python 3 solution for this coding contest problem. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Tags: implementation Correct Solution: ``` n, m = map(int, input().split()) ss = "" for i in range(n): s = str(input()) if 'X' in s: if ss == "": ss = s else: if s == ss: pass else: print("NO") exit() print("YES") ```
15,576
Provide tags and a correct Python 3 solution for this coding contest problem. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Tags: implementation Correct Solution: ``` def fail(): print('NO') exit() read = lambda: map(int, input().split()) n, m = read() a = [input() for i in range(n)] fst = None for i in range(n): for j in range(m): if a[i][j] == 'X': fst = i, j break if fst != None: break scd = None for i in range(n - 1, -1, -1): for j in range(m - 1, -1, -1): if a[i][j] == 'X': scd = i, j break if scd != None: break flag = True for i in range(n): for j in range(m): if fst[0] <= i <= scd[0] and fst[1] <= j <= scd[1]: if a[i][j] == '.': fail() else: if a[i][j] == 'X': fail() print('YES') ```
15,577
Provide tags and a correct Python 3 solution for this coding contest problem. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Tags: implementation Correct Solution: ``` import sys n, m = map(int, input().split()) a = [input() for _ in range(n)] min_i, min_j = n, m max_i, max_j = 0, 0 for i in range(n): for j in range(m): if a[i][j] == 'X': min_i, min_j = min(min_i, i), min(min_j, j) max_i, max_j = max(max_i, i), max(max_j, j) for i in range(min_i, max_i + 1): for j in range(min_j, max_j + 1): if a[i][j] != 'X': print("NO") sys.exit(0) print("YES") ```
15,578
Provide tags and a correct Python 3 solution for this coding contest problem. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Tags: implementation Correct Solution: ``` n, m = [int(i) for i in input().split()] pattern_dict = dict() pattern_0 = '.'*m no_flag = False for i in range(n): s = input() position = [j for j in range(m) if s[j]=='X'] if s != pattern_0: if s not in pattern_dict: pattern_dict[s] = [i] else: pattern_dict[s].append(i) if len(pattern_dict) > 1 or position[-1]-position[0]+1 != len(position): no_flag = True print("NO") break if not no_flag: for j in pattern_dict: s = pattern_dict[j] if s[-1]- s[0]+1 != len(s): print("NO") else: print("YES") ```
15,579
Provide tags and a correct Python 3 solution for this coding contest problem. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Tags: implementation Correct Solution: ``` n, m = map(int, input().split()) a = [] for i in range(n): a.append(input()) inf = 10 ** 9 p, q = inf, inf r, s = -1, -1 for i in range(n): for j in range(m): if a[i][j] == 'X': p = min(p, i) q = min(q, j) r = max(r, i) s = max(s, j) ok = True for i in range(n): for j in range(m): if a[i][j] == 'X': ok &= p <= i <= r and q <= j <= s if a[i][j] == '.': ok &= not (p <= i <= r and q <= j <= s) if ok: print('YES') if not ok: print('NO') ```
15,580
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Submitted Solution: ``` n,m=map(int,input().split()) osn=' ' iter=-1 p=-1 for i in range(n): y=input() if 'X' in y and osn==' ': k=y.count('X') f=y.index('X') for j in range(f,f+k): if y[j]!='X': p=0 osn=y iter=i elif osn==y and osn!=' ': if i==iter+1: iter+=1 else: p=0 elif 'X' in y and osn!=' ': p=0 if p==0: print('NO') else: print('YES') ``` Yes
15,581
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Submitted Solution: ``` def f(): s=input().split() m=int(s[0]) n=int(s[1]) ss=[] for i in range(m): ss.append(input()) for i in range(m): for j in range(n): if ss[i][j]=='X': return (ss,m,n,i,j) def ff(ss,m,n): for k in range(m-1,-1,-1): for l in range(n-1,-1,-1): if ss[k][l]=='X': return (k,l) ss,m,n,i,j=f() k,l=ff(ss,m,n) sss=[] for x in range(m): s="" for y in range(n): if (i<=x and x<=k and j<=y and y<=l): s+="X" else: s+="." sss.append(s) if(ss==sss): print("YES") else: print("NO") ``` Yes
15,582
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Submitted Solution: ``` n, m = map(int, input().split()) g = [input() for _ in range(n)] r, c = set(), set() for i in range(n): for j in range(m): if g[i][j] == 'X': r.add(i) c.add(j) g = g[min(r):max(r)+1] g = list(map(lambda x: x[min(c): max(c) + 1], g)) good = True for i in range(len(g)): for j in range(len(g[0])): if g[i][j] != 'X': good = False break print('YES' if good else 'NO') ``` Yes
15,583
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Submitted Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * # from fractions import * # from heapq import* from bisect import * from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ALPHA='abcdefghijklmnopqrstuvwxyz/' M=998244353 EPS=1e-6 def Ceil(a,b): return a//b+int(a%b>0) def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() def isRectangle(): leftUp = [inf,inf] rightDown = [-1,-1] tot = 0 for i in range(n): for j in range(m): if(a[i][j] == 'X'): leftUp[0] = min( leftUp[0] , i) leftUp[1] = min( leftUp[1] , j) rightDown[0] = max( rightDown[0], i) rightDown[1] = max( rightDown[1], j) tot += 1 l = abs(leftUp[0] - rightDown[0]) + 1 r = abs(leftUp[1] - rightDown[1]) + 1 # print(l,r,tot) return "YES" if l*r == tot else "NO" n,m = value() a = [] for i in range(n): a.append(input()) print(isRectangle()) ``` Yes
15,584
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Submitted Solution: ``` n, m = map(int, input().split()) num = 0 for i in range(n): a = input() num += a.count('X') if num % n == 0 or num % m == 0 or num <= max(n, m): print('YES') else: print('NO') ``` No
15,585
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Submitted Solution: ``` from sys import exit n, m = map(int, input().split()) a = [] for i in range(n): a.append(input()) left, right, top, bot = m, 0, m, 0 for i in range(n): for j in range(m): if a[i][j] == "X": left = min(left, j) right = max(right, j) top = min(top, i) bot = max(bot, i) for i in range(n): for j in range(m): if a[i][j] == "X": if (left <= j <= right) and (top <= i <= bot): continue else: print("NO") exit() else: if (left <= j <= right) and (top <= i <= bot): print("NO") exit() print("YES") ``` No
15,586
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Submitted Solution: ``` # You lost the game. n,m = map(int, input().split()) L = [str(input()) for _ in range(n)] x = y = ok = 0 for i in range(n): for j in range(m): if L[i][j] == 'X': x = i y = j ok = 1 break if ok: break l = y while l < m and L[x][l] == 'X': l += 1 ok = 0 for i in range(x+1, n): vu = 0 for j in range(m): if L[i][j] == 'X': vu = 1 if j < y or j >= l: ok = -1 break else: if vu and j >= y and j < l: ok = -1 break if ok: break if ok == -1: print("NO") else: print("YES") ``` No
15,587
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers n and m (1 ≀ n, m ≀ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 2 3 XXX XXX Output YES Input 2 2 .X XX Output NO Input 5 5 ..... ..X.. ..... ..... ..... Output YES Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... Submitted Solution: ``` '''input 5 5 ..... ..X.. ..... ..... ..... ''' s1, e1, s2, e2 = 500, -1, 500, -1 n, m = map(int, input().split()) l = [] for a in range(n): i = input() if "X" in i: s1 = min(s1, i.index("X")) e1 = max(e1, m - 1 - i[::-1].index("X")) s2 = min(s2, a) e2 = max(e2, a) l.append(i) if "".join(l).count("X") == 1: print("YES") quit() for r in range(n): if "." in l[r][s1:e1+1]: print("NO") break else: print("YES") ``` No
15,588
Provide tags and a correct Python 3 solution for this coding contest problem. Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least <image>, where Ξ΅ < 10 - 7. To better prepare himself, he wants to know the answer for q different values of pi. Since he is busy designing the battle strategy with Sam, he asks you for your help. Input First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer pi (1 ≀ pi ≀ 1000) β€” i-th query. Output Output q lines. On i-th of them output single integer β€” answer for i-th query. Examples Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2 Tags: dp, math, probabilities Correct Solution: ``` def main(): eps=10**-7 k,q=readIntArr() # dp[nTurns][nDistinctOrbs]=probability # dp[i][j]=dp[i-1][j]*j/k+dp[i-1][j-1]*(k-(j-1))/k def rowFactory(): row=[0 for _ in range(k+1)] return row dp=[rowFactory()] dp[0][0]=1 while dp[-1][-1]<=0.5: newRow=rowFactory() for j in range(1,k+1): newRow[j]+=dp[-1][j]*j/k+dp[-1][j-1]*(k-(j-1))/k dp.append(newRow) # print(dp) # break n=len(dp) for _ in range(q): p=int(input()) b=n+1 nTurns=0 while b>0: while nTurns+b<n and dp[nTurns+b][k]<(p/2000): nTurns+=b b//=2 nTurns+=1 print(nTurns) return #import sys #input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok) import sys input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS. def oneLineArrayPrint(arr): print(' '.join([str(x) for x in arr])) def multiLineArrayPrint(arr): print('\n'.join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print('\n'.join([' '.join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] inf=float('inf') MOD=10**9+7 main() ```
15,589
Provide tags and a correct Python 3 solution for this coding contest problem. Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least <image>, where Ξ΅ < 10 - 7. To better prepare himself, he wants to know the answer for q different values of pi. Since he is busy designing the battle strategy with Sam, he asks you for your help. Input First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer pi (1 ≀ pi ≀ 1000) β€” i-th query. Output Output q lines. On i-th of them output single integer β€” answer for i-th query. Examples Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2 Tags: dp, math, probabilities Correct Solution: ``` k, q = map(int, input().split()) t = [0] * (k + 1) t[1] = 1 d = [0] n = i = 1 while i < 1001: if 2000 * t[k] > i - 1e-7: d.append(n) i += 1 else: t = [0] + [(j * t[j] + (k - j + 1) * t[j - 1]) / k for j in range(1, k + 1)] n += 1 for i in range(q): print(d[int(input())]) ```
15,590
Provide tags and a correct Python 3 solution for this coding contest problem. Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least <image>, where Ξ΅ < 10 - 7. To better prepare himself, he wants to know the answer for q different values of pi. Since he is busy designing the battle strategy with Sam, he asks you for your help. Input First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer pi (1 ≀ pi ≀ 1000) β€” i-th query. Output Output q lines. On i-th of them output single integer β€” answer for i-th query. Examples Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2 Tags: dp, math, probabilities Correct Solution: ``` k, q = map(int, input().split()) t = [0] * (k + 1) t[1] = 1 c = [0] n = i = 1 while i < 1001: if (2000 * t[k] > i - (10**-7)): c.append(n) i += 1 else: t = [0] + [(j * t[j] + (k - j + 1) * t[j - 1]) / k for j in range(1, k + 1)] n += 1 for i in range(q): print(c[int(input())]) ```
15,591
Provide tags and a correct Python 3 solution for this coding contest problem. Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least <image>, where Ξ΅ < 10 - 7. To better prepare himself, he wants to know the answer for q different values of pi. Since he is busy designing the battle strategy with Sam, he asks you for your help. Input First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer pi (1 ≀ pi ≀ 1000) β€” i-th query. Output Output q lines. On i-th of them output single integer β€” answer for i-th query. Examples Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2 Tags: dp, math, probabilities Correct Solution: ``` k, q = map(int, input().split()) dp = [[0.0 for i in range(k + 1)] for j in range(10000)] dp[0][0] = 1.0 for i in range(1, 10000): for j in range(1, k + 1): dp[i][j] = dp[i - 1][j] * j / k + dp[i - 1][j - 1] * (k - j + 1) / k for t in range(q): p = int(input()) for i in range(10000): if p <= dp[i][k] * 2000: print(i) break ```
15,592
Provide tags and a correct Python 3 solution for this coding contest problem. Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least <image>, where Ξ΅ < 10 - 7. To better prepare himself, he wants to know the answer for q different values of pi. Since he is busy designing the battle strategy with Sam, he asks you for your help. Input First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer pi (1 ≀ pi ≀ 1000) β€” i-th query. Output Output q lines. On i-th of them output single integer β€” answer for i-th query. Examples Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2 Tags: dp, math, probabilities Correct Solution: ``` K, Q = map( int, input().split() ) dp = [ [ 0.0 for i in range( K + 1 ) ] for j in range( 10000 ) ] dp[ 0 ][ 0 ] = 1.0 for i in range( 1, 10000, 1 ): for j in range( 1, K + 1, 1 ): dp[ i ][ j ] = dp[ i - 1 ][ j ] * j / K + dp[ i - 1 ][ j - 1 ] * ( K - ( j - 1 ) ) / K for qi in range( Q ): P = int( input() ) for i in range( 10000 ): if P <= dp[ i ][ K ] * 2000: print( i ) break ```
15,593
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least <image>, where Ξ΅ < 10 - 7. To better prepare himself, he wants to know the answer for q different values of pi. Since he is busy designing the battle strategy with Sam, he asks you for your help. Input First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer pi (1 ≀ pi ≀ 1000) β€” i-th query. Output Output q lines. On i-th of them output single integer β€” answer for i-th query. Examples Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2 Submitted Solution: ``` k, q = map(int, input().split()) p = [int(input()) for i in range(q)] if k == 1: print('1\n' * q) exit() N = 1005 M = 10 ** 4 eps = 10 ** -6 cnt = [0] * M dp = [[0] * N for i in range(N)] j = 1 dp[0][0] = 1 kk = 1 for n in range(1, N): for i in range(1, k + 1): dp[n][i] = (i * dp[n - 1][i] + (k - i + 1) * dp[n - 1][i - 1]) if n % 10 == 0: kk = k ** 10 for i in range(k + 1): dp[n][i] /= kk kk = k ** (n % 10) while j < M and dp[n][k] * 2000 >= j * kk: cnt[j] = n j += 1 ans = [cnt[i] for i in p] print('\n'.join(map(str, ans))) ``` No
15,594
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least <image>, where Ξ΅ < 10 - 7. To better prepare himself, he wants to know the answer for q different values of pi. Since he is busy designing the battle strategy with Sam, he asks you for your help. Input First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer pi (1 ≀ pi ≀ 1000) β€” i-th query. Output Output q lines. On i-th of them output single integer β€” answer for i-th query. Examples Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2 Submitted Solution: ``` k, q = map(int, input().split()) for i in range(q): x = float(input()) #if (k == 1): #print(1) #continue j = float(k) l = k - 1 r = 10000 while (l != r - 1): m = (l + r) // 2 dp = k * [0] dp[0] = ((k - 1) / k)**m for i in range(1, k): dp[i] = ((k - 1) / k)**m + dp[i-1]*(1 - (((k - 1) / k)**m)) if (2000 - 2000 * dp[k - 1] >= x - 0.0002): r = m else: l = m print(r) ``` No
15,595
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least <image>, where Ξ΅ < 10 - 7. To better prepare himself, he wants to know the answer for q different values of pi. Since he is busy designing the battle strategy with Sam, he asks you for your help. Input First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer pi (1 ≀ pi ≀ 1000) β€” i-th query. Output Output q lines. On i-th of them output single integer β€” answer for i-th query. Examples Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2 Submitted Solution: ``` primeralinea=(input().split()) k=float(primeralinea[0]) q=float(primeralinea[1]) dp=[[0 for x in range(int(k)+1)] for y in range(10005)] dp[0][0]=1 i=1 while(i<=k): for j in range(i,10000): if(j>k): break #http://codeforces.com/blog/entry/50550 para formula #dp[n][x]= (x/k) * dp[n-1][x] + (k-x-1)/k *dp[n-1][x-1] #dp[i][j] = (i / k) * (dp[j - 1][i]) + ((k - i + 1) / k) * (dp[j - 1][i - 1]) dp[i][j] = (i / k) * (dp[i][j - 1]) + ((k - i + 1) / k) * (dp[i - 1][j - 1]) i+=1 qOG=int(q) while(q>0): y=float(input()) / 2000 for i in range(0,qOG): #print(i) if(i>qOG): break else: if (dp[int(k)][i] >= y): print(i) break q-=1 ``` No
15,596
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least <image>, where Ξ΅ < 10 - 7. To better prepare himself, he wants to know the answer for q different values of pi. Since he is busy designing the battle strategy with Sam, he asks you for your help. Input First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer pi (1 ≀ pi ≀ 1000) β€” i-th query. Output Output q lines. On i-th of them output single integer β€” answer for i-th query. Examples Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2 Submitted Solution: ``` k, q = map(int, input().split()) for i in range(q): x = float(input()) if (k == 1): print(1) continue j = float(k) l = k - 1 r = 10000 while (l != r - 1): m = (l + r) // 2 i = (((j - 1) / j)**m)**k if (1 - i > x / 2000): r = m else: l = m print(r) ``` No
15,597
Provide tags and a correct Python 3 solution for this coding contest problem. A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible. The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not. Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n. If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them. Input The first line of input contains n β€” a positive integer number without leading zeroes (1 ≀ n < 10100000). Output Print one number β€” any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1. Examples Input 1033 Output 33 Input 10 Output 0 Input 11 Output -1 Note In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two. Tags: dp, greedy, math, number theory Correct Solution: ``` def erase(s, n, m): p = '147' if n == 1 else '258' if sum(s.count(c) for c in p) < m: return [] t = list(reversed(s)) for c in p: while t.count(c) > 0 and m > 0: t.remove(c) m -= 1 while len(t) > 1 and t[-1] == '0': t.pop() return list(reversed(t)) def solve(s): n = sum(int(c) for c in s) % 3 if n == 0: a = b = s if n == 1: a = erase(s, 1, 1) b = erase(s, 2, 2) if n == 2: a = erase(s, 2, 1) b = erase(s, 1, 2) s = max(a, b, key=len) return '-1' if s == [] else s def divide_by_three(s): return solve(s) if __name__ == "__main__": s = list(input()) result = divide_by_three(s) print(*result, sep='') ```
15,598
Provide tags and a correct Python 3 solution for this coding contest problem. A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible. The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not. Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n. If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them. Input The first line of input contains n β€” a positive integer number without leading zeroes (1 ≀ n < 10100000). Output Print one number β€” any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1. Examples Input 1033 Output 33 Input 10 Output 0 Input 11 Output -1 Note In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two. Tags: dp, greedy, math, number theory Correct Solution: ``` n = list(input()) leng = 0 mp=[0,0,0] for x in n: leng += 1 mp[int(x)%3]+=1 tot = (mp[1]+2*mp[2])%3 if tot == 0: print("".join(n)) exit() if mp[tot] == 0: do = tot ^ 3 cnt = 2 else: if mp[tot] == 1 and int(n[0])%3==tot and n[1:3] == ['0','0']: do =tot^3 cnt = 2 if mp[do] == 0: do = tot cnt = 1 else: do = tot cnt =1 index = leng-1 if cnt>=leng: print(-1) exit() for x in range(cnt): while int(n[index])%3 != do: index-=1 n[index] = "" index -=1 index = 0 ans = "".join(n) while ans[index] == '0' and index<leng-cnt-1: index+=1 print(ans[index:]) ```
15,599