Numerology
Time limit2sMemory limit128 MB
For each (n, p), find the smallest base 2 to 10^6 where the digits of n form a pattern repetition and print the digit sequence.
- Level
Medium7 of 10
- Topics
- Brute force, Implementation, Math
- Solved
- No attempts yet
Problem
In many cultures certain numbers carry particular meanings. For example, 666 is often linked with the Devil, and 777 with good luck. Numbers like these tend to form a visual pattern, so a numerologist hunting for the next meaningful number cares about spotting such patterns.
A number only reveals an interesting pattern in the right base. For instance, 666 in hexadecimal is 29a, far less striking than its base-10 form. To account for every base that past (or even alien!) cultures might have used, you want to test whether a number matches a pattern of interest when written in some base.
We write a number converted to base as a sequence of symbols , where each symbol is an integer (its digit value in that base), listed most significant digit first. For example, the decimal number 10 written in base 2 is , giving and .
A pattern is a string, for example ab. A sequence matches a pattern if and only if both of the following hold:
- is at least the length of .
- There is a one-to-one correspondence between the distinct characters of and the distinct symbols of such that, when is repeatedly concatenated with itself and then truncated to a string of length , replacing every character of by its mapped symbol yields exactly .
Under this definition, 10 in base 2 matches the pattern ab (map a1 and b0, so abab becomes ).
Input
The first line contains the number of data sets . Each of the following lines contains a positive integer and a string : the number you care about and the pattern to match against. fits in a 64-bit integer, has at most 10 characters, and every character of is a lowercase letter from a to j.
Output
For each data set output Data Set x: on its own line, where is the data set's number starting from 1. Then output the smallest base with such that written in base matches the pattern . On the next line output the integer symbols of that matching sequence, separated by single spaces with no trailing space. If no base in the range produces a match, output No such base. instead of the base and the sequence. Separate consecutive data sets with a blank line.