Changing Digits
Time limit1sMemory limit1024 MB
Find the largest number reachable from N by changing one digit at a time so that each new value's remainder modulo M strictly increases.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Math
- Solved
- No attempts yet
Statement
Dovydas told Justas two natural numbers and . Justas's task is to turn into the largest possible number. He may do this by changing one digit at a time, so that after each change the remainder of the number divided by strictly increases. When changing a digit, the leading digit may not be changed to .
For example, if and , then the remainder of divided by is initially . If Justas first changes the last digit of , he may choose it among , because these options increase the remainder:
Suppose he chooses , so is now . If Justas now changes the leading digit, the only possible value is (note that the leading digit may not be changed to ):
The number can no longer be changed, because the largest possible remainder modulo has already been reached (). This would be Justas's result.
However, this is not the best possible result. The best result is , which can be reached, for example, with the following steps:
Determine the largest number Justas can reach.
Input
A single line contains the two integers and .
Output
Output the largest number Justas can reach.