Wavy Ranges
Practice
4.2 (38 votes)
Ready
Dynamic programming
Open
Approved
Hard
Problem
43% Success 116 Attempts 50 Points 5s Time Limit 256MB Memory 1024 KB Max Code

Learning about all the sinusoidal functions of trigonometry, Rahul has grown fond of numbers, whose digits increase and decrease alternatively.

Let's call d[i] is the ith digit of a number d. Formally, a wavy number is defined as a number d such that either d[0] < d[1] > d[2] ... or d[0] > d[1] < d[2] ...

Given a number N, can you find the integers L and R such that the range [L, N] and [N, R] each contain exactly K wavy numbers. Also, these ranges should be as large as possible.

Note that if no L >= 1 is found or the required value of R exceeds 1018, you should output -1.

Input:

The first line of input file contains T, the number of test cases to follow.
Each of the following T lines contain two space-separated integers N and K.

Output:

Output exactly T lines, each containing two integers L and R sepated by a single space.

Constraints:

1 <= T <= 625
1 <= K, N <= 1018

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:50
3 votes
Tags:
Algorithms4D dynamic programmingDynamic Programming