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.
A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.
The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.
Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.
If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.
Input
The first line of input contains n β a positive integer number without leading zeroes (1 β€ n < 10100000).
Output
Print one number β any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
Examples
Input
1033
Output
33
Input
10
Output
0
Input
11
Output
-1
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.
Submitted Solution:
```
import sys
n = list(input(""))
l = [int(c)%3 for c in n][::-1]
d = sum(l)%3
if (len(n) == 1):
if (n == ['3']):
print(3)
else:
print(-1)
sys.exit(0)
def li(x):
global n
del n[len(n)-1-l.index(x)]
del l[l.index(x)]
def cond(x):
x = x.lstrip('0')
if (len(x) == 0): return "0"
return x
if (d == 0):
pass
elif (d in l):
if (l.index(d) == len(l)-1 and int(n[1])==0 and l.count(3-d) >= 2):
li(3-d); li(3-d)
else:
li(d)
else:
li(3-d); li(3-d)
if (len(n) > 0):
q=cond("".join(n))
print(q)
else:
print(-1)
``` | instruction | 0 | 15,607 | 20 | 31,214 |
Yes | output | 1 | 15,607 | 20 | 31,215 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.
The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.
Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.
If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.
Input
The first line of input contains n β a positive integer number without leading zeroes (1 β€ n < 10100000).
Output
Print one number β any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
Examples
Input
1033
Output
33
Input
10
Output
0
Input
11
Output
-1
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.
Submitted Solution:
```
def f(t):
i, n = 0, len(t) - 1
while i < n and t[i] == '0': i += 1
return t[i:]
t = input()
n = len(t) - 1
p = [int(q) % 3 for q in t][::-1]
s = sum(p) % 3
if s == 0:
print(t)
exit()
u = v = ''
if s in p:
i = n - p.index(s)
u = f(t[:i] + t[i + 1:])
s = 3 - s
if p.count(s) > 1:
i = n - p.index(s)
j = n - p.index(s, n - i + 1)
v = f(t[:j] + t[j + 1:i] + t[i + 1:])
t = u if len(u) > len(v) else v
print(t if t else -1)
``` | instruction | 0 | 15,608 | 20 | 31,216 |
Yes | output | 1 | 15,608 | 20 | 31,217 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.
The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.
Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.
If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.
Input
The first line of input contains n β a positive integer number without leading zeroes (1 β€ n < 10100000).
Output
Print one number β any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
Examples
Input
1033
Output
33
Input
10
Output
0
Input
11
Output
-1
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.
Submitted Solution:
```
s = input()
def rm(s,m):
if s == None or len(s) == 1:
return None
i = len(s) - 1
while i >= 0:
if int(s[i]) % 3 == m:
break
i -= 1
if i == -1:
return None
else:
if i == 0:
k = i+1
while k < len(s) and s[k] == "0":
k += 1
if k == len(s):
return "0"
else:
return s[k:]
elif i == len(s)-1:
return s[:i]
else:
return s[:i] + s[i+1:]
def ans(s):
s_sum = 0
i = 0
while i<len(s):
s_sum += int(s[i])
s_sum = s_sum % 3
i += 1
if s_sum == 0:
return s
elif s_sum == 1:
s1 = rm(s,1)
s2 = rm(rm(s,2),2)
if s1 == None and s2 == None:
return -1
elif s1 == None:
return s2
elif s2 == None:
return s1
else:
if len(s1) > len(s2):
return s1
else:
return s2
elif s_sum == 2:
s1 = rm(s,2)
s2 = rm(rm(s,1),1)
if s1 == None and s2 == None:
return -1
elif s1 == None:
return s2
elif s2 == None:
return s1
else:
if len(s1) > len(s2):
return s1
else:
return s2
t = ans(s)
if t == None:
print(-1)
else:
print(t)
``` | instruction | 0 | 15,609 | 20 | 31,218 |
Yes | output | 1 | 15,609 | 20 | 31,219 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.
The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.
Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.
If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.
Input
The first line of input contains n β a positive integer number without leading zeroes (1 β€ n < 10100000).
Output
Print one number β any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
Examples
Input
1033
Output
33
Input
10
Output
0
Input
11
Output
-1
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.
Submitted Solution:
```
n = list(input())
leng = 0
a = 0
b = 0
for x in n:
leng += 1
if int(x)%3==1:a+=1
elif int(x)%3==2:b+=1
a1 = a%3
b1 = b%3
if abs(a-b)%3 > abs(a1-b1):a = a1;b = b1
count = abs(a-b)%3
if count >= leng:
print(-1)
exit()
index = leng-1
if a>b:
for x in range(count):
while int(n[index])%3 != 1:
index-=1
n[index] = ""
index -=1
else:
for x in range(count):
while int(n[index])%3 != 2:
index-=1
n[index] = ""
index -=1
ans = "".join(n)
if ans[0] == "0" and leng-count != 1:print(ans[1:]);exit()
print(ans)
``` | instruction | 0 | 15,610 | 20 | 31,220 |
No | output | 1 | 15,610 | 20 | 31,221 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.
The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.
Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.
If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.
Input
The first line of input contains n β a positive integer number without leading zeroes (1 β€ n < 10100000).
Output
Print one number β any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
Examples
Input
1033
Output
33
Input
10
Output
0
Input
11
Output
-1
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.
Submitted Solution:
```
def func(t):
an=""
l=len(t)
for i in range(l):
if(t[i]!="0"):
break
for j in range(i,l):
an+=t[i]
return an
for nitish in range(1):
# n,k,r=(map(int,input().strip().split(' ')))
s=input()
ss=0
n=len(s)
for i in range(n):
ss+=int(s[i])
if(ss%3==0):
print(s)
elif(ss%3==1):
ans=[]
for i in range(n-1,-1,-1):
if(int(s[i])%3==1):
ans.append(i)
break
ans1=[]
for i in range(n-1,-1,-1):
if(int(s[i])%3==2):
ans1.append(i)
if(len(ans1)==0):
break
if(len(ans)==0 and len(ans1)<2):
print(-1)
else:
cnt=10**20
cnt2=10**20
t=""
tt=""
if(len(ans)!=0):
t=""
cnt=1
for i in range(n):
if(i!=ans[0]):
t+=s[i]
for i in t:
if(i=="0"):
cnt+=1
else:
break
if(len(ans1)==2):
cnt1=2
tt=""
for i in range(n):
if i not in ans1:
tt+=s[i]
for i in tt:
if(i=="0"):
cnt1+=1
else:
break
if(t!="" and cnt<=cnt2):
an=func(t)
if(an!=""):
print(func(t))
else:
print(-1)
elif(tt!=""):
an=func(tt)
if(an!=""):
print(func(tt))
else:
print(-1)
else:
print(-1)
else:
ans=[]
for i in range(n-1,-1,-1):
if(int(s[i])%3==2):
ans.append(i)
break
ans1=[]
for i in range(n-1,-1,-1):
if(int(s[i])%3==1):
ans1.append(i)
if(len(ans1)==0):
break
if(len(ans)==0 and len(ans1)<2):
print(-1)
else:
cnt=10**20
cnt2=10**20
t=""
tt=""
if(len(ans)!=0):
t=""
cnt=1
for i in range(n):
if(i!=ans[0]):
t+=s[i]
for i in t:
if(i=="0"):
cnt+=1
else:
break
if(len(ans1)==2):
cnt1=2
tt=""
for i in range(n):
if i not in ans1:
tt+=s[i]
for i in tt:
if(i=="0"):
cnt1+=1
else:
break
if(t!="" and cnt<=cnt2):
an=func(t)
if(an!=""):
print(func(t))
else:
print(-1)
elif(tt!=""):
an=func(tt)
if(an!=""):
print(func(tt))
else:
print(-1)
else:
print(-1)
``` | instruction | 0 | 15,611 | 20 | 31,222 |
No | output | 1 | 15,611 | 20 | 31,223 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.
The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.
Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.
If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.
Input
The first line of input contains n β a positive integer number without leading zeroes (1 β€ n < 10100000).
Output
Print one number β any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
Examples
Input
1033
Output
33
Input
10
Output
0
Input
11
Output
-1
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.
Submitted Solution:
```
w = input()
a = [int(c) for c in w]
sm = sum(a)
def process_res(i, j=None):
k = 1 + bool(j is not None)
if (k >= len(a)):
return ""
else:
res = ""
non_zero = False
for n in range(len(a)):
if n != i and n != j:
if not non_zero and n == len(a) - 1 and a[n] == 0:
res += "0"
break
if not non_zero and a[n] != 0:
non_zero = True
res += str(a[n])
elif non_zero:
res += str(a[n])
return res
if sm % 3 == 0:
print(w)
else:
res1 = ''
if (sm % 3 == 1):
n1 = 1
n2 = 2
else:
n1 = 2
n2 = 1
for i in range(len(a) - 1, -1, -1):
if a[i] % 3 == n1:
res1 = process_res(i)
break
cnt = 0
frst = None
res2 = ''
for i in range(len(a) - 1, -1, -1):
if a[i] % 3 == n2:
cnt += 1
if (cnt == 2):
res2 = process_res(frst, i)
break
elif cnt == 1:
frst = i
if (res1 == '' and res2 == ''):
print(-1)
else:
if (len(res1) > len(res2)):
print(res1)
else:
print(res2)
``` | instruction | 0 | 15,612 | 20 | 31,224 |
No | output | 1 | 15,612 | 20 | 31,225 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.
The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.
Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.
If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.
Input
The first line of input contains n β a positive integer number without leading zeroes (1 β€ n < 10100000).
Output
Print one number β any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
Examples
Input
1033
Output
33
Input
10
Output
0
Input
11
Output
-1
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.
Submitted Solution:
```
a = input()
if len(a) == 1:
if int(a) % 3 == 0:
print(a)
else:
print(-1)
exit(0)
one = []
two = []
sum = 0
zs, zf = 0, 0
for i in range(len(a)):
q = int(a[i])
sum += q
if q == 0:
if zs == 0:
zs = i
else:
if zs != 0 and zf == 0:
zf = i
if q % 3 == 1:
if len(one) <= 4:
one.append(i)
elif q % 3 == 2:
if len(two) <= 4:
two.append(i)
if zf == len(a) - 1 :
zf = len(a) - 2
elif zf == 0 and zs != 0:
zf = len(a) - 1
if sum % 3 == 1:
if zs == 1:
if len(one) > 0:
if one[-1] == 0:
if len(two) > 1 and len(a) > 2:
if zf - zs + 1 > 2:
if two[-2] == 0:
print(a[zf:two[-1]] + a[two[-1] + 1:])
else:
print(a[:two[-2]] + a[two[-2] + 1:two[-1]] + a[two[-1] + 1:])
else:
print(a[zf:])
else:
print(a[:one[-1]] + a[one[-1] + 1:])
elif len(two) > 1 and len(a) > 2:
if two[-2] == 0:
print(a[zf:two[-1]] + a[two[-1] + 1:])
else:
print(a[:two[-2]] + a[two[-2] + 1:two[-1]] + a[two[-1] + 1:])
else:
print(-1)
elif zs == 2:
if len(one) > 0:
print(a[:one[-1]] + a[one[-1] + 1:])
elif len(two) > 1 and len(a) > 2:
if two[-2] == 0 and two[-1] == 1:
print(a[zf:])
else:
print(a[:two[-2]] + a[two[-2] + 1:two[-1]] + a[two[-1] + 1:])
else:
print(-1)
else:
if len(one) > 0:
print(a[:one[-1]] + a[one[-1] + 1:])
elif len(two) > 1 and len(a) > 2:
print(a[:two[-2]] + a[two[-2] + 1:two[-1]] + a[two[-1] + 1:])
else:
print(-1)
elif sum % 3 == 2:
if zs == 1:
if len(two) > 0:
if two[-1] == 0:
if len(one) > 1 and len(a) > 2:
if zf - zs + 1 > 2:
if one[-2] == 0:
print(a[zf:one[-1]] + a[one[-1] + 1:])
else:
print(a[:one[-2]] + a[one[-2] + 1:one[-1]] + a[one[-1] + 1:])
else:
print(a[zf:])
else:
print(a[:two[-1]] + a[two[-1] + 1:])
elif len(one) > 1 and len(a) > 2:
if one[-2] == 0:
print(a[zf:one[-1]] + a[one[-1] + 1:])
else:
print(a[:one[-2]] + a[one[-2] + 1:one[-1]] + a[one[-1] + 1:])
else:
print(-1)
elif zs == 2:
if len(two) > 0:
print(a[:two[-1]] + a[two[-1] + 1:])
elif len(one) > 1 and len(a) > 2:
if one[-2] == 0 and one[-1] == 1:
print(a[zf:])
else:
print(a[:one[-2]] + a[one[-2] + 1:one[-1]] + a[one[-1] + 1:])
else:
print(-1)
else:
if len(two) > 0:
print(a[:two[-1]] + a[two[-1] + 1:])
elif len(one) > 1 and len(a) > 2:
print(a[:one[-2]] + a[one[-2] + 1:one[-1]] + a[one[-1] + 1:])
else:
print(-1)
else:
print(a)
``` | instruction | 0 | 15,613 | 20 | 31,226 |
No | output | 1 | 15,613 | 20 | 31,227 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1. | instruction | 0 | 15,666 | 20 | 31,332 |
Tags: greedy
Correct Solution:
```
a=list(map(int,input()))
ans=0
if a.index(1)!=0 or a.count(1)>1:
ans+=(a.count(1)+1)
a=a[::-1]
#print(ans)
ans+=a[:a.index(1)].count(0)+(a[a.index(1):].count(0))*2
print(ans)
``` | output | 1 | 15,666 | 20 | 31,333 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1. | instruction | 0 | 15,667 | 20 | 31,334 |
Tags: greedy
Correct Solution:
```
x = list(map(int, input()))
cnt = 0
ans = 0
flag = 0
for i in range(1, len(x)):
if x[-i] == 0:
ans += 1
else:
if x[-i - 1] == 1:
if i == len(x) - 1:
cnt += 3
elif flag == 0:
cnt += 1
flag = 1
else:
cnt += 1
else:
x[-i - 1] = 1
ans += cnt + 2
flag = 0
cnt = 0
if i == len(x) - 1:
ans += cnt
print(ans)
``` | output | 1 | 15,667 | 20 | 31,335 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1. | instruction | 0 | 15,668 | 20 | 31,336 |
Tags: greedy
Correct Solution:
```
a = input()
if a == '1':
print(0)
exit()
ans = 0
if a.count('1') > 1:
ans = ans + a.count('1') + 1
a = a[::-1]
ans += a[:a.index('1')].count('0') + (a[a.index('1'):].count('0')) * 2
print(ans)
``` | output | 1 | 15,668 | 20 | 31,337 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1. | instruction | 0 | 15,669 | 20 | 31,338 |
Tags: greedy
Correct Solution:
```
import bisect
from itertools import accumulate
import os
import sys
import math
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io --------------------
a=input()[::-1].replace('',' ').split()
z=len(a)-1
ans=0
i=0
while i<z:
if a[i]=='0':
while i<z and a[i]=='0':i+=1;ans+=1
else:
ans+=1
while i<z and a[i]=='1':i+=1;ans+=1
if i==z:ans+=1
a[i]='1'
print(ans)
``` | output | 1 | 15,669 | 20 | 31,339 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1. | instruction | 0 | 15,670 | 20 | 31,340 |
Tags: greedy
Correct Solution:
```
n = list(map(int,input()))
l = 0
count = 0
for i in range(len(n) - 1, -1, -1):
if n[i] == 0 and l==0:
count += 1
elif n[i] == 0 and l==1:
count += 2
elif n[i] == 1 and i != 0:
count += 1
l = 1
elif n[i] == 1 and l==1:
count += 2
print(count)
``` | output | 1 | 15,670 | 20 | 31,341 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1. | instruction | 0 | 15,671 | 20 | 31,342 |
Tags: greedy
Correct Solution:
```
# 92B
from sys import stdin
__author__ = 'artyom'
digits = list(stdin.readline().strip())
count = 0
for i in range(len(digits) - 1, 0, -1):
count += 1
if digits[i] == '1' and digits[i - 1] != '1':
digits[i - 1] = '1'
count += 1
if len(digits) > 1 and i == 1 and digits[i] == '1':
count += 2
print(count)
``` | output | 1 | 15,671 | 20 | 31,343 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1. | instruction | 0 | 15,672 | 20 | 31,344 |
Tags: greedy
Correct Solution:
```
a=input()[::-1].replace('',' ').split()
z=len(a)-1
ans=0
i=0
while i<z:
if a[i]=='0':
while i<z and a[i]=='0':i+=1;ans+=1
else:
ans+=1
while i<z and a[i]=='1':i+=1;ans+=1
if i==z:ans+=1
a[i]='1'
print(ans)
``` | output | 1 | 15,672 | 20 | 31,345 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1. | instruction | 0 | 15,673 | 20 | 31,346 |
Tags: greedy
Correct Solution:
```
x = input()
lx = len(x)
numop = 0
if x.endswith('0'):
f = x.rindex('1')
numop += lx - f - 1
lx = f + 1
if lx == 1:
print(numop)
else:
numop += x.count('0',0,lx) + lx + 1
print(numop)
``` | output | 1 | 15,673 | 20 | 31,347 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1.
Submitted Solution:
```
t = input()
x, y, z = len(t), t.rfind('1'), t.count('1')
print(x + y - z + (2 if z > 1 else 0))
``` | instruction | 0 | 15,674 | 20 | 31,348 |
Yes | output | 1 | 15,674 | 20 | 31,349 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1.
Submitted Solution:
```
import math
import queue
from itertools import permutations
s=input()
t="1"+(len(s)-1)*"0"
a=0
b=0
for i in range(0,len(s)):
if s[i]=='0':
a+=1
i=len(s)-1
while True:
if s[i]=='1':
break
b+=1
i-=1
answer=len(s)+1+a-b
if s!=t:
print(answer)
else:
print((len(s)-1))
``` | instruction | 0 | 15,675 | 20 | 31,350 |
Yes | output | 1 | 15,675 | 20 | 31,351 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1.
Submitted Solution:
```
num = input()
x = len(num)
y = num.rfind('1')
z = num.count('1')
print(x+y-z+(2 if z>1 else 0))
``` | instruction | 0 | 15,676 | 20 | 31,352 |
Yes | output | 1 | 15,676 | 20 | 31,353 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1.
Submitted Solution:
```
s = str(input())
ct = 0
check = 0
for i in range(1, len(s)) :
if(s[-i] == '1') :
if(check == 1) :
ct += 1
else :
ct += 2
check = 1
else :
if(check == 1) :
ct += 2
else :
ct += 1
if(s[0] == '1' and check == 1) :
ct += 1
if(s[0] == '0' and check == 1) :
ct += 2
#print(ct, end = " ")
print(ct)
``` | instruction | 0 | 15,677 | 20 | 31,354 |
Yes | output | 1 | 15,677 | 20 | 31,355 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1.
Submitted Solution:
```
x = input()
y = int(str(x),2)
z = 0
while y!=1:
if y%2==0:
y/=2
z+=1
else:
y+=1
z+=1
print(z)
``` | instruction | 0 | 15,678 | 20 | 31,356 |
No | output | 1 | 15,678 | 20 | 31,357 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1.
Submitted Solution:
```
x = input()
print((x.count('1')-1)*2 + x.count('0'))
``` | instruction | 0 | 15,679 | 20 | 31,358 |
No | output | 1 | 15,679 | 20 | 31,359 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1.
Submitted Solution:
```
s = input()
ans = 0
n = len(s)
index = n-1
zeros = 0
ones = 0
while s[index] == 0:
index -= 1
ans += 1
if index == 0:
print(ans)
else:
while index>=0:
if s[index] == '0':
zeros += 1
else:
ones += 1
index -= 1
print(ans + ones + 2*zeros + 1)
``` | instruction | 0 | 15,680 | 20 | 31,360 |
No | output | 1 | 15,680 | 20 | 31,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.
Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.
How many actions should Fangy perform to get a number one from number x?
Input
The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.
Output
Print the required number of actions.
Examples
Input
1
Output
0
Input
1001001
Output
12
Input
101110
Output
8
Note
Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1.
Submitted Solution:
```
s=input()
if s=='1':
print(0)
exit()
s=list(s)
d={}
n=len(s)
pos=1
a=1
for i in range(1,n):
if s[i]=='1':
d[a]=i
a+=1
ans=n-1
for i in d.keys():
curr=d[i]
ans+=curr-pos
pos+=1
print(ans)
``` | instruction | 0 | 15,681 | 20 | 31,362 |
No | output | 1 | 15,681 | 20 | 31,363 |
Provide a correct Python 3 solution for this coding contest problem.
In the International System of Units (SI), various physical quantities are expressed in the form of "numerical value + prefix + unit" using prefixes such as kilo, mega, and giga. For example, "3.5 kilometers", "5.1 milligrams", and so on.
On the other hand, these physical quantities can be expressed as "3.5 * 10 ^ 3 meters" and "5.1 * 10 ^ -3 grams" using exponential notation.
Measurement of physical quantities always includes errors. Therefore, there is a concept of significant figures to express how accurate the measured physical quantity is. In the notation considering significant figures, the last digit may contain an error, but the other digits are considered reliable. For example, if you write "1.23", the true value is 1.225 or more and less than 1.235, and the digits with one decimal place or more are reliable, but the second decimal place contains an error. The number of significant digits when a physical quantity is expressed in the form of "reliable digit + 1 digit including error" is called the number of significant digits. For example, "1.23" has 3 significant digits.
If there is a 0 before the most significant non-zero digit, that 0 is not included in the number of significant digits. For example, "0.45" has two significant digits. If there is a 0 after the least significant non-zero digit, whether or not that 0 is included in the number of significant digits depends on the position of the decimal point. If there is a 0 to the right of the decimal point, that 0 is included in the number of significant digits. For example, "12.300" has 5 significant digits. On the other hand, when there is no 0 to the right of the decimal point like "12300", it is not clear whether to include the 0 on the right side in the number of significant digits, but it will be included in this problem. That is, the number of significant digits of "12300" is five.
Natsume was given a physics problem as a school task. I have to do calculations related to significant figures and units, but the trouble is that I still don't understand how to use significant figures and units. Therefore, I would like you to create a program that automatically calculates them and help Natsume.
What you write is a program that, given a prefixed notation, converts it to exponential notation with the same number of significant digits. The following 20 prefixes are used.
* yotta = 10 ^ 24
* zetta = 10 ^ 21
* exa = 10 ^ 18
* peta = 10 ^ 15
* tera = 10 ^ 12
* giga = 10 ^ 9
* mega = 10 ^ 6
* kilo = 10 ^ 3
* hecto = 10 ^ 2
* deca = 10 ^ 1
* deci = 10 ^ -1
* centi = 10 ^ -2
* milli = 10 ^ -3
* micro = 10 ^ -6
* nano = 10 ^ -9
* pico = 10 ^ -12
* femto = 10 ^ -15
* ato = 10 ^ -18
* zepto = 10 ^ -21
* yocto = 10 ^ -24
Notes on Submission
Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format.
Input
The input consists of only one line, which contains numbers, unit prefixes (if any), and units. Each is separated by a blank. In some cases, there is no unit prefix, in which case only numbers and units are included in the line. Units with the same name as the unit prefix do not appear. The most significant digit is never 0, except for the ones digit when a decimal is given. The number given is positive. The number given is 1000 digits or less including the decimal point, and the unit name is 50 characters or less.
Output
Output the quantity expressed in exponential notation in the form of a * 10 ^ b [unit]. However, 1 <= a <10. The difference between the singular and plural forms of the unit name is not considered. Output the unit name given to the input as it is.
Example
Input
7
12.3 kilo meters
0.45 mega watts
0.000000000000000000000001 yotta grams
1000000000000000000000000 yocto seconds
42 amperes
0.42 joules
1234.56789012345678901234567890 hecto pascals
Output
1.23 * 10^4 meters
4.5 * 10^5 watts
1 * 10^0 grams
1.000000000000000000000000 * 10^0 seconds
4.2 * 10^1 amperes
4.2 * 10^-1 joules
1.23456789012345678901234567890 * 10^5 pascals | instruction | 0 | 15,897 | 20 | 31,794 |
"Correct Solution:
```
m = {"yotta":24, "zetta":21, "exa":18, "peta":15, "tera":12, "giga":9, "mega":6, "kilo":3, "hecto":2, "deca":1, "deci":-1, "centi":-2, "milli":-3, "micro":-6, "nano":-9, "pico":-12, "femto":-15, "ato":-18, "zepto":-21, "yocto":-24}
for _ in range(int(input())):
v, *b = input().split()
if len(b) == 2:
k, b = b[0], b[1]
a = m[k]
else:b = b[0];a = 0
s = 0
for i in range(len(v)):
if v[i] in "123456789":
if i != 0:
a -= i - 1
if i != len(v) - 1:v = v[i] + "." + v[i + 1:]
else:v = v[i]
else:
try:
j = v[i:].index(".")
a += j - 1
v = v[0] + "." + v[1:j] + v[j + 1:]
except:
a += len(v) - 1
v = v[0] + "." + v[1:]
break
print("{} * 10^{} {}".format(v, a, b))
``` | output | 1 | 15,897 | 20 | 31,795 |
Provide a correct Python 3 solution for this coding contest problem.
In the International System of Units (SI), various physical quantities are expressed in the form of "numerical value + prefix + unit" using prefixes such as kilo, mega, and giga. For example, "3.5 kilometers", "5.1 milligrams", and so on.
On the other hand, these physical quantities can be expressed as "3.5 * 10 ^ 3 meters" and "5.1 * 10 ^ -3 grams" using exponential notation.
Measurement of physical quantities always includes errors. Therefore, there is a concept of significant figures to express how accurate the measured physical quantity is. In the notation considering significant figures, the last digit may contain an error, but the other digits are considered reliable. For example, if you write "1.23", the true value is 1.225 or more and less than 1.235, and the digits with one decimal place or more are reliable, but the second decimal place contains an error. The number of significant digits when a physical quantity is expressed in the form of "reliable digit + 1 digit including error" is called the number of significant digits. For example, "1.23" has 3 significant digits.
If there is a 0 before the most significant non-zero digit, that 0 is not included in the number of significant digits. For example, "0.45" has two significant digits. If there is a 0 after the least significant non-zero digit, whether or not that 0 is included in the number of significant digits depends on the position of the decimal point. If there is a 0 to the right of the decimal point, that 0 is included in the number of significant digits. For example, "12.300" has 5 significant digits. On the other hand, when there is no 0 to the right of the decimal point like "12300", it is not clear whether to include the 0 on the right side in the number of significant digits, but it will be included in this problem. That is, the number of significant digits of "12300" is five.
Natsume was given a physics problem as a school task. I have to do calculations related to significant figures and units, but the trouble is that I still don't understand how to use significant figures and units. Therefore, I would like you to create a program that automatically calculates them and help Natsume.
What you write is a program that, given a prefixed notation, converts it to exponential notation with the same number of significant digits. The following 20 prefixes are used.
* yotta = 10 ^ 24
* zetta = 10 ^ 21
* exa = 10 ^ 18
* peta = 10 ^ 15
* tera = 10 ^ 12
* giga = 10 ^ 9
* mega = 10 ^ 6
* kilo = 10 ^ 3
* hecto = 10 ^ 2
* deca = 10 ^ 1
* deci = 10 ^ -1
* centi = 10 ^ -2
* milli = 10 ^ -3
* micro = 10 ^ -6
* nano = 10 ^ -9
* pico = 10 ^ -12
* femto = 10 ^ -15
* ato = 10 ^ -18
* zepto = 10 ^ -21
* yocto = 10 ^ -24
Notes on Submission
Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format.
Input
The input consists of only one line, which contains numbers, unit prefixes (if any), and units. Each is separated by a blank. In some cases, there is no unit prefix, in which case only numbers and units are included in the line. Units with the same name as the unit prefix do not appear. The most significant digit is never 0, except for the ones digit when a decimal is given. The number given is positive. The number given is 1000 digits or less including the decimal point, and the unit name is 50 characters or less.
Output
Output the quantity expressed in exponential notation in the form of a * 10 ^ b [unit]. However, 1 <= a <10. The difference between the singular and plural forms of the unit name is not considered. Output the unit name given to the input as it is.
Example
Input
7
12.3 kilo meters
0.45 mega watts
0.000000000000000000000001 yotta grams
1000000000000000000000000 yocto seconds
42 amperes
0.42 joules
1234.56789012345678901234567890 hecto pascals
Output
1.23 * 10^4 meters
4.5 * 10^5 watts
1 * 10^0 grams
1.000000000000000000000000 * 10^0 seconds
4.2 * 10^1 amperes
4.2 * 10^-1 joules
1.23456789012345678901234567890 * 10^5 pascals | instruction | 0 | 15,898 | 20 | 31,796 |
"Correct Solution:
```
dic = {
"yotta":24,
"zetta":21,
"exa" :18,
"peta" :15,
"tera" :12,
"giga" :9,
"mega" :6,
"kilo" :3,
"hecto":2,
"deca" :1,
"deci" :-1,
"centi":-2,
"milli":-3,
"micro":-6,
"nano" :-9,
"pico" :-12,
"femto":-15,
"ato" :-18,
"zepto":-21,
"yocto":-24
}
n = int(input())
for _ in range(n):
lst = input().split()
if len(lst) == 2:
x, z = lst
sisuu = 0
else:
x, y, z = lst
sisuu = dic[y]
if "." not in x:
x = x + "."
ind = x.index(".")
x = x[:ind] + x[ind+1:]
while x[0] == "0":
x = x[1:]
ind -= 1
if len(x) != 1:
x = x[0] + "." + x[1:]
sisuu += (ind - 1)
print(x , "*", "10^"+str(sisuu), z)
``` | output | 1 | 15,898 | 20 | 31,797 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In the International System of Units (SI), various physical quantities are expressed in the form of "numerical value + prefix + unit" using prefixes such as kilo, mega, and giga. For example, "3.5 kilometers", "5.1 milligrams", and so on.
On the other hand, these physical quantities can be expressed as "3.5 * 10 ^ 3 meters" and "5.1 * 10 ^ -3 grams" using exponential notation.
Measurement of physical quantities always includes errors. Therefore, there is a concept of significant figures to express how accurate the measured physical quantity is. In the notation considering significant figures, the last digit may contain an error, but the other digits are considered reliable. For example, if you write "1.23", the true value is 1.225 or more and less than 1.235, and the digits with one decimal place or more are reliable, but the second decimal place contains an error. The number of significant digits when a physical quantity is expressed in the form of "reliable digit + 1 digit including error" is called the number of significant digits. For example, "1.23" has 3 significant digits.
If there is a 0 before the most significant non-zero digit, that 0 is not included in the number of significant digits. For example, "0.45" has two significant digits. If there is a 0 after the least significant non-zero digit, whether or not that 0 is included in the number of significant digits depends on the position of the decimal point. If there is a 0 to the right of the decimal point, that 0 is included in the number of significant digits. For example, "12.300" has 5 significant digits. On the other hand, when there is no 0 to the right of the decimal point like "12300", it is not clear whether to include the 0 on the right side in the number of significant digits, but it will be included in this problem. That is, the number of significant digits of "12300" is five.
Natsume was given a physics problem as a school task. I have to do calculations related to significant figures and units, but the trouble is that I still don't understand how to use significant figures and units. Therefore, I would like you to create a program that automatically calculates them and help Natsume.
What you write is a program that, given a prefixed notation, converts it to exponential notation with the same number of significant digits. The following 20 prefixes are used.
* yotta = 10 ^ 24
* zetta = 10 ^ 21
* exa = 10 ^ 18
* peta = 10 ^ 15
* tera = 10 ^ 12
* giga = 10 ^ 9
* mega = 10 ^ 6
* kilo = 10 ^ 3
* hecto = 10 ^ 2
* deca = 10 ^ 1
* deci = 10 ^ -1
* centi = 10 ^ -2
* milli = 10 ^ -3
* micro = 10 ^ -6
* nano = 10 ^ -9
* pico = 10 ^ -12
* femto = 10 ^ -15
* ato = 10 ^ -18
* zepto = 10 ^ -21
* yocto = 10 ^ -24
Notes on Submission
Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format.
Input
The input consists of only one line, which contains numbers, unit prefixes (if any), and units. Each is separated by a blank. In some cases, there is no unit prefix, in which case only numbers and units are included in the line. Units with the same name as the unit prefix do not appear. The most significant digit is never 0, except for the ones digit when a decimal is given. The number given is positive. The number given is 1000 digits or less including the decimal point, and the unit name is 50 characters or less.
Output
Output the quantity expressed in exponential notation in the form of a * 10 ^ b [unit]. However, 1 <= a <10. The difference between the singular and plural forms of the unit name is not considered. Output the unit name given to the input as it is.
Example
Input
7
12.3 kilo meters
0.45 mega watts
0.000000000000000000000001 yotta grams
1000000000000000000000000 yocto seconds
42 amperes
0.42 joules
1234.56789012345678901234567890 hecto pascals
Output
1.23 * 10^4 meters
4.5 * 10^5 watts
1 * 10^0 grams
1.000000000000000000000000 * 10^0 seconds
4.2 * 10^1 amperes
4.2 * 10^-1 joules
1.23456789012345678901234567890 * 10^5 pascals
Submitted Solution:
```
m = {"yotta":24, "zetta":21, "exa":18, "peta":15, "tera":12, "giga":9, "mega":6, "kilo":3, "hecto":2, "deca":1, "deci":-1, "centi":-2, "milli":-3, "micro":-6, "nano":-9, "pico":-12, "femto":-15, "ato":-18, "zepto":-21, "yocto":-24}
for _ in range(int(input())):
v, *b = input().split()
if len(b) == 2:
k, b = b[0], b[1]
a = m[k]
else:b = b[0];a = 0
s = 0
for i in range(len(v)):
if v[i] in "123456789":
s = len(v) - i
if "." in v[i + 1:]:s -= 1
break
x = float(v)
while x < 1:
x *= 10
a -= 1
while x >= 10:
x /= 10
a += 1
while 1:
if v[0] in "123456789":
if len(v) != 1:
v = v[0] + "." + v[1:]
if "." in v[2:]:
a = 2 + v[2:].index(".")
``` | instruction | 0 | 15,899 | 20 | 31,798 |
No | output | 1 | 15,899 | 20 | 31,799 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840. | instruction | 0 | 16,026 | 20 | 32,052 |
Tags: brute force, dp, shortest paths
Correct Solution:
```
def f(x, y, d):
while d <= 0:
d += 10
ans = 10000
for i in range(10):
for j in range(10):
if (i * x + j * y) % 10 == d % 10 and i + j > 0:
ans = min(ans, i + j)
if ans == 10000:
return -1
return max(ans - 1, 0)
arr = [0] * 10
for i in range(10):
arr[i] = [0] * 10
for i in range(10):
for j in range(10):
arr[i][j] = [0] * 10
for x in range(10):
for y in range(10):
for d in range(10):
arr[x][y][d] = f(x, y, d)
s = input()
b = []
for i in range(len(s) - 1):
b.append(int(s[i + 1]) - int(s[i]))
bb = [0] * 10
for i in b:
bb[i] += 1
ans = [0] * 10
for i in range(10):
ans[i] = [0] * 10
for x in range(10):
for y in range(10):
for d in range(10):
t = arr[x][y][d]
if t == -1 and bb[d] != 0:
ans[x][y] = -1
break
ans[x][y] += t * bb[d]
for x in range(10):
for y in range(10):
print(ans[x][y], end=' ')
print()
print()
# for x in range(10):
# for y in range(10):
# for d in range(10):
# print('x = ', x, ' y = ', y, ' d = ', d, ' --- ', arr[x][y][d])
``` | output | 1 | 16,026 | 20 | 32,053 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840. | instruction | 0 | 16,027 | 20 | 32,054 |
Tags: brute force, dp, shortest paths
Correct Solution:
```
from sys import stdin
from sys import setrecursionlimit as SRL
SRL(10 ** 7)
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())
s = str(rd().strip())
ans = [[0] * 11 for _j in range(11)]
ddp = [[[100000] * 11 for _k in range(11)] for kk in range(11)]
tt = [0] * 11
for i in range(10):
for j in range(10):
for u in range(11):
for v in range(11):
if u == 0 and v == 0:
continue
k = (i*u + j*v) % 10
ddp[i][j][k] = min(ddp[i][j][k], max(0,u+v-1))
pre = 0
for i in range(1, len(s)):
t = ((int(s[i]) + 10) - pre) % 10
tt[t] += 1
pre = int(s[i])
for i in range(10):
asi = ""
for j in range(10):
for k in range(11):
if tt[k] and ddp[i][j][k] >= 80000:
ans[i][j] = -1
break
ans[i][j] += tt[k] * ddp[i][j][k]
asi = asi + str(ans[i][j]) + ' '
print(asi)
``` | output | 1 | 16,027 | 20 | 32,055 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840. | instruction | 0 | 16,028 | 20 | 32,056 |
Tags: brute force, dp, shortest paths
Correct Solution:
```
s = input()
pairs = {a + b: 0 for a in "0123456789" for b in "0123456789"}
for a, b in zip(s, s[1:]):
pairs[a+b] += 1
def solve(x, y, i, j):
ans = 20
for a in range(10):
for b in range(10):
if (i + a * x + b * y + x) % 10 == j:
ans = min(ans, a + b)
if (i + a * x + b * y + y) % 10 == j:
ans = min(ans, a + b)
if ans == 20:
return -1
return ans
for x in range(10):
row = []
for y in range(10):
ans = 0
for i in range(10):
for j in range(10):
s = f"{i}{j}"
if pairs[s] > 0:
tmp = solve(x, y, i, j)
# print(x, y, i, j, tmp, pairs[s])
ans += tmp * pairs[s]
if tmp == -1:
ans = -1
break
else:
continue
break
row.append(ans)
print(*row)
``` | output | 1 | 16,028 | 20 | 32,057 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840. | instruction | 0 | 16,029 | 20 | 32,058 |
Tags: brute force, dp, shortest paths
Correct Solution:
```
from collections import*
s=*map(int,input()),
c=Counter((y-x)%10for x,y in zip(s,s[1:]))
r=[0]*100
for i in range(100):
a=[-1]*10;l=[0];k=0
while l:
m=[]
for x in l:
for j in(i//10,i%10):
y=(x+j)%10
if a[y]<0:a[y]=k;m+=y,
l=m;k+=1
for x in c:
if r[i]>=0:r[i]=-1if a[x]<0else r[i]+c[x]*a[x]
print(*r)
``` | output | 1 | 16,029 | 20 | 32,059 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840. | instruction | 0 | 16,030 | 20 | 32,060 |
Tags: brute force, dp, shortest paths
Correct Solution:
```
from collections import*
s=input()
c=Counter((ord(y)-ord(x))%10for x,y in zip(s,s[1:]))
r=[0]*100
for i in range(100):
a=[-1]*11;q=[10]
while q:
x=q.pop(0)
for j in(i//10,i%10):
y=(x+j)%10
if a[y]<0:a[y]=a[x]+1;q+=y,
for x in c:r[i]=-(0>r[i]|a[x]or-r[i]-c[x]*a[x])
print(*r)
``` | output | 1 | 16,030 | 20 | 32,061 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840. | instruction | 0 | 16,031 | 20 | 32,062 |
Tags: brute force, dp, shortest paths
Correct Solution:
```
dist = []
for i in range(10):
t = []
for j in range(10):
t.append([32]*10)
dist.append(t)
for i in range(10):
for j in range(10):
row = dist[i][j]
for a in range(10):
for b in range(10):
val = ( a*i + b*j )%10
s = a+b
if s > 0 and s<row[val]:
row[val] = s
for k in range(10):
if row[k] == 32:
row[k] = -1
else:
row[k] -= 1
# for i in range(10):
# for j in range(10):
# print(i,j,dist[i][j])
import sys
d = [int(i) for i in input()]
data = [(d[i+1] - d[i])%10 for i in range(len(d) - 1)]
#offs = 1 - len(data)
#print(data)
cnt = [0]*10
for d in data:
cnt[d] += 1
for i in range(10):
for j in range(10):
ans = 0
for c in range(10):
inc = dist[i][j][c]
if inc == -1 and cnt[c] > 0:
ans = -1
break
ans += inc * cnt[c]
# for d in data:
# inc = dist[i][j][d]
# #print(d, inc, end='#######\n')
# if inc == -1:
# ans = -1
# break
# else:
# ans += inc
sys.stdout.write(str(ans) + ' ')
#print(ans - offs , end=" ")
sys.stdout.write("\n")
``` | output | 1 | 16,031 | 20 | 32,063 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840. | instruction | 0 | 16,032 | 20 | 32,064 |
Tags: brute force, dp, shortest paths
Correct Solution:
```
s=input()
mp=[0]*10
for i in range(1,len(s)):
x=(int(s[i])-int(s[i-1])+10)%10
mp[x]+=1
def tab(a,b):
tb=[1000]*10
for i in range(10):
for j in range(10):
if i==0 and j==0:
continue
x=(a*i+b*j)%10
tb[x]=min(tb[x],i+j)
return tb
for x in range(10):
for y in range(10):
tb=tab(x,y)
ans=0
for t in range(10):
if mp[t]>0 and tb[t]==1000:
print(-1, end=' ')
break
ans+=mp[t]*(tb[t]-1)
else:
print(ans,end=' ')
print()
``` | output | 1 | 16,032 | 20 | 32,065 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840. | instruction | 0 | 16,033 | 20 | 32,066 |
Tags: brute force, dp, shortest paths
Correct Solution:
```
def cover(t, x, y):
tar = []
for xc in range(100):
for yc in range(100):
if xc == 0 and yc == 0:
continue
if (xc * x + yc * y) % 10 == t:
tar.append(xc + yc)
if tar:
return min(tar) - 1
else:
return -2
d = {(0, 0, 0): 0, (0, 0, 1): 0, (0, 0, 2): 0, (0, 0, 3): 0, (0, 0, 4): 0, (0, 0, 5): 0, (0, 0, 6): 0, (0, 0, 7): 0, (0, 0, 8): 0, (0, 0, 9): 0, (0, 1, 0): 0, (0, 1, 1): 9, (0, 1, 2): 4, (0, 1, 3): 3, (0, 1, 4): 3, (0, 1, 5): 1, (0, 1, 6): 4, (0, 1, 7): 3, (0, 1, 8): 2, (0, 1, 9): 1, (0, 2, 0): 0, (0, 2, 1): 4, (0, 2, 2): 4, (0, 2, 3): 3, (0, 2, 4): 2, (0, 2, 5): 1, (0, 2, 6): 2, (0, 2, 7): 4, (0, 2, 8): 1, (0, 2, 9): 2, (0, 3, 0): 0, (0, 3, 1): 3, (0, 3, 2): 3, (0, 3, 3): 9, (0, 3, 4): 2, (0, 3, 5): 1, (0, 3, 6): 4, (0, 3, 7): 1, (0, 3, 8): 4, (0, 3, 9): 3, (0, 4, 0): 0, (0, 4, 1): 3, (0, 4, 2): 2, (0, 4, 3): 2, (0, 4, 4): 4, (0, 4, 5): 1, (0, 4, 6): 1, (0, 4, 7): 4, (0, 4, 8): 2, (0, 4, 9): 4, (0, 5, 0): 0, (0, 5, 1): 1, (0, 5, 2): 1, (0, 5, 3): 1, (0, 5, 4): 1, (0, 5, 5): 1, (0, 5, 6): 1, (0, 5, 7): 1, (0, 5, 8): 1, (0, 5, 9): 1, (0, 6, 0): 0, (0, 6, 1): 4, (0, 6, 2): 2, (0, 6, 3): 4, (0, 6, 4): 1, (0, 6, 5): 1, (0, 6, 6): 4, (0, 6, 7): 2, (0, 6, 8): 2, (0, 6, 9): 3, (0, 7, 0): 0, (0, 7, 1): 3, (0, 7, 2): 4, (0, 7, 3): 1, (0, 7, 4): 4, (0, 7, 5): 1, (0, 7, 6): 2, (0, 7, 7): 9, (0, 7, 8): 3, (0, 7, 9): 3, (0, 8, 0): 0, (0, 8, 1): 2, (0, 8, 2): 1, (0, 8, 3): 4, (0, 8, 4): 2, (0, 8, 5): 1, (0, 8, 6): 2, (0, 8, 7): 3, (0, 8, 8): 4, (0, 8, 9): 4, (0, 9, 0): 0, (0, 9, 1): 1, (0, 9, 2): 2, (0, 9, 3): 3, (0, 9, 4): 4, (0, 9, 5): 1, (0, 9, 6): 3, (0, 9, 7): 3, (0, 9, 8): 4, (0, 9, 9): 9, (1, 0, 0): -2, (1, 0, 1): 0, (1, 0, 2): -2, (1, 0, 3): 6, (1, 0, 4): -2, (1, 0, 5): -2, (1, 0, 6): -2, (1, 0, 7): 2, (1, 0, 8): -2, (1, 0, 9): 8, (1, 1, 0): 0, (1, 1, 1): 0, (1, 1, 2): 0, (1, 1, 3): 0, (1, 1, 4): 0, (1, 1, 5): 0, (1, 1, 6): 0, (1, 1, 7): 0, (1, 1, 8): 0, (1, 1, 9): 0, (1, 2, 0): -2, (1, 2, 1): 0, (1, 2, 2): -2, (1, 2, 3): 3, (1, 2, 4): -2, (1, 2, 5): 3, (1, 2, 6): -2, (1, 2, 7): 2, (1, 2, 8): -2, (1, 2, 9): 1, (1, 3, 0): 6, (1, 3, 1): 0, (1, 3, 2): 3, (1, 3, 3): 6, (1, 3, 4): 2, (1, 3, 5): 2, (1, 3, 6): 3, (1, 3, 7): 2, (1, 3, 8): 1, (1, 3, 9): 2, (1, 4, 0): -2, (1, 4, 1): 0, (1, 4, 2): -2, (1, 4, 3): 2, (1, 4, 4): -2, (1, 4, 5): 4, (1, 4, 6): -2, (1, 4, 7): 1, (1, 4, 8): -2, (1, 4, 9): 3, (1, 5, 0): -2, (1, 5, 1): 0, (1, 5, 2): 3, (1, 5, 3): 2, (1, 5, 4): 4, (1, 5, 5): -2, (1, 5, 6): 1, (1, 5, 7): 2, (1, 5, 8): 2, (1, 5, 9): 4, (1, 6, 0): -2, (1, 6, 1): 0, (1, 6, 2): -2, (1, 6, 3): 3, (1, 6, 4): -2, (1, 6, 5): 1, (1, 6, 6): -2, (1, 6, 7): 2, (1, 6, 8): -2, (1, 6, 9): 2, (1, 7, 0): 2, (1, 7, 1): 0, (1, 7, 2): 2, (1, 7, 3): 2, (1, 7, 4): 1, (1, 7, 5): 2, (1, 7, 6): 2, (1, 7, 7): 2, (1, 7, 8): 2, (1, 7, 9): 2, (1, 8, 0): -2, (1, 8, 1): 0, (1, 8, 2): -2, (1, 8, 3): 1, (1, 8, 4): -2, (1, 8, 5): 2, (1, 8, 6): -2, (1, 8, 7): 2, (1, 8, 8): -2, (1, 8, 9): 4, (1, 9, 0): 8, (1, 9, 1): 0, (1, 9, 2): 1, (1, 9, 3): 2, (1, 9, 4): 3, (1, 9, 5): 4, (1, 9, 6): 2, (1, 9, 7): 2, (1, 9, 8): 4, (1, 9, 9): 8, (2, 0, 0): -2, (2, 0, 1): 1, (2, 0, 2): 0, (2, 0, 3): 3, (2, 0, 4): 2, (2, 0, 5): -2, (2, 0, 6): 1, (2, 0, 7): 5, (2, 0, 8): 3, (2, 0, 9): 7, (2, 1, 0): 1, (2, 1, 1): 1, (2, 1, 2): 0, (2, 1, 3): 1, (2, 1, 4): 1, (2, 1, 5): 1, (2, 1, 6): 1, (2, 1, 7): 1, (2, 1, 8): 1, (2, 1, 9): 1, (2, 2, 0): 0, (2, 2, 1): 0, (2, 2, 2): 0, (2, 2, 3): 0, (2, 2, 4): 0, (2, 2, 5): 0, (2, 2, 6): 0, (2, 2, 7): 0, (2, 2, 8): 0, (2, 2, 9): 0, (2, 3, 0): 3, (2, 3, 1): 1, (2, 3, 2): 0, (2, 3, 3): 3, (2, 3, 4): 2, (2, 3, 5): 3, (2, 3, 6): 1, (2, 3, 7): 3, (2, 3, 8): 3, (2, 3, 9): 1, (2, 4, 0): 2, (2, 4, 1): 1, (2, 4, 2): 0, (2, 4, 3): 2, (2, 4, 4): 2, (2, 4, 5): 2, (2, 4, 6): 1, (2, 4, 7): 2, (2, 4, 8): 1, (2, 4, 9): 2, (2, 5, 0): -2, (2, 5, 1): 1, (2, 5, 2): 0, (2, 5, 3): 3, (2, 5, 4): 2, (2, 5, 5): -2, (2, 5, 6): 1, (2, 5, 7): 1, (2, 5, 8): 3, (2, 5, 9): 3, (2, 6, 0): 1, (2, 6, 1): 1, (2, 6, 2): 0, (2, 6, 3): 1, (2, 6, 4): 1, (2, 6, 5): 1, (2, 6, 6): 1, (2, 6, 7): 1, (2, 6, 8): 1, (2, 6, 9): 1, (2, 7, 0): 5, (2, 7, 1): 1, (2, 7, 2): 0, (2, 7, 3): 3, (2, 7, 4): 2, (2, 7, 5): 1, (2, 7, 6): 1, (2, 7, 7): 5, (2, 7, 8): 2, (2, 7, 9): 3, (2, 8, 0): 3, (2, 8, 1): 1, (2, 8, 2): 0, (2, 8, 3): 3, (2, 8, 4): 1, (2, 8, 5): 3, (2, 8, 6): 1, (2, 8, 7): 2, (2, 8, 8): 3, (2, 8, 9): 3, (2, 9, 0): 7, (2, 9, 1): 1, (2, 9, 2): 0, (2, 9, 3): 1, (2, 9, 4): 2, (2, 9, 5): 3, (2, 9, 6): 1, (2, 9, 7): 3, (2, 9, 8): 3, (2, 9, 9): 7, (3, 0, 0): -2, (3, 0, 1): 2, (3, 0, 2): -2, (3, 0, 3): 0, (3, 0, 4): -2, (3, 0, 5): -2, (3, 0, 6): -2, (3, 0, 7): 8, (3, 0, 8): -2, (3, 0, 9): 6, (3, 1, 0): 2, (3, 1, 1): 2, (3, 1, 2): 1, (3, 1, 3): 0, (3, 1, 4): 2, (3, 1, 5): 2, (3, 1, 6): 2, (3, 1, 7): 2, (3, 1, 8): 2, (3, 1, 9): 2, (3, 2, 0): -2, (3, 2, 1): 1, (3, 2, 2): -2, (3, 2, 3): 0, (3, 2, 4): -2, (3, 2, 5): 4, (3, 2, 6): -2, (3, 2, 7): 3, (3, 2, 8): -2, (3, 2, 9): 2, (3, 3, 0): 0, (3, 3, 1): 0, (3, 3, 2): 0, (3, 3, 3): 0, (3, 3, 4): 0, (3, 3, 5): 0, (3, 3, 6): 0, (3, 3, 7): 0, (3, 3, 8): 0, (3, 3, 9): 0, (3, 4, 0): -2, (3, 4, 1): 2, (3, 4, 2): -2, (3, 4, 3): 0, (3, 4, 4): -2, (3, 4, 5): 2, (3, 4, 6): -2, (3, 4, 7): 4, (3, 4, 8): -2, (3, 4, 9): 1, (3, 5, 0): -2, (3, 5, 1): 2, (3, 5, 2): 4, (3, 5, 3): 0, (3, 5, 4): 2, (3, 5, 5): -2, (3, 5, 6): 3, (3, 5, 7): 4, (3, 5, 8): 1, (3, 5, 9): 2, (3, 6, 0): -2, (3, 6, 1): 2, (3, 6, 2): -2, (3, 6, 3): 0, (3, 6, 4): -2, (3, 6, 5): 3, (3, 6, 6): -2, (3, 6, 7): 1, (3, 6, 8): -2, (3, 6, 9): 3, (3, 7, 0): 8, (3, 7, 1): 2, (3, 7, 2): 3, (3, 7, 3): 0, (3, 7, 4): 4, (3, 7, 5): 4, (3, 7, 6): 1, (3, 7, 7): 8, (3, 7, 8): 2, (3, 7, 9): 2, (3, 8, 0): -2, (3, 8, 1): 2, (3, 8, 2): -2, (3, 8, 3): 0, (3, 8, 4): -2, (3, 8, 5): 1, (3, 8, 6): -2, (3, 8, 7): 2, (3, 8, 8): -2, (3, 8, 9): 3, (3, 9, 0): 6, (3, 9, 1): 2, (3, 9, 2): 2, (3, 9, 3): 0, (3, 9, 4): 1, (3, 9, 5): 2, (3, 9, 6): 3, (3, 9, 7): 2, (3, 9, 8): 3, (3, 9, 9): 6, (4, 0, 0): -2, (4, 0, 1): 3, (4, 0, 2): 1, (4, 0, 3): 7, (4, 0, 4): 0, (4, 0, 5): -2, (4, 0, 6): 3, (4, 0, 7): 1, (4, 0, 8): 2, (4, 0, 9): 5, (4, 1, 0): 3, (4, 1, 1): 3, (4, 1, 2): 1, (4, 1, 3): 1, (4, 1, 4): 0, (4, 1, 5): 3, (4, 1, 6): 3, (4, 1, 7): 1, (4, 1, 8): 2, (4, 1, 9): 3, (4, 2, 0): 1, (4, 2, 1): 1, (4, 2, 2): 1, (4, 2, 3): 1, (4, 2, 4): 0, (4, 2, 5): 1, (4, 2, 6): 1, (4, 2, 7): 1, (4, 2, 8): 1, (4, 2, 9): 1, (4, 3, 0): 7, (4, 3, 1): 1, (4, 3, 2): 1, (4, 3, 3): 7, (4, 3, 4): 0, (4, 3, 5): 3, (4, 3, 6): 3, (4, 3, 7): 1, (4, 3, 8): 2, (4, 3, 9): 3, (4, 4, 0): 0, (4, 4, 1): 0, (4, 4, 2): 0, (4, 4, 3): 0, (4, 4, 4): 0, (4, 4, 5): 0, (4, 4, 6): 0, (4, 4, 7): 0, (4, 4, 8): 0, (4, 4, 9): 0, (4, 5, 0): -2, (4, 5, 1): 3, (4, 5, 2): 1, (4, 5, 3): 3, (4, 5, 4): 0, (4, 5, 5): -2, (4, 5, 6): 3, (4, 5, 7): 1, (4, 5, 8): 2, (4, 5, 9): 1, (4, 6, 0): 3, (4, 6, 1): 3, (4, 6, 2): 1, (4, 6, 3): 3, (4, 6, 4): 0, (4, 6, 5): 3, (4, 6, 6): 3, (4, 6, 7): 1, (4, 6, 8): 1, (4, 6, 9): 2, (4, 7, 0): 1, (4, 7, 1): 1, (4, 7, 2): 1, (4, 7, 3): 1, (4, 7, 4): 0, (4, 7, 5): 1, (4, 7, 6): 1, (4, 7, 7): 1, (4, 7, 8): 1, (4, 7, 9): 1, (4, 8, 0): 2, (4, 8, 1): 2, (4, 8, 2): 1, (4, 8, 3): 2, (4, 8, 4): 0, (4, 8, 5): 2, (4, 8, 6): 1, (4, 8, 7): 1, (4, 8, 8): 2, (4, 8, 9): 2, (4, 9, 0): 5, (4, 9, 1): 3, (4, 9, 2): 1, (4, 9, 3): 3, (4, 9, 4): 0, (4, 9, 5): 1, (4, 9, 6): 2, (4, 9, 7): 1, (4, 9, 8): 2, (4, 9, 9): 5, (5, 0, 0): -2, (5, 0, 1): 4, (5, 0, 2): -2, (5, 0, 3): 4, (5, 0, 4): -2, (5, 0, 5): 0, (5, 0, 6): -2, (5, 0, 7): 4, (5, 0, 8): -2, (5, 0, 9): 4, (5, 1, 0): 4, (5, 1, 1): 4, (5, 1, 2): 2, (5, 1, 3): 2, (5, 1, 4): 1, (5, 1, 5): 0, (5, 1, 6): 4, (5, 1, 7): 2, (5, 1, 8): 3, (5, 1, 9): 4, (5, 2, 0): -2, (5, 2, 1): 2, (5, 2, 2): -2, (5, 2, 3): 1, (5, 2, 4): -2, (5, 2, 5): 0, (5, 2, 6): -2, (5, 2, 7): 4, (5, 2, 8): -2, (5, 2, 9): 3, (5, 3, 0): 4, (5, 3, 1): 2, (5, 3, 2): 1, (5, 3, 3): 4, (5, 3, 4): 3, (5, 3, 5): 0, (5, 3, 6): 2, (5, 3, 7): 4, (5, 3, 8): 4, (5, 3, 9): 2, (5, 4, 0): -2, (5, 4, 1): 1, (5, 4, 2): -2, (5, 4, 3): 3, (5, 4, 4): -2, (5, 4, 5): 0, (5, 4, 6): -2, (5, 4, 7): 2, (5, 4, 8): -2, (5, 4, 9): 4, (5, 5, 0): 0, (5, 5, 1): 0, (5, 5, 2): 0, (5, 5, 3): 0, (5, 5, 4): 0, (5, 5, 5): 0, (5, 5, 6): 0, (5, 5, 7): 0, (5, 5, 8): 0, (5, 5, 9): 0, (5, 6, 0): -2, (5, 6, 1): 4, (5, 6, 2): -2, (5, 6, 3): 2, (5, 6, 4): -2, (5, 6, 5): 0, (5, 6, 6): -2, (5, 6, 7): 3, (5, 6, 8): -2, (5, 6, 9): 1, (5, 7, 0): 4, (5, 7, 1): 2, (5, 7, 2): 4, (5, 7, 3): 4, (5, 7, 4): 2, (5, 7, 5): 0, (5, 7, 6): 3, (5, 7, 7): 4, (5, 7, 8): 1, (5, 7, 9): 2, (5, 8, 0): -2, (5, 8, 1): 3, (5, 8, 2): -2, (5, 8, 3): 4, (5, 8, 4): -2, (5, 8, 5): 0, (5, 8, 6): -2, (5, 8, 7): 1, (5, 8, 8): -2, (5, 8, 9): 2, (5, 9, 0): 4, (5, 9, 1): 4, (5, 9, 2): 3, (5, 9, 3): 2, (5, 9, 4): 4, (5, 9, 5): 0, (5, 9, 6): 1, (5, 9, 7): 2, (5, 9, 8): 2, (5, 9, 9): 4, (6, 0, 0): -2, (6, 0, 1): 5, (6, 0, 2): 2, (6, 0, 3): 1, (6, 0, 4): 3, (6, 0, 5): -2, (6, 0, 6): 0, (6, 0, 7): 7, (6, 0, 8): 1, (6, 0, 9): 3, (6, 1, 0): 5, (6, 1, 1): 5, (6, 1, 2): 2, (6, 1, 3): 1, (6, 1, 4): 2, (6, 1, 5): 1, (6, 1, 6): 0, (6, 1, 7): 3, (6, 1, 8): 1, (6, 1, 9): 3, (6, 2, 0): 2, (6, 2, 1): 2, (6, 2, 2): 2, (6, 2, 3): 1, (6, 2, 4): 1, (6, 2, 5): 2, (6, 2, 6): 0, (6, 2, 7): 2, (6, 2, 8): 1, (6, 2, 9): 2, (6, 3, 0): 1, (6, 3, 1): 1, (6, 3, 2): 1, (6, 3, 3): 1, (6, 3, 4): 1, (6, 3, 5): 1, (6, 3, 6): 0, (6, 3, 7): 1, (6, 3, 8): 1, (6, 3, 9): 1, (6, 4, 0): 3, (6, 4, 1): 2, (6, 4, 2): 1, (6, 4, 3): 1, (6, 4, 4): 3, (6, 4, 5): 3, (6, 4, 6): 0, (6, 4, 7): 3, (6, 4, 8): 1, (6, 4, 9): 3, (6, 5, 0): -2, (6, 5, 1): 1, (6, 5, 2): 2, (6, 5, 3): 1, (6, 5, 4): 3, (6, 5, 5): -2, (6, 5, 6): 0, (6, 5, 7): 3, (6, 5, 8): 1, (6, 5, 9): 3, (6, 6, 0): 0, (6, 6, 1): 0, (6, 6, 2): 0, (6, 6, 3): 0, (6, 6, 4): 0, (6, 6, 5): 0, (6, 6, 6): 0, (6, 6, 7): 0, (6, 6, 8): 0, (6, 6, 9): 0, (6, 7, 0): 7, (6, 7, 1): 3, (6, 7, 2): 2, (6, 7, 3): 1, (6, 7, 4): 3, (6, 7, 5): 3, (6, 7, 6): 0, (6, 7, 7): 7, (6, 7, 8): 1, (6, 7, 9): 1, (6, 8, 0): 1, (6, 8, 1): 1, (6, 8, 2): 1, (6, 8, 3): 1, (6, 8, 4): 1, (6, 8, 5): 1, (6, 8, 6): 0, (6, 8, 7): 1, (6, 8, 8): 1, (6, 8, 9): 1, (6, 9, 0): 3, (6, 9, 1): 3, (6, 9, 2): 2, (6, 9, 3): 1, (6, 9, 4): 3, (6, 9, 5): 3, (6, 9, 6): 0, (6, 9, 7): 1, (6, 9, 8): 1, (6, 9, 9): 3, (7, 0, 0): -2, (7, 0, 1): 6, (7, 0, 2): -2, (7, 0, 3): 8, (7, 0, 4): -2, (7, 0, 5): -2, (7, 0, 6): -2, (7, 0, 7): 0, (7, 0, 8): -2, (7, 0, 9): 2, (7, 1, 0): 6, (7, 1, 1): 6, (7, 1, 2): 3, (7, 1, 3): 2, (7, 1, 4): 3, (7, 1, 5): 2, (7, 1, 6): 1, (7, 1, 7): 0, (7, 1, 8): 2, (7, 1, 9): 2, (7, 2, 0): -2, (7, 2, 1): 3, (7, 2, 2): -2, (7, 2, 3): 2, (7, 2, 4): -2, (7, 2, 5): 1, (7, 2, 6): -2, (7, 2, 7): 0, (7, 2, 8): -2, (7, 2, 9): 2, (7, 3, 0): 8, (7, 3, 1): 2, (7, 3, 2): 2, (7, 3, 3): 8, (7, 3, 4): 1, (7, 3, 5): 4, (7, 3, 6): 4, (7, 3, 7): 0, (7, 3, 8): 3, (7, 3, 9): 2, (7, 4, 0): -2, (7, 4, 1): 3, (7, 4, 2): -2, (7, 4, 3): 1, (7, 4, 4): -2, (7, 4, 5): 3, (7, 4, 6): -2, (7, 4, 7): 0, (7, 4, 8): -2, (7, 4, 9): 2, (7, 5, 0): -2, (7, 5, 1): 2, (7, 5, 2): 1, (7, 5, 3): 4, (7, 5, 4): 3, (7, 5, 5): -2, (7, 5, 6): 2, (7, 5, 7): 0, (7, 5, 8): 4, (7, 5, 9): 2, (7, 6, 0): -2, (7, 6, 1): 1, (7, 6, 2): -2, (7, 6, 3): 4, (7, 6, 4): -2, (7, 6, 5): 2, (7, 6, 6): -2, (7, 6, 7): 0, (7, 6, 8): -2, (7, 6, 9): 2, (7, 7, 0): 0, (7, 7, 1): 0, (7, 7, 2): 0, (7, 7, 3): 0, (7, 7, 4): 0, (7, 7, 5): 0, (7, 7, 6): 0, (7, 7, 7): 0, (7, 7, 8): 0, (7, 7, 9): 0, (7, 8, 0): -2, (7, 8, 1): 2, (7, 8, 2): -2, (7, 8, 3): 3, (7, 8, 4): -2, (7, 8, 5): 4, (7, 8, 6): -2, (7, 8, 7): 0, (7, 8, 8): -2, (7, 8, 9): 1, (7, 9, 0): 2, (7, 9, 1): 2, (7, 9, 2): 2, (7, 9, 3): 2, (7, 9, 4): 2, (7, 9, 5): 2, (7, 9, 6): 2, (7, 9, 7): 0, (7, 9, 8): 1, (7, 9, 9): 2, (8, 0, 0): -2, (8, 0, 1): 7, (8, 0, 2): 3, (8, 0, 3): 5, (8, 0, 4): 1, (8, 0, 5): -2, (8, 0, 6): 2, (8, 0, 7): 3, (8, 0, 8): 0, (8, 0, 9): 1, (8, 1, 0): 7, (8, 1, 1): 7, (8, 1, 2): 3, (8, 1, 3): 3, (8, 1, 4): 1, (8, 1, 5): 3, (8, 1, 6): 2, (8, 1, 7): 1, (8, 1, 8): 0, (8, 1, 9): 1, (8, 2, 0): 3, (8, 2, 1): 3, (8, 2, 2): 3, (8, 2, 3): 2, (8, 2, 4): 1, (8, 2, 5): 3, (8, 2, 6): 1, (8, 2, 7): 3, (8, 2, 8): 0, (8, 2, 9): 1, (8, 3, 0): 5, (8, 3, 1): 3, (8, 3, 2): 2, (8, 3, 3): 5, (8, 3, 4): 1, (8, 3, 5): 1, (8, 3, 6): 2, (8, 3, 7): 3, (8, 3, 8): 0, (8, 3, 9): 1, (8, 4, 0): 1, (8, 4, 1): 1, (8, 4, 2): 1, (8, 4, 3): 1, (8, 4, 4): 1, (8, 4, 5): 1, (8, 4, 6): 1, (8, 4, 7): 1, (8, 4, 8): 0, (8, 4, 9): 1, (8, 5, 0): -2, (8, 5, 1): 3, (8, 5, 2): 3, (8, 5, 3): 1, (8, 5, 4): 1, (8, 5, 5): -2, (8, 5, 6): 2, (8, 5, 7): 3, (8, 5, 8): 0, (8, 5, 9): 1, (8, 6, 0): 2, (8, 6, 1): 2, (8, 6, 2): 1, (8, 6, 3): 2, (8, 6, 4): 1, (8, 6, 5): 2, (8, 6, 6): 2, (8, 6, 7): 2, (8, 6, 8): 0, (8, 6, 9): 1, (8, 7, 0): 3, (8, 7, 1): 1, (8, 7, 2): 3, (8, 7, 3): 3, (8, 7, 4): 1, (8, 7, 5): 3, (8, 7, 6): 2, (8, 7, 7): 3, (8, 7, 8): 0, (8, 7, 9): 1, (8, 8, 0): 0, (8, 8, 1): 0, (8, 8, 2): 0, (8, 8, 3): 0, (8, 8, 4): 0, (8, 8, 5): 0, (8, 8, 6): 0, (8, 8, 7): 0, (8, 8, 8): 0, (8, 8, 9): 0, (8, 9, 0): 1, (8, 9, 1): 1, (8, 9, 2): 1, (8, 9, 3): 1, (8, 9, 4): 1, (8, 9, 5): 1, (8, 9, 6): 1, (8, 9, 7): 1, (8, 9, 8): 0, (8, 9, 9): 1, (9, 0, 0): -2, (9, 0, 1): 8, (9, 0, 2): -2, (9, 0, 3): 2, (9, 0, 4): -2, (9, 0, 5): -2, (9, 0, 6): -2, (9, 0, 7): 6, (9, 0, 8): -2, (9, 0, 9): 0, (9, 1, 0): 8, (9, 1, 1): 8, (9, 1, 2): 4, (9, 1, 3): 2, (9, 1, 4): 2, (9, 1, 5): 4, (9, 1, 6): 3, (9, 1, 7): 2, (9, 1, 8): 1, (9, 1, 9): 0, (9, 2, 0): -2, (9, 2, 1): 4, (9, 2, 2): -2, (9, 2, 3): 2, (9, 2, 4): -2, (9, 2, 5): 2, (9, 2, 6): -2, (9, 2, 7): 1, (9, 2, 8): -2, (9, 2, 9): 0, (9, 3, 0): 2, (9, 3, 1): 2, (9, 3, 2): 2, (9, 3, 3): 2, (9, 3, 4): 2, (9, 3, 5): 2, (9, 3, 6): 1, (9, 3, 7): 2, (9, 3, 8): 2, (9, 3, 9): 0, (9, 4, 0): -2, (9, 4, 1): 2, (9, 4, 2): -2, (9, 4, 3): 2, (9, 4, 4): -2, (9, 4, 5): 1, (9, 4, 6): -2, (9, 4, 7): 3, (9, 4, 8): -2, (9, 4, 9): 0, (9, 5, 0): -2, (9, 5, 1): 4, (9, 5, 2): 2, (9, 5, 3): 2, (9, 5, 4): 1, (9, 5, 5): -2, (9, 5, 6): 4, (9, 5, 7): 2, (9, 5, 8): 3, (9, 5, 9): 0, (9, 6, 0): -2, (9, 6, 1): 3, (9, 6, 2): -2, (9, 6, 3): 1, (9, 6, 4): -2, (9, 6, 5): 4, (9, 6, 6): -2, (9, 6, 7): 2, (9, 6, 8): -2, (9, 6, 9): 0, (9, 7, 0): 6, (9, 7, 1): 2, (9, 7, 2): 1, (9, 7, 3): 2, (9, 7, 4): 3, (9, 7, 5): 2, (9, 7, 6): 2, (9, 7, 7): 6, (9, 7, 8): 3, (9, 7, 9): 0, (9, 8, 0): -2, (9, 8, 1): 1, (9, 8, 2): -2, (9, 8, 3): 2, (9, 8, 4): -2, (9, 8, 5): 3, (9, 8, 6): -2, (9, 8, 7): 3, (9, 8, 8): -2, (9, 8, 9): 0, (9, 9, 0): 0, (9, 9, 1): 0, (9, 9, 2): 0, (9, 9, 3): 0, (9, 9, 4): 0, (9, 9, 5): 0, (9, 9, 6): 0, (9, 9, 7): 0, (9, 9, 8): 0, (9, 9, 9): 0}
s = [int(x) for x in input()]
if s[0] != 0:
for _ in range(10):
print('-1 ' * 10)
exit()
a = []
for i in range(1, len(s)):
a.append((s[i] - s[i - 1]) % 10)
counts = [0] * 10
for x in a:
counts[x] += 1
for i in range(10):
for j in range(10):
skip = False
count = 0
for x in range(10):
if counts[x]:
tr = d[(x, i, j)]
if tr == -2:
print(-1, end=' ')
skip = True
break
else:
count += tr * counts[x]
if skip:
continue
print(count, end=' ')
print()
``` | output | 1 | 16,033 | 20 | 32,067 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840.
Submitted Solution:
```
# ------------------- fast io --------------------
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")
# ------------------- fast io --------------------
def main():
vals=[int(k) for k in input()];n=len(vals)
store=[[[[-1 for s in range(10)] for i in range(10)] for j in range(10)] for k in range(10)]
#[p1][p2][num1][num2]
for p1 in range(10):
for p2 in range(10):
for num1 in range(10):
for num2 in range(num1,10):
minans=99
for coef1 in range(10):
for coef2 in range(10):
if (p1+coef1*num1+coef2*num2)%10==p2 and (coef1>0 or coef2>0):
minans=min(minans,max(coef1+coef2-1,0))
if minans!=99:
store[p1][p2][num1][num2]=minans
store[p1][p2][num2][num1]=minans
setty={}
for s in range(n-1):
pair=(vals[s],vals[s+1])
if not(pair in setty):
setty[pair]=1
else:
setty[pair]+=1
ans=[[-1 for s in range(10)] for j in range(10)]
for num1 in range(10):
for num2 in range(num1,10):
count=0;broke=False
for pair in setty:
p1=pair[0];p2=pair[1]
if store[p1][p2][num1][num2]!=-1:
count+=store[p1][p2][num1][num2]*setty[pair]
else:
broke=True
break
if broke==False:
ans[num1][num2]=count
ans[num2][num1]=count
for i in range(10):
print(*ans[i])
main()
``` | instruction | 0 | 16,034 | 20 | 32,068 |
Yes | output | 1 | 16,034 | 20 | 32,069 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840.
Submitted Solution:
```
import sys
from collections import defaultdict
input = sys.stdin.readline
import math
def main():
s = input().rstrip()
opt = [[[[math.inf for _ in range(10)] for _ in range(10)] for _ in range(10)] for _ in range(10)]
# opt[x][y][a][b] - x-y counter, transition from a to b
pairs = [[0 for _ in range(10)] for _ in range(10)]
for x in range(10):
for y in range(10):
for a in range(10):
for cntx in range(10):
for cnty in range(10):
dig = (a + cntx * x + cnty * y) % 10
if cntx + cnty > 0:
opt[x][y][a][dig] = min(opt[x][y][a][dig], cntx+cnty)
for i in range(1, len(s)):
pairs[int(s[i-1])][int(s[i])] += 1
res = [[0 for _ in range(10)] for _ in range(10)]
for x in range(10):
for y in range(10):
for p1 in range(10):
for p2 in range(10):
p = pairs[p1][p2]
if p > 0:
if opt[x][y][p1][p2] == math.inf:
res[x][y] = -1
elif res[x][y] != -1:
res[x][y] += p * (opt[x][y][p1][p2]-1)
for x in range(10):
print(*res[x])
if __name__ == '__main__':
main()
``` | instruction | 0 | 16,035 | 20 | 32,070 |
Yes | output | 1 | 16,035 | 20 | 32,071 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840.
Submitted Solution:
```
import sys
word = [int(i) for i in input()]
cnt = [0]*10
for i in range(len(word) - 1):
cnt[( word[i+1] - word[i] )%10] += 1
row = [32]*10
for i in range(10):
for j in range(10):
row = [32]*10
for a in range(10):
for b in range(10):
val = ( a*i + b*j )%10
s = a+b-1
if s >= 0 and s<row[val]:
row[val] = s
ans = 0
for c in range(10):
inc = row[c]
if inc == 32 and cnt[c] > 0:
ans = -1
break
ans += inc * cnt[c]
sys.stdout.write(str(ans) + ' ')
sys.stdout.write("\n")
``` | instruction | 0 | 16,036 | 20 | 32,072 |
Yes | output | 1 | 16,036 | 20 | 32,073 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840.
Submitted Solution:
```
from itertools import product
def main():
a, cnt, res, hbb = 48, [-1, 0, 0, 0, 0, 0, 0, 0, 0, 0], {}, []
for b in map(ord, input()):
cnt[(b - a) % 10] += 1
a = b
cnt = [z for z in enumerate(cnt) if z[1]]
for a in range(10):
dist = [0] * 10
for t in range(1, 11):
x = a * t % 10
if dist[x]:
break
dist[x] = t
ha = [*(z for z in enumerate(dist) if z[1]), (0, 0)]
hbb.append(ha)
for b, hb in enumerate(hbb):
dist = [99999999] * 10
for (x, t), (y, u) in product(ha, hb):
x = (x + y) % 10
t += u
if 0 < t <= dist[x]:
dist[x] = t - 1
t = sum(c * dist[x] for x, c in cnt)
res[a, b] = res[b, a] = t if t < 99999999 else -1
for a in range(10):
print(*[res[a, b] for b in range(10)])
if __name__ == '__main__':
main()
``` | instruction | 0 | 16,037 | 20 | 32,074 |
Yes | output | 1 | 16,037 | 20 | 32,075 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840.
Submitted Solution:
```
from sys import stdin
from sys import setrecursionlimit as SRL; SRL(10**7)
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())
s = str(rd().strip())
dp = [[100000] * 11 for _i in range(11)]
ans = [[0] * 11 for _j in range(11)]
ddp = [[[100000] * 11 for _k in range(11)] for kk in range(11)]
tt = [0] * 11
for i in range(10):
for j in range(10):
dp[i][(i*j)%10] = min(dp[i][(i*j)%10],j)
for i in range(10):
for j in range(10):
for k in range(10):
for u in range(10):
for v in range(10):
if (u+v)%10 == k:
ddp[i][j][k] = min(ddp[i][j][k],dp[i][u]+dp[j][v])
print(ddp[6][8][6])
pre = 0
for i in range(1,len(s)):
tt[((int(s[i])+10)-pre)%10] += 1
pre = int(s[i])
for i in range(10):
asi = ""
for j in range(10):
for k in range(10):
if tt[k] and ddp[i][j][k] >= 100000:
ans[i][j] = -1
break
ans[i][j] += tt[k] * ddp[i][j][k]
if ans[i][j]!= -1:
ans[i][j] -= (len(s)-1)
asi = asi + str(ans[i][j]) + ' '
print(asi)
``` | instruction | 0 | 16,038 | 20 | 32,076 |
No | output | 1 | 16,038 | 20 | 32,077 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840.
Submitted Solution:
```
# ------------------- fast io --------------------
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")
# ------------------- fast io --------------------
from math import gcd, ceil
def prod(a, mod=10**9+7):
ans = 1
for each in a:
ans = (ans * each) % mod
return ans
def lcm(a, b): return a * b // gcd(a, b)
def binary(x, length=16):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
from math import inf
dp = [inf]*10000
def convert(i, j, k, l):
return i*1000 + j*100 + k*10 + l
def convert2(x):
l = x % 10
k = ((x - l) % 100)//10
j = ((x - l - k*10) % 1000)//100
i = x // 1000
return i, j, k, l
for i in range(10):
for j in range(10):
for k in range(10):
for l in range(10):
for m in range(10):
oof = (k + i*l + j*m) % 10
c = convert(i, j, k, oof)
dp[c] = min(dp[c], l+m)
# dp[i, j, k, l] = use i-j counter to convert k to l
for _ in range(int(input()) if not True else 1):
#n = int(input())
#n, k = map(int, input().split())
#a, b = map(int, input().split())
#c, d = map(int, input().split())
#a = list(map(int, input().split()))
#b = list(map(int, input().split()))
s = input()
di = {}
for i in range(len(s)-1):
st = (int(s[i]), int(s[i+1]))
if st not in di:
di[st] = 0
di[st] += 1
ans = [[0]*10 for i in range(10)]
for st in di:
for i in range(10):
for j in range(10):
# use i-j counter
if ans[i][j] == -1:continue
dx = dp[convert(i, j, st[0], st[1])]
if dx == inf:
ans[i][j] = -1
continue
ans[i][j] += (dx - 1)*di[st] if dx !=0 else 0
for each in ans:
print(*each)
``` | instruction | 0 | 16,039 | 20 | 32,078 |
No | output | 1 | 16,039 | 20 | 32,079 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840.
Submitted Solution:
```
def f(x, y, d):
while d < 0:
d += 10
ans = 10000
for i in range(10):
for j in range(10):
if (i * x + j * y) % 10 == d % 10:
ans = min(ans, i + j)
if ans == 100:
return -1
return max(ans - 1, 0)
arr = [0] * 10
for i in range(10):
arr[i] = [0] * 10
for i in range(10):
for j in range(10):
arr[i][j] = [0] * 10
for x in range(10):
for y in range(10):
for d in range(10):
arr[x][y][d] = f(x, y, d)
s = input()
b = []
for i in range(len(s) - 1):
b.append(int(s[i + 1]) - int(s[i]))
bb = [0] * 10
for i in b:
bb[i] += 1
ans = [0] * 10
for i in range(10):
ans[i] = [0] * 10
for x in range(10):
for y in range(10):
for d in range(10):
t = arr[x][y][d]
if t == -1 and bb[d] != 0:
ans[x][y] = t
break
ans[x][y] += t * bb[d]
for x in range(10):
for y in range(10):
print(ans[x][y], end=' ')
print()
print()
# for x in range(10):
# for y in range(10):
# for d in range(10):
# print(x, y, d, ' --- ', arr[x][y][d])
``` | instruction | 0 | 16,040 | 20 | 32,080 |
No | output | 1 | 16,040 | 20 | 32,081 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:
1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.
This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.
You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence.
For all 0 β€ x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed.
Input
The first line contains a single string s (1 β€ |s| β€ 2 β
10^6, s_i β \{0 - 9\}) β the remaining data you have. It's guaranteed that s_1 = 0.
Output
Print a 10 Γ 10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or -1 if there is no way to do so.
Example
Input
0840
Output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note
Let's take, for example, 4-3-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)0 (lost elements are in the brackets).
One of the possible outcomes a 2-3-counter could print is 0(35)8(1)4(7)0.
The 6-8-counter could print exactly the string 0840.
Submitted Solution:
```
def f(s1, s2, r, c):
## print(s1, s2, r, c)
razn = c - r
bb = []
bb.append([s1])
for k in range(10):
b = []
chislo = (bb[k][0] + r)%10
## print('chislo = ', chislo)
b.append(chislo)
if chislo == s2:
return k
for t in range(0, k+1):
chislo = (chislo + razn)%10
## print('chislo = ', chislo)
b.append(chislo)
if chislo == s2:
return k
bb.append(b)
## print(b)
return -1
def addD(s1, s2, val):
global d
if s1 not in d:
d[s1] = {}
d[s1][s2] = val
import sys
##input = sys.stdin.readline
s = input()
a = []
for i in range(10):
a.append([-1]*10)
for i in range(10):
for j in range(i, 10):
d = {}
sumRes = 0
for index in range(1, len(s)):
s1 = int(s[index-1])
s2 = int(s[index])
if s1 in d and s2 in d[s1]:
res = d[s1][s2]
else:
res = f(s1, s2, i, j)
addD(s1, s2, res)
if res == -1:
break
sumRes += res
a[i][j] = sumRes
a[j][i] = sumRes
print(*a[i])
``` | instruction | 0 | 16,041 | 20 | 32,082 |
No | output | 1 | 16,041 | 20 | 32,083 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di β ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 β 00" transforms s to 10031003, and query "3 β " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.
Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
Input
The first line contains string s (1 β€ |s| β€ 105), consisting of digits β the string before processing all the requests.
The second line contains a single integer n (0 β€ n β€ 105) β the number of queries.
The next n lines contain the descriptions of the queries. The i-th query is described by string "di->ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
Output
Print a single integer β remainder of division of the resulting number by 1000000007 (109 + 7).
Examples
Input
123123
1
2->00
Output
10031003
Input
123123
1
3->
Output
1212
Input
222
2
2->0
0->7
Output
777
Input
1000000008
0
Output
1
Note
Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample). | instruction | 0 | 16,336 | 20 | 32,672 |
Tags: dp
Correct Solution:
```
MOD = 1000000007
st,n,t,mp=input(),int(input()),[],{}
t.append(['',st])
for i in range(10):
mp[str(i)]=(10,i)
for i in range(n):
t.append(input().split("->"))
for i in range(n,-1,-1):
a,b=1,0
for j in t[i][1]:
a,b=a*mp[j][0]%MOD,(b*mp[j][0]+mp[j][1])%MOD
mp[t[i][0]]= a,b
print(mp[''][1])
# Made By Mostafa_Khaled
``` | output | 1 | 16,336 | 20 | 32,673 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di β ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 β 00" transforms s to 10031003, and query "3 β " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.
Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
Input
The first line contains string s (1 β€ |s| β€ 105), consisting of digits β the string before processing all the requests.
The second line contains a single integer n (0 β€ n β€ 105) β the number of queries.
The next n lines contain the descriptions of the queries. The i-th query is described by string "di->ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
Output
Print a single integer β remainder of division of the resulting number by 1000000007 (109 + 7).
Examples
Input
123123
1
2->00
Output
10031003
Input
123123
1
3->
Output
1212
Input
222
2
2->0
0->7
Output
777
Input
1000000008
0
Output
1
Note
Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample). | instruction | 0 | 16,337 | 20 | 32,674 |
Tags: dp
Correct Solution:
```
MOD = 1000000007
st,n,t,mp=input(),int(input()),[],{}
t.append(['',st])
for i in range(10):
mp[str(i)]=(10,i)
for i in range(n):
t.append(input().split("->"))
for i in range(n,-1,-1):
a,b=1,0
for j in t[i][1]:
a,b=a*mp[j][0]%MOD,(b*mp[j][0]+mp[j][1])%MOD
mp[t[i][0]]= a,b
print(mp[''][1])
``` | output | 1 | 16,337 | 20 | 32,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di β ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 β 00" transforms s to 10031003, and query "3 β " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.
Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
Input
The first line contains string s (1 β€ |s| β€ 105), consisting of digits β the string before processing all the requests.
The second line contains a single integer n (0 β€ n β€ 105) β the number of queries.
The next n lines contain the descriptions of the queries. The i-th query is described by string "di->ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
Output
Print a single integer β remainder of division of the resulting number by 1000000007 (109 + 7).
Examples
Input
123123
1
2->00
Output
10031003
Input
123123
1
3->
Output
1212
Input
222
2
2->0
0->7
Output
777
Input
1000000008
0
Output
1
Note
Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample). | instruction | 0 | 16,338 | 20 | 32,676 |
Tags: dp
Correct Solution:
```
def main():
s = input()
n = int(input())
M = 1000000007
a = {str(s):[10, s] for s in range(10)}
d = [['_', s]] + [input().split('->') for _ in range(n)]
for di, ti in reversed(d):
_p = 1
_v = 0
for c in ti:
_v = (_v * a[c][0] + a[c][1]) % M
_p = (_p * a[c][0]) % M
a[di] = [_p, _v]
print(a['_'][1])
if __name__ == '__main__':
main()
``` | output | 1 | 16,338 | 20 | 32,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di β ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 β 00" transforms s to 10031003, and query "3 β " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.
Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
Input
The first line contains string s (1 β€ |s| β€ 105), consisting of digits β the string before processing all the requests.
The second line contains a single integer n (0 β€ n β€ 105) β the number of queries.
The next n lines contain the descriptions of the queries. The i-th query is described by string "di->ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
Output
Print a single integer β remainder of division of the resulting number by 1000000007 (109 + 7).
Examples
Input
123123
1
2->00
Output
10031003
Input
123123
1
3->
Output
1212
Input
222
2
2->0
0->7
Output
777
Input
1000000008
0
Output
1
Note
Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample). | instruction | 0 | 16,339 | 20 | 32,678 |
Tags: dp
Correct Solution:
```
mod=pow(10,9)+7
def sub_and_eval(n):
if n=='':
return 0
ans=v[int(n[0])]
for i in range(1,len(n)):
ans=(d[int(n[i])]*ans+v[int(n[i])])%mod
return ans
def prod_d(n):
ans=1
for e in n:
ans=ans*d[int(e)]%mod
return ans
s=input()
v={i:i for i in range(10)}
d={i:10 for i in range(10)}
k=int(input())
arr=[]
for _ in range(k):
a,b=input().split('->')
arr.append((a,b))
for a,b in reversed(arr):
v[int(a)]=sub_and_eval(b)
d[int(a)]=prod_d(b)
print(sub_and_eval(s))
``` | output | 1 | 16,339 | 20 | 32,679 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di β ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 β 00" transforms s to 10031003, and query "3 β " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.
Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
Input
The first line contains string s (1 β€ |s| β€ 105), consisting of digits β the string before processing all the requests.
The second line contains a single integer n (0 β€ n β€ 105) β the number of queries.
The next n lines contain the descriptions of the queries. The i-th query is described by string "di->ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
Output
Print a single integer β remainder of division of the resulting number by 1000000007 (109 + 7).
Examples
Input
123123
1
2->00
Output
10031003
Input
123123
1
3->
Output
1212
Input
222
2
2->0
0->7
Output
777
Input
1000000008
0
Output
1
Note
Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample). | instruction | 0 | 16,340 | 20 | 32,680 |
Tags: dp
Correct Solution:
```
MOD = 10**9+7
s = input()
n = int(input())
qs = [['',s]]+[input().split('->') for i in range(n)]
ds = {}
for i in range(10):
ds[str(i)] = (10,i)
for i in range(n,-1,-1):
out = 0
mul = 1
for d in qs[i][1]:
out = (out * ds[d][0] + ds[d][1]) % MOD
mul = (mul * ds[d][0]) % MOD
ds[qs[i][0]] = (mul,out)
print(ds[''][1])
``` | output | 1 | 16,340 | 20 | 32,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di β ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 β 00" transforms s to 10031003, and query "3 β " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.
Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
Input
The first line contains string s (1 β€ |s| β€ 105), consisting of digits β the string before processing all the requests.
The second line contains a single integer n (0 β€ n β€ 105) β the number of queries.
The next n lines contain the descriptions of the queries. The i-th query is described by string "di->ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
Output
Print a single integer β remainder of division of the resulting number by 1000000007 (109 + 7).
Examples
Input
123123
1
2->00
Output
10031003
Input
123123
1
3->
Output
1212
Input
222
2
2->0
0->7
Output
777
Input
1000000008
0
Output
1
Note
Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample). | instruction | 0 | 16,341 | 20 | 32,682 |
Tags: dp
Correct Solution:
```
m = 1000000007
p = [(0, input())] + [input().split('->') for i in range(int(input()))]
s = [(10, i) for i in range(10)]
for d, t in p[::-1]:
a, b = 1, 0
for q in t:
x, y = s[int(q)]
a, b = a * x % m, (b * x + y) % m
s[int(d)] = a, b
print(s[0][1])
``` | output | 1 | 16,341 | 20 | 32,683 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di β ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 β 00" transforms s to 10031003, and query "3 β " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.
Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
Input
The first line contains string s (1 β€ |s| β€ 105), consisting of digits β the string before processing all the requests.
The second line contains a single integer n (0 β€ n β€ 105) β the number of queries.
The next n lines contain the descriptions of the queries. The i-th query is described by string "di->ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
Output
Print a single integer β remainder of division of the resulting number by 1000000007 (109 + 7).
Examples
Input
123123
1
2->00
Output
10031003
Input
123123
1
3->
Output
1212
Input
222
2
2->0
0->7
Output
777
Input
1000000008
0
Output
1
Note
Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).
Submitted Solution:
```
s = input()
n = int(input())
k = []
l = []
for i in range(n):
a, b = input().split("->")
k.append(a)
l.append(b)
if not s:
print(0)
else:
i = 1
while 1:
if s[-i] in k:
a = s[-i]
n = k.index(a)
k.pop(n)
s = s.replace(a,l.pop(n))
else:
if i == 10 or i == len(s):
break
else:
i+=1
if len(s) > 10:
print(int(s[:-10])%1000000007)
else:
print(int(s)%1000000007)
``` | instruction | 0 | 16,342 | 20 | 32,684 |
No | output | 1 | 16,342 | 20 | 32,685 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di β ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 β 00" transforms s to 10031003, and query "3 β " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.
Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
Input
The first line contains string s (1 β€ |s| β€ 105), consisting of digits β the string before processing all the requests.
The second line contains a single integer n (0 β€ n β€ 105) β the number of queries.
The next n lines contain the descriptions of the queries. The i-th query is described by string "di->ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
Output
Print a single integer β remainder of division of the resulting number by 1000000007 (109 + 7).
Examples
Input
123123
1
2->00
Output
10031003
Input
123123
1
3->
Output
1212
Input
222
2
2->0
0->7
Output
777
Input
1000000008
0
Output
1
Note
Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).
Submitted Solution:
```
def main():
s = input()
n = int(input())
M = 1000000007
a = {s:s for s in map(str, range(10))}
d = [input().split('->') for _ in range(n)]
v = list(range(9))
for di, ti in reversed(d):
_s = []
i = int(di)
_l = 0
v[i] = 0
for c in reversed(ti):
_s.append(a[c])
v[i] = (v[int(c)] * 10**_l + v[i]) % M
_l += len(a[c])
a[di] = ''.join(reversed(_s))
print(a, v)
res = 0
_l = 0
for c in reversed(s):
res = (int(a[c]) * 10**_l + res) % M
_l += len(a[c])
print(res)
if __name__ == '__main__':
main()
``` | instruction | 0 | 16,343 | 20 | 32,686 |
No | output | 1 | 16,343 | 20 | 32,687 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.