This problem was given at https://www.codechef.com/COOK61.
Problem link: DUALPAL
Problem statement:
A number is called a Dual Palindrome if it's representation in bases B1 and B2 are both palindromes. e.g. Let B1 = 3, B2 = 4, then number 130 (in base 10) will be called a Dual Palindrome, as it is palindrome in base 3 (11211) as well as in base 4 (2002). However, it is not a Dual Palindrome for B1 = 3 and B2 = 5 as it's not a palindrome in base 5(1010). Given two integers B1 and B2, Chef wants to find Dual Palindromes less than 260. If there are more than 1000 Dual Palindromes, then output the first 1000 only (these numbers should be in base 10).
Input:
The fine line of the input contains an integer T denoting the number of test cases. Each test case consists of a single line containing two space separated integers: B1 and B2, respectively.
Output:
For each test case, output a list of space separated integers which are Dual Palindromes less than 260 for the input bases. If there are more than 1000 Dual Palindromes, then output the first 1000 only. The numbers should be printed in an ascending order.
Constraints:
1 ≤ T ≤ 5
2 ≤ B1 < B2 ≤ 16
Any idea how to approach the problem?