text
stringlengths
198
433k
conversation_id
int64
0
109k
Provide tags and a correct Python 3 solution for this coding contest problem. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Tags: binary search, geometry, math Correct Solution: ``` import math n,r=input().split() n=int(n) r=float(r) deg=math.radians(360/n) R=(math.sqrt(1-math.cos(deg))*r)/(math.sqrt(2)-math.sqrt(1-math.cos(deg))) print(R) ```
89,300
Provide tags and a correct Python 3 solution for this coding contest problem. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Tags: binary search, geometry, math Correct Solution: ``` ''' Geometry & Math! ''' import math In = input().split() n = float(In[0]) r = float(In[1]) n = 360 / n; deg = n/2; deg = math.sin( deg * (math.pi/180.0)) R = (deg*r) / (1 - deg) print(R) ```
89,301
Provide tags and a correct Python 3 solution for this coding contest problem. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Tags: binary search, geometry, math Correct Solution: ``` import sys from math import sin, pi readline = sys.stdin.readline N, r = map(int, readline().split()) theta = pi/N ok = 10**10 ng = 0 for _ in range(100000): med = (ok+ng)/2 if (1/sin(theta)-1)*med > r: ok = med else: ng = med print(ok) ```
89,302
Provide tags and a correct Python 3 solution for this coding contest problem. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Tags: binary search, geometry, math Correct Solution: ``` import math n,r = [int(x) for x in input().split()] x = math.sin(math.pi/n) y = (x*r)/(1-x) print(y) ```
89,303
Provide tags and a correct Python 3 solution for this coding contest problem. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Tags: binary search, geometry, math Correct Solution: ``` import math s=list(map(int,input().split())) n,r=s[0],s[1] pi=math.radians(180/n) x=math.sin(pi) ans=(x*r)/(1-x) print(ans) ```
89,304
Provide tags and a correct Python 3 solution for this coding contest problem. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Tags: binary search, geometry, math Correct Solution: ``` from math import * n,r=map(int,input().split()) d=radians(360/(2*n)) k=sin(d) print(r*k/(1-k)) ```
89,305
Provide tags and a correct Python 3 solution for this coding contest problem. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Tags: binary search, geometry, math Correct Solution: ``` import math x,y = map(int,input().split()) o = math.sin(math.pi/x) ans = y*o/(1-o) print(ans) ```
89,306
Provide tags and a correct Python 3 solution for this coding contest problem. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Tags: binary search, geometry, math Correct Solution: ``` import math n, r = (int(i) for i in input().strip().split()) f = math.sin(math.radians(360/(2*n))) print(r*f/(1-f)) ```
89,307
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Submitted Solution: ``` import math inp = input().split() n = float(inp[0]) r = float(inp[1]) print(r*math.sin(math.pi/n)/(1 - math.sin(math.pi/n))) ``` Yes
89,308
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Submitted Solution: ``` import math pi=22/7 n,r=list(map(int,input().split())) angle=(2*math.pi)/n angle=angle/2 value=r*(math.sin(angle))/(1-math.sin(angle)) print(round(value,7)) ``` Yes
89,309
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Submitted Solution: ``` import math n,r=[int(x) for x in input().split()] a=math.cos((3.141592653589793*(n-2)/n)/2) print((a*r)/(1-a)) ``` Yes
89,310
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Submitted Solution: ``` import math n, r = map(int, input().split()) print("%.7f" % ((r * math.cos((math.pi * (n - 2)) / (2 * n))) / (1 - math.cos((math.pi * (n - 2)) / (2 * n))))) ``` Yes
89,311
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Submitted Solution: ``` from os import path import sys,time # mod = int(1e9 + 7) # import re from math import ceil, floor,gcd,log,log2 ,factorial,cos,sin,pi from collections import defaultdict ,Counter , OrderedDict , deque # from itertools import combinations from string import ascii_lowercase ,ascii_uppercase from bisect import * from functools import reduce from operator import mul maxx = float('inf') #----------------------------INPUT FUNCTIONS------------------------------------------# I = lambda :int(sys.stdin.buffer.readline()) tup= lambda : map(int , sys.stdin.buffer.readline().split()) lint = lambda :[int(x) for x in sys.stdin.buffer.readline().split()] S = lambda: sys.stdin.readline().strip('\n') grid = lambda r :[lint() for i in range(r)] stpr = lambda x : sys.stdout.write(f'{x}' + '\n') star = lambda x: print(' '.join(map(str, x))) localsys = 0 start_time = time.time() if (path.exists('input.txt')): sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w'); #left shift --- num*(2**k) --(k - shift) # input = sys.stdin.readline n , R = tup() theta = cos((2*pi) / n) # a**2 = b**2 + c**2 - 2*b.c.cos A lo = 0 hi = 10**9 m = maxx d = defaultdict(int) while lo <= hi: r = (lo+hi)/2 x = (2*r)**2 y = (1 - theta)*(2*((R+r)**2)) if x-y == 0: break elif x - y < 0 : lo = r - 1 ans = r else: hi = r + 1 d[r]+=1 if d[r] > 1: break print(r) if localsys: print("\n\nTime Elased :",time.time() - start_time,"seconds") ``` No
89,312
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Submitted Solution: ``` # import math n,m=map(int, input().split()) r=float(m) pi=3.14159265359 print("sin - ", math.sin(pi/float(n))) res= ( r * math.sin(pi/float(n)) ) / ( 1 - math.sin(pi/float(n)) ) print(res) ``` No
89,313
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Submitted Solution: ``` from math import pi, sin k,r = map(int,input().split()) print(r*(1/sin(pi/k)-1)) ``` No
89,314
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: <image> It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles n and the radius of the inner circle r. NN thinks that, using this information, you can exactly determine the radius of the outer circles R so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers n and r (3 ≀ n ≀ 100, 1 ≀ r ≀ 100) β€” the number of the outer circles and the radius of the inner circle respectively. Output Output a single number R β€” the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed 10^{-6}. Formally, if your answer is a and the jury's answer is b. Your answer is accepted if and only when (|a-b|)/(max(1, |b|)) ≀ 10^{-6}. Examples Input 3 1 Output 6.4641016 Input 6 1 Output 1.0000000 Input 100 100 Output 3.2429391 Submitted Solution: ``` # your code goes here import math n,r=map(int,input().split()) cs=math.cos(math.pi/n) print(r*cs/(1-cs)) ``` No
89,315
Provide tags and a correct Python 3 solution for this coding contest problem. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Tags: brute force, greedy Correct Solution: ``` from collections import defaultdict def dist(a, b, n): if b >= a: return b - a else: return n - a + b n, m = (int(x) for x in input().split()) stations = defaultdict(list) for _ in range(m): a, b = (int(x) for x in input().split()) stations[a].append(b) needs = {} for station, candies in stations.items(): if not candies: continue loops = len(candies)-1 closest = min(candies, key=lambda x:dist(station, x, n)) needs[station] = (loops, closest) maxloops = max([pair[0] for pair in needs.values()]) maxstats = [] finishes = [] for station, pair in needs.items(): if pair[0] == maxloops: maxstats.append(station) result = [] for start in range(1, n+1): time = 0 for station in needs: if not needs[station]: continue loops, closest = needs[station] t = dist(start, station, n) + loops*n + dist(station, closest, n) time = max(time, t) result.append(str(time)) print(' '.join(result)) ```
89,316
Provide tags and a correct Python 3 solution for this coding contest problem. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Tags: brute force, greedy Correct Solution: ``` #Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction import collections from itertools import permutations 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----------------------------------------------------- mii=lambda:map(int,input().split()) n,m=mii() a=[0 for _ in range(n)] c=[123456 for _ in range(n)] for _ in range(m): u,v=mii() u%=n v%=n if v<u: v+=n a[u]+=1 if c[u]>v: c[u]=v ans=[] for i in list(range(1,n))+[0]: out=0 for j in range(i,n): if not a[j]: continue tmp=(j-i)+(a[j]-1)*n+(c[j]-j) out=max(out,tmp) #print(1,i,j,tmp) for j in range(i): if not a[j]: continue tmp=(j+n-i)+(a[j]-1)*n+(c[j]-j) out=max(out,tmp) #print(2,i,j,tmp) ans.append(out) print(" ".join(map(str,ans))) ```
89,317
Provide tags and a correct Python 3 solution for this coding contest problem. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Tags: brute force, greedy Correct Solution: ``` def dist(a,b): return (b-a)%n n, m = map(int, input().split()) cnd = [0 for x in range(n+1)] mn = [5000 for x in range(n+1)] for i in range(m): a, b = map(int, input().split()) # print(a,b,dist(a,b)) cnd[a] += 1 mn[a] = min(mn[a], dist(a,b)) # print(mn) # print(cnd) for i in range(1,n+1): ans = 0 for j in range(1,n+1): if cnd[j] > 0: ans = max(ans, dist(i,j) + n*(cnd[j]-1) + mn[j]) print(ans, end=" ") ```
89,318
Provide tags and a correct Python 3 solution for this coding contest problem. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Tags: brute force, greedy Correct Solution: ``` #Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction from collections import defaultdict 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----------------------------------------------------- def dist(a,b): return (b-a)%n n, m = map(int, input().split()) cnd = [0 for x in range(n+1)] mn = [5000 for x in range(n+1)] for i in range(m): a, b = map(int, input().split()) # print(a,b,dist(a,b)) cnd[a] += 1 mn[a] = min(mn[a], dist(a,b)) # print(mn) # print(cnd) for i in range(1,n+1): ans = 0 for j in range(1,n+1): if cnd[j] > 0: ans = max(ans, dist(i,j) + n*(cnd[j]-1) + mn[j]) print(ans, end=" ") ```
89,319
Provide tags and a correct Python 3 solution for this coding contest problem. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Tags: brute force, greedy Correct Solution: ``` import os import sys from io import BytesIO, IOBase from collections import defaultdict, deque, Counter, OrderedDict import threading from copy import deepcopy def main(): n,m = map(int,input().split()) station = [[] for _ in range(n+1)] time = [0]*(n+1) ans = [0]*(n+1) for i in range(m): a,b = map(int,input().split()) station[a].append((b+n-a)%n) for i in range(1,n+1): station[i] = sorted(station[i]) for i in range(1,n+1): if len(station[i]): time[i] = (len(station[i])-1)*n + station[i][0] for i in range(1,n+1): for j in range(1,n+1): if time[j] != 0: ans[i] = max(ans[i],time[j]+(j+n-i)%n) print(*ans[1::]) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": """threading.stack_size(40960000) thread = threading.Thread(target=main) thread.start()""" main() ```
89,320
Provide tags and a correct Python 3 solution for this coding contest problem. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Tags: brute force, greedy Correct Solution: ``` if __name__ == "__main__": from sys import stdin n, m = list(map(int, stdin.readline().split())) c = {} for _ in range(m): a, b = list(map(int, stdin.readline().split())) if (a-1) not in c.keys(): c[a-1] = [] x = b-a + (n if b<a else 0) c[a-1].append(x) for k, l in c.items(): c[k] = min(l) + ((len(l)-1)*n) toprint = [] for x in range(n): res = 0 for y, v in c.items(): s = y-x + (n if y<x else 0) res = max(res, v+s) toprint.append(res) print(*toprint) ```
89,321
Provide tags and a correct Python 3 solution for this coding contest problem. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Tags: brute force, greedy Correct Solution: ``` import sys n, m = (int(t) for t in input().split(' ')) candies_total = [0 for _ in range(n)] closest_distance = [n for _ in range(n)] candies = sys.stdin.readlines() for i in range(m): from_, to = (int(t) - 1 for t in candies[i].split(' ')) candies_total[from_] += 1 distance = (to + n - from_) % n if distance < closest_distance[from_]: closest_distance[from_] = distance stations_best = [0 for _ in range(n)] for i in range(n): stations_best[i] = (candies_total[i] - 1) * n + closest_distance[i] answer = [] for i in range(n): cost = max([stations_best[j] + (j + n - i) % n for j in range(n) if stations_best[j] > 0]) answer.append(cost) print(*answer) ```
89,322
Provide tags and a correct Python 3 solution for this coding contest problem. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Tags: brute force, greedy Correct Solution: ``` def dist(a, b): return (b - a) % n n, m = list(map(int, input().split(" "))) sweets = {i: [] for i in range(n)} for i in range(m): s, t = list(map(int, input().split(" "))) sweets[s - 1].append(t - 1) t = {i: 0 for i in range(n)} for i in range(n): sweets[i] = sorted(sweets[i], key=lambda x: -dist(i, x)) if len(sweets[i]): t[i] = (len(sweets[i]) - 1) * n + dist(i, sweets[i][-1]) result = [] for s in range(n): max_dist = 0 for i in range(n): if t[i] and t[i] + dist(s, i) > max_dist: max_dist = t[i] + dist(s, i) result.append(max_dist) print(" ".join(map(str, result))) ```
89,323
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Submitted Solution: ``` n, m = map(int, input().split()) def dist(a, b): return (n + b - a) % n def main(): inp1 = [0] * (n + 1) inp2 = [n] * (n + 1) for _ in range(m): a, b = map(int, input().split()) inp1[a] += 1 inp2[a] = min(inp2[a], dist(a, b)) inp = tuple((((r1 - 1) * n + r2) for r1, r2 in zip(inp1, inp2))) print(*(max((dist(i, j) + inp[j] for j in range(1, n + 1) if inp[j])) for i in range(1, n + 1))) main() ``` Yes
89,324
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Submitted Solution: ``` import sys import math as mt input=sys.stdin.buffer.readline #t=int(input()) t=1 for ___ in range(t): n,m=map(int,input().split()) d={} for i in range(n+1): d[i]=[] for __ in range(m): a,b=map(int,input().split()) d[a].append(b) for i in range(n+1): d[i].sort() #print(d) sub=[0]*(n+1) for i in d: mini=10000 for j in range(len(d[i])): mini=min(mini,(d[i][j]-i)%n) sub[i]=mini for k in range(1,n+1): loop=0 maxi=0 for i in range(1,n+1): if len(d[i])>0: loop=(len(d[i])-1)*n+(i-k)%n+(sub[i]) maxi=max(maxi,loop) print(maxi,end=" ") ``` Yes
89,325
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Submitted Solution: ``` def dista(start, n): return lambda end: n - (start - end) if(start > end) else end - start n, m = map(int,input().split(' ')) dicta = [[] for i in range(n+1)] for i in range(m): s, d = map(int,input().split(' ')) dicta[s].append(d) for i in range(n+1): dicta[i].sort(key = dista(i, n)) maxlen = max((map(len, dicta))) result = (maxlen-1)*n minadd = 0 ansans = [] for k in range(1, n+1): disk = dista(k, n) minadd = 0 for i in range(1, n+1): lndicta = len(dicta[i]) tmp = 0 if(lndicta == maxlen-1): if lndicta != 0: tmp = min(map(lambda j: (disk(i) + dista(i, n)(j)), dicta[i])) - n elif(lndicta == maxlen): tmp = min(map(lambda j: (disk(i) + dista(i, n)(j)), dicta[i])) if(tmp > minadd): minadd = tmp ansans.append(str(minadd + result)) print(' '.join(ansans)) ``` Yes
89,326
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Submitted Solution: ``` import sys #sys.stdin=open("data.txt") input=sys.stdin.readline mii=lambda:map(int,input().split()) n,m=mii() a=[0 for _ in range(n)] c=[123456 for _ in range(n)] for _ in range(m): u,v=mii() u%=n v%=n if v<u: v+=n a[u]+=1 if c[u]>v: c[u]=v ans=[] for i in list(range(1,n))+[0]: out=0 for j in range(i,n): if not a[j]: continue tmp=(j-i)+(a[j]-1)*n+(c[j]-j) out=max(out,tmp) #print(1,i,j,tmp) for j in range(i): if not a[j]: continue tmp=(j+n-i)+(a[j]-1)*n+(c[j]-j) out=max(out,tmp) #print(2,i,j,tmp) ans.append(out) print(" ".join(map(str,ans))) ``` Yes
89,327
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Submitted Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write def in_num(): return int(raw_input()) def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): pr(' '.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(int,stdin.read().split()) range = xrange # not for python 3.0+ # main code n,m=in_arr() d=[[] for i in range(n+1)] for i in range(m): u,v=in_arr() d[u].append(v) dp=[0]*(n+1) for i in range(1,n+1): if d[i]: val=10**18 for j in d[i]: val=min(val,(j-i)%n) dp[i]=val+(n*(len(d[i])-1)) ans=[0]*n for i in range(1,n+1): val=0 for j in range(1,n+1): if not dp[j]: continue val=max(val,((j-i)%n)+dp[j]) ans[i-1]=val pr_arr(ans) ``` Yes
89,328
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Submitted Solution: ``` import os import sys from io import BytesIO, IOBase from collections import defaultdict, deque, Counter, OrderedDict import threading from copy import deepcopy def main(): n,m = map(int,input().split()) station = [[] for _ in range(n+1)] time = [0]*(n+1) ans = [0]*(n+1) for i in range(m): a,b = map(int,input().split()) if a > b: station[a].append(b+n) else: station[a].append(b) for i in range(1,n+1): station[i].sort() time_max = [-1,[]] for i in range(1,n+1): if len(station[i]):time[i] = (len(station[i])-1)*n + station[i][0]-i if time[i] == time_max[0]: time_max[1].append(i) if time[i] > time_max[0]: time_max = [time[i],[i]] for i in range(1,n+1): ans[i] = time_max[0] z = 0 for nx in time_max[1]: if i > nx: z = max(z,nx+n-i) else: z = max(z,nx-i) ans[i]+=z print(*ans[1::]) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": """threading.stack_size(40960000) thread = threading.Thread(target=main) thread.start()""" main() ``` No
89,329
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Submitted Solution: ``` import os import sys from io import BytesIO, IOBase from collections import defaultdict, deque, Counter, OrderedDict import threading from copy import deepcopy def main(): n,m = map(int,input().split()) station = [[] for _ in range(n+1)] time = [0]*(n+1) ans = [0]*(n+1) for i in range(m): a,b = map(int,input().split()) if a > b: station[a].append(b+n) else: station[a].append(b) for i in range(1,n+1): station[i].sort() for i in range(1,n+1): if len(station[i]): time[i] = (len(station[i])-1)*n + station[i][0]-i for i in range(1,n+1): for j in range(1,n+1): ans[i] = max(ans[i],time[j] + (j+n-i)%n) print(*ans[1::]) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": """threading.stack_size(40960000) thread = threading.Thread(target=main) thread.start()""" main() ``` No
89,330
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Submitted Solution: ``` n, m = map(int, input().split()) dict_station = dict() s = '' for i in range(m): a, b = map(int, input().split()) cs = dict_station.get(a, []) if cs: if b < cs[0]: cs.append(b) else: cs.insert(0, b) else: cs.append(b) dict_station[a] = cs for i in range(1, n + 1): count = m total = 0 ns = i + 1 cs = [] dict_now = dict() dict_now[i] = list(dict_station.get(i, [])) if dict_now[i]: cs.append(dict_now[i].pop(0)) while count > 0 or cs: if ns > n: ns = 1 length = len(cs) for j in range(length - 1, -1, -1): if cs[j] == ns: count -= 1 del cs[j] if dict_now.get(ns, None) is None: dict_now[ns] = list(dict_station.get(ns, [])) if dict_now.get(ns, []): cs.append(dict_now[ns].pop(0)) ns += 1 total += 1 s += ' ' + str(total) print(s[1:]) ``` No
89,331
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. Submitted Solution: ``` ''' CODED WITH LOVE BY SATYAM KUMAR ''' from sys import stdin, stdout import cProfile, math from collections import Counter from bisect import bisect_left,bisect,bisect_right import itertools from copy import deepcopy from fractions import Fraction import sys, threading import operator as op from functools import reduce sys.setrecursionlimit(10**6) # max depth of recursion threading.stack_size(2**27) # new thread will get stack of such size fac_warmup = False printHeap = str() memory_constrained = False P = 10**9+7 import sys class Operation: def __init__(self, name, function, function_on_equal, neutral_value=0): self.name = name self.f = function self.f_on_equal = function_on_equal def add_multiple(x, count): return x * count def min_multiple(x, count): return x def max_multiple(x, count): return x sum_operation = Operation("sum", sum, add_multiple, 0) min_operation = Operation("min", min, min_multiple, 1e9) max_operation = Operation("max", max, max_multiple, -1e9) class SegmentTree: def __init__(self, array, operations=[sum_operation, min_operation, max_operation]): self.array = array if type(operations) != list: raise TypeError("operations must be a list") self.operations = {} for op in operations: self.operations[op.name] = op self.root = SegmentTreeNode(0, len(array) - 1, self) def query(self, start, end, operation_name): if self.operations.get(operation_name) == None: raise Exception("This operation is not available") return self.root._query(start, end, self.operations[operation_name]) def summary(self): return self.root.values def update(self, position, value): self.root._update(position, value) def update_range(self, start, end, value): self.root._update_range(start, end, value) def __repr__(self): return self.root.__repr__() class SegmentTreeNode: def __init__(self, start, end, segment_tree): self.range = (start, end) self.parent_tree = segment_tree self.range_value = None self.values = {} self.left = None self.right = None if start == end: self._sync() return self.left = SegmentTreeNode(start, start + (end - start) // 2, segment_tree) self.right = SegmentTreeNode(start + (end - start) // 2 + 1, end, segment_tree) self._sync() def _query(self, start, end, operation): if end < self.range[0] or start > self.range[1]: return None if start <= self.range[0] and self.range[1] <= end: return self.values[operation.name] self._push() left_res = self.left._query(start, end, operation) if self.left else None right_res = self.right._query(start, end, operation) if self.right else None if left_res is None: return right_res if right_res is None: return left_res return operation.f([left_res, right_res]) def _update(self, position, value): if position < self.range[0] or position > self.range[1]: return if position == self.range[0] and self.range[1] == position: self.parent_tree.array[position] = value self._sync() return self._push() self.left._update(position, value) self.right._update(position, value) self._sync() def _update_range(self, start, end, value): if end < self.range[0] or start > self.range[1]: return if start <= self.range[0] and self.range[1] <= end: self.range_value = value self._sync() return self._push() self.left._update_range(start, end, value) self.right._update_range(start, end, value) self._sync() def _sync(self): if self.range[0] == self.range[1]: for op in self.parent_tree.operations.values(): current_value = self.parent_tree.array[self.range[0]] if self.range_value is not None: current_value = self.range_value self.values[op.name] = op.f([current_value]) else: for op in self.parent_tree.operations.values(): result = op.f( [self.left.values[op.name], self.right.values[op.name]]) if self.range_value is not None: bound_length = self.range[1] - self.range[0] + 1 result = op.f_on_equal(self.range_value, bound_length) self.values[op.name] = result def _push(self): if self.range_value is None: return if self.left: self.left.range_value = self.range_value self.right.range_value = self.range_value self.left._sync() self.right._sync() self.range_value = None def __repr__(self): ans = "({}, {}): {}\n".format(self.range[0], self.range[1], self.values) if self.left: ans += self.left.__repr__() if self.right: ans += self.right.__repr__() return ans def display(string_to_print): stdout.write(str(string_to_print) + "\n") def primeFactors(n): #n**0.5 complex factors = dict() for i in range(2,math.ceil(math.sqrt(n))+1): while n % i== 0: if i in factors: factors[i]+=1 else: factors[i]=1 n = n // i if n>2: factors[n]=1 return (factors) def binary(n,digits = 20): b = bin(n)[2:] b = '0'*(20-len(b))+b return b def isprime(n): """Returns True if n is prime.""" if n < 4: return True if n % 2 == 0: return False if n % 3 == 0: return False i = 5 w = 2 while i * i <= n: if n % i == 0: return False i += w w = 6 - w return True factorial_modP = [] def warm_up_fac(MOD): global factorial_modP,fac_warmup if fac_warmup: return factorial_modP= [1 for _ in range(fac_warmup_size+1)] for i in range(2,fac_warmup_size): factorial_modP[i]= (factorial_modP[i-1]*i) % MOD fac_warmup = True def InverseEuler(n,MOD): return pow(n,MOD-2,MOD) def nCr(n, r, MOD): global fac_warmup,factorial_modP if not fac_warmup: warm_up_fac(MOD) fac_warmup = True return (factorial_modP[n]*((pow(factorial_modP[r], MOD-2, MOD) * pow(factorial_modP[n-r], MOD-2, MOD)) % MOD)) % MOD def test_print(*args): if testingMode: print(args) def display_list(list1, sep=" "): stdout.write(sep.join(map(str, list1)) + "\n") def get_int(): return int(stdin.readline().strip()) def get_tuple(): return map(int, stdin.readline().split()) def get_list(): return list(map(int, stdin.readline().split())) import heapq,itertools pq = [] # list of entries arranged in a heap entry_finder = {} # mapping of tasks to entries REMOVED = '<removed-task>' def add_task(task, priority=0): 'Add a new task or update the priority of an existing task' if task in entry_finder: remove_task(task) count = next(counter) entry = [priority, count, task] entry_finder[task] = entry heapq.heappush(pq, entry) def remove_task(task): 'Mark an existing task as REMOVED. Raise KeyError if not found.' entry = entry_finder.pop(task) entry[-1] = REMOVED def pop_task(): 'Remove and return the lowest priority task. Raise KeyError if empty.' while pq: priority, count, task = heapq.heappop(pq) if task is not REMOVED: del entry_finder[task] return task raise KeyError('pop from an empty priority queue') memory = dict() def clear_cache(): global memory memory = dict() def cached_fn(fn, *args): global memory if args in memory: return memory[args] else: result = fn(*args) memory[args] = result return result def binary_serach(i,li): #print("Search for ",i) fn = lambda x: li[x]-x//i x = -1 b = len(li) while b>=1: #print(b,x) while b+x<len(li) and fn(b+x)>0: #Change this condition 2 to whatever you like x+=b b=b//2 return x # -------------------------------------------------------------- MAIN PROGRAM TestCases = False testingMode = False fac_warmup_size = 10**5+100 optimiseForReccursion = True #Can not be used clubbed with TestCases def main(): n, m = get_tuple() deliverables = [[] for _ in range(n)] for _ in range(m): a,b = get_tuple() deliverables[a-1].append(b-1) fixed = len(max(deliverables,key = lambda x: len(x))) #print(fixed,deliverables) k = [[i,x] for i,x in enumerate(deliverables)] processed = [] for li in k: index = li[0] g = n for lis in li[1]: g = min(g,(lis-index)%n) processed.append([index,g%n]) #print(processed) res = [] for i in range(n): #cost = fixed*n -n extra = 0 for j in processed: extra = max (extra, (j[0]-i)%n + j[1]) if len(deliverables[j[0]])==fixed else max(extra,n - (-j[0]+i)%n + j[1] ) if len(j)==fixed-1 else extra #print(i,j,extra) cost = (fixed-1)*n + extra res.append(cost) display_list(res) # --------------------------------------------------------------------- END= if TestCases: for _ in range(get_int()): cProfile.run('main()') if testingMode else main() else: (cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start() ``` No
89,332
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` n = int(input()) A = [int(i) for i in input().split()] cnt1 = A.count(1) cnt2 = A.count(2) if cnt1 == 0: print(*A) elif cnt2 > 0: print(*([2] + [1] + [2] * (cnt2 - 1) + [1] * (cnt1 - 1))) else: print(*A) ```
89,333
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` n = int(input()) ls = list(map(int, input().split())) ones = 0 twos = 0 for i in range(n): if ls[i] == 1: ones += 1 else: twos += 1 if ones: if twos: print(2, 1,end=' ') for i in range(twos-1): print(2, end=' ') for i in range(ones-1): print(1, end=' ') else: for i in range(ones): print(1, end=' ') else: for i in range(twos): print(2, end=' ') ```
89,334
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` def main(): n = int(input()) a = [int(x) for x in input().split()] c1, c2 = 0, 0 for i in a: if i == 1: c1 += 1 c2 = n - c1 if c1 == 0: ans = [2] * c2 elif c2 == 0: ans = [1] * c1 else: ans = [2, 1] ans += [2] * (c2 - 1) ans += [1] * (c1 - 1) for a in ans: print(a, end=" ") if __name__ == "__main__": main() ```
89,335
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` from collections import Counter n = int(input()) a = Counter(list(map(int,input().split()))) two = a[2] one = a[1] s = '' if a[2]: s+='2 ' a[2]-=1 if a[1]: s+='1 ' a[1]-=1 s += '2 '*a[2] s += '1 '*a[1] print(s.rstrip()) ```
89,336
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` n = int(input()) a = [int(x) for x in input().split()] arr = [0, 0, 0] for item in a: arr[item] += 1 seq = '' if arr[1] > 2: seq += '111' arr[1] -= 3 elif arr[1] == 2 and arr[2]: seq += '21' arr[1] -= 1 arr[2] -= 1 elif arr[1] == 1 and arr[2]: seq += '21' arr[1] -= 1 arr[2] -= 1 if arr[2]: seq += '2'*arr[2] arr[2] = 0 if arr[1]: seq += '1'*arr[1] arr[1] = 0 for char in seq: print(char, end=' ') ```
89,337
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` from collections import defaultdict from math import sqrt def is_prime(n): for i in range(2,int(sqrt(n))+1): if n%i == 0: return False return True hash = defaultdict(int) n = int(input()) l = list(map(int,input().split())) for i in l: if i == 2: hash[2] +=1 else: hash[1]+=1 ans = [0] boo = [] for i in range(n): if is_prime(ans[-1]+2) and hash[2]>0: ans.append(ans[-1]+2) hash[2]-=1 boo.append(2) elif is_prime(ans[-1]+1) and hash[1]>0: ans.append(ans[-1]+1) boo.append(1) hash[1]-=1 else: if hash[2]>0: ans.append(ans[-1]+2) boo.append(2) hash[2]-=1 elif hash[1]>0: ans.append(ans[-1]+1) boo.append(1) hash[1]-=1 print(*boo) ```
89,338
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` def sol(tiles): if len(tiles) == 1: return tiles[0] d = {'1': 0, '2': 0} for t in tiles: d[t] += 1 if d['1'] == 0: return ' '.join(['2'] * d['2']) if d['2'] == 0: return ' '.join(['1'] * d['1']) return ' '.join(['2','1'] + ['2']*(d['2']-1) + ['1']*(d['1']-1)) if __name__ == '__main__': _ = input() tiles = input().split() print(sol(tiles)) ```
89,339
Provide tags and a correct Python 3 solution for this coding contest problem. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Tags: constructive algorithms, greedy, math, number theory Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) t=a.count(2);o=a.count(1) if t: print(2,end=" ");t-=1;n-=1 if o: print(1,end=" ");o-=1;n-=1 for i in range(n): if t: print(2,end=" ");t-=1 else: print(1,end=" ") ```
89,340
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Submitted Solution: ``` n=int(input()) arr=list(map(int,input().split())) a=arr.count(1) b=arr.count(2) if a>=1 and b>=1: print('2'+ ' 1'+ ' 2'*(b-1)+ ' 1'*(a-1)) else: print(*arr) ``` Yes
89,341
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Submitted Solution: ``` # base on idea 1 is not a prime number, 2, 3 is a prime number # => 2 first numbers should be 2, 1, then all prime numbers are odd numbers n = int(input()) a = list(map(int, input().split())) c1 = c2 = 0 for i in a: if i == 1: c1 += 1 else: c2 += 1 b = [] if not c1: b = [2] * c2 elif not c2: b = [1] * c1 else: b = [2, 1] for i in range(c2 - 1): b.append(2) for i in range(c1 - 1): b.append(1) print (" ".join(map(str, b))) ``` Yes
89,342
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Submitted Solution: ``` #------------------------------warmup---------------------------- import os import sys 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----------------------------------------------------- n=int(input()) l=list(map(int,input().split())) c=0 d=0 ans=[] for i in range(n): if l[i]==1: c+=1 else: d+=1 if d>0: ans.append(2) d-=1 if c>0: ans.append(1) c-=1 for i in range(d): ans.append(2) for i in range(c): ans.append(1) print(*ans,sep=" ") ``` Yes
89,343
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Submitted Solution: ``` n=int(input()) y=list(map(int,input().split())) if n==1: print(y[0]) else: even=y.count(2) odd=y.count(1) if even==0 or odd==0: print(*y) else: y=sorted(y) y.reverse() i=0 while i<n: if y[i]==1: break i+=1 t=y[i] y[i]=y[1] y[1]=t print(*y) ``` Yes
89,344
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Submitted Solution: ``` from collections import Counter n=int(input()) lst=list(map(int,input().split())) dct=Counter(lst) if(len(dct)==1): print(*lst) else: if(dct[1]%2==0): dct[1]-=1 s="2 "+"1 "*dct[1]+"2 "*(dct[2]-1) print(s[:-1]) ``` No
89,345
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Submitted Solution: ``` from collections import Counter n = int(input()) l = list(map(int, input().split())) c = Counter(l) if 1 in c: nb_1 = c[1] ans = [1]*(1 + 2*(nb_1-1)//2) else: ans = [] if 2 in c: ans += [2]*c[2] if 1 in c and c[1] %2 ==0: ans.apend(1) print() ``` No
89,346
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Submitted Solution: ``` n = int(input()) a=[] a = input().split() n1 = a.count("1") n2 = a.count("2") res = "" if n1 == 1 and n2 >= 1: res = res+"2 1 " n2 = n2-1 for j in range(n2): res = res+"2 " if n1 == 2 and n2 >= 1: res = res+"2 1 1 " n2 = n2-1 for j in range(n2): res = res+"2 " else: for i in range(n): for j in range(3): if n1 > 0 : res = res+"1 " n1=n1-1 if n2 > 0 : n2 = n2-1 res = res+"2 " print(res) ``` No
89,347
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We're giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it β€” either 1 or 2. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of number tiles in the bag. The following line contains n space-separated integers a_1, a_2, ..., a_n (a_i ∈ \{1, 2\}) β€” the values written on the tiles. Output Output a permutation b_1, b_2, ..., b_n of the input sequence (a_1, a_2, ..., a_n) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Examples Input 5 1 2 1 2 1 Output 1 1 1 2 2 Input 9 1 1 2 1 1 1 2 1 1 Output 1 1 1 2 1 1 1 2 1 Note The first solution produces the prefix sums 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, \mathbf{\color{blue}{7}} (four primes constructed), while the prefix sums in the second solution are 1, \mathbf{\color{blue}{2}}, \mathbf{\color{blue}{3}}, \mathbf{\color{blue}{5}}, 6, \mathbf{\color{blue}{7}}, 8, 10, \mathbf{\color{blue}{11}} (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible. Submitted Solution: ``` from math import sqrt from collections import Counter n = int(input()) arr = [int(i) for i in input().split()] c = Counter(arr) summa = sum(arr) sieve = [True] * (summa + 1) if summa == 1: exit() for i in range(2, int(sqrt(summa)) + 1): if sieve[i]: for j in range(i ** 2, summa + 1, i): sieve[j] = False s = 0 for i in range(2, summa + 1, 1): ok = sieve[i] if ok: k = abs(i - s) if k // 2: if c[2] > k // 2: s += (k // 2) * 2 c[2] -= k // 2 print((k // 2) * '2 ', end='') k %= 2 elif c[2]: print(c[2] * '2 ', end='') k -= c[2] * 2 s += c[2] * 2 c[2] = 0 if k: if c[1] > k: print('1 ' * k, end='') s += k c[1] -= k elif c[1]: print(c[1] * '1 ', end='') s += c[1] c[1] = 0 ``` No
89,348
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At Moscow Workshops ICPC team gets a balloon for each problem they solved first. Team MSU Red Panda got so many balloons that they didn't know how to spend them. So they came up with a problem with them. There are several balloons, not more than 10^6 in total, each one is colored in one of k colors. We can perform the following operation: choose k-1 balloons such that they are of k-1 different colors, and recolor them all into remaining color. We can perform this operation any finite number of times (for example, we can only perform the operation if there are at least k-1 different colors among current balls). How many different balloon configurations can we get? Only number of balloons of each color matters, configurations differing only by the order of balloons are counted as equal. As this number can be very large, output it modulo 998244353. Input The first line contains a single integer k (2 ≀ k ≀ 10^5) β€”the number of colors. The second line contains k integers a_1, a_2, …, a_k (0 ≀ a_i) β€”initial configuration of balloons. a_i is number of balloons of color i. The total number of balloons doesn't exceed 10^6. In other words, a_1 + a_2 + a_3 + … + a_k ≀ 10^6. Output Output number of possible configurations modulo 998244353. Examples Input 3 0 1 2 Output 3 Input 4 1 1 1 1 Output 5 Input 5 0 0 1 2 3 Output 1 Input 3 2 2 8 Output 31 Note In the first example, there are 3 configurations we can get: [0, 1, 2], [2, 0, 1], [1, 2, 0]. In the second example, we can apply the operation not more than once, and possible configurations are: [1, 1, 1, 1], [0, 0, 0, 4], [0, 0, 4, 0], [0, 4, 0, 0], [4, 0, 0, 0]. In the third example, we can't apply any operations, so the only achievable configuration is the starting one. Submitted Solution: ``` n = int(input()) print(pow(3, n - 1, 10 ** 9 + 7) - 1) ``` No
89,349
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mishka's favourite experimental indie band has recently dropped a new album! Songs of that album share one gimmick. Each name s_i is one of the following types: * 1~c β€” a single lowercase Latin letter; * 2~j~c β€” name s_j (1 ≀ j < i) with a single lowercase Latin letter appended to its end. Songs are numbered from 1 to n. It's guaranteed that the first song is always of type 1. Vova is rather interested in the new album but he really doesn't have the time to listen to it entirely. Thus he asks Mishka some questions about it to determine if some song is worth listening to. Questions have the following format: * i~t β€” count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters. Mishka doesn't question the purpose of that information, yet he struggles to provide it. Can you please help Mishka answer all Vova's questions? Input The first line contains a single integer n (1 ≀ n ≀ 4 β‹… 10^5) β€” the number of songs in the album. Each of the next n lines contains the desciption of the i-th song of the album in the following format: * 1~c β€” s_i is a single lowercase Latin letter; * 2~j~c β€” s_i is the name s_j (1 ≀ j < i) with a single lowercase Latin letter appended to its end. The next line contains a single integer m (1 ≀ m ≀ 4 β‹… 10^5) β€” the number of Vova's questions. Each of the next m lines contains the desciption of the j-th Vova's question in the following format: * i~t (1 ≀ i ≀ n, 1 ≀ |t| ≀ 4 β‹… 10^5) β€” count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters. It's guaranteed that the total length of question strings t doesn't exceed 4 β‹… 10^5. Output For each question print a single integer β€” the number of occurrences of the question string t in the name of the i-th song of the album as a continuous substring. Example Input 20 1 d 2 1 a 2 2 d 2 3 a 2 4 d 2 5 a 2 6 d 2 7 a 1 d 2 9 o 2 10 k 2 11 i 2 12 d 2 13 o 2 14 k 2 15 i 2 1 o 2 17 k 2 18 i 2 15 i 12 8 da 8 dada 8 ada 6 dada 3 dada 19 doki 19 ok 16 doki 15 doki 9 d 1 a 20 doki Output 4 3 3 2 0 1 1 2 1 1 0 2 Note Song names of the first example: 1. d 2. da 3. dad 4. dada 5. dadad 6. dadada 7. dadadad 8. dadadada 9. d 10. do 11. dok 12. doki 13. dokid 14. dokido 15. dokidok 16. dokidoki 17. do 18. dok 19. doki 20. dokidoki Thus the occurrences for each question string are: 1. string "da" starts in positions [1, 3, 5, 7] in the name "dadadada"; 2. string "dada" starts in positions [1, 3, 5] in the name "dadadada"; 3. string "ada" starts in positions [2, 4, 6] in the name "dadadada"; 4. string "dada" starts in positions [1, 3] in the name "dadada"; 5. no occurrences of string "dada" in the name "dad"; 6. string "doki" starts in position [1] in the name "doki"; 7. string "ok" starts in position [2] in the name "doki"; 8. string "doki" starts in positions [1, 5] in the name "dokidoki"; 9. string "doki" starts in position [1] in the name "dokidok"; 10. string "d" starts in position [1] in the name "d"; 11. no occurrences of string "a" in the name "d"; 12. string "doki" starts in positions [1, 5] in the name "dokidoki". Submitted Solution: ``` def f(string,t): n1=len(t) n2=len(string) c=0 for i in range(n2-n1+1): if string[i:i+n1]==t: c+=1 return c n=int(input()) l=[0 for i in range(n)] for w in range(n): s=list(map(str,input().strip().split())) if s[0]=="1": l[w]=s[1] else: l[w]=l[int(s[1])-1]+s[2] m=int(input()) for t in range(m): s2=list(map(str,input().strip().split())) i=int(s2[0]) t=s2[1:] print(f(l[i-1],t)) ``` No
89,350
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mishka's favourite experimental indie band has recently dropped a new album! Songs of that album share one gimmick. Each name s_i is one of the following types: * 1~c β€” a single lowercase Latin letter; * 2~j~c β€” name s_j (1 ≀ j < i) with a single lowercase Latin letter appended to its end. Songs are numbered from 1 to n. It's guaranteed that the first song is always of type 1. Vova is rather interested in the new album but he really doesn't have the time to listen to it entirely. Thus he asks Mishka some questions about it to determine if some song is worth listening to. Questions have the following format: * i~t β€” count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters. Mishka doesn't question the purpose of that information, yet he struggles to provide it. Can you please help Mishka answer all Vova's questions? Input The first line contains a single integer n (1 ≀ n ≀ 4 β‹… 10^5) β€” the number of songs in the album. Each of the next n lines contains the desciption of the i-th song of the album in the following format: * 1~c β€” s_i is a single lowercase Latin letter; * 2~j~c β€” s_i is the name s_j (1 ≀ j < i) with a single lowercase Latin letter appended to its end. The next line contains a single integer m (1 ≀ m ≀ 4 β‹… 10^5) β€” the number of Vova's questions. Each of the next m lines contains the desciption of the j-th Vova's question in the following format: * i~t (1 ≀ i ≀ n, 1 ≀ |t| ≀ 4 β‹… 10^5) β€” count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters. It's guaranteed that the total length of question strings t doesn't exceed 4 β‹… 10^5. Output For each question print a single integer β€” the number of occurrences of the question string t in the name of the i-th song of the album as a continuous substring. Example Input 20 1 d 2 1 a 2 2 d 2 3 a 2 4 d 2 5 a 2 6 d 2 7 a 1 d 2 9 o 2 10 k 2 11 i 2 12 d 2 13 o 2 14 k 2 15 i 2 1 o 2 17 k 2 18 i 2 15 i 12 8 da 8 dada 8 ada 6 dada 3 dada 19 doki 19 ok 16 doki 15 doki 9 d 1 a 20 doki Output 4 3 3 2 0 1 1 2 1 1 0 2 Note Song names of the first example: 1. d 2. da 3. dad 4. dada 5. dadad 6. dadada 7. dadadad 8. dadadada 9. d 10. do 11. dok 12. doki 13. dokid 14. dokido 15. dokidok 16. dokidoki 17. do 18. dok 19. doki 20. dokidoki Thus the occurrences for each question string are: 1. string "da" starts in positions [1, 3, 5, 7] in the name "dadadada"; 2. string "dada" starts in positions [1, 3, 5] in the name "dadadada"; 3. string "ada" starts in positions [2, 4, 6] in the name "dadadada"; 4. string "dada" starts in positions [1, 3] in the name "dadada"; 5. no occurrences of string "dada" in the name "dad"; 6. string "doki" starts in position [1] in the name "doki"; 7. string "ok" starts in position [2] in the name "doki"; 8. string "doki" starts in positions [1, 5] in the name "dokidoki"; 9. string "doki" starts in position [1] in the name "dokidok"; 10. string "d" starts in position [1] in the name "d"; 11. no occurrences of string "a" in the name "d"; 12. string "doki" starts in positions [1, 5] in the name "dokidoki". Submitted Solution: ``` int_tot = int(input()) alb_name = [] que_vov = [] for x in range(0, int_tot): temp = input().split(" ") if temp[0] == '1': alb_name.append(temp[1]) else: alb_name.append(alb_name[int(temp[1]) - 1] + temp[2]) int_tot = int(input()) for x in range(0, int_tot): que_vov.append(input().split(" ")) for s in range(0, len(que_vov)): counter = 0 temp_alb_name = alb_name[int(que_vov[s][0]) - 1] for y in range(0, len(temp_alb_name)): hp = y + len(que_vov[s][1]) if len(temp_alb_name) - y > len(que_vov[s][1]): temp_ = temp_alb_name[y:y + len(que_vov[s][1])] if temp_ == que_vov[s][1]: counter += 1 print(counter) ``` No
89,351
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mishka's favourite experimental indie band has recently dropped a new album! Songs of that album share one gimmick. Each name s_i is one of the following types: * 1~c β€” a single lowercase Latin letter; * 2~j~c β€” name s_j (1 ≀ j < i) with a single lowercase Latin letter appended to its end. Songs are numbered from 1 to n. It's guaranteed that the first song is always of type 1. Vova is rather interested in the new album but he really doesn't have the time to listen to it entirely. Thus he asks Mishka some questions about it to determine if some song is worth listening to. Questions have the following format: * i~t β€” count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters. Mishka doesn't question the purpose of that information, yet he struggles to provide it. Can you please help Mishka answer all Vova's questions? Input The first line contains a single integer n (1 ≀ n ≀ 4 β‹… 10^5) β€” the number of songs in the album. Each of the next n lines contains the desciption of the i-th song of the album in the following format: * 1~c β€” s_i is a single lowercase Latin letter; * 2~j~c β€” s_i is the name s_j (1 ≀ j < i) with a single lowercase Latin letter appended to its end. The next line contains a single integer m (1 ≀ m ≀ 4 β‹… 10^5) β€” the number of Vova's questions. Each of the next m lines contains the desciption of the j-th Vova's question in the following format: * i~t (1 ≀ i ≀ n, 1 ≀ |t| ≀ 4 β‹… 10^5) β€” count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters. It's guaranteed that the total length of question strings t doesn't exceed 4 β‹… 10^5. Output For each question print a single integer β€” the number of occurrences of the question string t in the name of the i-th song of the album as a continuous substring. Example Input 20 1 d 2 1 a 2 2 d 2 3 a 2 4 d 2 5 a 2 6 d 2 7 a 1 d 2 9 o 2 10 k 2 11 i 2 12 d 2 13 o 2 14 k 2 15 i 2 1 o 2 17 k 2 18 i 2 15 i 12 8 da 8 dada 8 ada 6 dada 3 dada 19 doki 19 ok 16 doki 15 doki 9 d 1 a 20 doki Output 4 3 3 2 0 1 1 2 1 1 0 2 Note Song names of the first example: 1. d 2. da 3. dad 4. dada 5. dadad 6. dadada 7. dadadad 8. dadadada 9. d 10. do 11. dok 12. doki 13. dokid 14. dokido 15. dokidok 16. dokidoki 17. do 18. dok 19. doki 20. dokidoki Thus the occurrences for each question string are: 1. string "da" starts in positions [1, 3, 5, 7] in the name "dadadada"; 2. string "dada" starts in positions [1, 3, 5] in the name "dadadada"; 3. string "ada" starts in positions [2, 4, 6] in the name "dadadada"; 4. string "dada" starts in positions [1, 3] in the name "dadada"; 5. no occurrences of string "dada" in the name "dad"; 6. string "doki" starts in position [1] in the name "doki"; 7. string "ok" starts in position [2] in the name "doki"; 8. string "doki" starts in positions [1, 5] in the name "dokidoki"; 9. string "doki" starts in position [1] in the name "dokidok"; 10. string "d" starts in position [1] in the name "d"; 11. no occurrences of string "a" in the name "d"; 12. string "doki" starts in positions [1, 5] in the name "dokidoki". Submitted Solution: ``` int_tot = int(input()) alb_name = [] que_vov = [] for x in range(0, int_tot): temp = input().split(" ") if temp[0] == '1': alb_name.append(temp[1]) else: alb_name.append(alb_name[int(temp[1]) - 1] + temp[2]) int_tot = int(input()) for x in range(0, int_tot): que_vov.append(input().split(" ")) for s in range(0, len(que_vov)): counter = 0 temp_alb_name = alb_name[int(que_vov[s][0]) - 1] if len(temp_alb_name) < len(que_vov[s][1]): break for y in range(0, len(temp_alb_name)): temp_ = temp_alb_name[y:y + len(que_vov[s][1])] if temp_ == que_vov[s][1]: counter += 1 print(counter) ``` No
89,352
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mishka's favourite experimental indie band has recently dropped a new album! Songs of that album share one gimmick. Each name s_i is one of the following types: * 1~c β€” a single lowercase Latin letter; * 2~j~c β€” name s_j (1 ≀ j < i) with a single lowercase Latin letter appended to its end. Songs are numbered from 1 to n. It's guaranteed that the first song is always of type 1. Vova is rather interested in the new album but he really doesn't have the time to listen to it entirely. Thus he asks Mishka some questions about it to determine if some song is worth listening to. Questions have the following format: * i~t β€” count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters. Mishka doesn't question the purpose of that information, yet he struggles to provide it. Can you please help Mishka answer all Vova's questions? Input The first line contains a single integer n (1 ≀ n ≀ 4 β‹… 10^5) β€” the number of songs in the album. Each of the next n lines contains the desciption of the i-th song of the album in the following format: * 1~c β€” s_i is a single lowercase Latin letter; * 2~j~c β€” s_i is the name s_j (1 ≀ j < i) with a single lowercase Latin letter appended to its end. The next line contains a single integer m (1 ≀ m ≀ 4 β‹… 10^5) β€” the number of Vova's questions. Each of the next m lines contains the desciption of the j-th Vova's question in the following format: * i~t (1 ≀ i ≀ n, 1 ≀ |t| ≀ 4 β‹… 10^5) β€” count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters. It's guaranteed that the total length of question strings t doesn't exceed 4 β‹… 10^5. Output For each question print a single integer β€” the number of occurrences of the question string t in the name of the i-th song of the album as a continuous substring. Example Input 20 1 d 2 1 a 2 2 d 2 3 a 2 4 d 2 5 a 2 6 d 2 7 a 1 d 2 9 o 2 10 k 2 11 i 2 12 d 2 13 o 2 14 k 2 15 i 2 1 o 2 17 k 2 18 i 2 15 i 12 8 da 8 dada 8 ada 6 dada 3 dada 19 doki 19 ok 16 doki 15 doki 9 d 1 a 20 doki Output 4 3 3 2 0 1 1 2 1 1 0 2 Note Song names of the first example: 1. d 2. da 3. dad 4. dada 5. dadad 6. dadada 7. dadadad 8. dadadada 9. d 10. do 11. dok 12. doki 13. dokid 14. dokido 15. dokidok 16. dokidoki 17. do 18. dok 19. doki 20. dokidoki Thus the occurrences for each question string are: 1. string "da" starts in positions [1, 3, 5, 7] in the name "dadadada"; 2. string "dada" starts in positions [1, 3, 5] in the name "dadadada"; 3. string "ada" starts in positions [2, 4, 6] in the name "dadadada"; 4. string "dada" starts in positions [1, 3] in the name "dadada"; 5. no occurrences of string "dada" in the name "dad"; 6. string "doki" starts in position [1] in the name "doki"; 7. string "ok" starts in position [2] in the name "doki"; 8. string "doki" starts in positions [1, 5] in the name "dokidoki"; 9. string "doki" starts in position [1] in the name "dokidok"; 10. string "d" starts in position [1] in the name "d"; 11. no occurrences of string "a" in the name "d"; 12. string "doki" starts in positions [1, 5] in the name "dokidoki". Submitted Solution: ``` int_tot = int(input()) alb_name = [] que_vov = [] for x in range(0, int_tot): temp = input().split(" ") if temp[0] == '1': alb_name.append(temp[1]) else: alb_name.append(alb_name[int(temp[1]) - 1] + temp[2]) int_tot = int(input()) for x in range(0, int_tot): que_vov.append(input().split(" ")) for s in range(0, len(que_vov)): counter = 0 temp_alb_name = alb_name[int(que_vov[s][0]) - 1] for y in range(s,len(que_vov[s][0])): temp_ = temp_alb_name[y:y + len(que_vov[s][1])] if temp_ == que_vov[s][1]: counter += 1 print(counter) ``` No
89,353
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n positive integers written on the blackboard. Also, a positive number k β‰₯ 2 is chosen, and none of the numbers on the blackboard are divisible by k. In one operation, you can choose any two integers x and y, erase them and write one extra number f(x + y), where f(x) is equal to x if x is not divisible by k, otherwise f(x) = f(x / k). In the end, there will be a single number of the blackboard. Is it possible to make the final number equal to 1? If so, restore any sequence of operations to do so. Input The first line contains two integers n and k β€” the initial number of integers on the blackboard, and the chosen number (2 ≀ n ≀ 16, 2 ≀ k ≀ 2000). The second line contains n positive integers a_1, …, a_n initially written on the blackboard. It is guaranteed that none of the numbers a_i is divisible by k, and the sum of all a_i does not exceed 2000. Output If it is impossible to obtain 1 as the final number, print "NO" in the only line. Otherwise, print "YES" on the first line, followed by n - 1 lines describing operations. The i-th of these lines has to contain two integers x_i and y_i to be erased and replaced with f(x_i + y_i) on the i-th operation. If there are several suitable ways, output any of them. Examples Input 2 2 1 1 Output YES 1 1 Input 4 3 7 8 13 23 Output YES 23 13 8 7 5 4 Input 3 4 1 2 3 Output NO Note In the second sample case: * f(8 + 7) = f(15) = f(5) = 5; * f(23 + 13) = f(36) = f(12) = f(4) = 4; * f(5 + 4) = f(9) = f(3) = f(1) = 1. Submitted Solution: ``` def func(x,k): while x % k ==0: x=x//k return x n,k = map(int, input().split()) list_numbers = list(map(int,input().split())) i=0 string_list = [] while len(list_numbers) >1: curr_elem = list_numbers[i] flag1 = True list_test = list_numbers[:] list_test.remove(curr_elem) for elem1 in list_test: if (curr_elem+elem1) % k ==0: list_numbers.remove(elem1) list_numbers.remove(curr_elem) list_numbers.append(func(elem1+curr_elem,k)) string_list.append('{0} {1}'.format(elem1,curr_elem)) flag1 = False break if flag1: i +=1 if i >= len(list_numbers): elem1 = list_numbers[0] elem2 = list_numbers[1] list_numbers.remove(elem1) list_numbers.remove(elem2) list_numbers.append(func(elem1+elem2,k)) string_list.append('{0} {1}'.format(elem1,elem2)) i = 0 else: i = 0 if list_numbers == [1]: print('YES') for i in range(len(string_list)): print(string_list[i]) else: print('NO') ``` No
89,354
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n positive integers written on the blackboard. Also, a positive number k β‰₯ 2 is chosen, and none of the numbers on the blackboard are divisible by k. In one operation, you can choose any two integers x and y, erase them and write one extra number f(x + y), where f(x) is equal to x if x is not divisible by k, otherwise f(x) = f(x / k). In the end, there will be a single number of the blackboard. Is it possible to make the final number equal to 1? If so, restore any sequence of operations to do so. Input The first line contains two integers n and k β€” the initial number of integers on the blackboard, and the chosen number (2 ≀ n ≀ 16, 2 ≀ k ≀ 2000). The second line contains n positive integers a_1, …, a_n initially written on the blackboard. It is guaranteed that none of the numbers a_i is divisible by k, and the sum of all a_i does not exceed 2000. Output If it is impossible to obtain 1 as the final number, print "NO" in the only line. Otherwise, print "YES" on the first line, followed by n - 1 lines describing operations. The i-th of these lines has to contain two integers x_i and y_i to be erased and replaced with f(x_i + y_i) on the i-th operation. If there are several suitable ways, output any of them. Examples Input 2 2 1 1 Output YES 1 1 Input 4 3 7 8 13 23 Output YES 23 13 8 7 5 4 Input 3 4 1 2 3 Output NO Note In the second sample case: * f(8 + 7) = f(15) = f(5) = 5; * f(23 + 13) = f(36) = f(12) = f(4) = 4; * f(5 + 4) = f(9) = f(3) = f(1) = 1. Submitted Solution: ``` def func(x,k): while x % k ==0: x=x//k return x n,k = map(int, input().split()) list_numbers = list(map(int,input().split())) i=0 string_list = [] while len(list_numbers) >1: curr_elem = list_numbers[i] flag1 = True list_test = list_numbers[:] list_test.remove(curr_elem) for elem1 in list_test: if (curr_elem+elem1) % k ==0: list_numbers.remove(elem1) list_numbers.remove(curr_elem) list_numbers.append(func(elem1+curr_elem,k)) string_list.append('{0} {1}'.format(elem1,curr_elem)) flag1 = False break if flag1: i +=1 if i >= len(list_numbers): break else: i = 0 if list_numbers == [1]: print('YES') for i in range(len(string_list)): print(string_list[i]) else: print('NO') ``` No
89,355
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n positive integers written on the blackboard. Also, a positive number k β‰₯ 2 is chosen, and none of the numbers on the blackboard are divisible by k. In one operation, you can choose any two integers x and y, erase them and write one extra number f(x + y), where f(x) is equal to x if x is not divisible by k, otherwise f(x) = f(x / k). In the end, there will be a single number of the blackboard. Is it possible to make the final number equal to 1? If so, restore any sequence of operations to do so. Input The first line contains two integers n and k β€” the initial number of integers on the blackboard, and the chosen number (2 ≀ n ≀ 16, 2 ≀ k ≀ 2000). The second line contains n positive integers a_1, …, a_n initially written on the blackboard. It is guaranteed that none of the numbers a_i is divisible by k, and the sum of all a_i does not exceed 2000. Output If it is impossible to obtain 1 as the final number, print "NO" in the only line. Otherwise, print "YES" on the first line, followed by n - 1 lines describing operations. The i-th of these lines has to contain two integers x_i and y_i to be erased and replaced with f(x_i + y_i) on the i-th operation. If there are several suitable ways, output any of them. Examples Input 2 2 1 1 Output YES 1 1 Input 4 3 7 8 13 23 Output YES 23 13 8 7 5 4 Input 3 4 1 2 3 Output NO Note In the second sample case: * f(8 + 7) = f(15) = f(5) = 5; * f(23 + 13) = f(36) = f(12) = f(4) = 4; * f(5 + 4) = f(9) = f(3) = f(1) = 1. Submitted Solution: ``` n, k = map(int, input().split()) a = list(map(lambda x: (int(x), int(x) % k), input().split())) c = 0 for i in a: c += i[1] if c % k != 0: print('NO') else: print('YES') while len(a) > 1: k1 = a.pop(0)[0] k2 = a.pop(0)[0] print(k1, k2) s = k1 + k2 while s % k == 0: s //= k a.append((s, s % k)) ``` No
89,356
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n positive integers written on the blackboard. Also, a positive number k β‰₯ 2 is chosen, and none of the numbers on the blackboard are divisible by k. In one operation, you can choose any two integers x and y, erase them and write one extra number f(x + y), where f(x) is equal to x if x is not divisible by k, otherwise f(x) = f(x / k). In the end, there will be a single number of the blackboard. Is it possible to make the final number equal to 1? If so, restore any sequence of operations to do so. Input The first line contains two integers n and k β€” the initial number of integers on the blackboard, and the chosen number (2 ≀ n ≀ 16, 2 ≀ k ≀ 2000). The second line contains n positive integers a_1, …, a_n initially written on the blackboard. It is guaranteed that none of the numbers a_i is divisible by k, and the sum of all a_i does not exceed 2000. Output If it is impossible to obtain 1 as the final number, print "NO" in the only line. Otherwise, print "YES" on the first line, followed by n - 1 lines describing operations. The i-th of these lines has to contain two integers x_i and y_i to be erased and replaced with f(x_i + y_i) on the i-th operation. If there are several suitable ways, output any of them. Examples Input 2 2 1 1 Output YES 1 1 Input 4 3 7 8 13 23 Output YES 23 13 8 7 5 4 Input 3 4 1 2 3 Output NO Note In the second sample case: * f(8 + 7) = f(15) = f(5) = 5; * f(23 + 13) = f(36) = f(12) = f(4) = 4; * f(5 + 4) = f(9) = f(3) = f(1) = 1. Submitted Solution: ``` count, divisor = [int(x) for x in input().split()] sequence = [int(x) for x in input().split()] summ = sum(sequence) if summ % divisor != 0: print("NO") exit() else: path = list() while len(sequence) != 1: for i in range(count - 1): try: a = sequence[i] + sequence[i + 1] path.append([sequence[i], sequence[i + 1]]) sequence[i] = sequence[i] + sequence[i + 1] sequence[i + 1] = '' except: pass sequence = [part for part in sequence if part != ''] if sequence[0] % divisor == 0: print("YES") for i in range(len(path)): print(*path[i]) ``` No
89,357
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Tags: implementation, number theory, strings Correct Solution: ``` import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush # from math import * 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 import copy import time starttime = time.time() mod = int(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)] try: # sys.setrecursionlimit(int(pow(10,4))) sys.stdin = open("input.txt", "r") # sys.stdout = open("../output.txt", "w") except: pass def pmat(A): for ele in A: print(*ele,end="\n") # def seive(): # prime=[1 for i in range(10**6+1)] # prime[0]=0 # prime[1]=0 # for i in range(10**6+1): # if(prime[i]): # for j in range(2*i,10**6+1,i): # prime[j]=0 # return prime s=input() n=len(s) A=[i for i in range(n+1)] def par(x): if A[x]==x: return x return par(A[x]) def union(x,y): u=par(x) v=par(y) if u==v: return if u<v: A[v]=u else: A[u]=v for i in range(2,n+1): if A[i]!=i: continue for j in range(2*i,n+1,i): union(i,j) d={} for i in range(1,n+1): d[A[i]]=d.get(A[i],0)+1 cnt={} for c in s: cnt[c]=cnt.get(c,0)+1 B1=[[d[i],i] for i in d] B2=[[cnt[i],i] for i in cnt] B1.sort(reverse=True) B2.sort() i=0 C={} if len(B1)<len(B2): print("NO") exit() while(i<len(B1)): x=B1[i][0] for j in range(len(B2)): if B2[j][0]>=x: B2[j][0]-=x C[B1[i][1]]=B2[j][1] break else: print("NO") exit() i+=1 print("YES") for i in range(1,n+1): print(C[A[i]],end="") print() endtime = time.time() # print(f"Runtime of the program is {endtime - starttime}") ```
89,358
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Tags: implementation, number theory, strings Correct Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * from fractions import * from bisect import * from heapq 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' MOD=1000000007 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])] s=input() n=len(s) primes=primeN(n) C=Counter(s) ans=['' for i in range(n)] have={} for i in primes: have[i]=n//i moreThanOne=[] need=set() for p in have: if(have[p]>1): moreThanOne.append(p) for i in range(p-1,n,p): need.add(i) need=len(need) # print(need,have) ok=False for i in C: if(C[i]>=need): ok=True ele=i C[i]-=need break if(not ok): print("NO") exit() print("YES") for p in moreThanOne: for i in range(p-1,n,p): ans[i]=ele rem=list(set(s)) ind=0 for i in range(n): if(ans[i]==''): while(C[rem[ind]]==0): ind+=1 ans[i]=rem[ind] C[rem[ind]]-=1 print(*ans,sep="") ```
89,359
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Tags: implementation, number theory, strings Correct Solution: ``` #!/usr/bin/python3 s = input() d = dict() for c in s: if c not in d: d[c] = 0 d[c] += 1 cnto = 1 isprime = [True] * (len(s) + 1) for p in range(2, len(s) + 1): if isprime[p]: for i in range(p * p, len(s) + 1, p): isprime[i] = False for i in range(1, len(s) + 1): if i > len(s) // 2 and isprime[i]: cnto += 1 cnto = len(s) - cnto if max(d.values()) < cnto: print("NO") else: print("YES") m = max(d.values()) for c, v in d.items(): if v == m: d[c] -= cnto mc = c break ans = [] buf = [] for c, v in d.items(): for i in range(v): buf.append(c) for i in range(1, len(s) + 1): if i == 1 or (i > len(s) // 2 and isprime[i]): ans.append(buf[-1]) buf.pop() else: ans.append(mc) print("".join(ans)) ```
89,360
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Tags: implementation, number theory, strings Correct Solution: ``` from collections import Counter def is_prime(x): if x < 2: return 0 for i in range(2, x): if x % i == 0: return False return True def proc(s): n = len(s) same = set() for p in range(2,n+1): if not is_prime(p): continue if p * 2 > n: continue for i in range(2, n//p+1): same.add(p*i) same.add(p) counter = Counter(s) ch, count = counter.most_common(1)[0] if count < len(same): print("NO") return same = [x-1 for x in same] w = [x for x in s] for i in same: if w[i] == ch: continue for j in range(n): if j not in same and w[j] == ch: tmp = w[j] w[j] = w[i] w[i] = tmp break print("YES") print(''.join(w)) s = input() proc(s) ```
89,361
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Tags: implementation, number theory, strings Correct Solution: ``` import math ch='abcdefghijklmnopqrstuvwxyz' def sieve(n): 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 s = ['#']+list(input()) lis=[0]*26 n = len(s)-1 prime = [True for i in range(1000 + 1)] sieve(1000) ans=['']*(n+1) aa=[] aa.append(1) for i in s[1:]: lis[ord(i)-ord('a')]+=1 for i in range(n//2+1,n+1,1): if prime[i]: aa.append(i) v = n-len(aa) th=-1 for i in range(26): if lis[i]>=v: th=i if th==-1: print("NO") exit() for i in range(2,n+1): if i not in aa: ans[i]=ch[th] lis[th]-=1 j=0 #print(ans,aa,lis) for i in aa: while j<26 and lis[j]<=0: j+=1 ans[i]=ch[j] lis[j]-=1 # print(ans,lis) print("YES") print(*ans[1:],sep='') ```
89,362
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Tags: implementation, number theory, strings Correct Solution: ``` S = input() s = [] d = {} n = 0 for i in S: if ord('a') <= ord(i) <= ord('z'): n += 1 if i in d: d[i] += 1 else: d[i] = 1 primes = [] for i in range(max(3,n // 2 + 1), n+1): j = 2 f = 0 while j * j <= i: if i % j == 0: f = 1 break j += 1 if not f: primes.append(i) b = [] for i in d: b.append([d[i], i]) ans = ['' for i in range(n)] b.sort() for i in primes: ans[i-1] = b[0][1] b[0][0] -= 1 if b[0][0] == 0: b.pop(0) if len(b) >2 or (len(b)==2 and b[0][0]!=1): print('NO') else: print('YES') for i in ans: if i == '': print(b[0][1], end='') b[0][0]-=1 if b[0][0]==0: b.pop(0) else: print(i,end='') ```
89,363
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Tags: implementation, number theory, strings Correct Solution: ``` def prime(a): i = 2 while i * i <= a: if a % i == 0: return False i += 1 return True tab = list(input()) letter_count = {} for letter in tab: if letter in letter_count: letter_count[letter] += 1 else: letter_count[letter] = 1 max_amount_letter = 'a' max_amount = 0 for letter, amount in letter_count.items(): if max_amount < amount: max_amount = amount max_amount_letter = letter good_prime = [False for i in range(len(tab))] good_prime[0] = True for n in range((len(tab) + 2) // 2, (len(tab) + 1)): good_prime[n - 1] = prime(n) if max_amount >= good_prime.count(False): print("YES") letter_count[max_amount_letter] = 0 for i in range(len(good_prime)): if good_prime[i] == False: good_prime[i] = max_amount_letter max_amount -= 1 for i in range(len(good_prime)): if good_prime[i] == True and max_amount > 0: good_prime[i] = max_amount_letter max_amount -= 1 i = 0 for letter, amount in letter_count.items(): for a in range(amount): while good_prime[i] != True: i += 1 good_prime[i] = letter i += 1 print(''.join(good_prime)) else: print("NO") ```
89,364
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Tags: implementation, number theory, strings Correct Solution: ``` from collections import Counter d, t = 'NO', input() c, n = Counter(t), len(t) p = [0] * (n + 1) for i in range(2, n // 2 + 1): if 1 - p[i]: p[i::i] = [1] * (n // i) s = sum(p) u = v = '' for q, k in c.items(): if v or k < s: u += q * k else: u += q * (k - s) v = q if v: d, j = 'YES\n', 0 for i in range(1, n + 1): if p[i]: d += v else: d += u[j] j += 1 print(d) ```
89,365
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` import os,io from sys import stdout import collections import random import math from operator import itemgetter input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from collections import Counter # import sys # sys.setrecursionlimit(10**6) def primes(n): """ Returns a list of primes < n """ sieve = [True] * n for i in range(3,int(n**0.5)+1,2): if sieve[i]: sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1) return [2] + [i for i in range(3,n,2) if sieve[i]] def binomial_coefficient(n, k): if 0 <= k <= n: ntok = 1 ktok = 1 for t in range(1, min(k, n - k) + 1): ntok *= n ktok *= t n -= 1 return ntok // ktok else: return 0 def powerOfK(k, max): if k == 1: return [1] if k == -1: return [-1, 1] result = [] n = 1 while n <= max: result.append(n) n *= k return result def prefixSum(arr): for i in range(1, len(arr)): arr[i] = arr[i] + arr[i-1] return arr def divisors(n): i = 1 result = [] while i*i <= n: if n%i == 0: if n/i == i: result.append(i) else: result.append(i) result.append(n/i) i+=1 return result # from functools import lru_cache # @lru_cache(maxsize=None) s = input().decode('utf-8').strip() # n = int(input()) # l = list(map(int, input().split())) c = Counter(s) dp = [0] * len(s) primes = primes(len(s)+1) components = [] for p in primes: l = [] for i in range(p, len(s)+1, p): l.append(i) for comp in components: if len(list(set(comp) & set(l))): comp += l break else: if len(l): components.append(l) need = [] for comp in components: need.append(len(set(comp))) import heapq heap = [] for e, v in c.items(): heapq.heappush(heap, (-v, e)) result = [""] * (len(s)) for comp in components: i, letter = heapq.heappop(heap) i = -i indexes = set(comp) if i >= len(indexes): heapq.heappush(heap, (-(i-len(indexes)), letter)) for index in set(comp): result[index-1] = letter else: print("NO") break else: while len(heap): i, letter = heapq.heappop(heap) for j in range(len(result)): if i == 0: break if result[j] == "": result[j] = letter i -= 1 print("YES") print("".join(result)) # a = sorted(list(c.values())) # b = sorted(need) # # i, j = len(b)-1, len(a)-1 # while i >= 0 and j >= 0: # if b[i] <= a[j]: # a[j] -= b[i] # i -= 1 # else: # j -= 1 # # if i == -1: # print("YES") # else: # print("NO") ``` Yes
89,366
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` s = input() if len(s) == 1: exit(print('YES\n' + s)) d, ans, sieve = {}, [0] * len(s), [0] * 1005 for i in s: d[i] = d.get(i, 0) + 1 for i in range(2, len(s) + 1): if not sieve[i]: for j in range(i * i, 1005, i): sieve[j] = 1 mx = max(d, key=lambda x: d[x]) for j in range(2, len(s) // 2 + 1): ans[j - 1] = mx d[mx] -= 1 if d[mx] < 0: exit(print('NO')) for i in range(len(s) // 2 + 1, len(s) + 1): if not sieve[i]: mx = max(d, key=lambda x: d[x] and x != ans[1]) if not d[mx]: mx = ans[1] ans[i - 1] = mx d[mx] -= 1 else: ans[i - 1] = ans[1] d[ans[1]] -= 1 if d[mx] < 0 or d[ans[1]] < 0: exit(print('NO')) print('YES') mx = max(d, key=lambda x: d[x]) print(mx + ''.join(ans[1:])) ``` Yes
89,367
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` import sys import math from collections import defaultdict MAXNUM = math.inf MINNUM = -1 * math.inf ASCIILOWER = 97 ASCIIUPPER = 65 def getInt(): return int(sys.stdin.readline().rstrip()) def getInts(): return map(int, sys.stdin.readline().rstrip().split(" ")) def getString(): return sys.stdin.readline().rstrip() def printOutput(ans): sys.stdout.write() pass def getPrimes(s): primes = [2] MAX = s + 1 for i in range(3, MAX): STOP = math.ceil(math.sqrt(i) + 1) isPrime = True for j in range(2, STOP): if i % j == 0: isPrime = False if isPrime: primes.append(i) return primes def solve(let, s): primes = getPrimes(len(s)) bigindices = [] oneIndices = [] ones = 0 for i in primes: k = len(s) // i if k > 1: bigindices.append(i) if k == 1: oneIndices.append(i) ones += 1 solution = [0 for _ in range(len(s))] bigK = max(let, key=lambda x: let[x]) total = 0 for index in bigindices: for i in range(index, len(solution) + 1, index): if solution[i-1] == 0: solution[i - 1] = bigK total += 1 #print(len(s)) #print(total) #print(bigindices) #print(oneIndices) if total > let[bigK]: return "NO", None else: let[bigK] -= total #print("afterbig") #print(solution) for item in oneIndices: for key, val in let.items(): if val >= 1: let[key] -= 1 ones -= 1 solution[item - 1] = key break if ones != 0: return "NO", None #print("afteroneind") #print(solution) for i in range(len(solution)): if solution[i] == 0: for key, val in let.items(): if val > 0: val -= 1 solution[i] = key break return "YES", "".join(solution) def readinput(): lettercount = defaultdict(int) string = getString() for ele in string: lettercount[ele] += 1 ans = solve(lettercount, string) print(ans[0]) if ans[0] != "NO": print(ans[1]) readinput() ``` Yes
89,368
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` def h(s, k): res = '' for i in s: if i != k: res += i return res def ans(s, k, times, Pr: list): d = h(s, k) j = 0 res = '' cc = 0 for i in range(len(s)): if (i + 1) not in Pr: res += k cc += 1 elif j < len(d): res += d[j] j += 1 else: res += k return res def ans1(s, k, times, Pr: list): d = h(s, k) res = '' res += d[0] j = 1 for i in range(len(s) - 1): if (i + 2) not in Pr: res += k elif j < len(d): res += d[j] j += 1 else: res += k return res def isprime(n): i = 2 while i*i <= n: if n % i == 0: return False i += 1 return True def getprimes(n): res = [] for i in range(2, n + 1): if isprime(i): res.append(i) return res s = input() A = [] Primes = getprimes(len(s)) def f(A: list, l): res = 0 resPr = [] for i in A: if i > l // 2: res += 1 resPr.append(i) return res, resPr g, Pr = f(Primes, len(s)) Dict = dict() for i in s: if i in Dict: Dict[i] += 1 else: Dict[i] = 1 for k, v in Dict.items(): if v >= len(s) - g: print('YES') print(ans(s, k, len(s) - g, Pr), end='') exit() elif v == len(s) - g - 1: print('YES') print(ans1(s, k, len(s) - g, Pr), end='') exit() print('NO') ``` Yes
89,369
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` def h(s, k): res = '' for i in s: if i != k: res += i return res def ans(s, k, times, Pr: list): d = h(s, k) j = 0 res = '' cc = 0 for i in range(len(s)): if (i + 1) not in Pr: res += k cc += 1 elif j < len(d): res += d[j] j += 1 else: res += k return res def ans1(s, k, times, Pr: list): d = h(s, k) res = '' res += d[0] m = s.find(d[0]) res += ans(s[:m] + s[m + 1:], k, times, Pr) return res def isprime(n): i = 2 while i*i <= n: if n % i == 0: return False i += 1 return True def getprimes(n): res = [] for i in range(2, n + 1): if isprime(i): res.append(i) return res s = input() A = [] Primes = getprimes(len(s)) def f(A: list, l): res = 0 resPr = [] for i in A: if i > l // 2: res += 1 resPr.append(i) return res, resPr g, Pr = f(Primes, len(s)) Dict = dict() for i in s: if i in Dict: Dict[i] += 1 else: Dict[i] = 1 for k, v in Dict.items(): if v >= len(s) - g: print('YES') print(ans(s, k, len(s) - g, Pr), end='') exit() elif v == len(s) - g - 1: print('YES') print(ans1(s, k, len(s) - g, Pr), end='') exit() print('NO') ``` No
89,370
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` s = input() d, ans, sieve = {}, [0] * len(s), [0] * 1005 for i in s: d[i] = d.get(i, 0) + 1 for i in range(2, len(s) + 1): if not sieve[i]: for j in range(i * i, 1005, i): sieve[j] = 1 mx = max(d, key=lambda x: d[x]) for j in range(2, (len(s) + 1) // 2): ans[j - 1] = mx d[mx] -= 1 if d[mx] < 0: exit(print('NO')) for i in range((len(s) + 1) // 2, len(s) + 1): if not sieve[i]: mx = max(d, key=lambda x: d[x]) ans[i - 1] = mx d[mx] -= 1 else: ans[i - 1] = ans[1] d[ans[1]] -= 1 if d[mx] < 0 or d[ans[1]] < 0: exit(print('NO')) print('YES') mx = max(d, key=lambda x: d[x]) print(mx + ''.join(ans[1:])) ``` No
89,371
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` import os,io from sys import stdout import collections import random import math from operator import itemgetter input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from collections import Counter # import sys # sys.setrecursionlimit(10**6) def primes(n): """ Returns a list of primes < n """ sieve = [True] * n for i in range(3,int(n**0.5)+1,2): if sieve[i]: sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1) return [2] + [i for i in range(3,n,2) if sieve[i]] def binomial_coefficient(n, k): if 0 <= k <= n: ntok = 1 ktok = 1 for t in range(1, min(k, n - k) + 1): ntok *= n ktok *= t n -= 1 return ntok // ktok else: return 0 def powerOfK(k, max): if k == 1: return [1] if k == -1: return [-1, 1] result = [] n = 1 while n <= max: result.append(n) n *= k return result def prefixSum(arr): for i in range(1, len(arr)): arr[i] = arr[i] + arr[i-1] return arr def divisors(n): i = 1 result = [] while i*i <= n: if n%i == 0: if n/i == i: result.append(i) else: result.append(i) result.append(n/i) i+=1 return result # from functools import lru_cache # @lru_cache(maxsize=None) s = input().decode('utf-8').strip() # n = int(input()) # l = list(map(int, input().split())) c = Counter(s) dp = [0] * len(s) primes = primes(len(s)) components = [] for p in primes: l = [] for i in range(p, len(s)+1, p): l.append(i) for comp in components: if len(list(set(comp) & set(l))): comp += l break else: if len(l): components.append(l) need = [] for comp in components: need.append(len(set(comp))) import heapq heap = [] for e, v in c.items(): heapq.heappush(heap, (-v, e)) result = [""] * (len(s)) for comp in components: i, letter = heapq.heappop(heap) i = -i indexes = set(comp) if i >= len(indexes): heapq.heappush(heap, (-(i-len(indexes)), letter)) for index in set(comp): result[index-1] = letter else: print("NO") break else: while len(heap): i, letter = heapq.heappop(heap) for j in range(len(result)): if i == 0: break if result[j] == "": result[j] = letter i -= 1 print("YES") print("".join(result)) # a = sorted(list(c.values())) # b = sorted(need) # # i, j = len(b)-1, len(a)-1 # while i >= 0 and j >= 0: # if b[i] <= a[j]: # a[j] -= b[i] # i -= 1 # else: # j -= 1 # # if i == -1: # print("YES") # else: # print("NO") ``` No
89,372
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ— i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` import math from os import startfile import random from queue import Queue import time import heapq import sys def get_primes(n): arr=[True]*(n+1) for i in range(2,int(math.sqrt(n))+1): if arr[i]: for j in range(i**2,len(arr),i): arr[i]=False primes=[] for i in range(2,len(arr)): if arr[i]: primes.append(i) return primes def main(s): n=len(s) primes=get_primes(n) cnt={} for e in s: if e not in cnt: cnt[e]=0 cnt[e]+=1 s=[0]*n for p in primes: num_needed=max(0,(n-2*p)//p)+1 a,b=None,float('inf') for e in cnt: if num_needed<=cnt[e] and cnt[e]<b: a=e b=cnt[e] if b==float('inf'): print("NO") return else: cnt[a]-=num_needed for i in range(p,n+1,p): s[i-1]=a if cnt[a]==0: cnt.pop(a) left=[] for e in cnt: for i in range(cnt[e]): left.append(e) idx=0 for i in range(len(s)): if not s[i]: s[i]=left[idx] idx+=1 print("YES") print(''.join(s)) return s=input() (main(s)) ``` No
89,373
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Tags: brute force, math Correct Solution: ``` T_ON = 0 DEBUG_ON = 0 MOD = 998244353 def solve(): n = read_int() if n == 1: print(10, 9) else: print(n * 3, n * 2) def main(): T = read_int() if T_ON else 1 for i in range(T): solve() def debug(*xargs): if DEBUG_ON: print(*xargs) from collections import * import math #---------------------------------FAST_IO--------------------------------------- import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #----------------------------------IO_WRAP-------------------------------------- def read_int(): return int(input()) def read_ints(): return list(map(int, input().split())) def print_nums(nums): print(" ".join(map(str, nums))) def YES(): print("YES") def Yes(): print("Yes") def NO(): print("NO") def No(): print("No") #----------------------------------FIB-------------------------------------- def fib(n): a, b = 0, 1 for _ in range(n): a, b = b, a + b return a def fib_ns(n): assert n >= 1 f = [0 for _ in range(n + 1)] f[0] = 0 f[1] = 1 for i in range(2, n + 1): f[i] = f[i - 1] + f[i - 2] return f #----------------------------------MOD-------------------------------------- def gcd(a, b): if a == 0: return b return gcd(b % a, a) def xgcd(a, b): """return (g, x, y) such that a*x + b*y = g = gcd(a, b)""" x0, x1, y0, y1 = 0, 1, 1, 0 while a != 0: (q, a), b = divmod(b, a), a y0, y1 = y1, y0 - q * y1 x0, x1 = x1, x0 - q * x1 return b, x0, y0 def modinv(a, m): """return x such that (a * x) % m == 1""" g, x, _ = xgcd(a, m) if g != 1: raise Exception('gcd(a, m) != 1') return x % m def mod_add(x, y): x += y while x >= MOD: x -= MOD while x < 0: x += MOD return x def mod_mul(x, y): return (x * y) % MOD def mod_pow(x, y): if y == 0: return 1 if y % 2: return mod_mul(x, mod_pow(x, y - 1)) p = mod_pow(x, y // 2) return mod_mul(p, p) def mod_inv(y): return mod_pow(y, MOD - 2) def mod_div(x, y): # y^(-1): Fermat little theorem, MOD is a prime return mod_mul(x, mod_inv(y)) #---------------------------------PRIME--------------------------------------- def is_prime(n): if n == 1: return False for i in range(2, int(n ** 0.5) + 1): if n % i: return False return True def gen_primes(n): """ generate primes of [1..n] using sieve's method """ P = [True for _ in range(n + 1)] P[0] = P[1] = False for i in range(int(n ** 0.5) + 1): if P[i]: for j in range(2 * i, n + 1, i): P[j] = False return P #---------------------------------MAIN--------------------------------------- main() ```
89,374
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Tags: brute force, math Correct Solution: ``` n = int(input()) def comp(n): for i in range(2, int(n**0.5) + 1): if n % i == 0: return True return False for i in range(4, 1000000): if comp(i) and comp(n + i): print(n + i, i) break ```
89,375
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Tags: brute force, math Correct Solution: ``` n=int(input()) if n%2==0: n1=4 n2=n+4 else: n1=9 n2=n+9 print(n2,n1) ```
89,376
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Tags: brute force, math Correct Solution: ``` import sys input = sys.stdin.readline n = int(input()) if not n % 2: print(n + 4, 4) sys.exit() else: print(n + 9, 9) ```
89,377
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Tags: brute force, math Correct Solution: ``` n=int(input()) b=4 a=b+n aa=1 while aa!=2: qa,qb=0,0 for i in range(2,a): if a%i==0: qa=1 break for i in range(2,b): if b%i==0: qb=1 break aa=qa+qb if aa==2: break else: a+=1 b+=1 print(a,b) ```
89,378
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Tags: brute force, math Correct Solution: ``` from math import sqrt def Nprime(n): for i in range(2, int(sqrt(n))+1): if n % i == 0: return True else: return False a = int(input()) l = len(str(a)) n = ["1"] + ["0"]*l n = int("".join(n)) if n - a <=4: n *= 10 while(True): if Nprime(n) and Nprime(n-a): print(n, n-a) break if Nprime(n): pass else: n -= 1 if Nprime(n-a): pass else: n -= 1 ```
89,379
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Tags: brute force, math Correct Solution: ``` n = int(input()) if(n==1): print("9 8") elif(n==2): print("6 4") elif(n==3): print("9 6") elif(n%2==0): print(n+4,"4") else: print(n+9,"9") ```
89,380
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Tags: brute force, math Correct Solution: ``` def check_prime(n): for i in range(2, int(n**0.5)): if n % i == 0: return True return False def main(): n = int(input()) a, b = 0, 4 while True: a = n + b if check_prime(a): return str(a) + " " + str(b) b += 2 if __name__ == "__main__": print(main()) ```
89,381
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Submitted Solution: ``` n = int(input()) if n==2: print(8,6) elif n%2 == 0: print(n*2,n) elif n==1: print(10,9) else: print(n*4-n,n*2) ``` Yes
89,382
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Submitted Solution: ``` k = 10**7 + 5 n = int(input()) if(n%2==0): print('{} {}'.format(2*n+2,n+2)) else: print('{} {}'.format(k,k-n)) ``` Yes
89,383
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Submitted Solution: ``` def isPrime(n) : # Corner cases if (n <= 1) : return False if (n <= 3) : return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True n=int(input('')) a=0 for i in range(2,n+10**9): if not (isPrime(i)): if not (isPrime(n+i)): a=i break print(i+n," ",i) ``` Yes
89,384
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Submitted Solution: ``` n = int(input()) print(f'{n*9} {n*8}') ``` Yes
89,385
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Submitted Solution: ``` n = int(input()) print(2*n, 3*n) ``` No
89,386
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Submitted Solution: ``` n=int(input()) if n==1: print(7777, 7776) else: print(3*n,n) ``` No
89,387
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Submitted Solution: ``` n = int(input()) print(1000000000 , 1000000000-n) ``` No
89,388
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: * the following numbers are composite: 1024, 4, 6, 9; * the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n. It can be proven that solution always exists. Input The input contains one integer n (1 ≀ n ≀ 10^7): the given integer. Output Print two composite integers a,b (2 ≀ a, b ≀ 10^9, a-b=n). It can be proven, that solution always exists. If there are several possible solutions, you can print any. Examples Input 1 Output 9 8 Input 512 Output 4608 4096 Submitted Solution: ``` fact = 10*9*8*7*6*5*4*3*2*1 t = int(input()) print(fact,fact+t) ``` No
89,389
Provide tags and a correct Python 3 solution for this coding contest problem. Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification. You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n-1. For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 β†’ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne. Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 3000) β€” the number of digits in the original number. The second line of each test case contains a non-negative integer number s, consisting of n digits. It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000. Output For each test case given in the input print the answer in the following format: * If it is impossible to create an ebne number, print "-1" (without quotes); * Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits. Example Input 4 4 1227 1 0 6 177013 24 222373204424185217171912 Output 1227 -1 17703 2237344218521717191 Note In the first test case of the example, 1227 is already an ebne number (as 1 + 2 + 2 + 7 = 12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don't need to delete any digits. Answers such as 127 and 17 will also be accepted. In the second test case of the example, it is clearly impossible to create an ebne number from the given number. In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes. Explanation: * 1 + 7 + 7 + 0 + 3 = 18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013; * 1 + 7 + 0 + 1 = 9. Because 9 is not divisible by 2, 1701 is not an ebne number; * 7 + 7 + 0 = 14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number. In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 β†’ 22237320442418521717191 (delete the last digit). Tags: greedy, math, strings Correct Solution: ``` def ans(s): ans = 0 for i in s: ans = ans * 10 + i return ans def f(s, a): if len(s) == 1: return -1 elif s[-1] % 2 != 0 and a % 2 == 0: return ans(s) elif s[-1] % 2 == 0: while s[-1] % 2 == 0 and len(s) > 1: a -= s[-1] del s[-1] return f(s, a) else: for i in s: if i % 2 !=0: s.remove(i) a -= i break return f(s, a) t = int(input()) for i in range(t): x = int(input()) s = list(map(int, list(input()))) print(f(s, sum(s))) ```
89,390
Provide tags and a correct Python 3 solution for this coding contest problem. Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification. You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n-1. For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 β†’ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne. Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 3000) β€” the number of digits in the original number. The second line of each test case contains a non-negative integer number s, consisting of n digits. It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000. Output For each test case given in the input print the answer in the following format: * If it is impossible to create an ebne number, print "-1" (without quotes); * Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits. Example Input 4 4 1227 1 0 6 177013 24 222373204424185217171912 Output 1227 -1 17703 2237344218521717191 Note In the first test case of the example, 1227 is already an ebne number (as 1 + 2 + 2 + 7 = 12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don't need to delete any digits. Answers such as 127 and 17 will also be accepted. In the second test case of the example, it is clearly impossible to create an ebne number from the given number. In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes. Explanation: * 1 + 7 + 7 + 0 + 3 = 18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013; * 1 + 7 + 0 + 1 = 9. Because 9 is not divisible by 2, 1701 is not an ebne number; * 7 + 7 + 0 = 14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number. In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 β†’ 22237320442418521717191 (delete the last digit). Tags: greedy, math, strings Correct Solution: ``` #Ashish Sagar q=int(input()) for _ in range(q): n=int(input()) #l=list(map(int,input().split())) l=list(input()) for i in range(n): l[i]=int(l[i]) x=[] for i in range(n): if l[i]%2!=0 and len(x)<2: x.append(l[i]) if len(x)==2: print(x[0],end="") print(x[1]) else: print(-1) ```
89,391
Provide tags and a correct Python 3 solution for this coding contest problem. Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification. You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n-1. For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 β†’ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne. Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 3000) β€” the number of digits in the original number. The second line of each test case contains a non-negative integer number s, consisting of n digits. It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000. Output For each test case given in the input print the answer in the following format: * If it is impossible to create an ebne number, print "-1" (without quotes); * Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits. Example Input 4 4 1227 1 0 6 177013 24 222373204424185217171912 Output 1227 -1 17703 2237344218521717191 Note In the first test case of the example, 1227 is already an ebne number (as 1 + 2 + 2 + 7 = 12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don't need to delete any digits. Answers such as 127 and 17 will also be accepted. In the second test case of the example, it is clearly impossible to create an ebne number from the given number. In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes. Explanation: * 1 + 7 + 7 + 0 + 3 = 18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013; * 1 + 7 + 0 + 1 = 9. Because 9 is not divisible by 2, 1701 is not an ebne number; * 7 + 7 + 0 = 14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number. In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 β†’ 22237320442418521717191 (delete the last digit). Tags: greedy, math, strings Correct Solution: ``` for _ in range(int(input())): n = int(input()) s = input() sm = 0 l = [] count = 0 for i in range(n): sm += int(s[i]) if(int(s[i])%2==1): l.append(i) count += 1 ans = -1 if(sm %2 == 0): if(count > 0): ans = s[:l[-1]+1] else: if(count >= 3): ans = s[l[0]:l[1]] + s[l[1]+1:l[2]+1] print(ans) ```
89,392
Provide tags and a correct Python 3 solution for this coding contest problem. Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification. You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n-1. For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 β†’ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne. Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 3000) β€” the number of digits in the original number. The second line of each test case contains a non-negative integer number s, consisting of n digits. It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000. Output For each test case given in the input print the answer in the following format: * If it is impossible to create an ebne number, print "-1" (without quotes); * Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits. Example Input 4 4 1227 1 0 6 177013 24 222373204424185217171912 Output 1227 -1 17703 2237344218521717191 Note In the first test case of the example, 1227 is already an ebne number (as 1 + 2 + 2 + 7 = 12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don't need to delete any digits. Answers such as 127 and 17 will also be accepted. In the second test case of the example, it is clearly impossible to create an ebne number from the given number. In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes. Explanation: * 1 + 7 + 7 + 0 + 3 = 18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013; * 1 + 7 + 0 + 1 = 9. Because 9 is not divisible by 2, 1701 is not an ebne number; * 7 + 7 + 0 = 14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number. In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 β†’ 22237320442418521717191 (delete the last digit). Tags: greedy, math, strings Correct Solution: ``` for _ in range(int(input())): n = int(input()) s = input() odd=0;total=0;flag='waiting' for i in range(n): total+=int(s[i]) if int(s[i])%2==1: odd+=1 if total%2==0 and total>0 and odd%2==0 and int(s[i])%2==1: print(s[:i+1]) flag='done' break if flag=='waiting': print(-1) ```
89,393
Provide tags and a correct Python 3 solution for this coding contest problem. Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification. You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n-1. For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 β†’ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne. Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 3000) β€” the number of digits in the original number. The second line of each test case contains a non-negative integer number s, consisting of n digits. It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000. Output For each test case given in the input print the answer in the following format: * If it is impossible to create an ebne number, print "-1" (without quotes); * Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits. Example Input 4 4 1227 1 0 6 177013 24 222373204424185217171912 Output 1227 -1 17703 2237344218521717191 Note In the first test case of the example, 1227 is already an ebne number (as 1 + 2 + 2 + 7 = 12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don't need to delete any digits. Answers such as 127 and 17 will also be accepted. In the second test case of the example, it is clearly impossible to create an ebne number from the given number. In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes. Explanation: * 1 + 7 + 7 + 0 + 3 = 18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013; * 1 + 7 + 0 + 1 = 9. Because 9 is not divisible by 2, 1701 is not an ebne number; * 7 + 7 + 0 = 14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number. In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 β†’ 22237320442418521717191 (delete the last digit). Tags: greedy, math, strings Correct Solution: ``` t = int(input()) for _ in range(t): n = int(input()) l = list(input()) ans = '' for i in range(n): if int(l[i]) % 2 != 0: ans += l[i] if len(ans) == 2: print(ans) break else: print(-1) ```
89,394
Provide tags and a correct Python 3 solution for this coding contest problem. Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification. You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n-1. For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 β†’ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne. Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 3000) β€” the number of digits in the original number. The second line of each test case contains a non-negative integer number s, consisting of n digits. It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000. Output For each test case given in the input print the answer in the following format: * If it is impossible to create an ebne number, print "-1" (without quotes); * Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits. Example Input 4 4 1227 1 0 6 177013 24 222373204424185217171912 Output 1227 -1 17703 2237344218521717191 Note In the first test case of the example, 1227 is already an ebne number (as 1 + 2 + 2 + 7 = 12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don't need to delete any digits. Answers such as 127 and 17 will also be accepted. In the second test case of the example, it is clearly impossible to create an ebne number from the given number. In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes. Explanation: * 1 + 7 + 7 + 0 + 3 = 18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013; * 1 + 7 + 0 + 1 = 9. Because 9 is not divisible by 2, 1701 is not an ebne number; * 7 + 7 + 0 = 14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number. In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 β†’ 22237320442418521717191 (delete the last digit). Tags: greedy, math, strings Correct Solution: ``` for _ in range(int(input())): n = int(input()) a = list(map(int, input())) o = sum(a) % 2 while o or (a and a[-1] % 2 == 0): o ^= a[-1] % 2 a.pop() if a: print(''.join(map(str, a))) else: print(-1) ```
89,395
Provide tags and a correct Python 3 solution for this coding contest problem. Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification. You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n-1. For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 β†’ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne. Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 3000) β€” the number of digits in the original number. The second line of each test case contains a non-negative integer number s, consisting of n digits. It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000. Output For each test case given in the input print the answer in the following format: * If it is impossible to create an ebne number, print "-1" (without quotes); * Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits. Example Input 4 4 1227 1 0 6 177013 24 222373204424185217171912 Output 1227 -1 17703 2237344218521717191 Note In the first test case of the example, 1227 is already an ebne number (as 1 + 2 + 2 + 7 = 12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don't need to delete any digits. Answers such as 127 and 17 will also be accepted. In the second test case of the example, it is clearly impossible to create an ebne number from the given number. In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes. Explanation: * 1 + 7 + 7 + 0 + 3 = 18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013; * 1 + 7 + 0 + 1 = 9. Because 9 is not divisible by 2, 1701 is not an ebne number; * 7 + 7 + 0 = 14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number. In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 β†’ 22237320442418521717191 (delete the last digit). Tags: greedy, math, strings Correct Solution: ``` t = int(input()) for i in range(t): summ = 0 bul = False a = [] l_a = 0 n = int(input()) s = input() for i in range(len(s) - 1, -1, -1): if l_a < 2 and int(s[i]) % 2 == 1: a.append(i) bul = True l_a += 1 if bul: summ += int(s[i]) if l_a == 0 or a[0] == 0: print(-1) elif summ % 2 == 1 and l_a == 1: print(-1) elif summ % 2 == 0: print(s[:a[0] + 1]) else: print(s[:a[1]] + s[a[1] + 1:a[0] + 1]) ```
89,396
Provide tags and a correct Python 3 solution for this coding contest problem. Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification. You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n-1. For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 β†’ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne. Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 3000) β€” the number of digits in the original number. The second line of each test case contains a non-negative integer number s, consisting of n digits. It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000. Output For each test case given in the input print the answer in the following format: * If it is impossible to create an ebne number, print "-1" (without quotes); * Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits. Example Input 4 4 1227 1 0 6 177013 24 222373204424185217171912 Output 1227 -1 17703 2237344218521717191 Note In the first test case of the example, 1227 is already an ebne number (as 1 + 2 + 2 + 7 = 12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don't need to delete any digits. Answers such as 127 and 17 will also be accepted. In the second test case of the example, it is clearly impossible to create an ebne number from the given number. In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes. Explanation: * 1 + 7 + 7 + 0 + 3 = 18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013; * 1 + 7 + 0 + 1 = 9. Because 9 is not divisible by 2, 1701 is not an ebne number; * 7 + 7 + 0 = 14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number. In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 β†’ 22237320442418521717191 (delete the last digit). Tags: greedy, math, strings Correct Solution: ``` q=int(input()) for i in range(q): s='' t='' n=0 ans=0 n=int(input()) s=input() for j in range(n): if (int(s[j])%2==1): t+=s[j] ans+=1 if (ans>=2): print(t[0]+t[1]) else: print(-1) ```
89,397
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification. You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n-1. For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 β†’ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne. Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 3000) β€” the number of digits in the original number. The second line of each test case contains a non-negative integer number s, consisting of n digits. It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000. Output For each test case given in the input print the answer in the following format: * If it is impossible to create an ebne number, print "-1" (without quotes); * Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits. Example Input 4 4 1227 1 0 6 177013 24 222373204424185217171912 Output 1227 -1 17703 2237344218521717191 Note In the first test case of the example, 1227 is already an ebne number (as 1 + 2 + 2 + 7 = 12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don't need to delete any digits. Answers such as 127 and 17 will also be accepted. In the second test case of the example, it is clearly impossible to create an ebne number from the given number. In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes. Explanation: * 1 + 7 + 7 + 0 + 3 = 18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013; * 1 + 7 + 0 + 1 = 9. Because 9 is not divisible by 2, 1701 is not an ebne number; * 7 + 7 + 0 = 14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number. In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 β†’ 22237320442418521717191 (delete the last digit). Submitted Solution: ``` t = int(input()) for _ in range(t): _ = int(input()) n = input() ans = "" cnt = 0 for i in n: if int(i) % 2: ans += i cnt += 1 if cnt >= 2: break if cnt < 2: print(-1) else: print(ans) ``` Yes
89,398
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification. You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n-1. For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 β†’ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne. Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 3000) β€” the number of digits in the original number. The second line of each test case contains a non-negative integer number s, consisting of n digits. It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000. Output For each test case given in the input print the answer in the following format: * If it is impossible to create an ebne number, print "-1" (without quotes); * Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits. Example Input 4 4 1227 1 0 6 177013 24 222373204424185217171912 Output 1227 -1 17703 2237344218521717191 Note In the first test case of the example, 1227 is already an ebne number (as 1 + 2 + 2 + 7 = 12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don't need to delete any digits. Answers such as 127 and 17 will also be accepted. In the second test case of the example, it is clearly impossible to create an ebne number from the given number. In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes. Explanation: * 1 + 7 + 7 + 0 + 3 = 18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013; * 1 + 7 + 0 + 1 = 9. Because 9 is not divisible by 2, 1701 is not an ebne number; * 7 + 7 + 0 = 14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number. In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 β†’ 22237320442418521717191 (delete the last digit). Submitted Solution: ``` for _ in range(int(input())): n=int(input()) s=input() ls=[] for i in s: if int(i)&1:ls.append(i) if len(ls)>=2: print(ls[0],end="") print(ls[1]) else:print(-1) ``` Yes
89,399