Problem D
Number Anagrams

Tom is really interested in numbers, so he decided to become an accountant. To his great pleasure, he gets to see lots of numbers every day in his new job. Even though it is exciting to see all these numbers, the actual work happens to be boring. In order to combat boredom, he has created a game to help him have fun with all the interesting numbers.
From all the numbers he sees during a day, he picks one.
Then he adds a digit, and rearranges the digits so that they
form another number he has seen during the day. For example,
Letś say that he has seen the numbers
Tom likes long numbers, so he tries to make the numbers with
the largest number of digits possible. In the example above, he
would choose to make
Help Tom find out how many ways he can make numbers with the
largest number of digits possible, for a given list of
Since Tom sees a lot of numbers every day, we will use a
Pseudo Random Number Generator to generate the numbers. The
Input
The first line of the input consists of two space-separated
integers
The second line of the input consists of four space-separated
integers
The next
Output
For each of the
Example
The numbers that would be generated in the example are
Limits
-
-
-
-
All starting numbers
will be numbers that Tom has seen during the day.
Sample Input 1 | Sample Output 1 |
---|---|
10 2 3 2 100 1 1 5 |
3 4 |