Smallest Hex Multiple
Time limit2sMemory limit256 MB
Find the smallest multiple of N whose base-16 representation uses only the allowed digits, or report that none exists.
- Level
Medium6 of 10
- Topics
- BFS, Graph, Shortest path, Number theory
- Solved
- No attempts yet
Problem
You are given an integer and a list of hexadecimal digits. Find the smallest positive integer that is divisible by and whose base-16 representation uses only the given digits.
Input
The first line contains the number of test cases ().
Each test case takes two lines. The first line contains and in base 10, separated by a space (, ). The second line contains the allowed hexadecimal digits separated by spaces. Each digit is one of 0 to 9 and a to f, the digits are distinct, and they are given in increasing order.
Output
For each test case, print one line. Print, in base 16, the smallest positive integer that is divisible by and uses only the given digits. Write a to f in lowercase and print no leading zeros. If no such number exists, print no solution.
The answer can be very long. It does not always fit in a 64-bit integer, so build it as a string.