message
stringlengths
2
59.7k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
37
108k
cluster
float64
20
20
__index_level_0__
int64
74
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means replacing it with digit 9 - t. Help Chewbacca to transform the initial number x to the minimum possible positive number by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero. Input The first line contains a single integer x (1 ≀ x ≀ 1018) β€” the number that Luke Skywalker gave to Chewbacca. Output Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes. Examples Input 27 Output 22 Input 4545 Output 4444 Submitted Solution: ``` n = input() l = len(n) n = int(n) s = [] while n > 0: s.append(n%10) n//=10 print(str(min(s))*l) ```
instruction
0
61,852
20
123,704
No
output
1
61,852
20
123,705
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means replacing it with digit 9 - t. Help Chewbacca to transform the initial number x to the minimum possible positive number by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero. Input The first line contains a single integer x (1 ≀ x ≀ 1018) β€” the number that Luke Skywalker gave to Chewbacca. Output Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes. Examples Input 27 Output 22 Input 4545 Output 4444 Submitted Solution: ``` st = str(input()) lst = [] for i in st: lst.append(int(i)) for i in range(len(lst)): if lst[i] >= 5: lst[i] = 9 - lst[i] lst = map(str,lst) print("".join(lst)) ```
instruction
0
61,853
20
123,706
No
output
1
61,853
20
123,707
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means replacing it with digit 9 - t. Help Chewbacca to transform the initial number x to the minimum possible positive number by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero. Input The first line contains a single integer x (1 ≀ x ≀ 1018) β€” the number that Luke Skywalker gave to Chewbacca. Output Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes. Examples Input 27 Output 22 Input 4545 Output 4444 Submitted Solution: ``` string=input() for letter in string: digit=int(letter) if digit>4: print(9-digit,end='') else: print(digit,end='') ```
instruction
0
61,854
20
123,708
No
output
1
61,854
20
123,709
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means replacing it with digit 9 - t. Help Chewbacca to transform the initial number x to the minimum possible positive number by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero. Input The first line contains a single integer x (1 ≀ x ≀ 1018) β€” the number that Luke Skywalker gave to Chewbacca. Output Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes. Examples Input 27 Output 22 Input 4545 Output 4444 Submitted Solution: ``` n = int(input()) t = n t1 = 0 while t : a = t%10 if (9-a) <= a : t1 = t1*10 + (9-a) else : t1 = t1*10 + a t //= 10 print(t1) ```
instruction
0
61,855
20
123,710
No
output
1
61,855
20
123,711
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive integer n greater or equal to 2. For every pair of integers a and b (2 ≀ |a|, |b| ≀ n), you can transform a into b if and only if there exists an integer x such that 1 < |x| and (a β‹… x = b or b β‹… x = a), where |x| denotes the absolute value of x. After such a transformation, your score increases by |x| points and you are not allowed to transform a into b nor b into a anymore. Initially, you have a score of 0. You can start at any integer and transform it as many times as you like. What is the maximum score you can achieve? Input A single line contains a single integer n (2 ≀ n ≀ 100 000) β€” the given integer described above. Output Print an only integer β€” the maximum score that can be achieved with the transformations. If it is not possible to perform even a single transformation for all possible starting integers, print 0. Examples Input 4 Output 8 Input 6 Output 28 Input 2 Output 0 Note In the first example, the transformations are 2 β†’ 4 β†’ (-2) β†’ (-4) β†’ 2. In the third example, it is impossible to perform even a single transformation.
instruction
0
62,294
20
124,588
Tags: dfs and similar, graphs, implementation, math Correct Solution: ``` # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! from sys import stdin, stdout import collections N = int(input()) #s = input() #N,Q = [int(x) for x in stdin.readline().split()] res = 0 for m in range(2,N+1): d = N//m d -= 1 res += d*m if d==0: break print(4*res) ```
output
1
62,294
20
124,589
Provide tags and a correct Python 3 solution for this coding contest problem. Andrey's favourite number is n. Andrey's friends gave him two identical numbers n as a New Year present. He hung them on a wall and watched them adoringly. Then Andrey got bored from looking at the same number and he started to swap digits first in one, then in the other number, then again in the first number and so on (arbitrary number of changes could be made in each number). At some point it turned out that if we sum the resulting numbers, then the number of zeroes with which the sum will end would be maximum among the possible variants of digit permutations in those numbers. Given number n, can you find the two digit permutations that have this property? Input The first line contains a positive integer n β€” the original number. The number of digits in this number does not exceed 105. The number is written without any leading zeroes. Output Print two permutations of digits of number n, such that the sum of these numbers ends with the maximum number of zeroes. The permutations can have leading zeroes (if they are present, they all should be printed). The permutations do not have to be different. If there are several answers, print any of them. Examples Input 198 Output 981 819 Input 500 Output 500 500
instruction
0
62,477
20
124,954
Tags: greedy Correct Solution: ``` import itertools def countZeroes(s): ret = 0 for i in s: if i != '0': break ret += 1 return ret def stupid(n): ansMax = 0 bn1 = n bn2 = n for n1 in itertools.permutations(n): for n2 in itertools.permutations(n): val = str(int(''.join(n1)) + int(''.join(n2)))[::-1] cnt = countZeroes(val) if cnt > ansMax: ansMax = cnt bn1 = ''.join(n1) bn2 = ''.join(n2) return (bn1, bn2) def solution(n): ansMax = n.count('0') bestN1 = n.replace('0', '') + ansMax * '0' bestN2 = n.replace('0', '') + ansMax * '0' for i in range(1, 10): cnt1 = [n.count(str(j)) for j in range(10)] cnt2 = [n.count(str(j)) for j in range(10)] if cnt1[i] == 0 or cnt2[10 - i] == 0: continue cnt1[i] -= 1 cnt2[10 - i] -= 1 curN1 = str(i) curN2 = str(10 - i) ansCur = 1 for j in range(10): addend = min(cnt1[j], cnt2[9 - j]) ansCur += addend cnt1[j] -= addend cnt2[9 - j] -= addend curN1 += str(j) * addend curN2 += str(9 - j) * addend if cnt1[0] > 0 and cnt2[0] > 0: addend = min(cnt1[0], cnt2[0]) ansCur += addend cnt1[0] -= addend cnt2[0] -= addend curN1 = '0' * addend + curN1 curN2 = '0' * addend + curN2 if ansCur > ansMax: ansMax = ansCur f = lambda x: str(x[0]) * x[1] bestN1 = ''.join(map(f, enumerate(cnt1))) + curN1[::-1] bestN2 = ''.join(map(f, enumerate(cnt2))) + curN2[::-1] return (bestN1, bestN2) n = input() print('\n'.join(solution(n))) ```
output
1
62,477
20
124,955
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Andrey's favourite number is n. Andrey's friends gave him two identical numbers n as a New Year present. He hung them on a wall and watched them adoringly. Then Andrey got bored from looking at the same number and he started to swap digits first in one, then in the other number, then again in the first number and so on (arbitrary number of changes could be made in each number). At some point it turned out that if we sum the resulting numbers, then the number of zeroes with which the sum will end would be maximum among the possible variants of digit permutations in those numbers. Given number n, can you find the two digit permutations that have this property? Input The first line contains a positive integer n β€” the original number. The number of digits in this number does not exceed 105. The number is written without any leading zeroes. Output Print two permutations of digits of number n, such that the sum of these numbers ends with the maximum number of zeroes. The permutations can have leading zeroes (if they are present, they all should be printed). The permutations do not have to be different. If there are several answers, print any of them. Examples Input 198 Output 981 819 Input 500 Output 500 500 Submitted Solution: ``` n = input() zeroes = n.count('0') n = n.replace('0', '') ansMax = 0 bestN1 = n bestN2 = n for i in range(1, 10): cnt1 = [0] + [n.count(str(j)) for j in range(1, 10)] cnt2 = [0] + [n.count(str(j)) for j in range(1, 10)] if cnt1[i] == 0 or cnt2[10 - i] == 0: continue cnt1[i] -= 1 cnt2[10 - i] -= 1 curN1 = str(i) curN2 = str(10 - i) ansCur = 1 for j in range(10): addend = min(cnt1[j], cnt2[9 - j]) ansCur += addend cnt1[j] -= addend cnt2[9 - j] -= addend curN1 += str(j) * addend curN2 += str(9 - j) * addend if ansCur > ansMax: ansMax = ansCur f = lambda x: str(x[0]) * x[1] bestN1 = ''.join(map(f, enumerate(cnt1))) + curN1[::-1] bestN2 = ''.join(map(f, enumerate(cnt2))) + curN2[::-1] print(bestN1 + '0' * zeroes) print(bestN2 + '0' * zeroes) ```
instruction
0
62,478
20
124,956
No
output
1
62,478
20
124,957
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect. The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him! Input The first line contains two integers, n and k (0 ≀ k < n ≀ 105). The second line contains a string consisting of n digits. Output Print the answer to the problem modulo 109 + 7. Examples Input 3 1 108 Output 27 Input 3 2 108 Output 9 Note In the first sample the result equals (1 + 08) + (10 + 8) = 27. In the second sample the result equals 1 + 0 + 8 = 9.
instruction
0
62,724
20
125,448
Tags: combinatorics, dp, math, number theory Correct Solution: ``` n, k = map(int, input().split()) t = list(map(int, input())) p, d = 1, 10 ** 9 + 7 s, f = 0, [1] * n for i in range(2, n): f[i] = (i * f[i - 1]) % d c = lambda a, b: 0 if a > b else (f[b] * pow(f[a] * f[b - a], d - 2, d)) % d if k: u = [0] * (n + 1) p = [1] * (n + 1) for i in range(n): u[i] = (p[i] * c(k - 1, n - 2 - i) + u[i - 1]) % d p[i + 1] = (10 * p[i]) % d for i in range(n): v = u[n - 2 - i] + p[n - 1 - i] * c(k, i) s = (s + t[i] * v) % d else: for i in t: s = (s * 10 + i) % d print(s) ```
output
1
62,724
20
125,449
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect. The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him! Input The first line contains two integers, n and k (0 ≀ k < n ≀ 105). The second line contains a string consisting of n digits. Output Print the answer to the problem modulo 109 + 7. Examples Input 3 1 108 Output 27 Input 3 2 108 Output 9 Note In the first sample the result equals (1 + 08) + (10 + 8) = 27. In the second sample the result equals 1 + 0 + 8 = 9.
instruction
0
62,725
20
125,450
Tags: combinatorics, dp, math, number theory Correct Solution: ``` n, k = map(int, input().split()) t = list(map(int, input())) p, d = 1, 10**9 + 7 s = 0 f = [1] * n for i in range(2, n): f[i] = (i * f[i - 1]) % d c = lambda a, b: 0 if a > b else (f[b] * pow(f[a] * f[b -a], d - 2, d)) % d if k: u = [0] * (n + 1) p = [1] * (n + 1) for i in range(n): u[i] = (p[i] * c(k - 1, n - 2 - i) + u[i - 1]) % d p[i + 1] = (10 * p[i]) % d for i in range(n): v = u[n - 2 - i] + p[n - 1 - i] * c(k,i) s = (s + t[i] * v) % d else: for i in t: s = (s * 10 + i) % d print(s) #GG ```
output
1
62,725
20
125,451
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect. The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him! Input The first line contains two integers, n and k (0 ≀ k < n ≀ 105). The second line contains a string consisting of n digits. Output Print the answer to the problem modulo 109 + 7. Examples Input 3 1 108 Output 27 Input 3 2 108 Output 9 Note In the first sample the result equals (1 + 08) + (10 + 8) = 27. In the second sample the result equals 1 + 0 + 8 = 9.
instruction
0
62,726
20
125,452
Tags: combinatorics, dp, math, number theory Correct Solution: ``` n, k = map(int, input().split()) t = list(map(int, input())) p, d = 1, 10 ** 9 + 7 s, f = 0, [1] * n for i in range(2, n): f[i] = (i * f[i - 1]) % d c = lambda a, b: 0 if a > b else (f[b] * pow(f[a] * f[b - a], d - 2, d)) % d if k: u = [0] * (n + 1) p = [1] * (n + 1) for i in range(n): u[i] = (p[i] * c(k - 1, n - 2 - i) + u[i - 1]) % d p[i + 1] = (10 * p[i]) % d for i in range(n): v = u[n - 2 - i] + p[n - 1 - i] * c(k, i) s = (s + t[i] * v) % d else: for i in t: s = (s * 10 + i) % d print(s) # Made By Mostafa_Khaled ```
output
1
62,726
20
125,453
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect. The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him! Input The first line contains two integers, n and k (0 ≀ k < n ≀ 105). The second line contains a string consisting of n digits. Output Print the answer to the problem modulo 109 + 7. Examples Input 3 1 108 Output 27 Input 3 2 108 Output 9 Note In the first sample the result equals (1 + 08) + (10 + 8) = 27. In the second sample the result equals 1 + 0 + 8 = 9.
instruction
0
62,727
20
125,454
Tags: combinatorics, dp, math, number theory Correct Solution: ``` n, k = map(int, input().split()) t = list(map(int, input())) s = 0 d = 10 ** 9 + 7 f = [1] * n for i in range(2, n): f[i] = (i * f[i - 1]) % d c = lambda a, b: 0 if a > b else (f[b] * pow(f[a] * f[b - a], d - 2, d)) % d if k: p = [1] * (n + 1) for i in range(n): p[i + 1] = (10 * p[i]) % d x = [p[i] * c(k - 1, n - 2 - i) for i in range(n + 1)] for i in range(n): x[i] = (x[i] + x[i - 1]) % d for i in range(n): y = x[n - 2 - i] + p[n - 1 - i] * c(k, i) s = (s + t[i] * y) % d else: for i in t: s = (s * 10 + i) % d print(s) ```
output
1
62,727
20
125,455
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect. The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him! Input The first line contains two integers, n and k (0 ≀ k < n ≀ 105). The second line contains a string consisting of n digits. Output Print the answer to the problem modulo 109 + 7. Examples Input 3 1 108 Output 27 Input 3 2 108 Output 9 Note In the first sample the result equals (1 + 08) + (10 + 8) = 27. In the second sample the result equals 1 + 0 + 8 = 9. Submitted Solution: ``` n, k = map(int, input().split()) h = input() t = list(map(int, h)) d = 10 ** 9 + 7 s, f = 0, [1] * n for i in range(2, n): f[i] = (i * f[i - 1]) % d if k > 1: for i in range(1, n - 1): x = 0 for j in range(i, n - 1): x = (10 * x + t[j]) % d b = n - 3 - j + i a = k - 2 if b < a: break c = (f[b] * pow(f[a] * f[b - a], d - 2, d)) % d s = (s + c * x) % d x = 0 for j in range(n - 1): x = (10 * x + t[j]) % d b = n - 2 - j a = k - 1 if b < a: break c = (f[b] * pow(f[a] * f[b - a], d - 2, d)) % d s = (s + c * x) % d for j in range(k, n): b = j - 1 a = k - 1 x = int(h[j:]) % d c = (f[b] * pow(f[a] * f[b - a], d - 2, d)) % d s = (s + c * x) % d print(s) ```
instruction
0
62,728
20
125,456
No
output
1
62,728
20
125,457
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect. The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him! Input The first line contains two integers, n and k (0 ≀ k < n ≀ 105). The second line contains a string consisting of n digits. Output Print the answer to the problem modulo 109 + 7. Examples Input 3 1 108 Output 27 Input 3 2 108 Output 9 Note In the first sample the result equals (1 + 08) + (10 + 8) = 27. In the second sample the result equals 1 + 0 + 8 = 9. Submitted Solution: ``` i, dlina = 0, 0 mas = [0] * 999 pluspos = [] def r(n, k): if n == 1: mas[n + 1] = k res = [] res.append(0) for i in range(1, dlina + 1): if mas[i + 1] == 1: res.append(i) res.append(dlina + 1) pluspos.append(res) else: mas[n + 1] = 0 if n - 1 >= k: r(n - 1, k) mas[n + 1] = 1 if k - 1 >= 0: r(n - 1, k - 1) n, k = [int(i) for i in input().split()] inp = str(input()) res = 0 dlina = n - 1 r(dlina, k) for i in range(len(pluspos)): for j in range(k + 1): res += int(inp[pluspos[i][j] : pluspos[i][j + 1]]) print(res % (10 ** 9 + 7)) ```
instruction
0
62,729
20
125,458
No
output
1
62,729
20
125,459
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect. The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him! Input The first line contains two integers, n and k (0 ≀ k < n ≀ 105). The second line contains a string consisting of n digits. Output Print the answer to the problem modulo 109 + 7. Examples Input 3 1 108 Output 27 Input 3 2 108 Output 9 Note In the first sample the result equals (1 + 08) + (10 + 8) = 27. In the second sample the result equals 1 + 0 + 8 = 9. Submitted Solution: ``` n, k = input().split() def f(t, k): if not k: return int(t), 1 S = N = 0 for i in range(len(t) - k): s, n = f(t[i + 1:], k - 1) S += int(t[:i + 1]) * n + s N += n return S, N print(f(input(), int(k))[0]) ```
instruction
0
62,730
20
125,460
No
output
1
62,730
20
125,461
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect. The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him! Input The first line contains two integers, n and k (0 ≀ k < n ≀ 105). The second line contains a string consisting of n digits. Output Print the answer to the problem modulo 109 + 7. Examples Input 3 1 108 Output 27 Input 3 2 108 Output 9 Note In the first sample the result equals (1 + 08) + (10 + 8) = 27. In the second sample the result equals 1 + 0 + 8 = 9. Submitted Solution: ``` n, k = map(int, input().split()) h = input() t = list(map(int, h)) d = 10 ** 9 + 7 s, f = 0, [1] * n for i in range(2, n): f[i] = (i * f[i - 1]) % d for i in range(1, n - 1): x = 0 for j in range(i, n - 1): x = (10 * x + t[j]) % d b = n - 3 - j + i a = k - 2 if b < a: break c = (f[b] * pow(f[a] * f[b - a], d - 2, d)) % d s = (s + c * x) % d x = 0 for j in range(n - 1): x = (10 * x + t[j]) % d b = n - 2 - j a = k - 1 if b < a: break c = (f[b] * pow(f[a] * f[b - a], d - 2, d)) % d s = (s + c * x) % d x = 0 for j in range(k, n): b = j - 1 a = k - 1 x = int(h[j:]) % d c = (f[b] * pow(f[a] * f[b - a], d - 2, d)) % d s = (s + c * x) % d print(s) ```
instruction
0
62,731
20
125,462
No
output
1
62,731
20
125,463
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.
instruction
0
62,825
20
125,650
Tags: brute force, implementation Correct Solution: ``` n, m = map(int, input().split()) N = set(map(int, input().split())) M = set(map(int, input().split())) mn = min(N) mm = min(M) N.intersection_update(M) if len(N) == 0: if mn < mm: print(str(mn) + str(mm)) elif mm < mn: print(str(mm) + str(mn)) else: print(min(N)) ```
output
1
62,825
20
125,651
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.
instruction
0
62,826
20
125,652
Tags: brute force, implementation Correct Solution: ``` _ = input() lst = [] for _ in range(2): lst.append(list(map(int, input().split()))) a = set(lst[0]) & set(lst[1]) num = [min(lst[0]), min(lst[1])] num.sort() if len(a) and (min(a) < (num[0] * 10 + num[1])): print(min(a)) elif num[0] == num[1]: print(num[0]) else: print(num[0] * 10 + num[1]) ```
output
1
62,826
20
125,653
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.
instruction
0
62,827
20
125,654
Tags: brute force, implementation Correct Solution: ``` n,m=map(int,input().split()) a=set(map(int,input().split())) b=set(map(int,input().split())) if a&b: print(min(a&b)) else: x=min(a) y=min(b) print(min(x,y)*10+max(x,y)) ```
output
1
62,827
20
125,655
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.
instruction
0
62,828
20
125,656
Tags: brute force, implementation Correct Solution: ``` a,b=input().split() a=int(a) b=int(b) c=input().split() m=input().split() min1=10000 min2=10000 k=[] for i in range(a): if(int(c[i])<=min1): min1=int(c[i]) for i in range(b): if(int(m[i])<=min2): min2=int(m[i]) fl=1 min=100 for i in range (b): for j in range(a): if(m[i]==c[j]): min1=int(m[i]) min2=int(m[i]) if (min1 == min2 and min > int(min1)): min = min1 if(min!=100): print(min,sep='',end='') elif(min1 == min2): print(min1,sep='',end='') elif(min1<min2): print(min1,min2,sep='',end='') else: print(min2,min1,sep='',end='') ```
output
1
62,828
20
125,657
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.
instruction
0
62,829
20
125,658
Tags: brute force, implementation Correct Solution: ``` n,m=map(int,input().split()) li=list(map(int,input().split())) li=sorted(li) li1=sorted(list(map(int,input().split()))) a=max(li1)*10+max(li) for i in li1: if i in li: a=i break a1=min(li) b1=min(li1) b=min((a1*10+b1),(b1*10+a1)) print(min(a, b)) ```
output
1
62,829
20
125,659
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.
instruction
0
62,830
20
125,660
Tags: brute force, implementation Correct Solution: ``` s = input().split(' ') l1 = int(s[0]) l2 = int(s[1]) s = input().split(' ') a1 = s for i in range(l1): a1[i] = int(a1[i]) a1.sort() s = input().split(' ') a2 = s for i in range(l2): a2[i] = int(a2[i]) a2.sort() for i in range(l1): for j in range(l2): if a1[i] == a2[j]: print(str(a1[i])) quit() print(str(min(a1[0],a2[0]))+str(max(a1[0],a2[0]))) ```
output
1
62,830
20
125,661
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.
instruction
0
62,831
20
125,662
Tags: brute force, implementation Correct Solution: ``` R = lambda:map(int,input().split()) n,m = R() a = set(input().split()) b = set(input().split()) print(min(a&b) if a&b else min(min(a)+min(b), min(b)+ min(a))) ```
output
1
62,831
20
125,663
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.
instruction
0
62,832
20
125,664
Tags: brute force, implementation Correct Solution: ``` n,m = map(int,input().split(' ')) n1 = input().split(' ') m1 = input().split(' ') lis = 100 for i in range(100): st = str(i) if len(st) == 1: if (st in m1) and (st in n1) and (i<lis): lis = i else: st1 = st[0] st2 = st[1] if (((st1 in n1) and (st2 in m1)) or ((st1 in m1) and (st2 in n1))) and (i<lis): lis = i print(lis) ```
output
1
62,832
20
125,665
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer. Submitted Solution: ``` def list_intersection(l1, l2): out = [] for i1 in l1: for i2 in l2: if i1 == i2: out.append(i1) out.sort() return out n, m = [int(i) for i in input().split()] a_s = [int(i) for i in input().split()] a_s.sort() a = a_s[0] b_s = [int(i) for i in input().split()] b_s.sort() b = b_s[0] if a == b: print(a) else: intersection = list_intersection(a_s, b_s) if intersection != []: print(intersection[0]) elif a > b: print(str(b)+str(a)) elif a < b: print(str(a)+str(b)) ```
instruction
0
62,833
20
125,666
Yes
output
1
62,833
20
125,667
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer. Submitted Solution: ``` def nonZero(a, b): a.sort() for i in a: if b.count(i) > 0: return i return min(min(a) * 10 + min(b), min(b) * 10 + min(a)) nm = [int(i) for i in input().split()] a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] print(nonZero(a, b)) ```
instruction
0
62,834
20
125,668
Yes
output
1
62,834
20
125,669
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer. Submitted Solution: ``` n, m = map(int, input().split()) nas = list(map(int, input().split())) mas = list(map(int, input().split())) minn = [] minn.append(10) minn.append(15) h = 0 g = 0 for i in range(n): for j in range(m): if nas[i] == mas[j]: minn.append(nas[i]) if min(minn) != 10 : print(min(minn)) else: if min(nas) >= min(mas): h = min(mas) g = min(nas) else: h = min(nas) g = min(mas) print(h*10 + g) ```
instruction
0
62,835
20
125,670
Yes
output
1
62,835
20
125,671
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer. Submitted Solution: ``` #!/usr/bin/env python3 def m(s1, s2): si = s1.intersection(s2) if si: return min(si) smin, smax = sorted(map(min, (s1, s2))) return smin * 10 + smax if __name__ == "__main__": input() print(m(*(set(map(int, input().split())) for i in range(2)))) ```
instruction
0
62,836
20
125,672
Yes
output
1
62,836
20
125,673
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer. Submitted Solution: ``` n, m = list(map(int, input().split())) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) n1 = min(l1) n2 = min(l2) if n1 == n2: print(n1) elif n1 > n2: print(str(n2)+str(n1)) else: print(str(n1)+str(n2)) ```
instruction
0
62,837
20
125,674
No
output
1
62,837
20
125,675
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer. Submitted Solution: ``` n, m = input().split() n = input().split() m = input().split() a = [] for i in n: for j in m: if i == j: a.append(int(i)) else: a.append(int(i+j)) print(min(a)) ```
instruction
0
62,838
20
125,676
No
output
1
62,838
20
125,677
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer. Submitted Solution: ``` a=[] b=[] n,m=map(int,input().split()) a=input().split() for i in range(n): a[i]=int(a[i]) k=min(a) b=input().split() for i in range(m): b[i]=int(b[i]) d=min(b) """for i in range(n): if a[i]<k: k=a[i] for i in range(m): if b[i]<d: d=b[i]""" if d==k: print(d) if d>k: print(10*k+d) if k>d: print(10*d+k) ```
instruction
0
62,839
20
125,678
No
output
1
62,839
20
125,679
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two lists of non-zero digits. Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers n and m (1 ≀ n, m ≀ 9) β€” the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≀ ai ≀ 9) β€” the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≀ bi ≀ 9) β€” the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer. Submitted Solution: ``` n,m=[int(i) for i in input().split()] a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] e=min(a) f=min(b) if(e!=f): if(e<f): print(str(e)+str(f)) elif(e>f): print(str(f)+str(e)) else: print(e) ```
instruction
0
62,840
20
125,680
No
output
1
62,840
20
125,681
Provide tags and a correct Python 3 solution for this coding contest problem. You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1. For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. <image> Input The first and the only line contain one integer n (1 ≀ n ≀ 10^5). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output 2n numbers β€” numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them. Examples Input 3 Output YES 1 4 5 2 3 6 Input 4 Output NO Note Example from the statement is shown for the first example. It can be proved that there is no solution in the second example.
instruction
0
63,221
20
126,442
Tags: constructive algorithms, greedy, math Correct Solution: ``` from sys import stdin,stdout for _ in range(1):#int(stdin.readline())): n=int(stdin.readline()) # a=list(map(int,stdin.readline().split())) a=[0]*(2*n) l,r=1,2*n for i in range(n): if i&1==0: a[i]=l a[i+n]=l+1 l+=2 else: a[i]=r a[i+n]=r-1 r-=2 if a[2*n-1]>a[2*n-1-n]: print('YES') print(*a) else:print('NO') ```
output
1
63,221
20
126,443
Provide tags and a correct Python 3 solution for this coding contest problem. You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1. For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. <image> Input The first and the only line contain one integer n (1 ≀ n ≀ 10^5). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output 2n numbers β€” numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them. Examples Input 3 Output YES 1 4 5 2 3 6 Input 4 Output NO Note Example from the statement is shown for the first example. It can be proved that there is no solution in the second example.
instruction
0
63,222
20
126,444
Tags: constructive algorithms, greedy, math Correct 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 def main(): n = int(input()) if n % 2 == 0: print('NO') return sol = [0] * (2 * n) sol[0] = 1 sol[n] = 2 for i in range(1, n): if i % 2: sol[n - i] = (i * 2) + 1 sol[-i] = (i * 2) + 2 else: sol[-i] = (i * 2) + 1 sol[n - i] = (i * 2) + 2 print("YES") print(*sol) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") 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() ```
output
1
63,222
20
126,445
Provide tags and a correct Python 3 solution for this coding contest problem. You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1. For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. <image> Input The first and the only line contain one integer n (1 ≀ n ≀ 10^5). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output 2n numbers β€” numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them. Examples Input 3 Output YES 1 4 5 2 3 6 Input 4 Output NO Note Example from the statement is shown for the first example. It can be proved that there is no solution in the second example.
instruction
0
63,227
20
126,454
Tags: constructive algorithms, greedy, math Correct Solution: ``` import sys import math import random input = sys.stdin.readline n = int(input()) res = [0] * (2*n) i = 0 if n%2 == 0: print("NO") exit() value = 1 #res[0] = 1 i = 0 while(True): if res[i] != 0 or i>=n or value >= 2*n: break res[i] = value res[n + i] = res[i] + 1 if n + i + 1 >= 2*n: break res[n + i + 1] = res[n + i] + 1 res[i + 1] = res[n + i + 1] + 1 value = value + 4 i += 2 #print(i, n, value) #print(res) print('YES') for i in res: print(i, end=" ") ```
output
1
63,227
20
126,455
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7
instruction
0
63,354
20
126,708
Tags: dp, greedy, shortest paths Correct Solution: ``` memo = {} def solve (n): if n<=6 : return n elif n<=11: return 2+(11-n) elif n in memo: return memo[n] else: digits = [int(c) for c in str(n)] s = len(digits) Ln = (10**s)//9 if (n==Ln): return s if n<Ln: Ln = Ln//10 s -=1 Un = 10*Ln +1 ## Ln < n < Un and Ln and Un digits are ones # s is the number of ones in the lower bound Ln # possibility 1: n = q Ln + r ==> q s + number for (r) # possibility 2: n = Un - (Un - n) # and Ln <= Un - n < Un ==> solve for (Un - n) len1 = solve (n%Ln) + s*(n//Ln) n2 = Un-n len2 = solve (n2%Ln) + s*(n2//Ln)+ s+1 ans = min(len1,len2) memo[n]=ans return ans n = int(input()) print(solve(n)) ```
output
1
63,354
20
126,709
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7
instruction
0
63,355
20
126,710
Tags: dp, greedy, shortest paths Correct Solution: ``` #!/usr/bin/env python import os import sys from io import BytesIO, IOBase def func(n): # print("n", n) if n < 0: raise Exception("Wrong") if n <= 6: return n length = len(str(n)) # print("length", length) big1 = int("1" * length) if n <= big1: return length + func(big1 - n) big5 = int("5" * length) big6 = int("6" * length) if n <= big5: # print("down") return (n // big1) * length + func(n % big1) else: big11 = int("1" * (length + 1)) if n >= big6: factor = (big11 - n) // big1 # print("up", length, factor, length + 1 + factor * length) return length + 1 + factor * length + func(big11 - factor * big1 - n) return min( 5 * length + func(n - big5), 5 * length + 1 + func(big11 - 4 * big1 - n) ) def main(): n = int(parse_input()) print(func(n)) # region fastio # BUFSIZE = 8192 # class FastIO(IOBase): # newlines = 0 # def __init__(self, file): # self._fd = file.fileno() # self.buffer = BytesIO() # self.writable = "x" in file.mode or "r" not in file.mode # self.write = self.buffer.write if self.writable else None # def read(self): # while True: # b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) # if not b: # break # ptr = self.buffer.tell() # self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) # self.newlines = 0 # return self.buffer.read() # def readline(self): # while self.newlines == 0: # b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) # self.newlines = b.count(b"\n") + (not b) # ptr = self.buffer.tell() # self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) # self.newlines -= 1 # return self.buffer.readline() # def flush(self): # if self.writable: # os.write(self._fd, self.buffer.getvalue()) # self.buffer.truncate(0), self.buffer.seek(0) # class IOWrapper(IOBase): # def __init__(self, file): # self.buffer = FastIO(file) # self.flush = self.buffer.flush # self.writable = self.buffer.writable # self.write = lambda s: self.buffer.write(s.encode("ascii")) # self.read = lambda: self.buffer.read().decode("ascii") # self.readline = lambda: self.buffer.readline().decode("ascii") # sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) parse_input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
output
1
63,355
20
126,711
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7
instruction
0
63,356
20
126,712
Tags: dp, greedy, shortest paths Correct Solution: ``` import sys def read_ints(): return [int(i) for i in sys.stdin.readline().strip().split()] def read_int(): return int(sys.stdin.readline().strip()) CACHE = {} def n_ones(n): if n < 5: return n if n in CACHE: return CACHE[n] n = abs(n) s = str(n) ones = int("1" * len(s)) dig = int(s[0]) guess = ones * dig if n == ones: return len(s) elif guess <= n: below = guess if dig == 9: above = int("1" * (len(s) + 1)) else: above = guess + ones else: above = guess if dig == 1: below = int("9" * (len(s) - 1)) else: below = guess - ones above_digit = int(str(above)[0]) below_digit = int(str(below)[0]) #print("n: %d above2: %d" % (n, above2)) below_ones = below_digit * len(str(below)) + n_ones(n - below) above_ones = above_digit * len(str(above)) + n_ones(above - n) #print("n %d above %d below %d above_ones %d below_ones %d" % (n, above, below, above_ones, below_ones)) result = min(below_ones, above_ones) if dig > 5: above2 = int("1" * (len(s) + 1)) above2_digit = int(str(above2)[0]) above2_ones = above2_digit * len(str(above2)) + n_ones(above2 - n) result = min(result, above2_ones) CACHE[n] = result return result """ assert n_ones(1) == 1 assert n_ones(2) == 2 assert n_ones(5) == 5 assert n_ones(9) == 4 # 11 - 1 - 1 assert n_ones(24) == 6 # 11 + 11 + 1 + 1 assert n_ones(97) == 8 # 111 - 11 - 1 - 1 - 1 assert n_ones(98) == 7 # 111 - 11 - 1 - 1 assert n_ones(99) == 6 # 111 - 11 - 1 assert n_ones(111) == 3 # 111 assert n_ones(102) == 7 # 111 - 11 + 1 + 1 assert n_ones(529) == 23 """ n = read_int() print(n_ones(n)) ```
output
1
63,356
20
126,713
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7
instruction
0
63,357
20
126,714
Tags: dp, greedy, shortest paths Correct Solution: ``` #!/usr/bin/python import sys def solve(N): strN = str(N) no_digits = len(strN) res_list = [] if int(strN[0]) < 8: res_list.append((0, N)) if int(strN[0]) > 4: felso = int('1' * (no_digits + 1)) res_list.append((no_digits + 1, felso - N)) for i in range(no_digits): res_list_copy = [r for r in res_list] res_list = [] my_num = no_digits - i oneone = int('1' * my_num) for steps, remainder in res_list_copy: also = remainder // oneone felso = also + 1 res_list.append((steps + also * my_num, remainder - oneone * also)) res_list.append((steps + felso * my_num, oneone * felso - remainder)) steps = [s[0] for s in res_list] step_min = min(steps) step_max =(my_num + 1) * 6 + step_min res_list = [r for r in res_list if r[0] < step_max] res_list = [x - i for (x,i) in res_list] return (min(res_list)) def run(): out = "" N = int(input()) print(solve(N)) run() ```
output
1
63,357
20
126,715
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7
instruction
0
63,358
20
126,716
Tags: dp, greedy, shortest paths Correct Solution: ``` import math from collections import defaultdict n = input() opt = defaultdict(lambda: 1e9) def calc(s, cost): if cost >= opt[s]: return 1e9 opt[s] = cost if cost + min(int(s[0]),4)*len(s) > opt['0']: return 1e9 if s=='0': return cost if s[0]<'6': return calc( str(abs(int(s) - int('1'*len(s)))), cost + len(s)) if s[0]>'6': return calc( str(int('1'*(len(s)+1)) - int(s)), cost + len(s)+1) a = calc( str(abs(int(s) - int('1'*len(s)))), cost + len(s)) b = calc( str(int('1'*(len(s)+1)) - int(s)), cost + len(s)+1) return min(a,b) calc(n,0) print(opt['0']) ```
output
1
63,358
20
126,717
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7
instruction
0
63,359
20
126,718
Tags: dp, greedy, shortest paths Correct Solution: ``` DIGITS = 55 MAX_CNT = 6 CUTOFF_RATIO = 0.618 NUMBERS = [0] * (DIGITS + 1) CUTOFFS = [0] * (DIGITS + 1) for d in range(1, DIGITS + 1): NUMBERS[d] = 10 * NUMBERS[d - 1] + 1 CUTOFFS[d] = CUTOFF_RATIO * 10**(d - 1) + 15 X = int(input()) N = int(len(str(X))) candidates = {X: 0} for d in range(N + 1, 0, -1): ncandidates = {} def insert(value, cost): if value not in ncandidates or cost < ncandidates[value]: ncandidates[value] = cost for value in candidates: cost = candidates[value] for cnt in range(MAX_CNT + 1): nvalue = abs(value - cnt * NUMBERS[d]) if nvalue <= CUTOFFS[d]: insert(nvalue, cost + cnt * d) candidates = ncandidates print(candidates[0]) ```
output
1
63,359
20
126,719
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7
instruction
0
63,360
20
126,720
Tags: dp, greedy, shortest paths Correct Solution: ``` import math from collections import defaultdict n = input() opt = defaultdict(lambda: 1e9) def calc(s, cost): if cost >= opt[s]: return 1e9 opt[s] = cost if cost + min(int(s[0]),4)*len(s) > opt['0']: return 1e9 if s=='0': return cost if s[0]<'4': return calc( str(abs(int(s) - int('1'*len(s)))), cost + len(s)) if s[0]>'6': return calc( str(int('1'*(len(s)+1)) - int(s)), cost + len(s)+1) a = calc( str(abs(int(s) - int('1'*len(s)))), cost + len(s)) b = calc( str(int('1'*(len(s)+1)) - int(s)), cost + len(s)+1) return min(a,b) calc(n,0) print(opt['0']) ```
output
1
63,360
20
126,721
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7
instruction
0
63,361
20
126,722
Tags: dp, greedy, shortest paths Correct Solution: ``` n = int(input()) res={} def cal(x): if x==0: return 0 if x in res.keys(): return res[x] larger = 1 while x>=larger: larger = larger*10+1 smaller = larger//10 l = len(str(smaller)) a = cal( x%smaller ) + (x//smaller) * l b = cal( (larger-x)%smaller) + l+1 + ((larger - x) // smaller) * l res[x] = min(a,b) return res[x] print(cal(n)) ```
output
1
63,361
20
126,723
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7 Submitted Solution: ``` n = int(input().strip()) a = {} def decom(i): if i == 0: return 0 if i == 1: return 1 if i in a.keys(): return a[i] if upper(i)[0] - i > i: return decom(i - lower(i)[0]) + lower(i)[1] else: k = min(decom(i - lower(i)[0]) + lower(i)[1], decom(upper(i)[0] - i) + upper(i)[1]) a[i] = k return k def upper(i): d = 1 n = 1 while d < i: d = d * 10 + 1 n += 1 return (d, n) def lower(i): if (upper(i)[0] - 1) // 10 == 0: return (1, 1) return ((upper(i)[0] - 1) // 10, upper(i)[1] - 1) print(decom(n)) ```
instruction
0
63,362
20
126,724
Yes
output
1
63,362
20
126,725
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7 Submitted Solution: ``` ''' Author: Wa-Automaton Time: 202102162 14:44:00 UTC+8 Website: codeforces.com Contest: Educational Codeforces Round 104 (Rated for Div. 2) Problem: F. Ones Result: Solution: More: Thanks RUSH_D_CAT. Learn it from RUSH_D_CAT's solution 107487575. ''' n = int(input()) dp = {} def calc(x): # print(x) # if x < 10 : return x # wa if 0 == x : return 0 # ok if x in dp.keys() : return dp[x] l = len(str(x)) u = 10 ** l // 9 if x < u : u = u // 10; l -= 1 v = u * 10 + 1 u1 = calc(x % u) + (x // u * l) # u2 = calc(v - x) + l + 1 # tle u2 = calc((v - x) % u) + (l + 1) + ((v - x) // u * l) # ok res = min(u1, u2) dp[x] = res # print(x, u, v, u1, u2, res) return res print(calc(n)) ```
instruction
0
63,363
20
126,726
Yes
output
1
63,363
20
126,727
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7 Submitted Solution: ``` import sys, os if os.environ['USERNAME']=='kissz': s='4897332836602749275099151826428737446171648588383' #s='34035589464304102353337687153068150731986217146' #s='102' #s='9105539' def debug(*args): print(*args,file=sys.stderr) sys.stderr.flush() else: s=input().strip() def debug(*args): pass # SCRIPT STARTS HERE def sub(x,y): n=len(x) m=len(y) assert(n==m) z=[0]*n greater=0 for a,b in zip(x,y): if a>b: greater=1 break elif a<b: greater=-1 break if greater==0: return z elif greater==-1: x,y=y,x c=0 for i in range(max(n,m)-1,-1,-1): a,b=x[i],y[i] b=b+c z[i]=(a-b)%10 c=(a<b) return z x0=[int(c) for c in s] while x0[0]==0: x0.pop(0) x0=[0]+x0 n=len(x0) o=0 best=1<<30 visit=0 Q=[(0,n,x0)] while Q: r,l,x=Q.pop() visit+=1 if l==0: debug(best,len(Q),visit) continue if r>=best: continue zlim=9 for a in range(x[0]-1,x[0]+2): z=sub([a]*l,x) if l==1 and z[0]==0: best=min(best,r+a) elif z[0]==0 and z[1]<=zlim: zlim=z[1] Q.append((r+a*l,l-1,z[1:])) debug(visit) print(best) ```
instruction
0
63,364
20
126,728
Yes
output
1
63,364
20
126,729
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7 Submitted Solution: ``` n = int(input().strip()) a = {} temp = {} def decom(i): if i == 0: return 0 if i == 1: return 1 if i in a.keys(): return a[i] if i in temp: return decom(i - lower(i)[0]) + lower(i)[1] else: temp[i] = 0 k = min(decom(i - lower(i)[0]) + lower(i)[1], decom(upper(i)[0] - i) + upper(i)[1]) a[i] = k return k def upper(i): d = 1 n = 1 while d < i: d = d * 10 + 1 n += 1 return (d, n) def lower(i): if (upper(i)[0] - 1) // 10 == 0: return (1, 1) return ((upper(i)[0] - 1) // 10, upper(i)[1] - 1) print(decom(n)) ```
instruction
0
63,365
20
126,730
Yes
output
1
63,365
20
126,731
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7 Submitted Solution: ``` import sys line = sys.stdin.readline() n = int(line) ones = 0 nums = [1] for i in range(60): nums.append(10 * nums[-1] + 1) while (n > 0): cands = sorted([(abs(n - num), num, index) for index, num in enumerate(nums)]) best = cands[0][1] ones += cands[0][2] + 1 n -= best n = abs(n) print(ones) ```
instruction
0
63,366
20
126,732
No
output
1
63,366
20
126,733
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7 Submitted Solution: ``` divs = [] ones = [] def parse_num(num, level): # print('parse', num, level) if level == 0: return num if num == 0: return 0 base = num // ones[level] cnt = base * (level + 1) rest1 = num - base * ones[level] if rest1 == 0: return cnt if rest1 <= divs[level]: return parse_num(rest1, level - 1) + cnt else: return parse_num(ones[level] - rest1, level - 1) + cnt + level + 1 def find_divs(level): left, right = 0, ones[level] base_cnt = level + 1 while left < right: mid = left + (right - left + 1) // 2 # if level == 1: # print(left, right, mid, parse_num(left, level - 1), parse_num(ones[level] - mid, level - 1) + base_cnt) if parse_num(mid, level - 1) >= parse_num(ones[level] - mid, level - 1) + base_cnt: right = mid - 1 else: left = mid # if level == 1: # print(left, right, mid, parse_num(left, level - 1), parse_num(ones[level] - mid, level - 1) + base_cnt) return left # maxn = 1000 divs.append(0) ones.append(1) num = int(input()) while ones[-1] <= num: cur_lv = len(ones) ones.append(ones[-1] * 10 + 1) divs.append(find_divs(cur_lv)) # print(ones) # print(divs) print(parse_num(num, len(ones) - 1)) ```
instruction
0
63,368
20
126,736
No
output
1
63,368
20
126,737
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a positive (greater than zero) integer n. You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer n (1 ≀ n < 10^{50}). Output Print one integer x β€” the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total. Examples Input 24 Output 6 Input 102 Output 7 Submitted Solution: ``` ans = 0 ans2 = 0 s = str(input()) lnn = len(s) s2 = str(int((lnn+1)*'1')-int(s)) s = s[::-1] lnn2 = len(s2) s2 = s2[::-1] s = s +'0' s2 = s2 +'0' for j2 in range(lnn2): kk2 = j2+1 ans2 = ans2 + (abs(int(s2[j2])-int(s2[j2+1])) *kk2) for j in range(lnn): kk = j+1 ans = ans + (abs(int(s[j])-int(s[j+1])) *kk) print (min(ans,ans2+lnn+1)) ```
instruction
0
63,369
20
126,738
No
output
1
63,369
20
126,739
Provide tags and a correct Python 3 solution for this coding contest problem. You have number a, whose decimal representation quite luckily contains digits 1, 6, 8, 9. Rearrange the digits in its decimal representation so that the resulting number will be divisible by 7. Number a doesn't contain any leading zeroes and contains digits 1, 6, 8, 9 (it also can contain another digits). The resulting number also mustn't contain any leading zeroes. Input The first line contains positive integer a in the decimal record. It is guaranteed that the record of number a contains digits: 1, 6, 8, 9. Number a doesn't contain any leading zeroes. The decimal representation of number a contains at least 4 and at most 106 characters. Output Print a number in the decimal notation without leading zeroes β€” the result of the permutation. If it is impossible to rearrange the digits of the number a in the required manner, print 0. Examples Input 1689 Output 1869 Input 18906 Output 18690
instruction
0
63,472
20
126,944
Tags: math, number theory Correct Solution: ``` n = [int(i) for i in input()] for x in [1, 6, 8, 9]: for i in range(len(n)): if n[i] == x: del n[i] break prefix = [1869, 6189, 1689, 6198, 1698, 9861, 1896] res = sum([n[i] * pow(10, len(n)-i-1, 7) for i in range(len(n))]) print(prefix[-res * pow(10, 5*len(n), 7) % 7], end='') print(*n, sep='') ```
output
1
63,472
20
126,945