text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. <image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13 Submitted Solution: ``` n = int(input()) if(n%2 == 0): print((12*(n-1)) + 1) else: print((12*n)+1) ``` No
5,300
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6 Tags: brute force, dfs and similar, implementation Correct Solution: ``` #!/usr/local/bin/python3 from __future__ import print_function import sys DEBUG = '-d' in sys.argv def debug(*args, **kwargs): if DEBUG: print(*args, file=sys.stderr, **kwargs) return None def main(): n = int(input()) cnt = [0] * (n + 1) edge = [] for i in range(0, n + 1): edge.append(set()) for i in range(0, 2 * n): s, t = map(int, input().split()) edge[s].add(t) edge[t].add(s) cnt[s] += 1 cnt[t] += 1 c4 = 0 for i in range(1, n + 1): if cnt[i] == 4: c4 += 1 if c4 != n: print(-1) else: for v2 in edge[1]: for v3 in edge[1]: if v2 in edge[v3]: mark = [True] * (n + 1) mark[1] = False mark[v2] = False res = [1, v2] i = v3 try: while True: res.append(i) mark[i] = False if len(res) == n: print(' '.join([str(x) for x in res])) sys.exit(0) for e in edge[i]: if e != i and mark[e] and res[-2] in edge[e]: i = e break if not mark[i]: raise StopIteration except StopIteration: pass print(-1) if __name__ == '__main__': main() ```
5,301
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6 Tags: brute force, dfs and similar, implementation Correct Solution: ``` from sys import stdin all_in = stdin.readlines() n = int(all_in[0]) pairs = list(map(lambda x: tuple(map(int, x.split())), all_in[1:])) if n == 5: print(1, 2, 3, 4, 5) exit() neigs = {i: set() for i in range(1, n + 1)} for (a, b) in pairs: neigs[a].add(b) neigs[b].add(a) for el in neigs.values(): if len(el) != 4: print(-1) exit() ans = [1] used = {i: False for i in range(1, n + 1)} used[1] = True for i in range(n - 1): el_ = ans[-1] ne = neigs[el_] for el in ne: ne_ = neigs[el] and_ = ne & ne_ if len(and_) == 2: if i: if ans[-2] not in and_: continue if not used[el]: ans.append(el) used[el] = True break if len(ans) < n: print(-1) exit() print(' '.join(map(str, ans))) ```
5,302
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6 Submitted Solution: ``` #!/usr/local/bin/python3 from __future__ import print_function import sys DEBUG = '-d' in sys.argv def debug(*args, **kwargs): if DEBUG: print(*args, file=sys.stderr, **kwargs) return None n = int(input()) cnt = [0] * (n+1) edge = [] for i in range (0, n + 1): edge.append(set()) for i in range(0, 2 * n): s, t = map(int, input().split()) edge[s].add(t) edge[t].add(s) cnt[s] += 1 cnt[t] += 1 c4 = 0 for i in range(1, n+1): if cnt[i] == 4: c4 += 1 if c4 != n: print(-1) else: def go(i, res, mark): res.append(i) mark[i] = False if len(res) == n: print(' '.join([str(x) for x in res])) raise StopIteration for e in edge[i]: if e != i and mark[e] and res[-1] in edge[e]: go(e, res, mark) return try: for v2 in edge[1]: for v3 in edge[1]: if v2 in edge[v3]: mark = [True] * (n + 1) mark[1] = False mark[v2] = False go(v3, [1, v2], mark) print(-1) except StopIteration: pass ``` No
5,303
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6 Submitted Solution: ``` input=__import__('sys').stdin.readline n=int(input()) if n==5:exit(print(1,2,3,4,5)) ans=[1] g=[set()for _ in range(n+1)] vis=[1]*(n+1) for _ in range(n*2):a,b=map(int,input().split());g[a].add(b);g[b].add(a) for _ in range(n-1): for i in g[ans[-1]]: c=g[ans[-1]]&g[i] if len(c)==2and not(_ and ans[-2]not in c)and vis[i]:ans.append(i);vis[i]=0;break if len(ans)==n:print(*ans) else:print(-1) ``` No
5,304
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6 Submitted Solution: ``` from sys import stdin, stdout, stderr import sys inints=lambda: [int(x) for x in stdin.readline().split()] fastwrite=lambda s: stdout.write( str(s) + "\n" ) fasterr=lambda s: stderr.write( str(s) + "\n" ) irange=lambda x,y: range(x, y+1) #### def adj(x): d=[0]*(n+1) for i in g[x]: for j in g[i]: if j in g[x] and j not in result: d[j]+=1 return d.index(max(d)) fasterr("please input") n,=inints() g=[ [] for i in range(n+1) ] result=[0]*n for i in range(2*n): a,b=inints() g[a].append(b) g[b].append(a) if n==5: for i in irange(1,5): stdout.write(str(i)+" ") sys.exit() elif n==6: disconnected=0 for i in irange(2,6): if i not in g[1]: disconnected=i break r=[i for i in irange(1,6)] x=r.index(disconnected) r[3],r[x]=r[x],r[3] if r[1] not in g[r[2]]: r[2],r[4]=r[4],r[2] for i in r: stdout.write(str(i)+" ") sys.exit() for i in range(n): result[i]=adj( 1 if i==0 else result[i-1] ) for i in result: stdout.write(str(i)+" ") ``` No
5,305
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6 Submitted Solution: ``` n = int(input()) e = {x + 1: [] for x in range(n)} for _ in range(n * 2): s, r = list(map(int, input().split())) e[s] += [r] e[r] += [s] variants = [[1]] for _ in range(n - 1): newVariants = [] for mas in variants: lastE = mas[len(mas) - 1] l = e[lastE][0] r = e[lastE][1] mas1 = mas mas2 = mas if not (l in mas1): mas1 = mas + [l] if not (r in mas2): mas2 = mas + [r] newVariants += [mas1] newVariants += [mas2] l1 = e[lastE][2] r2 = e[lastE][3] mas3 = mas mas4 = mas if not (l1 in mas3): mas3 = mas + [l1] if not (r2 in mas4): mas6 = mas + [r2] newVariants += [mas3] newVariants += [mas4] variants = newVariants res = [] for mas in variants: lastE = mas[len(mas) - 1] if len(mas) == n and (1 in e[lastE]): res = mas if len(res) == n: print(" ".join(str(x) for x in res)) else: print(-1) ``` No
5,306
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Tags: constructive algorithms, math Correct Solution: ``` n = int(input()) if n % 4 > 1: print(-1) exit() a = [i for i in range(0, n+1)] for i in range(1, n//2+1, 2): p, q, r, s = i, i+1, n-i,n-i+1 a[p], a[q], a[r], a[s] = a[q], a[s], a[p], a[r] def check(arr): for i in range(1, n+1): k = arr[i] if arr[arr[k]] != n-k+1: return False return True # print(check(a)) print(*a[1:]) ```
5,307
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Tags: constructive algorithms, math Correct Solution: ``` n=int(input()) if n%4==2 or n%4==3: from sys import exit print(-1);exit() res,i=[0]*n,0 for i in range(0,n//2,2): res[i],res[i+1]=i+2,n-i res[n-i-1],res[n-i-2]=n-i-1,i+1 i+=2 if n%4==1:res[n//2]=n//2+1 print(*res) ```
5,308
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Tags: constructive algorithms, math Correct Solution: ``` from math import * n = int(input()) if n == 1: print(1) elif n % 4 in [2, 3]: print(-1) else: ans = [0] * n for i in range(n // 2): if i & 1: ans[i] = n - 2 * (i // 2) else: ans[i] = 2 + i for i in range(1, (n // 2), 2): ans[ans[i] - 1] = n - i ans[n - i - 1] = n - (n - i) if n % 4 < 3 and n % 4 > 0: ans[n // 2] = ceil(n / 2) elif n % 4 == 3: ans[int(floor(n / 2))] = ans[int(floor(n / 2)) - 1] + 1 ans[int(ceil(n / 2))] = ans[int(ceil(n / 2)) - 1] - 2 print(*ans) ```
5,309
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Tags: constructive algorithms, math Correct Solution: ``` n=int(input()) L=[0]*(n+1) X=[False]*(n+1) if(n%4!=0 and n%4!=1): print(-1) else: for i in range(1,n+1): if(X[i]): continue X[i]=True X[n-i+1]=True for j in range(i+1,n+1): if(X[j]): continue X[j]=True X[n-j+1]=True L[i]=j L[n-i+1]=n-j+1 L[j]=n-i+1 L[n-j+1]=i break if(n%4==1): L[n//2+1]=n//2+1 for i in range(1,n): print(L[i],end=" ") print(L[n]) ```
5,310
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Tags: constructive algorithms, math Correct Solution: ``` n=int(input()) if(n%4>1): print(-1) else: ans=[0]*(n+1) i,j,a,b=1,n,1,n while(i<j and a<=n and b>=1): ans[i],ans[j]=a+1,b-1 ans[i+1],ans[j-1]=b,a i+=2 j-=2 a+=2 b-=2 if(i==j): ans[i]=a for i in range(1,n+1): print(ans[i],end=' ') ```
5,311
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Tags: constructive algorithms, math Correct Solution: ``` n=int(input()) if (n//2)&1: print(-1) else: ans=[0]*(n+1) for i in range(1,(n//2)+1,2): ans[i]=i+1 ans[i+1]=n-i+1 ans[n-i+1]=n-i ans[n-i]=i if n%2: ans[(n//2)+1]=(n//2)+1 print(*ans[1:]) ```
5,312
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Tags: constructive algorithms, math Correct Solution: ``` ''' Created on @author: linhz ''' import sys usedNum=0 n=int(input()) p=[0 for i in range(n+1)] usedNum=0 if n%4==3 or n%4==2: print(-1) else: i=1 j=n a=1 b=n while j>i: p[i]=a+1 p[i+1]=b p[j]=b-1 p[j-1]=a i+=2 j-=2 a+=2 b-=2 if j==i: p[i]=a ans="" for i in range(1,n+1): ans+=str(p[i])+" " print(ans) ```
5,313
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Tags: constructive algorithms, math Correct Solution: ``` from itertools import permutations from sys import stdin def checkit(vector, upto=-1): if upto == -1: upto = len(vector) for i in range(0, upto): if vector[vector[i] - 1] != len(vector) - (i + 1) + 1: return False return True def calculate(n): numbers = list(range(1, n + 1)) result = [0] * n for i in range(0, n): if result[i] != 0: continue if i > 0 and checkit(result, i): continue expected = n - i for v in numbers: if v - 1 == i and expected != v: continue if v == expected: result[v-1] = v numbers.remove(v) break elif result[v - 1] == expected: numbers.remove(v) result[i] = v break elif result[v - 1] == 0: assert expected in numbers result[i] = v result[v - 1] = expected numbers.remove(v) numbers.remove(expected) break return result def calculate_v2(n): result = [0] * n first_sum = n + 2 second_sum = n nf = n i = 0 while nf > first_sum // 2: result[i] = first_sum - nf result[i + 1] = nf nf -= 2 i += 2 if n % 2 == 1: result[i] = i + 1 i = n - 1 while i > n // 2: result[i] = i result[i-1] = second_sum - i i -= 2 return result def main(): number = int(stdin.readline()) result = calculate_v2(number) if not checkit(result): print(-1) else: print(" ".join([str(v) for v in result])) if __name__ == "__main__": main() ```
5,314
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Submitted Solution: ``` n = int(input()) if n%4 > 1: print(-1) else: a = [n+1>>1]*n for i in range(n//4): j = i*2 a[j], a[j+1], a[-2-j], a[-1-j] = j+2, n-j, j+1, n-1-j print(' '.join(map(str, a))) ``` Yes
5,315
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Submitted Solution: ``` n=int(input()) if n%4>1:print(-1) else: ans=[i for i in range(1,n+1)] for i in range(0,n//2,2): ans[i]=i+2 ans[i+1]=n-i ans[n-i-1]=n-i-1 ans[n-i-2]=i+1 print(*ans) ``` Yes
5,316
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Submitted Solution: ``` n = int(input()) if n % 4 > 1: print(-1) exit() a = [i for i in range(0, n+1)] for i in range(1, n//2+1, 2): p, q, r, s = i, i+1, n-i,n-i+1 a[p], a[q], a[r], a[s] = a[q], a[s], a[p], a[r] def check(arr): for i in range(1, n+1): k = arr[i] if arr[arr[k]] != n-k+1: return False return True # print(check(a)) print(*a[1:]) # Made By Mostafa_Khaled ``` Yes
5,317
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Submitted Solution: ``` n=int(input()) if n==1: print (1) exit() if n%4>1: print (-1) exit() ans=[-1]*n left=n start=n-2 nums=1 nume=n while left>=4: ans[start]=nums ans[nums-1]=nums+1 ans[nums]=nume ans[nume-1]=nume-1 start-=2 nums+=2 nume-=2 left-=4 # print (ans) if left==1: ans[start+1]=start+2 print (*ans) ``` Yes
5,318
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Submitted Solution: ``` import math n = int(input()) ans = 1 s = [1, 1, 1] for a in range(n-100, n-1): for b in range(a+1, n): m1 = a * b for c in range(b+1, n+1): if (m1 * c // math.gcd(c, m1) >= ans): ans = m1 * c // math.gcd(a, m1) s = [a, b, c] print(ans) # print(s) ``` No
5,319
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Submitted Solution: ``` import sys from collections import deque n = int(input()) if n % 4 == 2 or n % 4 == 3: print('-1') sys.exit(0) arr = [None] * (n + 1) qt = deque([i for i in range(1, n + 1)]) mark = set() while qt: while qt and qt[0] in mark: qt.popleft() if not qt: break a = qt.popleft() while qt and qt[0] in mark: qt.popleft() if not qt: break b = qt.popleft() for i in range(4): mark.add(a) mark.add(b) arr[a] = b arr[b] = n - a + 1 a = b b = arr[b] for i in range(1, n + 1): if not arr[i]: arr[i] = a break ``` No
5,320
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Submitted Solution: ``` 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") ########################################################## #q.sort(key=lambda x:((x[1]-x[0]),-x[0])) #from collections import Counter #from fractions import Fraction from bisect import bisect_left from collections import Counter #s=iter(input()) #from collections import deque #ls=list(map(int,input().split())) #for in range(m): #for _ in range(int(input())): #n=int(input()) # n,k=map(int,input().split()) # arr=list(map(int,input().split())) #for i in range(int(input())): #n=int(input()) #arr=sorted([(x,i) for i,x in enumerate(map(int,input().split()))])[::-1] #print(arr) #arr=sorted(list(map(int,input().split())))[::-1] n=int(input()) if n==1: print(1) elif n==2: print(-1) else: print(n-1,end=" ") print(1,end=" ") for i in range(2,n+1): if i==n-1: continue print(i,end=" ") ``` No
5,321
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4 Submitted Solution: ``` from math import * n = int(input()) if n == 1: print(1) elif n % 4 == 2: print(-1) else: ans = [0] * n for i in range(n // 2): if i & 1: ans[i] = n - 2 * (i // 2) else: ans[i] = 2 + i for i in range(1, (n // 2), 2): ans[ans[i] - 1] = n - i ans[n - i - 1] = n - (n - i) if n % 4 < 3 and n % 4 > 0: ans[n // 2] = ceil(n / 2) elif n % 4 == 3: ans[int(floor(n / 2))] = ans[int(floor(n / 2)) - 1] + 1 ans[int(ceil(n / 2))] = ans[int(ceil(n / 2)) - 1] - 2 print(*ans) ``` No
5,322
Provide tags and a correct Python 3 solution for this coding contest problem. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Tags: constructive algorithms, implementation Correct Solution: ``` import sys input=sys.stdin.readline from collections import defaultdict as dc from collections import Counter from bisect import bisect_right, bisect_left import math from operator import itemgetter from heapq import heapify, heappop, heappush from queue import PriorityQueue as pq n,k=map(int,input().split()) if k>=n*(n-1)//2: print("no solution") else: for i in range(n): print(0,i) ```
5,323
Provide tags and a correct Python 3 solution for this coding contest problem. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Tags: constructive algorithms, implementation Correct Solution: ``` n, k = map(int, input().split()) up = (int)(n * (n - 1) / 2); if k >= up: print ("no solution") else: for i in range (0, n): print (0, i) ```
5,324
Provide tags and a correct Python 3 solution for this coding contest problem. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Tags: constructive algorithms, implementation Correct Solution: ``` n,k = map(int ,input().split()) if((n&1)==0) : time = (n//2)*(n-1) else : time = ((n-1)//2)*n if(k>=time): print("no solution") else : for i in range(n): print("0 "+str(i)) ```
5,325
Provide tags and a correct Python 3 solution for this coding contest problem. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Tags: constructive algorithms, implementation Correct Solution: ``` n,k = map(int,input().split()) if n*(n-1) <= k*2: print('no solution') else: x = 11 for i in range(n-1): print(1,x) x+=3 print(1,x-5) ```
5,326
Provide tags and a correct Python 3 solution for this coding contest problem. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Tags: constructive algorithms, implementation Correct Solution: ``` n, k = map(int, input().split()) if (k >= n * (n - 1) // 2): print("no solution") else: for i in range(n): print(0, i) ```
5,327
Provide tags and a correct Python 3 solution for this coding contest problem. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Tags: constructive algorithms, implementation Correct Solution: ``` n,k=map(int, input().split()) if(n*(n-1)//2 <=k): print('no solution') else: for i in range(n): print(0,i) ```
5,328
Provide tags and a correct Python 3 solution for this coding contest problem. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Tags: constructive algorithms, implementation Correct Solution: ``` n, k = map(int, input().split()) tot = 0 for i in range(n): for j in range(i + 1, n): tot += 1 if tot <= k: print('no solution') else: for i in range(n): print(1, i) ```
5,329
Provide tags and a correct Python 3 solution for this coding contest problem. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Tags: constructive algorithms, implementation Correct Solution: ``` n,k = map(int,input().split()) if n*(n-1) <= k*2: print('no solution') else: for i in range(n): print(0,i) ```
5,330
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Submitted Solution: ``` n,k=map(int,input().split()) if n*(n-1)/2<=k: print("no solution") else: for i in range(0,n):print(0,i) # Made By Mostafa_Khaled ``` Yes
5,331
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Submitted Solution: ``` n,k=list(map(int,input().split())) if (n*n-n)//2<=k: print("no solution") else: x=0 y=0 store=[] count=0 store.append(str(x)+' '+str(y)) while len(store)<n: y+=1 store.append(str(x)+' '+str(y)) for j in store: print(j) ``` Yes
5,332
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Submitted Solution: ``` s=input().split() n=int(s[0]) k=int(s[1]) ans=[] a=0 for i in range(n): for j in range(i+1,n): a+=1 if(a<=k): print("no solution") else: for i in range(-10**9,-10**9+n): print("0 "+str(i)) ``` Yes
5,333
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Submitted Solution: ``` n, k = map(int, input().split()) if k >= n * (n - 1) // 2: print("no solution") else: for i in range(n): print(i, i * (n + 1)) ``` Yes
5,334
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Submitted Solution: ``` s=input().split() n=int(s[0]) k=int(s[0]) ans=[] a=0 for i in range(1,n+1): for j in range(i+1,n+1): a+=1 if(a<=k): print("no solution") else: for i in range(2): for j in range(n//2): ans.append(str(i)+" "+str(j)) if(len(ans)<n): ans.append("2 0") for i in range(n): print(ans[i]) ``` No
5,335
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Submitted Solution: ``` s=input().split() n=int(s[0]) k=int(s[0]) ans=[] a=0 for i in range(n): for j in range(i+1,n): a+=1 if(a<k): print("no solution") else: for i in range(2): for j in range(n//2): ans.append(str(i)+" "+str(j)) if(len(ans)<n): ans.append("2 0") for i in range(n): print(ans[i]) ``` No
5,336
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Submitted Solution: ``` s=input().split() n=int(s[0]) k=int(s[0]) ans=[] a=0 for i in range(1,n+1): for j in range(i+1,n+1): a+=1 if(a<k): print("no solution") else: for i in range(2): for j in range(n//2): ans.append(str(i)+" "+str(j)) if(len(ans)<n): ans.append("2 0") for i in range(n): print(ans[i]) ``` No
5,337
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>. The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Input A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: * All the points must be distinct. * |xi|, |yi| ≤ 109. * After running the given code, the value of tot should be larger than k. Examples Input 4 3 Output 0 0 0 1 1 0 1 1 Input 2 100 Output no solution Submitted Solution: ``` n,k = map(int,input().split()) if n*(n-1) < k*2: print('no solution') else: for i in range(1,n+1): print(1,i) ``` No
5,338
Provide tags and a correct Python 3 solution for this coding contest problem. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Tags: binary search, constructive algorithms, greedy Correct Solution: ``` s = input() n = int(input()) d = {} r = 0 for a in s: d.setdefault(a, 0) d[a] += 1 if(d[a] > r): r = d[a] if (len(d) > n): print(-1) else: l = 0 while r - l > 1: k = (l + r) // 2 cur = 0 for x in d.values(): cur += (x+k-1) // k if cur > n: l = k else: r = k print(r) s = '' for a in d.keys(): s += a * ((d[a] + r - 1) // r) l=len(s) s += 'a' * (n-len(s)) print(s) ```
5,339
Provide tags and a correct Python 3 solution for this coding contest problem. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Tags: binary search, constructive algorithms, greedy Correct Solution: ``` from math import ceil p = {i: 0 for i in 'abcdefghijklmnopqrstuvwxyz'} t = input() for i in t: p[i] += 1 p = {i: p[i] for i in p if p[i] > 0} n = int(input()) if len(p) > n: print(-1) elif len(t) > n: r = [[p[i], p[i], 1, i] for i in p] for i in range(n - len(p)): j = max(r) j[2] += 1 j[0] = j[1] / j[2] print(ceil(max(r)[0])) print(''.join(j[3] * j[2] for j in r)) else: print('1\n' + t * (n // len(t)) + t[: n % len(t)]) ```
5,340
Provide tags and a correct Python 3 solution for this coding contest problem. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Tags: binary search, constructive algorithms, greedy Correct Solution: ``` import sys import os 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") import math s = input() n = int(input()) d = {} reserve = '' unique = 0 max_occur = 1 all_letters = '' for x in s: if x in d: d[x] += 1 if d[x] > max_occur: max_occur = d[x] else: d[x] = 1 unique += 1 all_letters += x if unique > n: print(-1) elif unique == n: print(max_occur) print(all_letters) else: l = 1 r = max_occur ans = max_occur check = 0 final_str = all_letters check_str = '' while l<=r: mid = l + (r-l)//2 check = 0 check_str = '' reserve = '' for i in d.keys(): if d[i] > mid: check += math.ceil(d[i]/mid) check_str += i*math.ceil(d[i]/mid) reserve += i*(d[i] - math.ceil(d[i]/mid)) else: check += 1 check_str += i reserve += i*(d[i]-1) if check <= n: final_str = '%s' % check_str if len(final_str) < n: final_str += reserve[:n-len(final_str)] if len(final_str) < n: final_str += 'a'*(n-len(final_str)) ans = mid r = mid-1 else: l = mid+1 print(ans) print(final_str) ```
5,341
Provide tags and a correct Python 3 solution for this coding contest problem. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Tags: binary search, constructive algorithms, greedy Correct Solution: ``` import math from sys import stdin from math import ceil if __name__ == '__main__': s = input() n = int(input()) dictionary = {} for i in s: if i in dictionary: dictionary[i] = dictionary[i] + 1 else: dictionary[i] = 1 if len(dictionary) > n: print(-1) else: if len(s) < n: print(1) newS = s else: lengthS = len(s) // n newLength = len(s) while lengthS < newLength: mid = (lengthS + newLength) // 2 total = 0 for i in dictionary: total = total + ceil(dictionary[i] / mid) if total > n: lengthS = mid + 1 else: newLength = mid print(lengthS) newS = '' for i in dictionary: for j in range(ceil(dictionary[i] / lengthS)): newS = newS + i for i in range(n - len(newS)): newS = newS + s[0] print(newS) ```
5,342
Provide tags and a correct Python 3 solution for this coding contest problem. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Tags: binary search, constructive algorithms, greedy Correct Solution: ``` from math import ceil s = input () n = int (input ()) count = {} for i in s: if i in count: count[i] += 1 else: count[i] = 1 if len(count) > n: print (-1) else: if len(s) < n: print (1) ret = s else: l,h = len (s)//n, len (s) while (l < h): m = (l+h) // 2 tot = 0 for i in count: tot += ceil (count[i]/m) if tot > n: l = m+1 else: h = m print (l) ret = '' for i in count: for j in range (ceil (count[i]/l)): ret += i for i in range (n-len(ret)): ret += s[0] print (ret) ```
5,343
Provide tags and a correct Python 3 solution for this coding contest problem. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Tags: binary search, constructive algorithms, greedy Correct Solution: ``` s = input() n = int(input()) symb_cnt = {} for c in s: symb_cnt[c] = symb_cnt[c] + 1 if c in symb_cnt else 1 for cnt in range(1, len(s) + 1): s1 = "" for c in symb_cnt: s1 += c * ((symb_cnt[c] + cnt - 1) // cnt) if len(s1) <= n: for i in range(n - len(s1)): s1 += 'a' print(cnt) print(s1) exit(0) print(-1) ```
5,344
Provide tags and a correct Python 3 solution for this coding contest problem. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Tags: binary search, constructive algorithms, greedy Correct Solution: ``` from collections import Counter def main(): s = input() l = int(input()) d = Counter(s) if len(d) > l: print(-1) return lo = 0 hi = 10000 while lo + 1 < hi: mid = (lo + hi) // 2 c = 0 for x in iter(d.values()): c += (x + mid - 1) // mid if c > l: lo = mid else: hi = mid print(hi) ans = [] for x in iter(d.items()): ans.append(x[0] * int(((x[1] + hi - 1) / hi))) t = ''.join(ans) if len(t) < l: t += 'a' * (l - len(t)) print(t) main() ```
5,345
Provide tags and a correct Python 3 solution for this coding contest problem. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Tags: binary search, constructive algorithms, greedy Correct Solution: ``` s = input() n = int(input()) freq = [0 for i in range(0, 300)] raport = [0 for i in range(0, 300)] differentLetters = 0 tickets = 0 sol = '' for c in s: freq[ord(c)] += 1 for i in freq: if i > 0: differentLetters += 1 if differentLetters > n: print('-1') exit() for i in 'abcdefghijklmnopqrstuvwxyz': if freq[ord(i)] == 0: continue sol += i freq[ord(i)] -= 1 raport[ord(i)] = freq[ord(i)] for i in range(differentLetters, n): #pun litera cu cea mai mare frecventa maxRaport = raport[ord('z')] chosenLetter = 'z' for j in 'abcdefghijklmnopqrstuvwxyz': if raport[ord(j)] > maxRaport: maxRaport = raport[ord(j)] chosenLetter = j sol += chosenLetter raport[ord(chosenLetter)] = freq[ord(chosenLetter)] / sol.count(chosenLetter) for i in sol: a = s.count(i) b = sol.count(i) if a%b == 0: tickets = max(tickets, int(a//b)) else: tickets = max(tickets, int(a//b) + 1) print(tickets) print(sol) ```
5,346
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Submitted Solution: ``` from collections import Counter from heapq import heappushpop def main(): cnt, n = Counter(input()), int(input()) if n < len(cnt): print(-1) return h = list((1 / v, 1, c) for c, v in cnt.most_common()) res = list(cnt.keys()) _, v, c = h.pop(0) for _ in range(n - len(cnt)): res.append(c) v += 1 _, v, c = heappushpop(h, (v / cnt[c], v, c)) print((cnt[c] + v - 1) // v) print(''.join(res)) if __name__ == '__main__': main() ``` Yes
5,347
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Submitted Solution: ``` #!/usr/bin/env python from __future__ import division, print_function import os import sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip from collections import Counter import heapq as pq from math import ceil def solve(): s = insr() n = inp() count_map = Counter(s) if n < len(count_map): print(-1) return copy_map = {el: 1 for el in count_map.keys()} used = len(count_map) q = [(-el, key) for key, el in count_map.items()] pq.heapify(q) while used < n and q: copy, c = pq.heappop(q) copy_map[c] += 1 copies = ceil(count_map[c] / copy_map[c]) if copies > 1: pq.heappush(q, (-copies, c)) used += 1 if not q: print(1) else: print(pq.heappop(q)[0] * -1) res = [] for el, copies in copy_map.items(): res.extend([el] * copies) if len(res) < n: res.extend(['a'] * (n - len(res))) print("".join(res)) def main(): t = 1 for i in range(t): solve() BUFSIZE = 8192 def inp(): return (int(input())) def inlt(): return (list(map(int, input().split()))) def insr(): return (input().strip()) def invr(): return (map(int, input().split())) 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") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ``` Yes
5,348
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Submitted Solution: ``` s = input() n = int(input()) cnt = {} for c in s: if cnt.get(c) == None: cnt[c] = 1 else: cnt[c] += 1 if (n < len(cnt)): print(-1) else: ansNum = 0; while(True): ansNum+=1 l = 0; char = [] for c, v in cnt.items(): need = (v + ansNum -1)//ansNum l+= need char.append((c, need)) if (l > n): continue print(ansNum) ans = "".join([str(c[0])*c[1] for c in char]) ans = ans + 'a'*(n - len(ans)) print(ans) break ``` Yes
5,349
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Submitted Solution: ``` s = input() n = int(input()) from collections import Counter c = Counter(s) out = Counter() contrib = Counter() for letter in c: out[letter] = 1 contrib[letter] = c[letter] sum_vals = sum(out.values()) from math import ceil from fractions import Fraction if sum_vals > n: print(-1) else: while sum_vals < n: el, _ = contrib.most_common(1)[0] out[el] += 1 sum_vals += 1 contrib[el] = ceil(Fraction(c[el], out[el])) print(max(contrib.values())) print(''.join(out.elements())) ``` Yes
5,350
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Submitted Solution: ``` from operator import itemgetter class CodeforcesTask335ASolution: def __init__(self): self.result = '' self.s = '' self.n = 0 def read_input(self): self.s = input() self.n = int(input()) def process_task(self): letters = 'abcdefghijklmnopqrstuvwxyz' occs = [[c, self.s.count(c)] for c in letters if self.s.count(c)] occs.sort(key=itemgetter(1), reverse=True) if len(occs) > self.n: self.result = "-1" else: resulting = "".join([x[0] for x in occs]) while len(resulting) < self.n: priorities = [[c, resulting.count(c)] for c in letters if resulting.count(c)] priorities.sort(key=itemgetter(1), reverse=True) lel = [] for x in range(len(priorities)): lel.append([occs[x][0], occs[x][1] / priorities[x][1]]) #print(resulting, priorities, occs, lel) #print(lel) lel.sort(key=itemgetter(1), reverse=True) resulting += lel[0][0] priorities = [[c, resulting.count(c)] for c in letters if resulting.count(c)] #print(priorities) priorities.sort(key=itemgetter(1), reverse=True) lel = [] for x in range(len(priorities)): lel.append([occs[x][0], occs[x][1] / priorities[x][1]]) #print(lel) self.result = "{0}\n{1}".format(int(max([x[1] for x in lel])), resulting) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask335ASolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ``` No
5,351
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Submitted Solution: ``` from collections import Counter import math s = input() n = int(input()) c = Counter(s) if len(c) > n: print(-1) exit() ans = "" d = Counter() for x in c: ans += x d[x] += 1 c.most_common() # print(c) for x in c: if len(ans) < n: t = min(n - len(ans), c[x] - 1) ans += x * t d[x] += t else: break maxm = 1 for x in d: maxm = max(c[x] / d[x], maxm) print(int(math.ceil(maxm))) print(ans) ``` No
5,352
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Submitted Solution: ``` s = input() n = int(input()) d = {} r = 0 for a in s: d.setdefault(a, 0) d[a] += 1 if(d[a] > r): r=d[a] if (len(d) > n): print(-1) else: l = 0 while r - l > 1: k = (l + r) // 2 cur = 0 for x in d.values(): cur += (x+k-1) // k if cur > n: l = k else: r = k print(r) s = '' for a in d.keys(): s += a * ((d[a]+r-1) // r) print(s) ``` No
5,353
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". Submitted Solution: ``` import math S=input() N=int(input()) Srep={} ansrep={} for item in "abcdefghijklmnopqrstuvwxyz": Srep[item]=0 ansrep[item]=0 for item in S: Srep[item]+=1 ansrep[item]+=1 Q=list(set(S)) if(len(Q)>N): print(-1) else: n=len(Q) ans=list(S) num=1 req=1 n=len(ans) while(len(ans)>N): n=len(ans) for item in ans: if(ansrep[item]==1): continue if(math.ceil(Srep[item]/(ansrep[item]-1))>req): continue else: ansrep[item]-=1 ans.remove(item) break if(n==len(ans)): for item in ans: if(ansrep[item]==1): continue ansrep[item]-=1 ans.remove(item) req+=1 break g="" if(len(ans)<N): g=S[0]*(N-len(ans)) print(req) for item in ans: print(item,end="") print(g) ``` No
5,354
Provide tags and a correct Python 3 solution for this coding contest problem. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Tags: brute force, implementation Correct Solution: ``` n = int(input()) seq = list(map(int, input().split())) if n == 1: print("no") exit(0) pairs = [] cur = [-1, seq[0]] fail = False for i in range(1, len(seq)): cur[0], cur[1] = cur[1], seq[i] if cur[1] < cur[0]: pairs.append([cur[1], cur[0]]) else: pairs.append(cur[:]) #print(pairs) for x1, x2 in pairs: if not fail: for x3, x4 in pairs: if (x1 != x3 or x2 != x4) and (x1 < x3 < x2 < x4 or x3 < x1 < x4 < x2): fail = True break else: break if fail: print("yes") else: print("no") ```
5,355
Provide tags and a correct Python 3 solution for this coding contest problem. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Tags: brute force, implementation Correct Solution: ``` n = int(input()) Min = -1e10 Max = 1e10 ans = "no" def f(x, y): return x[0] < y[0] < x[1] < y[1] or \ y[0] < x[0] < y[1] < x[1] xs = list(map(int, input().split())) for i in range(1, len(xs) - 1): for j in range(0, i): if f([min(xs[i:i+2]), max(xs[i:i+2])], [min(xs[j:j+2]), max(xs[j:j+2])]): ans = "yes" break print(ans) ```
5,356
Provide tags and a correct Python 3 solution for this coding contest problem. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Tags: brute force, implementation Correct Solution: ``` n,l=int(input()),list(map(int,input().split())) for i in range(n-1): for j in range(n-1): x=sorted([l[i],l[i+1]])+sorted([l[j],l[j+1]]) if x[0]<x[2]<x[1]<x[3]: exit(print('yes')) print('no') ```
5,357
Provide tags and a correct Python 3 solution for this coding contest problem. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Tags: brute force, implementation Correct Solution: ``` def self_intersect(points:list): for i in range(len(points)): check_point = points[i] for j in range(len(points)): if i != j: check_point2 = points[j] if check_point[0] < check_point2[0] < check_point[1] < check_point2[1]: return True if check_point2[0] < check_point[0] < check_point2[1] < check_point[1]: return True return False n = int(input()) points = list(map(int,input().split(' '))) semi_circles = [] for i in range(0,len(points)-1): if points[i] < points[i+1]: semi_circles.append([points[i],points[i+1]]) else: semi_circles.append([points[i+1],points[i]]) if self_intersect(semi_circles): print("yes") else: print("no") ```
5,358
Provide tags and a correct Python 3 solution for this coding contest problem. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Tags: brute force, implementation Correct Solution: ``` n = int(input()) arr = list(map(int, input().split())) flag = 0 for i in range(n-1): for j in range(i+1,n-1): a,b = min(arr[i],arr[i+1]),max(arr[i],arr[i+1]) c,d = min(arr[j],arr[j+1]),max(arr[j],arr[j+1]) if a<c<b<d or c<a<d<b: flag=1 break if flag==1: break if flag==0: print("no") else: print("yes") ```
5,359
Provide tags and a correct Python 3 solution for this coding contest problem. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Tags: brute force, implementation Correct Solution: ``` def intersect(p1,p2): if (p2[0] < p1[1] and p2[0] > p1[0] and p2[1] > p1[1]) or (p2[1] < p1[1] and p2[1] > p1[0] and p2[0] < p1[0]): return True return False def check(points): for x in range(len(points)): for i in range(x + 1,len(points)): if intersect(points[x],points[i]): return "yes" return "no" n = int(input()) temp_in = [int(x) for x in input().split(' ')] points = [] for x in range(n - 1): points.append((min(temp_in[x],temp_in[x + 1]),max(temp_in[x],temp_in[x + 1]))) #print(points) print(check(points)) #chyba przypadek z tym ze wspolrzedne pozniej sa mniejsze i wtedy warunek nie wchodzi na przeciecie ''' 2 0 15 13 20 ans: yes semicircles will intersect with each other in one case: begininning of the second one will be earlier than ending of the first. and there will be no subcircle (circle which is included in the one above) brute force O(N^2+N / 2) ''' ```
5,360
Provide tags and a correct Python 3 solution for this coding contest problem. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Tags: brute force, implementation Correct Solution: ``` #http://codeforces.com/problemset/problem/358/A #not accepted n = eval(input()) points = [(int)(i) for i in input().split()] poles = [] ''' left = 0 right = 0 prevDir = '-' currentDir = '-' ''' def is_intersect(points): if(n <= 2): return False; global poles poles.append([points[0],points[1]]) for i in range(2,n): prevPoint = points[i-1] currentPoint = points[i] for pole in poles: _min = min(pole[0],pole[1]) _max = max(pole[0],pole[1]) if (prevPoint in range(_min+1,_max) and\ currentPoint not in range(_min,_max+1)) or\ (currentPoint in range(_min+1,_max) and\ prevPoint not in range(_min,_max+1)): return True poles.append([prevPoint,currentPoint]); return False ''' global right global left global prevDir global currentDir left = min(points[0],points[1]) right = max(points[0],points[1]) prevDir = points[1] > points[0] if 'right' else 'left' currentDir = '-' for i in range(2,n): prevPoint = points[i-1] currentPoint = points[i] currentDir = currentPoint > prevPoint if 'right' else 'left' if prevDir == currentDir: if currentDir == 'left': right = prevPoint; left = currentPoint; elif currentDir == 'right': right = currentPoint left = prevPoint else: pass prevDir = currentDir return False ''' if is_intersect(points): print('yes') else: print('no') ```
5,361
Provide tags and a correct Python 3 solution for this coding contest problem. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Tags: brute force, implementation Correct Solution: ``` n = int(input()) x = [int(i) for i in input().split()] if (n < 3): print ('no') else: ans = 'no' for i in range(3, n): prev = x[i - 1] num = x[i] for j in range(i - 2): num1 = min(x[j], x[j + 1]) num2 = max(x[j], x[j + 1]) if (prev < num1 or prev > num2) and (num > num1 and num < num2): ans = 'yes' break elif ((num < num1 or num > num2) and (prev > num1 and prev < num2)): ans = 'yes' break print (ans) ```
5,362
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Submitted Solution: ``` n=int(input()) l=list(map(int,input().split())) maxx=l[0] minn=l[0] for i in range(0,n-1): if l[i+1]>l[i]: if l[i]>minn and l[i]<maxx and l[i+1]>maxx: print("yes") exit() elif l[i]<minn and l[i+1]>minn and l[i+1]<maxx : print("yes") exit() else: if l[i]>maxx: maxx=l[i] if l[i]<minn: minn=l[i] elif l[i+1]<l[i]: if l[i+1]>minn and l[i+1]<maxx and l[i]>maxx: print("yes") exit() elif l[i+1]<minn and l[i]>minn and l[i]<maxx: print("yes") exit() else: if l[i]>maxx: maxx=l[i] if l[i]<minn: minn=l[i] print("no") ``` Yes
5,363
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Submitted Solution: ``` ''' Created on Oct 31, 2013 @author: Ismael ''' def main(): input() l = list(map(int,input().split())) #l = [0,10,5,15] #l = [0,15,5,10] if(len(l)<=3): print("no") return xMin = min(l[0],l[1]) xMax = max(l[0],l[1]) for i in range(2,len(l)-1): if(l[i]<=xMin or l[i]>=xMax):#3eme ext, next point must be outside [xMin,xMax] if(l[i+1]>xMin and l[i+1]<xMax): print("yes") return elif(l[i]>=xMin and l[i]<=xMax):#3eme int, next point must be inside [xMin,xMax] if(l[i+1]<xMin or l[i+1]>xMax): print("yes") return min123 = min(xMin,l[i]) max123 = max(xMax,l[i]) if(l[i+1]<min123 or l[i+1]>max123): xMin = min123 xMax = max123 else: elems = [l[i+1],xMin,xMax,l[i]] elems.sort() ind4 = elems.index(l[i+1]) xMin = elems[ind4-1] xMax = elems[ind4+1] print("no") main() ``` Yes
5,364
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Submitted Solution: ``` n = int(input()) a = [int(y) for y in input().split()] flag = 0 for i in range(len(a)-2): for j in range(i, 0, -1): r = min(a[j], a[j-1]) s = max(a[j], a[j-1]) if (a[i+2] > r and a[i+2] < s) and (a[i+1] > s or a[i+1] < r): flag += 1 break elif (a[i+1] > r and a[i+1] < s) and (a[i+2] > s or a[i+2] < r): flag += 1 break if(flag == 0): print("no") else: print("yes") ``` Yes
5,365
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Submitted Solution: ``` def take_input(s): #for integer inputs if s == 1: return int(input()) return map(int, input().split()) n = take_input(1) if n == 1: print("no"); exit() a = list(take_input(n)) flag = 0 for i in range(1,n-1): for j in range(i): init_1 = min(a[i],a[i+1]); fin_1 = max(a[i],a[i+1]) init_2 = min(a[j],a[j+1]); fin_2 = max(a[j],a[j+1]) if ((init_1 > init_2 and init_1 < fin_2 and fin_1 > fin_2) or (init_1 < init_2 and fin_1 > init_2 and fin_1<fin_2)) and flag == 0: print("yes") flag = 1 break if flag == 0: print("no") ``` Yes
5,366
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Submitted Solution: ``` n = int(input().strip()) points = list(map(int, input().strip().split())) val = True for i in range(n - 1): if points[i] > points[i+1]: if points[i] < max(points[i+1:]): val = False break if val: print("no") else: print("yes") ``` No
5,367
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Submitted Solution: ``` n = int(input()) pos = [int(k) for k in input().split()] sigue = True if len(pos)>2: for i in range(len(pos)-2): x1 = pos[i] x2 = pos[i+1] for k in range(i+2,len(pos)-1): x3 = pos[k] x4 = pos[k+1] if x1<x3<x2<x4 or x3<x1<x4<x2 or x1<x4<x2<x3 or x4<x1<x3<x2: print("yes") sigue = False break if not sigue: break if sigue: print("no") ``` No
5,368
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Submitted Solution: ``` import sys import math import itertools import collections def getdict(n): d = {} if type(n) is list or type(n) is str: for i in n: if i in d: d[i] += 1 else: d[i] = 1 else: for i in range(n): t = ii() if t in d: d[t] += 1 else: d[t] = 1 return d def divs(n, start=1): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def cdiv(n, k): return n // k + (n % k != 0) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a*b) // math.gcd(a, b) def wr(arr): return '\n'.join(map(str, arr)) def revn(n): return int(str(n)[::-1]) def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True n = ii() x = li() if n < 4: print('yes') else: mnx = min(x) mxx = max(x) if x[0] == mnx and x[1] == mxx: lp, rp = mnx, mxx for i in range(2, n): if i % 2 == 0: if lp < x[i] and x[i] < rp: lp = x[i] else: exit(print('yes')) else: if lp < x[i] and x[i] < rp: rp = x[i] else: exit(print('yes')) elif x[0] == mxx and x[1] == mnx: lp, rp = mnx, mxx for i in range(2, n): if i % 2 == 0: if lp < x[i] and x[i] < rp: rp = x[i] else: exit(print('yes')) else: if lp < x[i] and x[i] < rp: lp = x[i] else: exit(print('yes')) elif x[1] < x[0]: lp, rp = x[1], x[0] for i in range(2, n): if i % 2 == 0: if rp < x[i]: rp = x[i] else: exit(print('no')) else: if x[i] < lp: rp = x[i] else: exit(print('yes')) elif x[0] < x[1]: lp, rp = x[0], x[1] for i in range(2, n): if i % 2 == 0: if x[i] < lp: lp = x[i] else: exit(print('yes')) else: if rp < x[i]: rp = x[i] else: exit(print('yes')) print('no') ``` No
5,369
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). <image> Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Examples Input 4 0 10 5 15 Output yes Input 4 0 15 5 10 Output no Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. Submitted Solution: ``` n=int(input()) x = list(map(int, input().split())) c=0 d=0 if(n<=3): d+=1 else: for i in range(n-3): if(x[i]<x[i+2]<x[i+1]<x[i+3] or x[i+2]<x[i]<x[i+3]<x[i+1]): c+=1 else: d+=1 if(c>0): print("yes") else: print("no") ``` No
5,370
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100). Output In a single line print a single integer — the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232. Submitted Solution: ``` n, m = input().split() m = int(m) def variations(string): if string: head = string[0] tail = string[1:] for var in variations(tail): for i in range(len(var)+1): yield var[:i] + head + var[i:] else: yield string res = 0 for v in variations(n): if not v.startswith("0"): if int(v) % m == 0: res += 1 print(res) ``` No
5,371
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100). Output In a single line print a single integer — the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232. Submitted Solution: ``` def fact(n): j = 1 for i in range(1,n+1): j*=i return j n,m = map(int,input().split()) l = len(str(n)) n = str(n) mp = dict() mp2 = dict() for i in range(l): if int(n[i]) in mp2.keys(): mp2[int(n[i])]+=1 else : mp2[int(n[i])] = 1 for i in range(l): for j in range(l): for k in range(l): if i!=j and j!=k and i!=k : y = int(n[i]+n[j]+n[k])%m if not y : mp[str(n[i]+n[j]+n[k])] = y if l<=3: if l==1: if int(n)==0 or int(n)%m!=0: print(0) else: print(1) elif l==2: x = n[1]+n[0] c = 0 if int(n)%m==0: c+=1 if x[0]!='0' and int(x)%m==0: c+=1 print(c) else: c = 0 for i in mp: if i[0]!='0': c+=1 print(c) exit(0) a = 0 for i in mp.keys(): mp3 = dict(mp2) c,c2 = 0,1 for l in i: mp3[int(l)]-=1 for j in mp3.keys(): c2 *= fact(mp3[j]) c += mp3[j] if 0 in mp3.keys() and mp3[0]>=1: a+= fact(c-1)*(c-1)//c2 else: a += fact(c)//c2 print(a) ``` No
5,372
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100). Output In a single line print a single integer — the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232. Submitted Solution: ``` nums=list(map(int,input().split())) nums_modified=[] d1={str(i):0 for i in range(0,10)} d2={str(i):0 for i in range(0,10)} for index in range(1,len(nums)): x=list(str(nums[index])) x2=list(map(int,x)) small=min(x2);large=max(x2) n=str(small*7+large*11) if len(n)>=3: n=n[1:] nums_modified.append(n) if index%2!=0: d1[n[0]]+=1 else:d2[n[0]]+=1 ans=[0]* 10 for key,value in d1.items(): if value==2: ans[int(key)]+=1 elif value>2: ans[int(key)]+=2 for key,value in d2.items(): if value==2: ans[int(key)]+=1 elif value>2: ans[int(key)]+=2 c=0 for i in ans: if i>2: c+=2 else: c+=i print(c) ``` No
5,373
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100). Output In a single line print a single integer — the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232. Submitted Solution: ``` def fact(n): j = 1 for i in range(1,n+1): j*=i return j n,m = map(int,input().split()) l = len(str(n)) n = str(n) mp = dict() mp2 = dict() for i in range(l): if int(n[i]) in mp2.keys(): mp2[int(n[i])]+=1 else : mp2[int(n[i])] = 1 for i in range(l): for j in range(l): for k in range(l): if i!=j and j!=k and i!=k : y = int(n[i]+n[j]+n[k])%m if not y : mp[str(n[i]+n[j]+n[k])] = y if l<=3: if l==1: if int(n)==0 or int(n)%m!=0: print(0) else: print(1) elif l==2: x = n[1]+n[0] c = 0 if int(n)%m==0: c+=1 if x[0]!='0' and int(x)%m==0: c+=1 print(c) else: c = 0 for i in mp: if i[0]!='0': c+=1 print(c) exit(0) a = 0 print(mp2) for i in mp.keys(): mp3 = dict(mp2) c,c2 = 0,1 print(i) for l in i: mp3[int(l)]-=1 for j in mp3.keys(): c2 *= fact(mp3[j]) c += mp3[j] if 0 in mp3.keys() and mp3[0]>=1: a+= fact(c-1)*(c-1)//c2 else: a += fact(c)//c2 print(mp3) print(a) ``` No
5,374
Provide tags and a correct Python 3 solution for this coding contest problem. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Tags: brute force, implementation, math Correct Solution: ``` from sys import * t=int(stdin.readline()) mm=0 for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) mm=2*max(d1,d2)-min(d1,d2) if (k-2*d1-d2>=0 and (k-2*d1-d2)%3==0 and n-2*d2-d1-k>=0 and (n-2*d2-d1-k)%3==0) or (k-2*d2-d1>=0 and (k-2*d2-d1)%3==0 and n-2*d1-d2-k>=0 and (n-2*d1-d2-k)%3==0) or (k-d1-d2>=0 and (k-d1-d2)%3==0 and n-mm-k>=0 and (n-mm-k)%3==0) or (k-mm>=0 and (k-mm)%3==0 and n-d1-d2-k>=0 and (n-d1-d2-k)%3==0): print("yes") else: print("no") ```
5,375
Provide tags and a correct Python 3 solution for this coding contest problem. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Tags: brute force, implementation, math Correct Solution: ``` from sys import stdin def solve(n, k, d1, d2): d = n - k # a >= b and c >= b c1 = (k - d1 + 2 * d2) // 3 a1 = d1 - d2 + c1 b1 = k - a1 - c1 # a >= b and c < b c2 = (k - d1 - 2 * d2) // 3 a2 = d1 + d2 + c2 b2 = k - a2 - c2 # a < b and c < b c3 = (k + d1 - 2 * d2) // 3 a3 = c3 + d2 - d1 b3 = k - a3 - c3 # a < b and c >= b c4 = (k + d1 + 2 * d2) // 3 a4 = c4 - d1 - d2 b4 = k - a4 - c4 if c1 * 3 == 2 * d2 - d1 + k and a1 >= 0 and b1 >= 0 and c1 >= 0: _max = max(a1, b1, c1) diff = sum(_max - e for e in [a1, b1, c1]) if d >= diff and (d - diff) % 3 == 0: return True if c2 * 3 == k - d1 - 2 * d2 and a2 >= 0 and b2 >= 0 and c2 >= 0: _max = max(a2, b2, c2) diff = sum(_max - e for e in [a2, b2, c2]) if d >= diff and (d - diff) % 3 == 0: return True if c3 * 3 == k + d1 - 2 * d2 and a3 >= 0 and b3 >= 0 and c3 >= 0: _max = max(a3, b3, c3) diff = sum(_max - e for e in [a3, b3, c3]) if d >= diff and (d - diff) % 3 == 0: return True if c4 * 3 == k + d1 + 2 * d2 and a4 >= 0 and b4 >= 0 and c4 >= 0: _max = max(a4, b4, c4) diff = sum(_max - e for e in [a4, b4, c4]) if d >= diff and (d - diff) % 3 == 0: return True return False def main(): test = stdin.readlines() output = [] for i in range(1, int(test[0]) + 1): ans = 'yes' if solve(*map(int, test[i].split())) else 'no' output.append(ans) print('\n'.join(output)) if __name__ == "__main__": main() ```
5,376
Provide tags and a correct Python 3 solution for this coding contest problem. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Tags: brute force, implementation, math Correct Solution: ``` input=__import__('sys').stdin.readline for _ in range(int(input())): n,k,d1,d2 = map(int,input().split()) lis=[[2*d1+d2 , 2*d2+d1] , [2*d2+d1 , 2*d1+d2] , [2*max(d1,d2)-min(d1,d2) , d1+d2] , [d1+d2 , 2*max(d1,d2) - min(d1,d2)]] flag=1 for i in lis: if i[0]<=k and i[0]%3==k%3 and n-k-i[1]>=0 and (n-k-i[1])%3==0: print("yes") flag=0 break if flag: print("no") ```
5,377
Provide tags and a correct Python 3 solution for this coding contest problem. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Tags: brute force, implementation, math Correct Solution: ``` t=int(input()) for j in range(t): inp=[int(n) for n in input().split()] n=inp[0] k=inp[1] d1=inp[2] d2=inp[3] if d2<d1: s=d1 d1=d2 d2=s if ((k>=2*d1+d2) and ((k-2*d1-d2)%3==0) and (n-k>=d1+2*d2) and ((n-k-d1-2*d2)%3==0)): print('yes') elif ((k>=2*d2+d1) and ((k-2*d2-d1)%3==0) and (n-k>=d2+2*d1) and ((n-k-d2-2*d1)%3==0)): print('yes') elif ((k>=d1+d2) and ((k-d1-d2)%3==0) and (n-k>=2*d2-d1) and ((n-k-2*d2+d1)%3==0)): print('yes') elif ((k>=2*d2-d1) and ((k-2*d2+d1)%3==0) and (n-k>=d1+d2) and ((n-k-d1-d2)%3==0)): print('yes') else: print('no') ```
5,378
Provide tags and a correct Python 3 solution for this coding contest problem. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Tags: brute force, implementation, math Correct Solution: ``` from sys import * t=int(stdin.readline()) mm,mmm,mmmm,m=0,0,0,0 for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) m=d1+d2 mm=2*max(d1,d2)-min(d1,d2) mmm=2*d1+d2 mmmm=2*d2+d1 if (k-mmm>=0 and (k-mmm)%3==0 and n-mmmm-k>=0 and (n-mmmm-k)%3==0) or (k-mmmm>=0 and (k-mmmm)%3==0 and n-mmm-k>=0 and (n-mmm-k)%3==0) or (k-m>=0 and (k-m)%3==0 and n-mm-k>=0 and (n-mm-k)%3==0) or (k-mm>=0 and (k-mm)%3==0 and n-m-k>=0 and (n-m-k)%3==0): print("yes") else: print("no") ```
5,379
Provide tags and a correct Python 3 solution for this coding contest problem. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Tags: brute force, implementation, math Correct Solution: ``` import sys import operator input = sys.stdin.readline def read_int(): return int(input().strip()) def read_ints(): return list(map(int, input().strip().split(' '))) def solve(): """ n, k, d1, d2 d1 = abs(w1-w2) d2 = abs(w2-w3) w1+w2+w3=k 1) w1 >= w2 and w2 >= w3 d1 = w1-w2 d2 = w2-w3 d1-d2 = w1+w3-2*w2 d1-d2-k = -3*w2 w1 = d1-(d1-d2-k)/3 w2 = -(d1-d2-k)/3 w3 = -(d1-d2-k)/3-d2 n/3-w1 >= 0 n/3-w2 >= 0 n/3-w3 >= 0 """ n, k, d1, d2 = read_ints() if n%3 != 0: return 'no' # w1 >= w2 and w2 >= w3 def check(n, k, d1, d2, op1, op2): if (d1-d2-k)%3 != 0: return False w1 = d1-(d1-d2-k)//3 w2 = -(d1-d2-k)//3 w3 = -(d1-d2-k)//3-d2 if w1 >= 0 and w2 >= 0 and w3 >= 0 and n//3-w1 >= 0 and n//3-w2 >= 0 and n//3-w3 >= 0 and op1(w1, w2) and op2(w2, w3): return True return False if check(n, k, d1, d2, operator.ge, operator.ge): return 'yes' if check(n, k, d1, -d2, operator.ge, operator.lt): return 'yes' if check(n, k, -d1, d2, operator.lt, operator.ge): return 'yes' if check(n, k, -d1, -d2, operator.lt, operator.lt): return 'yes' return 'no' if __name__ == '__main__': T = read_int() for _ in range(T): print(solve()) ```
5,380
Provide tags and a correct Python 3 solution for this coding contest problem. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Tags: brute force, implementation, math Correct Solution: ``` t = int(input()) ret = [] while t>0: t-=1 n,k,d1,d2 = map(int,input().split()) # ans = [] y1 = (k-(d1-d2))//3 x1 = y1+d1 z1 = y1-d2 # ans = [y1,z1,x1] # ans = sorted(ans) # ans1 = 2*ans[2]-(ans[0]+ans[1]) ans1 = 2*x1-(z1+y1) if x1+y1+z1==k and min(z1,y1)>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue # ans = [] y1 = (k-(d1+d2))//3 x1 = y1+d1 z1 = y1+d2 if d1>=d2: # ans = [y1,z1,x1] ans1 = 2*x1-(y1+z1) else: # ans = [y1,x1,z1] ans1 = 2*z1-(y1+x1) # ans = sorted(ans) # ans1 = 2*ans[2]-(ans[0]+ans[1]) if x1+y1+z1==k and y1>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue y1 = (k-(d2-d1))//3 x1 = y1-d1 z1 = y1+d2 # ans = [x1,y1,z1] # ans = sorted(ans) ans1 = 2*z1-(x1+y1) if x1+y1+z1==k and min(x1,y1)>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue y1 = (k+(d2+d1))//3 x1 = y1-d1 z1 = y1-d2 # ans = [x1,y1,z1] # ans = sorted(ans) ans1 = 2*y1-(x1+z1) if x1+y1+z1==k and min(x1,z1)>=0 and ans1<=n-k and (n-k-ans1)%3==0: ret.append('yes') continue # if d1>=d2: # ans.append(2*d1-d2) # ans.append(d2+2*(d1-d2)) # else: # ans.append(2*d2-d1) # ans.append(d1+2*(d2-d1)) # ans+=[d1+2*d2,d2+2*d1,d1+d2] # done = False # print(ans) # for a in ans: # # if (a==0 and (n-k)%3==0) or (a!=0 and (n-k)//a>1 and (n-k)%a==0): # if (a<=n-k) and (n-k-a)%3==0: # print(a) # done = True # break # if done: # print('yes') # else: ret.append('no') print(*ret, sep = '\n') ```
5,381
Provide tags and a correct Python 3 solution for this coding contest problem. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Tags: brute force, implementation, math Correct Solution: ``` def check(n, k, d1, d2): if (k-d1-d2) % 3 != 0: return False y = (k-d1-d2) // 3 x = y+d1 z = y+d2 t = n // 3 if x < 0 or y < 0 or z < 0: return False if x > t or y > t or z > t: return False return True t = int(input()) for _ in range(t): n, k, d1, d2 = map(int, input().split()) if n % 3 != 0: print ('no') continue if check(n, k, d1, d2): print ('yes') continue if check(n, k, -d1, d2): print ('yes') continue if check(n, k, d1, -d2): print ('yes') continue if check(n, k, -d1, -d2): print ('yes') continue print ('no') ```
5,382
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Submitted Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * # from fractions import * # from heapq import* from bisect import * from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ALPHA='abcdefghijklmnopqrstuvwxyz' M=10**9+7 EPS=1e-6 def Ceil(a,b): return a//b+int(a%b>0) def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() def check(d1,d2): wa=0 wb=0 wc=0 # wa-wb=d1 # wb-wc=d2 # wa-wc=d1+d2 # wa+wb+wc=k wa=2*d1+d2+k if(wa%3): return False wa//=3 wb=wa-d1 wc=wb-d2 if(wa<0 or wb<0 or wc<0): return False wa,wb,wc=sorted((wa,wb,wc)) need=2*wc-wa-wb have=n-k-need # print(need,have,'-->',wa,wb,wc) return have>=0 and have%3==0 for _ in range(Int()): n,k,d1,d2=value() d3,d4=-d1,-d2 d1=[d1,d3] d2=[d2,d4] ok=False for i in d1: for j in d2: ok|=check(i,j) if(ok): print("yes") else: print("no") ``` Yes
5,383
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Submitted Solution: ``` def main(): t = int(input()) for z in range(t): n, k, d1, d2 = map(int, input().split()) if n % 3 != 0: print('no') continue f = 0 for i in [-1, +1]: for j in [-1, +1]: w = (k - i * d1 - j * d2) if f == 0 and (w % 3 == 0) and (n//3)>=(w//3)>=0 and (n//3)>=(w//3 + i * d1)>=0 and (n//3)>=(w//3 + j * d2)>=0: print('yes') f = 1 if f == 0: print('no') main() ``` Yes
5,384
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Submitted Solution: ``` t = int(input()) for i in range(t): n, k, a, b = map(int, input().split()) if n % 3 != 0: print("no") else: for i in range(2): for j in range(2): flagf = False if i == 0: a1 = a else: a1 = -a if j == 0: b1 = b else: b1 = -b t2 = (k - a1 + b1)/3 t1 = a1 + t2 t3 = t2 - b1 # print(t1, t2, t3) flag1 = False flag2 = False # valores válidos if (k-a1+b1) % 3 == 0 and t1 >= 0 and t2 >= 0 and t3 >= 0: if t1 <= n/3 and t2 <= n/3 and t3 <= n/3: if i == 0 and t1 >= t2: flag1 = True elif i == 1 and t1 < t2: flag1 = True if j == 0 and t2 >= t3: flag2 = True elif j == 1 and t2 < t3: flag2 = True if flag1 and flag2: if t1 == t2 and t2 == t3 and (n-k) % 3 == 0: flagf = True break else: v = [t1, t2, t3] v = sorted(v) faltam = n-k faltam -= v[2] - v[1] faltam -= v[2] - v[0] if faltam < 0: break elif faltam == 0 or faltam %3 == 0: flagf = True break if flagf: print("yes") break else: print("no") # 3 1 1 0 ``` Yes
5,385
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Submitted Solution: ``` from sys import stdin def solve(n, k, d1, d2): if (d1 + 2 * d2 + k) % 3 != 0: return False c = (d1 + 2 * d2 + k) // 3 a = c - d1 - d2 b = k - a - c if a >= 0 and b >= 0 and c >= 0 and a + b + c == k: _max = max(a, b, c) diff = sum(_max - e for e in [a, b, c]) d = n - k if d >= diff and (d - diff) % 3 == 0: return True return False def main(): test = stdin.readlines() output = [] for i in range(1, int(test[0]) + 1): n, k, d1, d2 = map(int, test[i].split()) for i, j in [(-1, -1), (-1, 1), (1, -1), (1, 1)]: if solve(n, k, i * d1, j * d2): output.append('yes') break else: output.append('no') print('\n'.join(output)) if __name__ == "__main__": main() ``` Yes
5,386
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Submitted Solution: ``` from sys import * t=int(stdin.readline()) for i in range(t): n,k,d1,d2=(int(z) for z in stdin.readline().split()) p1=n-(2*d1+d2+k) p2=n-(2*d2+d1+k) if (p1>=0 and not p1%3) or (p2>=0 and not p2%3): print("yes") else: print("no") ``` No
5,387
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Submitted Solution: ``` for t in range(int(input())): n,k,d1,d2=map(int,input().split()) x=(k+(2*d1)+d2)//3 a,b,c=x,x-d1,x-d1-d2 n1=n//3 #print(x,a,b,c) if n%3!=0 or (abs(n1-a)+abs(n1-b)+abs(n1-c))!=(n-k) or a+b+c!=k or a>n1 or b>n1 or c>n1 : print('no') else: print('yes') ``` No
5,388
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Submitted Solution: ``` a = [] z = int(input()) for i in range(z): n, k, d1, d2 = map(int, input().split()) d1, d2 = sorted((d1, d2, )) zn = n - k v1 = 2 * d1 + d2 if v1 <= zn: a.append('yes') else: a.append('no') print(*a, sep = '\n') ``` No
5,389
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2. You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament? Note that outcome of a match can not be a draw, it has to be either win or loss. Input The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case. Output For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). Examples Input 5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2 Output yes yes yes no no Note Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes". Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). Submitted Solution: ``` for _ in range(int(input())): n,k,d1,d2 = map(int,input().split()) lis=[[2*d1+d2 , 2*d2+d1] , [2*d2+d1 , 2*d1+d2] , [2*max(d1,2)-min(d1,d2) , d1+d2] , [d1+d2 , 2*max(d1,d2) - min(d1,d2)]] flag=1 for i in lis: if i[0]<=k and (k-i[0])%3==0 and n-k-i[1]>=0 and (n-k-i[1])%3==0: print("yes") flag=0 break if flag: print("no") ``` No
5,390
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. Tags: binary search, implementation Correct Solution: ``` import sys import math a=int(input()) b=list(map(int,input().split())) c=int(input()) d=list(map(int,input().split())) e=[] f=0 for i in b: #print(f,i) e.append(f+i) f=i+f g=[0]*(e[-1]+1) h=0 for i in range(a): for j in range(h,e[i]+1): g[j]=i+1 h=e[i]+1 for k in d: print(g[k]) ```
5,391
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. Tags: binary search, implementation Correct Solution: ``` a=int(input()) b= list(map(int,input().split())) c=int(input()) d= list(map(int,input().split())) for k in range(1,a): b[k] += b[k-1] for j in d: l=0 r= a-1 while l<= r: if j <= b[0]: mid=0 break mid= (l+r)//2 if b[mid] >= j and b[mid-1] < j: break elif b[mid] > j: r=mid-1 else: l=mid+1 print(mid+1) ```
5,392
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. Tags: binary search, implementation Correct Solution: ``` def read(): input() sizes = list(map(int, input().split())) input() queries = list(map(int, input().split())) return sizes, queries def calc_indexes(sizes): start_indexes = [1] for i in range(len(sizes)): start_indexes.append(start_indexes[i] + sizes[i]) return start_indexes def bin_search(ar, query): begin = 0 end = len(ar) while (end - begin) > 1: middle = (end + begin) // 2 if query < ar[middle]: end = middle else: begin = middle return begin # for i in range(begin, end): # if ar[i] > query: # return i - 1 # return end - 1 sizes, queries = read() start_indexes = calc_indexes(sizes) for q in queries: print (bin_search(start_indexes, q) + 1) ```
5,393
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. Tags: binary search, implementation Correct Solution: ``` import bisect as bs n = int(input()) A = list(map(int, input().split())) m = int(input()) B = list(map(int, input().split())) S = [0] for a in A: S.append(S[-1] + a) for b in B: print(bs.bisect_left(S, b)) ```
5,394
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. Tags: binary search, implementation Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) m=int(input()) q=list(map(int,input().split())) d=[0] for i in range(n): d.append(d[i]+a[i]) d.append(1000001) for i in range(m): start=0;end=len(d)-1 while start<end: k=(start+end)//2 if q[i]<d[k]: end=k-1 else: start=k+1 for t in range(max(0,end-4),n+1): if q[i]<=d[t]: print(t) break else: print(n) ```
5,395
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. Tags: binary search, implementation Correct Solution: ``` input() d=[] m=1 for i in map(int,input().split()): for j in range(i): d.append(m) m+=1 input() for i in map(int,input().split()): print(d[i-1]) ```
5,396
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. Tags: binary search, implementation Correct Solution: ``` n=int(input()) x=list(map(int,input().split())) t=int(input()) y=list(map(int,input().split())) ans=[] for i in range(len(x)): ans.extend([i+1 for j in range(x[i])]) for i in y: print(ans[i-1]) ```
5,397
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. Tags: binary search, implementation Correct Solution: ``` n=int(input()) s=list(map(int,input().split())) n1=int(input()) s1=list(map(int,input().split())) w=[] i=-1 d=n-1 r=0 while i!=d: i+=1 for j in range(s[i]): r+=1 w.append(r) ans=[] for i in range(n1): l,r=-1,n while (l+1<r): md=(l+r)//2 if s1[i]<=w[md]: r=md else: l=md ans.append(r+1) print(*ans,sep='\n') ```
5,398
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) k=0 l=[] c=1 for i in range(len(a)): x=a[i] for j in range(k,k+x): l.append(c) c+=1 k=k+x t=int(input()) b=list(map(int,input().split())) for i in b: print(l[i-1]) ``` Yes
5,399