Largest Subsequence Number
Time limit3sMemory limit128 MB
Pick digits from N in order, without a leading zero, to form the largest value that leaves remainder R when divided by Q.
- Level
Medium6 of 10
- Topics
- Dynamic programming, String, Math
- Solved
- No attempts yet
Problem
You are given three numbers , , and . Find that satisfies all of the following.
- is positive.
- The decimal representation of is a subsequence of the decimal representation of , so you can build by deleting zero or more digits of .
- leaves a remainder of when divided by .
- is the largest value that satisfies the conditions.
A decimal representation does not start with 0, so the first digit you keep cannot be 0.
Input
The first line contains the number of test cases (). Each of the next lines contains one test case.
Each line has three integers separated by single spaces, , , and , in this order (, ). No number in the input has a leading zero.
Output
For each test case, print the described above on a single line, with no leading zeros. If no such exists, print Not found on a single line instead.
Hint
For , , , the number is divisible by , so the largest is .
For , , , the subsequences of are , , , , , , . Among them is not positive and has a leading zero, so both drop out. Of the rest, only leaves a remainder of when divided by .
For , , , no subsequence leaves a remainder of when divided by .