Mancunian and K-Reachability
Practice
4 (6 votes)
Mathematics
Easy Medium
Mathematics
Mathamatics
Problem
49% Success 1681 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

An integer is said to be k-reachable from another integer if the latter can be converted to the former by replacing atmost k digits. Note that, leading 0's are not considered the part of the number and hence are not allowed to replaced with non-zero integers.

For example,

1123 is 15-reachable from 2223 but not 1-reachable.

6 is 2-reachable from 10, but 10 is not 2-reachable from 6

Mancunian has been given the following problem. He is provided the initial integer N and the value of k. Now he has to answer Q queries. Each query is represented by a pair (L, R). The answer for a query is the number of integers in the range L to R (inclusive) which are k-reachable from N.

Input:
The first line contains three integers N, k and Q.
Each of the next Q lines is of the form L R denoting a specific query.

Output:
For each query, output the answer to the given query in a new line.

Constraints:
1 <= N < 107
1 <= k <= 5
1 <= Q <= 105
1 <= L <= R < 107

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
62 votes
Tags:
MathematicsOpenApprovedEasy-MediumMathamatics
Points:30
1 votes
Tags:
Easy-MediumEasy
Points:30
57 votes
Tags:
ReadyMathematicsOpenApprovedEasy-MediumMathamatics