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.
Sasha grew up and went to first grade. To celebrate this event her mother bought her a multiplication table M with n rows and n columns such that M_{ij}=a_i ⋅ a_j where a_1, ..., a_n is some sequence of positive integers.
Of course, the girl decided to take it to school with her. But while she was having lunch, hooligan Grisha erased numbers on the main diagonal and threw away the array a_1, ..., a_n. Help Sasha restore the array!
Input
The first line contains a single integer n (3 ⩽ n ⩽ 10^3), the size of the table.
The next n lines contain n integers each. The j-th number of the i-th line contains the number M_{ij} (1 ≤ M_{ij} ≤ 10^9). The table has zeroes on the main diagonal, that is, M_{ii}=0.
Output
In a single line print n integers, the original array a_1, ..., a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that an answer exists. If there are multiple answers, print any.
Examples
Input
5
0 4 6 2 4
4 0 6 2 4
6 6 0 3 6
2 2 3 0 2
4 4 6 2 0
Output
2 2 3 1 2
Input
3
0 99990000 99970002
99990000 0 99980000
99970002 99980000 0
Output
9999 10000 9998
Submitted Solution:
```
def find_gcd(x, y):
while(y):
x,y = y, x % y
return x
import math
n=int(input())
a=[]
x=[]
for i in range(0,n):
x=list(map(int,input().split()))
a.append(x)
f=0
ind=0
for i in range(0,n):
for j in range(0,n-1):
if(a[i][j]!=a[i][j+1] and a[i][j]!=0 and a[i][j+1]!=0):
f=1
ind=i
break
if(f==1):
break
if(f==0):
temp=int(math.sqrt(a[0][1]))
ans=[temp]*n
print(*ans)
else:
f=a[ind][0]
s=a[ind][1]
temp=find_gcd(f,s)
for i in range(2,n):
temp=find_gcd(temp,a[ind][i])
ans=[0]*n
for i in range(0,n):
if(i==ind):
ans[i]=temp
else:
ans[i]=int(a[ind][i]/temp)
print(*ans)
``` | instruction | 0 | 4,269 | 20 | 8,538 |
No | output | 1 | 4,269 | 20 | 8,539 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha grew up and went to first grade. To celebrate this event her mother bought her a multiplication table M with n rows and n columns such that M_{ij}=a_i ⋅ a_j where a_1, ..., a_n is some sequence of positive integers.
Of course, the girl decided to take it to school with her. But while she was having lunch, hooligan Grisha erased numbers on the main diagonal and threw away the array a_1, ..., a_n. Help Sasha restore the array!
Input
The first line contains a single integer n (3 ⩽ n ⩽ 10^3), the size of the table.
The next n lines contain n integers each. The j-th number of the i-th line contains the number M_{ij} (1 ≤ M_{ij} ≤ 10^9). The table has zeroes on the main diagonal, that is, M_{ii}=0.
Output
In a single line print n integers, the original array a_1, ..., a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that an answer exists. If there are multiple answers, print any.
Examples
Input
5
0 4 6 2 4
4 0 6 2 4
6 6 0 3 6
2 2 3 0 2
4 4 6 2 0
Output
2 2 3 1 2
Input
3
0 99990000 99970002
99990000 0 99980000
99970002 99980000 0
Output
9999 10000 9998
Submitted Solution:
```
import math
n = int(input(""))
a = list(map(int, input("").split()))
g = a[1]
for i in range(2,n):
g = math.gcd(g,a[i])
for i in range(n-1):
b = list(map(int, input("").split()))
print(g,end=' ')
for i in range(1,n):
print(a[i]//g, end = ' ')
``` | instruction | 0 | 4,270 | 20 | 8,540 |
No | output | 1 | 4,270 | 20 | 8,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).
Input
The first line contains the integer n (2 ≤ n ≤ 2·105). The second line contains elements of the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000). All the elements are positive integers.
Output
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.
If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.
Examples
Input
5
1 2 3 4 5
Output
1
3
Input
4
50 50 50 50
Output
4
1 2 3 4 | instruction | 0 | 4,335 | 20 | 8,670 |
Tags: brute force, implementation
Correct Solution:
```
n=int(input())
L=list(map(int,input().split()))
s=sum(L)
Ans=""
cnt=0
for i in range(n):
x=(s-L[i])//(n-1)
if(x*(n-1)!=s-L[i]):
continue
if(x==L[i]):
Ans+=str(i+1)+" "
cnt+=1
print(cnt)
if(cnt!=0):
print(Ans)
``` | output | 1 | 4,335 | 20 | 8,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).
Input
The first line contains the integer n (2 ≤ n ≤ 2·105). The second line contains elements of the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000). All the elements are positive integers.
Output
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.
If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.
Examples
Input
5
1 2 3 4 5
Output
1
3
Input
4
50 50 50 50
Output
4
1 2 3 4 | instruction | 0 | 4,341 | 20 | 8,682 |
Tags: brute force, implementation
Correct Solution:
```
import sys
S = sys.stdin.read()
S = [int(x) for x in S.split('\n')[1].split()]
s = sum(S)
n = len(S)
T = [i+1 for i in range(n) if n*S[i] == s]
print(len(T))
print(' '.join([str(x) for x in T]))
``` | output | 1 | 4,341 | 20 | 8,683 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).
Input
The first line contains the integer n (2 ≤ n ≤ 2·105). The second line contains elements of the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000). All the elements are positive integers.
Output
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.
If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.
Examples
Input
5
1 2 3 4 5
Output
1
3
Input
4
50 50 50 50
Output
4
1 2 3 4 | instruction | 0 | 4,342 | 20 | 8,684 |
Tags: brute force, implementation
Correct Solution:
```
m = int(input())
num = [int(n) for n in input().split()]
sum = 0
for i in range(0,m):
sum += num[i]
if sum % m:
print('0')
else:
ave = sum // m
cnt = 0
for i in range(0,m):
if num[i] == ave:
cnt += 1
print(cnt)
for i in range(0,m):
if num[i] == ave:
print(i+1,end=' ')
# I AK IOI
``` | output | 1 | 4,342 | 20 | 8,685 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44. | instruction | 0 | 4,452 | 20 | 8,904 |
Tags: binary search, combinatorics, dp
Correct Solution:
```
######### ## ## ## #### ##### ## # ## # ##
# # # # # # # # # # # # # # # # # # #
# # # # ### # # # # # # # # # # # #
# ##### # # # # ### # # # # # # # # #####
# # # # # # # # # # # # # # # # # #
######### # # # # ##### # ##### # ## # ## # #
"""
PPPPPPP RRRRRRR OOOO VV VV EEEEEEEEEE
PPPPPPPP RRRRRRRR OOOOOO VV VV EE
PPPPPPPPP RRRRRRRRR OOOOOOOO VV VV EE
PPPPPPPP RRRRRRRR OOOOOOOO VV VV EEEEEE
PPPPPPP RRRRRRR OOOOOOOO VV VV EEEEEEE
PP RRRR OOOOOOOO VV VV EEEEEE
PP RR RR OOOOOOOO VV VV EE
PP RR RR OOOOOO VV VV EE
PP RR RR OOOO VVVV EEEEEEEEEE
"""
"""
Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away.
"""
import sys
input = sys.stdin.readline
read = lambda: map(int, input().split())
# from bisect import bisect_left as lower_bound;
# from bisect import bisect_right as upper_bound;
# from math import ceil, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def factorial(x, m):
val = 1
while x>0:
val = (val * x) % m
x -= 1
return val
def fact(x):
val = 1
while x > 0:
val *= x
x -= 1
return val
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
## gcd function
def gcd(a,b):
if b == 0:
return a;
return gcd(b, a % b);
## lcm function
def lcm(a, b):
return (a * b) // gcd(a, b)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if k > n:
return 0
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0, hi = None):
if hi == None:
hi = len(a);
while lo < hi:
mid = (lo+hi)//2;
if a[mid] < x:
lo = mid+1;
else:
hi = mid;
return lo;
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b;
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
prime[0], prime[1] = False, False
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
# Euler's Toitent Function phi
def phi(n) :
result = n
p = 2
while(p * p<= n) :
if (n % p == 0) :
while (n % p == 0) :
n = n // p
result = result * (1.0 - (1.0 / (float) (p)))
p = p + 1
if (n > 1) :
result = result * (1.0 - (1.0 / (float)(n)))
return (int)(result)
def is_prime(n):
if n == 0:
return False
if n == 1:
return True
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
res = []
for i in range(2, int(x ** 0.5) + 1):
while x % i == 0:
res.append(i)
x //= i
if x != 1:
res.append(x)
return res
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
def factors(n):
res = []
for i in range(1, int(n ** 0.5) + 1):
if n % i == 0:
res.append(i)
res.append(n // i)
return list(set(res))
## taking integer array input
def int_array():
return list(map(int, input().strip().split()));
def float_array():
return list(map(float, input().strip().split()));
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
from itertools import permutations
import math
from bisect import bisect_left
def calc(x):
# print(x // 10)
return x if x < 10 else x // 10 + 9 - (0 if str(x)[0] <= str(x)[-1] else 1)
def solve():
l, r = read()
print(calc(r) - calc(l - 1))
if __name__ == '__main__':
for _ in range(1):
solve()
# fin_time = datetime.now()
# print("Execution time (for loop): ", (fin_time-init_time))
``` | output | 1 | 4,452 | 20 | 8,905 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44. | instruction | 0 | 4,453 | 20 | 8,906 |
Tags: binary search, combinatorics, dp
Correct Solution:
```
a1 = list(map(int,input().split()))
if (a1[0] < 10) & (a1[1] < 10):
print(a1[1] - a1[0]+1)
elif (a1[0] < 10) :
b = a1[1] - int(str(a1[1])[-1])
ans = 10 - a1[0] + b//10
if str(a1[1])[0] > str(a1[1])[len(str(a1[1]))-1]:
ans -= 1
else:
pass
print(ans)
else:
b1 = a1[0] - int(str(a1[0])[-1])
b2 = a1[1] - int(str(a1[1])[-1])
ans = (b2 - b1)//10 +1
if str(a1[0])[0] < str(a1[0])[len(str(a1[0]))-1]:
ans -= 1
if str(a1[1])[0] > str(a1[1])[len(str(a1[1]))-1]:
ans -= 1
print(ans)
``` | output | 1 | 4,453 | 20 | 8,907 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44. | instruction | 0 | 4,454 | 20 | 8,908 |
Tags: binary search, combinatorics, dp
Correct Solution:
```
def func(b):
n2 = len(b)
ans = 0
for i in range(1, n2):
if i == 1: ans += 9
else:
ans += 9 * (10 ** (i - 2))
for i in range(0, n2 - 1):
if n2 > 2:
if i == 0: tmp = (int(b[i]) - 1) * (10 ** (n2 - 2 - i))
else: tmp = (int(b[i])) * (10 ** (n2 - 2 - i))
else:
tmp = int(b[i]) - 1
ans += tmp
if n2 == 1: ans = int(b)
elif int(b[n2 - 1]) >= int(b[0]): ans -= -1
return ans
a, b = map(int, input().split())
a += -1
a = str(a)
b = str(b)
ans1 = func(a)
ans2 = func(b)
# print(ans1, ans2)
print(func(b) - func(a))
``` | output | 1 | 4,454 | 20 | 8,909 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44. | instruction | 0 | 4,455 | 20 | 8,910 |
Tags: binary search, combinatorics, dp
Correct Solution:
```
l, r = map(int, input().split())
result = 0
for digit in range(1, 10):
d_str = str(digit)
for length in range(3, 19):
if int(d_str + (length - 2) * '0' + d_str) <= r and \
int(d_str + (length - 2) * '9' + d_str) >= l:
a, b, c = 0, int((length - 2) * '9'), 0
while a < b:
c = (a + b) // 2
if int(d_str + '0' * (length - 2 - len(str(c))) + str(c) + d_str) < l:
a = c + 1
else:
b = c
l_end = a
a, b, c = 0, int((length - 2) * '9'), 0
while a < b:
c = (a + b + 1) // 2
if int(d_str + '0' * (length - 2 - len(str(c))) + str(c) + d_str) > r:
b = c - 1
else:
a = c
r_end = a
#print(str(l_end) + " " + str(r_end))
#print(" " + str(d_str + '0' * (length - 2 - len(str(l_end))) + str(l_end) + d_str))
#print(" " + str(d_str + '0' * (length - 2 - len(str(r_end))) + str(r_end) + d_str))
result += r_end - l_end + 1
for digit in range(1, 10):
length = 1
if l <= digit and digit <= r:
result += 1
length = 2
if int(str(digit) + str(digit)) >= l and int(str(digit) + str(digit)) <= r:
result += 1
print(result)
``` | output | 1 | 4,455 | 20 | 8,911 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44. | instruction | 0 | 4,456 | 20 | 8,912 |
Tags: binary search, combinatorics, dp
Correct Solution:
```
def f(x): return x if x<10 else x//10+9-(0 if str(x)[0]<=str(x)[-1] else 1)
l, r = map(int,input().split())
print(f(r)-f(l-1))
# Made By Mostafa_Khaled
``` | output | 1 | 4,456 | 20 | 8,913 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44. | instruction | 0 | 4,457 | 20 | 8,914 |
Tags: binary search, combinatorics, dp
Correct Solution:
```
import sys
import math as m
p = [0]*20
def fi():
return sys.stdin.readline()
def check(n):
ans = 0
ans+=min(9,n)
for i in range (10):
if i :
if 10*i + i <=n:
ans+=1
for i in range (3,19):
for j in range (1,10):
no = p[i-1]*j+j
lo = -1
hi = p[i-2]
res = 0
while hi-lo >1:
mid = (lo+hi)//2
if mid*10+no <= n:
lo = mid
else:
hi = mid
ans+=lo+1
return ans
if __name__ == '__main__':
p[0]=1
for i in range (1,19):
p[i]=p[i-1]*10
l,r = map(int, fi().split())
ans = check(r)-check(l-1)
print(ans)
``` | output | 1 | 4,457 | 20 | 8,915 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44. | instruction | 0 | 4,458 | 20 | 8,916 |
Tags: binary search, combinatorics, dp
Correct Solution:
```
#from bisect import bisect_left as bl #c++ lowerbound bl(array,element)
#from bisect import bisect_right as br #c++ upperbound br(array,element)
#from __future__ import print_function, division #while using python2
# from itertools import accumulate
# from collections import defaultdict, Counter
def modinv(n,p):
return pow(n,p-2,p)
def get_numbers(s):
if len(s) == 1:
return int(s)
ans = 0
n = len(s)
for i in range(1, n):
ans += (9 * (10 ** (max(0, i-2))))
x = n-2
for i in range(n):
k = int(s[i])
if i == 0:
k -= 1
# print("i = ", i, ", k = ", k)
ans += (k * (10 ** x))
x -= 1
if x < 0:
break
if int(s[-1]) >= int(s[0]):
ans += 1
return ans
def main():
#sys.stdin = open('input.txt', 'r')
#sys.stdout = open('output.txt', 'w')
# print(get_numbers(input()))
l, r = [int(x) for x in input().split()]
# print(get_numbers(str(l-1)))
# print(get_numbers(str(r)))
print(get_numbers(str(r)) - get_numbers(str(l-1)))
#------------------ Python 2 and 3 footer by Pajenegod and c1729-----------------------------------------
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
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')
if __name__ == '__main__':
main()
``` | output | 1 | 4,458 | 20 | 8,917 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44. | instruction | 0 | 4,459 | 20 | 8,918 |
Tags: binary search, combinatorics, dp
Correct Solution:
```
l, r = map(int, input().split())
ans, t = 0, dict({l - 1: 0, r: 0})
if l == r:
exit(print(1 if str(l)[0] == str(l)[-1] else 0))
for num in [l - 1, r]:
le = 1
while True:
if len(str(num)) > le:
if le < 3:
t[num] += 9
else:
t[num] += (10 ** (le - 2)) * 9
else:
if le == 1:
t[num] += num
elif le == 2:
t[num] += int(str(num)[0]) if str(num)[1] >= str(num)[0] else int(str(num)[0]) - 1
else:
t[num] += int(str(num)[1:-1]) if int(str(num)[-1]) < int(str(num)[0]) else int(str(num)[1:-1]) + 1
t[num] += (int(str(num)[0]) - 1) * (10 ** (le - 2))
break
le += 1
# print(t)
print(t[r] - t[l - 1])
``` | output | 1 | 4,459 | 20 | 8,919 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
# author: sharrad99
def get(x):
return x if x < 10 else x // 10 + 9 - (0 if str(x)[0] <= str(x)[-1] else 1)
l, r = map(int, input().split())
print(get(r) - get(l - 1))
``` | instruction | 0 | 4,460 | 20 | 8,920 |
Yes | output | 1 | 4,460 | 20 | 8,921 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l, r = list( map( int, input().split() ) )
def cnt( x ) :
ret = 0
for i in range( 1, x+1 ):
s = str(i)
if s[0] == s[-1]:
ret += 1
#print(s)
return ( ret )
def cnt2( x ):
ret = 0
s = str(x)
L = len(s)
if L >= 2 and x == 10**(L-1):
return ( cnt2( 10**(L-1)-1 ) )
if L <= 1:
return ( x )
elif L <= 2:
while x//10 != x%10:
x -= 1
return ( 9 + x//10 )
else:
ret = int( "1" + "0"*(L-3) + "8" )
num = list( str(x) )
#print( num )
if num[0] != num[-1]:
if int(num[0]) > int( num[-1] ):
how = int(num[-1]) + 10-int(num[0])
else:
how = int(num[-1]) - int(num[0])
x -= how
num = str(x)
d = x % 10
#print( 'num',num )
#print( 'd', d )
#print( 'num', num, 'ret before', ret )
ret += ( int(d-1) ) * int( "9" * (L-2) ) + int( num[1:-1] ) + int(d)
return ( ret )
#print( cnt(1024), cnt2(1024) )
#print( cnt(47), cnt2(47) )
#print( cnt(9), cnt2(9) )
#print( cnt(58), cnt2(58) )
#print( cnt(999), cnt2(999) )
#print( cnt(8987), cnt2(8987) )
#print()
#print( cnt(99999), cnt2(99999) )
#print( cnt(99899), cnt2(99899) )
#print( cnt(1000), cnt2(1000) )
#print( cnt2(1) )
#exit(0)
print( cnt2(r) - cnt2(l-1) )
``` | instruction | 0 | 4,461 | 20 | 8,922 |
Yes | output | 1 | 4,461 | 20 | 8,923 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l,r=map(int,input().split())
'''
cnt = 0
for i in range(l,r+1):
if str(i)[0]==str(i)[-1]: cnt+=1
print(cnt)
def upto_x(n):
if n<=1000:
cnt = 0
for i in range(1,n+1):
if str(i)[0]==str(i)[-1]: cnt+=1
return cnt
s=str(n)
sm=0
slen=len(s)
if slen>=1:
sm=0
elif slen>=2:
sm+=9
elif slen>=3:
sm+=10
else:
sm=10**(slen-2)
print(sm)
now=10**(slen-1)
now*=int(s[0])
if s[-1]>=s[0]:
now+=10**(slen-1)
sm+=now
return sm
print(upto_x(r))
print(upto_x(r)-upto_x(l-1))'''
def upto_x(n):
if n<10:
return n
sm=n//10 +9
if str(n)[-1]<str(n)[0]:
sm-=1
return sm
print(upto_x(r)-upto_x(l-1))
``` | instruction | 0 | 4,462 | 20 | 8,924 |
Yes | output | 1 | 4,462 | 20 | 8,925 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz'
M=10**9+7
EPS=1e-6
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
length_wise=[0,10,19]
for i in range(3,19):
length_wise.append(9*(10**(i-2))+length_wise[-1])
# print(length_wise)
def operate(x):
if(x==-1): return 0
x=str(x)
if(len(x)==1): return int(x)+1
ans=length_wise[len(x)-1]
key=min(int(x[0]),int(x[-1]))
key1=max(int(x[0]),int(x[-1]))
if(key==int(x[0])): type=1
else: type=2
# print(key,ans)
x=x[1:-1]
ans1=0
try:ans1=int(x)
except:pass
if(type==2):
ans+=(key1-1)*(10**len(x))
else:
ans+=(key-1)*(10**len(x))
ans1+=1
# print(ans,ans1,x)
return ans+ans1
l,r=value()
# print(operate(l-1))
# print(operate(r))
print(operate(r)-operate(l-1))
# BruteForce
# ans=0
#
# for i in range(l,r+1):
# x=str(i)
# if(x[0]==x[-1]):
# # print(x)
# ans+=1
# print(ans)
``` | instruction | 0 | 4,463 | 20 | 8,926 |
Yes | output | 1 | 4,463 | 20 | 8,927 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l, r = map(int, input().split())
ans, t = 0, dict({l - 1: 0, r: 0})
if l == r:
exit(print(1))
for num in [l - 1, r]:
le = 1
while True:
if len(str(num)) > le:
if le < 3:
t[num] += 9
else:
t[num] += (le - 2) * 10 * 9
else:
if le == 1:
t[num] += num
elif le == 2:
t[num] += int(str(num)[0]) if str(num)[1] >= str(num)[0] else int(str(num)[0]) - 1
else:
t[num] += int(str(num)[1:-1]) if int(str(num)[-1]) < int(str(num)[0]) else int(str(num)[1:-1]) + 1
t[num] += (9 * int('1' * (le - 2))) * (int(str(num)[0]) - 1)
break
le += 1
# print(t)
print(t[r] - t[l - 1])
``` | instruction | 0 | 4,464 | 20 | 8,928 |
No | output | 1 | 4,464 | 20 | 8,929 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l, r = map(int, input().split())
result = 0
for digit in range(1, 10):
d_str = str(digit)
for length in range(3, 18):
if int(d_str + (length - 2) * '0' + d_str) <= r and \
int(d_str + (length - 2) * '9' + d_str) >= l:
a, b, c = 0, int((length - 2) * '9'), 0
while a < b:
c = (a + b) // 2
if int(d_str + '0' * (length - 2 - len(str(c))) + str(c) + d_str) < l:
a = c + 1
else:
b = c
l_end = a
a, b, c = 0, int((length - 2) * '9'), 0
while a < b:
c = (a + b + 1) // 2
if int(d_str + '0' * (length - 2 - len(str(c))) + str(c) + d_str) > r:
b = c - 1
else:
a = c
r_end = a
#print(str(l_end) + " " + str(r_end))
#print(" " + str(d_str + '0' * (length - 2 - len(str(l_end))) + str(l_end) + d_str))
#print(" " + str(d_str + '0' * (length - 2 - len(str(r_end))) + str(r_end) + d_str))
result += r_end - l_end + 1
for digit in range(1, 10):
length = 1
if l <= digit and digit <= r:
result += 1
length = 2
if int(str(digit) + str(digit)) >= l and int(str(digit) + str(digit)) <= r:
result += 1
print(result)
``` | instruction | 0 | 4,465 | 20 | 8,930 |
No | output | 1 | 4,465 | 20 | 8,931 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l, r = map(int, input().split())
ans, t = 0, dict({l - 1: 0, r: 0})
if l == r:
exit(print(1))
for num in [l - 1, r]:
le = 1
while True:
if len(str(num)) > le:
if le < 3:
t[num] += 9
else:
t[num] += (10 ** (le - 2)) * 9
else:
if le == 1:
t[num] += num
elif le == 2:
t[num] += int(str(num)[0]) if str(num)[1] >= str(num)[0] else int(str(num)[0]) - 1
else:
t[num] += int(str(num)[1:-1]) if int(str(num)[-1]) < int(str(num)[0]) else int(str(num)[1:-1]) + 1
t[num] += (9 * int('1' * (le - 2))) * (int(str(num)[0]) - 1)
break
le += 1
# print(t)
print(t[r] - t[l - 1])
``` | instruction | 0 | 4,466 | 20 | 8,932 |
No | output | 1 | 4,466 | 20 | 8,933 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l,r=map(int,input().split())
'''
cnt = 0
for i in range(l,r+1):
if str(i)[0]==str(i)[-1]: cnt+=1
print(cnt)
def upto_x(n):
if n<=1000:
cnt = 0
for i in range(1,n+1):
if str(i)[0]==str(i)[-1]: cnt+=1
return cnt
s=str(n)
sm=0
slen=len(s)
if slen>=1:
sm=0
elif slen>=2:
sm+=9
elif slen>=3:
sm+=10
else:
sm=10**(slen-2)
print(sm)
now=10**(slen-1)
now*=int(s[0])
if s[-1]>=s[0]:
now+=10**(slen-1)
sm+=now
return sm
print(upto_x(r))
print(upto_x(r)-upto_x(l-1))'''
def upto_x(n):
if n<10:
return 9
sm=n//10 +9
if str(n)[-1]<str(n)[0]:
sm-=1
return sm
print(upto_x(r)-upto_x(l-1))
``` | instruction | 0 | 4,467 | 20 | 8,934 |
No | output | 1 | 4,467 | 20 | 8,935 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits. | instruction | 0 | 4,516 | 20 | 9,032 |
Tags: combinatorics, math
Correct Solution:
```
input_string = input()
n = int(input())
len_tmp = len(input_string)
const = 1000000007
def fast_pow(a, b, mod):
c=bin(b)[2:]
result=1
for i in c:
result = (result*result * a**int(i))%mod
return result
def inverse(a, mod):
#mod-простое
return fast_pow(a, mod-2, mod)
#-------------------------------
num = 0
tmp = 1
"""
for i in range(len_tmp):
if int(input_string[i])%5==0:
num = (num + fast_pow(2, i, const))
"""
for i in range(len_tmp):
if int(input_string[i])%5==0:
num = (num + tmp)
tmp=(tmp*2)%const
num %= const
l = fast_pow(2, len_tmp, const)
print((num*(fast_pow(l, n, const)-1)*inverse(l-1, const))%const)
``` | output | 1 | 4,516 | 20 | 9,033 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits. | instruction | 0 | 4,517 | 20 | 9,034 |
Tags: combinatorics, math
Correct Solution:
```
A = input()
n = int(input())
m = 1000000007
a = len(A)
x = pow(2,a*n,m)-1+m
y = pow(2,a,m)-1+m
f = x*pow(y,m-2,m)%m
s = 0
for r in range(a):
if A[r] in '05':
s = (s+pow(2,r,m))%m
print((s*f)%m)
``` | output | 1 | 4,517 | 20 | 9,035 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits. | instruction | 0 | 4,518 | 20 | 9,036 |
Tags: combinatorics, math
Correct Solution:
```
mod = 1000000007
#gcd(a, m) = 1
def inv_mod(a, m):
a %= m
return pow(a, m-2, m)
a = input()
k = int(input())
t = len(a)
d = 0
for i, c in enumerate(a):
if not int(c) % 5:
d += pow(2, i, mod)
d %= mod
print(d*(pow(2, k*t, mod)-1)*inv_mod(pow(2, t, mod)-1, mod)%mod)
``` | output | 1 | 4,518 | 20 | 9,037 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits. | instruction | 0 | 4,519 | 20 | 9,038 |
Tags: combinatorics, math
Correct Solution:
```
n=input()
k=int(input())
X=list(n)
index=[]
mo=1000000007
for i in range(len(X)):
if X[i]=="0" or X[i]=="5":
index.append(i)
ans=0
R=pow(2,len(X),mo)
for i in index:
ans=(ans+pow(2,i,mo))%mo
ans=(ans*(pow(R,k,mo)-1)*pow(R-1,mo-2,mo))%mo
print(ans)
``` | output | 1 | 4,519 | 20 | 9,039 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits. | instruction | 0 | 4,520 | 20 | 9,040 |
Tags: combinatorics, math
Correct Solution:
```
a,k = input(), int(input())
n,m = len(a), 10 ** 9 + 7
ans = 0
t = (pow(2, k * n, m) - 1) * pow(pow(2, n, m) - 1, m - 2, m) % m
for i in range(n - 1, -1, -1):
if int(a[i]) % 5 == 0:
ans = (ans + pow(2, i, m) * t) % m
print(ans)
``` | output | 1 | 4,520 | 20 | 9,041 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits. | instruction | 0 | 4,521 | 20 | 9,042 |
Tags: combinatorics, math
Correct Solution:
```
s=input()
k=int(input())
p=10**9+7
n=len(s)
amodp = (pow(2,k*n,p)-1)%p
b1modp = pow ( (2**n) -1 , p-2 , p )
MOD = (amodp*b1modp) % p
ans=0
for i in range(len(s)):
if(s[i]=='5' or s[i]=='0'):
ans+=pow(2,i,10**9+7)
Ans=0
Ans+= ans*( MOD )
Ans%=p
##for i in range(k):
## Ans+=ans*pow(2,i*len(s),10**9+7)
print(Ans)
##x^k - 1
##---------
##x - 1
##
##
##x = 2**n
##
##2^kn - 1
##---------
##2^n - 1
##
##amodp = (pow(2,k*n,p)-1)%p
##
##b1modp = pow ( 2**k-1 , p-2 , p )
##
##MOD = (amodp*b1modp) % p
##
##
##
##(a / b) mod p = ((a mod p) * (b^(-1) mod p)) mod p
##
##b^(-1) mod p = b^(p - 2) mod p
``` | output | 1 | 4,521 | 20 | 9,043 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits. | instruction | 0 | 4,522 | 20 | 9,044 |
Tags: combinatorics, math
Correct Solution:
```
mod = 1000000007
def egcd(a, b):
d = a
if b != 0:
d, y, x = egcd(b, a%b)
y -= (a // b) * x
return d, x, y
else:
return a, 1, 0
def inv(a, m):
d, x, y = egcd(a, m)
return (m+x%m)%m
a = input()
k = int(input())
t = len(a)
d = 0
for i, c in enumerate(a):
if not int(c) % 5:
d += pow(2, i, mod)
d %= mod
d %= mod
print(d*(pow(2, k*t, mod)-1)*inv((pow(2, t, mod)-1)%mod, mod)%mod)
``` | output | 1 | 4,522 | 20 | 9,045 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits. | instruction | 0 | 4,523 | 20 | 9,046 |
Tags: combinatorics, math
Correct Solution:
```
t, k = input(), int(input())
s, n, d = 0, 1, 1000000007
for i in t:
if i in '05': s += n
n = (n << 1) % d
p = (pow(n, k, d) - 1) * pow(n - 1, d - 2, d)
print(((p % d) * (s % d)) % d)
``` | output | 1 | 4,523 | 20 | 9,047 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.
Submitted Solution:
```
mod = 1000000007
#gcd(a, m) = 1
def inv_mod(a, m):
a %= m
return pow(a, m-2, m)
a = input()
k = int(input())
t = len(a)
d = 0
for i, c in enumerate(a):
if not int(c) % 5:
d += pow(2, i, mod)
d %= mod
print(d*(pow(2, (k*t)%(mod-1), mod)-1)*inv_mod(pow(2, t, mod)-1, mod)%mod)
``` | instruction | 0 | 4,524 | 20 | 9,048 |
Yes | output | 1 | 4,524 | 20 | 9,049 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.
Submitted Solution:
```
s=input()
k=int(input())
N=len(s)
m=1000000007
a=m+1-pow(2,N*k,m)
b=m+1-pow(2,N,m)
l=(a*pow(b,m-2,m))%m
p=0
for i in range(N):
if s[i] in ('0','5'):
p=(p+pow(2,i,m))
print((p*l)%m)
``` | instruction | 0 | 4,525 | 20 | 9,050 |
Yes | output | 1 | 4,525 | 20 | 9,051 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.
Submitted Solution:
```
def invmod( x , y , MOD ):
return (x*pow(y,MOD-2,MOD))%MOD
s=input()
k=int(input())
n=len(s)
MOD=10**9+7
sumG = invmod( pow(2,k*n,MOD)-1 , pow(2,n,MOD)-1 , MOD)
ans=0
for i in range(len(s)):
if(s[i]=='5' or s[i]=='0'):
ans+=pow(2,i,MOD)
ans*=sumG
print(ans%MOD)
``` | instruction | 0 | 4,526 | 20 | 9,052 |
Yes | output | 1 | 4,526 | 20 | 9,053 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.
Submitted Solution:
```
import sys
fin = sys.stdin
M = 1000000007
def egcd(a, b):
d = a
if b != 0:
d, y, x = egcd(b, a % b)
y -= (a // b) * x
return d, x, y
else:
return a, 1, 0
def inv(a, m):
d, x, y = egcd(a, m)
return (m + x % m) % m
a = fin.readline().strip()
k = int(fin.readline())
n = len(a)
D = (inv(pow(2, n, M) - 1, M) * (pow(2, n * k, M) - 1)) % M
S = 0
for i in range(n):
if a[i] in ['0', '5']:
S = (S + pow(2, i, M)) % M
print((D * S) % M)
``` | instruction | 0 | 4,527 | 20 | 9,054 |
Yes | output | 1 | 4,527 | 20 | 9,055 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.
Submitted Solution:
```
######### ## ## ## #### ##### ## # ## # ##
# # # # # # # # # # # # # # # # # # #
# # # # ### # # # # # # # # # # # #
# ##### # # # # ### # # # # # # # # #####
# # # # # # # # # # # # # # # # # #
######### # # # # ##### # ##### # ## # ## # #
"""
PPPPPPP RRRRRRR OOOO VV VV EEEEEEEEEE
PPPPPPPP RRRRRRRR OOOOOO VV VV EE
PPPPPPPPP RRRRRRRRR OOOOOOOO VV VV EE
PPPPPPPP RRRRRRRR OOOOOOOO VV VV EEEEEE
PPPPPPP RRRRRRR OOOOOOOO VV VV EEEEEEE
PP RRRR OOOOOOOO VV VV EEEEEE
PP RR RR OOOOOOOO VV VV EE
PP RR RR OOOOOO VV VV EE
PP RR RR OOOO VVVV EEEEEEEEEE
"""
"""
Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away.
"""
import sys
input = sys.stdin.readline
read = lambda: map(int, input().split())
# from bisect import bisect_left as lower_bound;
# from bisect import bisect_right as upper_bound;
# from math import ceil, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def factorial(x, m):
val = 1
while x>0:
val = (val * x) % m
x -= 1
return val
def fact(x):
val = 1
while x > 0:
val *= x
x -= 1
return val
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
## gcd function
def gcd(a,b):
if b == 0:
return a;
return gcd(b, a % b);
## lcm function
def lcm(a, b):
return (a * b) // math.gcd(a, b)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if k > n:
return 0
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b;
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
prime[0], prime[1] = False, False
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
# Euler's Toitent Function phi
def phi(n) :
result = n
p = 2
while(p * p<= n) :
if (n % p == 0) :
while (n % p == 0) :
n = n // p
result = result * (1.0 - (1.0 / (float) (p)))
p = p + 1
if (n > 1) :
result = result * (1.0 - (1.0 / (float)(n)))
return (int)(result)
def is_prime(n):
if n == 0:
return False
if n == 1:
return True
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
res = []
for i in range(2, int(x ** 0.5) + 1):
while x % i == 0:
res.append(i)
x //= i
if x != 1:
res.append(x)
return res
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
def factors(n):
res = []
for i in range(1, int(n ** 0.5) + 1):
if n % i == 0:
res.append(i)
res.append(n // i)
return list(set(res))
## taking integer array input
def int_array():
return list(map(int, input().strip().split()));
def float_array():
return list(map(float, input().strip().split()));
## taking string array input
def str_array():
return input().strip().split();
def binary_search(low, high, w, h, n):
while low < high:
mid = low + (high - low) // 2
# print(low, mid, high)
if check(mid, w, h, n):
high = mid
else:
low = mid + 1
return low
## for checking any conditions
def check(time, m, assistants):
summ = 0
for p in assistants:
time_needed_with_rest = p[0] * p[1] + p[2]
time_needed_without_rest = p[0]
baloons_can_be_inflated = p[1]
summ += (time // time_needed_with_rest) * baloons_can_be_inflated
ntime = time % time_needed_with_rest
summ += min(ntime // time_needed_without_rest, baloons_can_be_inflated)
return summ >= m
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
from itertools import permutations
import math
import bisect as bis
import random
import sys
def solve():
a = input().rstrip()
k = int(input())
s = 0
for i in range(len(a)):
if a[i] == '5' or a[i] == '0':
s += 2 ** i
# print(s, (2 ** (len(a) * k) - 1) // (2 ** len(a) - 1), len(a), k)
print((s * (power(2, (len(a) * k) - 1, MOD) // power(2, len(a) - 1, MOD))) % MOD)
if __name__ == '__main__':
for _ in range(1):
solve()
# fin_time = datetime.now()
# print("Execution time (for loop): ", (fin_time-init_time))
``` | instruction | 0 | 4,528 | 20 | 9,056 |
No | output | 1 | 4,528 | 20 | 9,057 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.
Submitted Solution:
```
t, k = input(), int(input())
n, d = len(t), 1000000007
def f(i): return ((1 << i) * (pow(2, k * n, d) - 1) * pow((1 << n) - 1, d - 2, d)) % d
print(sum(f(i) for i in range(n) if t[i] in '05'))
``` | instruction | 0 | 4,529 | 20 | 9,058 |
No | output | 1 | 4,529 | 20 | 9,059 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.
Submitted Solution:
```
input_string = input()
n = int(input())
len_tmp = len(input_string)
const = 1000000007
def fast_pow(a, b, mod):
c=bin(b)[2:]
result=1
for i in c:
result = (result**2 * a**int(i))%mod
return result
def inverse(a, mod):
#mod-простое
return fast_pow(a, mod-2, mod)
#-------------------------------
num = 0
for i in range(len_tmp):
if int(input_string[i])%5==0:
num = (num + fast_pow(2, i, const))%const
l = fast_pow(2, len_tmp, const)
print((num*(fast_pow(l, n, const)-1)*inverse(l, const))%const)
``` | instruction | 0 | 4,530 | 20 | 9,060 |
No | output | 1 | 4,530 | 20 | 9,061 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.
Look at the input part of the statement, s is given in a special form.
Input
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k.
Output
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
Examples
Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
Note
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.
Submitted Solution:
```
def power(a,b):
r = a
val = 1
while b>0:
if b%2==1:
val = val*r
r = r*r
b= b//2
return val
s = input()
k = int(input())
si = len(s)
val = power(2,si)
x = val//2
val = (power(val,k) - 1)//(val-1)
ans = 0
for i in range(si-1,-1,-1):
if s[i]=='0' or s[i]=='5':
ans+=val*(x)
x = x//2
print(ans)
``` | instruction | 0 | 4,531 | 20 | 9,062 |
No | output | 1 | 4,531 | 20 | 9,063 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the teachers' office.
The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactly p decimal digits, and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the "very beautiful number" is minimal possible.
The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us!
Input
The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).
Output
If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful number" without leading zeroes.
Examples
Input
6 5
Output
142857
Input
1 2
Output
Impossible
Input
6 4
Output
102564
Note
Sample 1: 142857·5 = 714285.
Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible". | instruction | 0 | 4,533 | 20 | 9,066 |
Tags: math
Correct Solution:
```
p,k = input().split()
p = int (p)
k = int (k)
y = 10*k -1
x = ((10 ** (p-1)) - k) % y
b = True
for i in range(k,10):
if( (x*i % y) == 0):
#z = (x*i) // y
#print(z*10 + i)
z = i*10
while( (z-i)%y ):
z *= 10
part = str ((z-i)//y)
print(part * (p//len(part)))
b= False
break
if(b):
print("Impossible")
``` | output | 1 | 4,533 | 20 | 9,067 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the teachers' office.
The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactly p decimal digits, and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the "very beautiful number" is minimal possible.
The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us!
Input
The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).
Output
If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful number" without leading zeroes.
Examples
Input
6 5
Output
142857
Input
1 2
Output
Impossible
Input
6 4
Output
102564
Note
Sample 1: 142857·5 = 714285.
Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible". | instruction | 0 | 4,534 | 20 | 9,068 |
Tags: math
Correct Solution:
```
p, k = map(int, input().split())
u = 10 * k - 1
v = pow(10, p - 1, u) - k
for y in range(k, 10):
if (y * v) % u == 0:
q = d = 9 * y
while q % u: q = 10 * q + d
q = str(q // u)
print(q * (p // len(q)))
break
else: print('Impossible')
# Made By Mostafa_Khaled
``` | output | 1 | 4,534 | 20 | 9,069 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the teachers' office.
The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactly p decimal digits, and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the "very beautiful number" is minimal possible.
The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us!
Input
The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).
Output
If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful number" without leading zeroes.
Examples
Input
6 5
Output
142857
Input
1 2
Output
Impossible
Input
6 4
Output
102564
Note
Sample 1: 142857·5 = 714285.
Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible". | instruction | 0 | 4,535 | 20 | 9,070 |
Tags: math
Correct Solution:
```
p, k = map(int, input().split())
u = 10 * k - 1
v = pow(10, p - 1, u) - k
for y in range(k, 10):
if (y * v) % u == 0:
q = d = 9 * y
while q % u: q = 10 * q + d
q = str(q // u)
print(q * (p // len(q)))
break
else: print('Impossible')
``` | output | 1 | 4,535 | 20 | 9,071 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the teachers' office.
The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactly p decimal digits, and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the "very beautiful number" is minimal possible.
The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us!
Input
The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).
Output
If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful number" without leading zeroes.
Examples
Input
6 5
Output
142857
Input
1 2
Output
Impossible
Input
6 4
Output
102564
Note
Sample 1: 142857·5 = 714285.
Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible".
Submitted Solution:
```
p, n = map(int, input().split())
ok = 0
for i in range(1, 10):
x = (i * 10 ** (p - 1) - n * i) / (n * 10 - 1)
if x == int(x) and (len(str(int(x))) + len(str(i))) == p:
print('%d%d' % (int(x), i))
ok = 1
break
if not ok:
print('Impossible')
``` | instruction | 0 | 4,536 | 20 | 9,072 |
No | output | 1 | 4,536 | 20 | 9,073 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the teachers' office.
The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactly p decimal digits, and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the "very beautiful number" is minimal possible.
The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us!
Input
The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).
Output
If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful number" without leading zeroes.
Examples
Input
6 5
Output
142857
Input
1 2
Output
Impossible
Input
6 4
Output
102564
Note
Sample 1: 142857·5 = 714285.
Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible".
Submitted Solution:
```
p,x=map(int,input().split())
c=0
for i in range (10**(p-1),10**p):
y=(i%10)*10**(p-1)
z=i//10
if (y+z)==x*i:
print(i)
c=c+1
break
if c==0:
print("impossible")
``` | instruction | 0 | 4,537 | 20 | 9,074 |
No | output | 1 | 4,537 | 20 | 9,075 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the teachers' office.
The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactly p decimal digits, and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the "very beautiful number" is minimal possible.
The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us!
Input
The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).
Output
If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful number" without leading zeroes.
Examples
Input
6 5
Output
142857
Input
1 2
Output
Impossible
Input
6 4
Output
102564
Note
Sample 1: 142857·5 = 714285.
Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible".
Submitted Solution:
```
import math
s = input()
s = s.split(' ')
p = s[0]
x = s[1]
p = int(p)
x = int(x)
if p == 1 and x == 1:
print (0)
else:
nem = math.pow(10,p-1) - x
den = 10*x - 1
flag = True
for i in range(1,10,1):
ans = i*nem/den;
if ans.is_integer() and len(str(int(ans))) == p-1:
print(str(int(ans)) + str(i))
global flag
flag = False
break
if flag:
print("Impossible")
``` | instruction | 0 | 4,538 | 20 | 9,076 |
No | output | 1 | 4,538 | 20 | 9,077 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the teachers' office.
The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactly p decimal digits, and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the "very beautiful number" is minimal possible.
The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us!
Input
The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).
Output
If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful number" without leading zeroes.
Examples
Input
6 5
Output
142857
Input
1 2
Output
Impossible
Input
6 4
Output
102564
Note
Sample 1: 142857·5 = 714285.
Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible".
Submitted Solution:
```
p,x=map(int,input().split())
t=pow(10,p)-1
t2=pow(10,p-1)
ok=0;
for d in range(1,9):
n=(d*t)
if n%(10*x-1)==0:
n//=(10*x-1)
n2=(n-d)//10+d*t2
if (n-d)%10==0:
n*=x
if n==n2 and n>0 and n2>0:
print(n)
ok=1
break;
if ok==0:
print("Impossible")
``` | instruction | 0 | 4,539 | 20 | 9,078 |
No | output | 1 | 4,539 | 20 | 9,079 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 109) — the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 ≤ i ≤ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 ≤ i ≤ n), that ai < bi, and for any j (1 ≤ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | instruction | 0 | 5,123 | 20 | 10,246 |
Tags: brute force, combinatorics, number theory
Correct Solution:
```
import math
def good(x):
while x > 0:
if x % 10 != 4 and x % 10 != 7:
return False
x //=10
return True
n, k = map(int, input().split())
l = 1
r = n
if n >= 15:
l = n-14
if n <= 15 and math.factorial(n) < k:
print(-1)
else:
L = r - l + 1
a = []
for i in range(L):
a.append(i)
b = []
k -= 1
for i in range(L):
x = k//math.factorial(L-i-1)
y = a[x]
b.append(y+l)
a.remove(y)
k -= x * math.factorial(L-i-1)
c = []
if 4 < l:
c.append(4)
if 7 < l:
c.append(7)
ans = 0
while len(c) > 0:
ans += len(c)
cc = []
for x in c:
if x * 10 + 4 < l:
cc.append(x * 10 + 4)
if x * 10 + 7 < l:
cc.append(x * 10 + 7)
c = cc
for i in range(L):
if good(i+l) and good(b[i]):
ans += 1
print(ans)
``` | output | 1 | 5,123 | 20 | 10,247 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 109) — the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 ≤ i ≤ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 ≤ i ≤ n), that ai < bi, and for any j (1 ≤ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | instruction | 0 | 5,124 | 20 | 10,248 |
Tags: brute force, combinatorics, number theory
Correct Solution:
```
def lucky(x):
s=str(x)
return s.count('4')+s.count('7')==len(s)
def Gen_lucky(n):
if(len(n)==1):
if(n<"4"):
return 0
if(n<"7"):
return 1
return 2
s=str(n)
if(s[0]<'4'):
return 0
if(s[0]=='4'):
return Gen_lucky(s[1:])
if(s[0]<'7'):
return 2**(len(s)-1)
if(s[0]=='7'):
return 2**(len(s)-1)+Gen_lucky(s[1:])
else:
return 2**len(s)
def Form(X,k):
if(k==0):
return X
for i in range(len(X)):
if(k>=F[len(X)-i-1]):
h=k//F[len(X)-i-1]
r=k%F[len(X)-i-1]
G=list(X[i+1:])
G.remove(X[i+h])
G=[X[i]]+G
return Form(X[:i]+[X[i+h]]+G,r)
p=1
F=[1]
i=1
while(p<=10**15):
p*=i
F.append(p)
i+=1
n,k=map(int,input().split())
if(n<=14):
if(k>F[n]):
print(-1)
else:
L=Form(list(range(1,n+1)),k-1)
x=0
for i in range(n):
if(lucky(i+1) and lucky(L[i])):
x+=1
print(x)
else:
L=Form(list(range(n-14,n+1)),k-1)
ss=str(n-15)
x=0
for i in range(1,len(ss)):
x+=2**i
x+=Gen_lucky(ss)
for i in range(n-14,n+1):
if(lucky(L[i-n+14]) and lucky(i)):
x+=1
print(x)
``` | output | 1 | 5,124 | 20 | 10,249 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 109) — the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 ≤ i ≤ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 ≤ i ≤ n), that ai < bi, and for any j (1 ≤ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
Submitted Solution:
```
def lucky(x):
s=str(x)
return s.count('4')+s.count('7')==len(s)
def Gen_lucky(n):
s=str(n)
if(len(s)==1):
if(s[0]>='7'):
return 2
if(s[0]>='4'):
return 1
return 0
x=0
for i in range(1,len(s)):
x+=2**i
if(s[0]<'4'):
return x
if(s[0]>'7'):
return x+2**len(s)
if(s[0]=='5' or s[0]=='6'):
return x+(2**(len(s)-1))
if(s[0]=='7'):
x+=2**(len(s)-1)
x+=Gen_lucky(s[1:])
return x
def Form(X,k):
if(k==0):
return X
for i in range(len(X)):
if(k>=F[len(X)-i-1]):
h=k//F[len(X)-i-1]
r=k%F[len(X)-i-1]
G=list(X[i+1:])
G.remove(X[i+h])
G=[X[i]]+G
return Form(X[:i]+[X[i+h]]+G,r)
p=1
F=[1]
i=1
while(p<=10**15):
p*=i
F.append(p)
i+=1
n,k=map(int,input().split())
if(n<=14):
if(k>F[n]):
print(-1)
else:
L=Form(list(range(1,n+1)),k-1)
x=0
for i in range(n):
if(lucky(i+1) and lucky(L[i])):
x+=1
print(x)
else:
L=Form(list(range(n-14,n+1)),k-1)
x=Gen_lucky(n-15)
for i in range(n-14,n+1):
if(lucky(L[i-n+14]) and lucky(i)):
x+=1
print(x)
``` | instruction | 0 | 5,125 | 20 | 10,250 |
No | output | 1 | 5,125 | 20 | 10,251 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 109) — the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 ≤ i ≤ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 ≤ i ≤ n), that ai < bi, and for any j (1 ≤ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
Submitted Solution:
```
def lucky(x):
s=str(x)
return s.count('4')+s.count('7')==len(s)
def Gen_lucky(n):
s=str(n)
if(len(s)==1):
if(s[0]=='4' or s[0]=='7'):
return 1
return 0
x=0
for i in range(1,len(s)):
x+=2**i
if(s[0]<'4'):
return x
if(s[0]>'7'):
return x+2**len(s)
if(s[0]=='5' or s[0]=='6'):
return x+(2**(len(s)-1))
if(s[0]=='7'):
x+=2**(len(s)-1)
x+=Getlucky(s[1:])
return x
def Form(X,k):
if(k==0):
return X
for i in range(len(X)):
if(k>=F[len(X)-i-1]):
h=k//F[len(X)-i-1]
r=k%F[len(X)-i-1]
G=list(X[i+1:])
G.remove(X[i+h])
G=[X[i]]+G
return Form(X[:i]+[X[i+h]]+G,r)
p=1
F=[1]
i=1
while(p<=10**10):
p*=i
F.append(p)
i+=1
n,k=map(int,input().split())
if(n<=14):
if(k>F[n]):
print(-1)
else:
L=Form(list(range(1,n+1)),k-1)
x=0
for i in range(n):
if(lucky(i+1) and lucky(L[i])):
x+=1
print(x)
else:
L=Form(list(range(n-13,n+1)),k-1)
x=Gen_lucky(n-14)
for i in range(n-13,n+1):
if(lucky(L[i-n+13]) and lucky(i)):
x+=1
print(x)
``` | instruction | 0 | 5,126 | 20 | 10,252 |
No | output | 1 | 5,126 | 20 | 10,253 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 109) — the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 ≤ i ≤ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 ≤ i ≤ n), that ai < bi, and for any j (1 ≤ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
Submitted Solution:
```
def lucky(x):
s=str(x)
return s.count('4')+s.count('7')==len(s)
def Gen_lucky(n):
s=str(n)
if(len(s)==1):
if(s[0]=='4' or s[0]=='7'):
return 1
return 0
x=0
for i in range(1,len(s)):
x+=2**i
if(s[0]<'4'):
return x
if(s[0]>'7'):
return x+2**len(s)
if(s[0]=='5' or s[0]=='6'):
return x+(2**(len(s)-1))
if(s[0]=='7'):
x+=2**(len(s)-1)
x+=Getlucky(s[1:])
return x
def Form(X,k):
if(k==0):
return X
for i in range(len(X)):
if(k>=F[len(X)-i-1]):
h=k//F[len(X)-i-1]
r=k%F[len(X)-i-1]
X[i],X[i+h]=X[i+h],X[i]
return [X[0]]+Form(X[1:],r)
p=1
F=[1]
i=1
while(p<=10**10):
p*=i
F.append(p)
i+=1
n,k=map(int,input().split())
if(n<=15):
L=Form(list(range(1,n+1)),k-1)
x=0
for i in range(n):
if(lucky(i+1) and lucky(L[i])):
x+=1
print(x)
else:
L=Form(list(range(n-13,n+1)),k-1)
x=Gen_lucky(n-14)
for i in range(n-13,n+1):
if(lucky(L[i-n+13]) and lucky(i)):
x+=1
print(x)
``` | instruction | 0 | 5,127 | 20 | 10,254 |
No | output | 1 | 5,127 | 20 | 10,255 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 109) — the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 ≤ i ≤ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 ≤ i ≤ n), that ai < bi, and for any j (1 ≤ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
Submitted Solution:
```
print(1)
``` | instruction | 0 | 5,128 | 20 | 10,256 |
No | output | 1 | 5,128 | 20 | 10,257 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.