This problem is a programming version of Problem 74 from projecteuler.net
The number is well known for the property that the sum of the factorial of its digits is equal to :
Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it turns out that there are only three such loops that exist:
It is not difficult to prove that EVERY starting number will eventually get stuck in a loop. For example,
Starting with produces a chain of five non-repeating terms, but the longest non-repeating chain with a starting number below one million is sixty terms.
For a given length and limit print all the integers which have chain length
Input Format
First line contains , followed by lines.
Each line contains and separated by space.
Constraints
Output Format
Print the integers separated by space for each testcase. Where there are no such number for a given , print -1
.
Sample Input
10
221 7
147 1
258 4
265 8
210 2
175 7
29 2
24 3
273 4
261 4
Sample Output
24 42 104 114 140 141
1 2 145
78 87 196 236
4 27 39 72 93 107 117 170 171
0 10 11 154
24 42 104 114 140 141
0 10 11
-1
78 87 196 236 263
78 87 196 236