Enigma
Time limit1sMemory limit1024 MB
Given a digit pattern with question marks and an N, find the smallest matching number with no leading zero that is divisible by N.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Math, Implementation, Greedy
- Solved
- No attempts yet
Problem
Cornelius Bakker was a pirate who also worked as an astronomer and a mathematician. He buried most of his fortune on the Caribbean island of Saint Basil, where the peak Pico Colombo is the usual landmark. Bakker vanished in 1617 when a hurricane caught his fleet of three ships. Before that last voyage he sent a letter to a niece in the Netherlands, giving the exact distance from Pico Colombo, due south, to the treasure.
Bakker did not want the letter to help a thief, so he did not write the distance itself. He multiplied it by a second number and wrote the product in the letter, together with the value of and a description of the computation. Few robbers of that time knew how to divide two numbers. The letter reached Europe, but the niece had already entered a convent and never opened it.
Exactly four centuries later, Maria inherited a chest holding that nun's belongings and found the letter, still sealed. She is planning a trip to look for the treasure. The value of is still readable, but moths have eaten part of , so some of its digits are gone. The only clue she has is that the leftmost digit of is not zero, because Bakker wrote that in the letter.
Given the damaged form of and the value of , find the smallest value can have.
Input
The first line contains a string and an integer (), separated by a space. is not empty and its length is at most 1000. Each character of is a decimal digit, or a question mark ? marking a position the moths ate. The first character of is not 0, and at least one character is a question mark.
Output
Consider every integer that has exactly digits, is a multiple of , has a leading digit other than 0, and matches at every position where holds a digit. Print the smallest such on one line. If no such exists, print a single asterisk *.