Given three integers 'X', 'Y', and 'BOUND', return a list of all the powerful integers that have a value less than or equal to bound.
An integer is powerful if it can be represented as 'X'^i + 'Y'^j for some integers i >= 0 and j >= 0.
Example:Suppose given ‘X’ is 2, ‘Y’ is 3 and ‘BOUND’ is 10 then
The list of all powerful integers is [2, 3, 4, 5, 7, 9, 10] as:
2 = 2^0 + 3^0
3 = 2^1 + 3^0
4 = 2^0 + 3^1
5 = 2^1 + 3^1
7 = 2^2 + 3^1
9 = 2^3 + 3^0
10 = 2^0 + 3^2
Note:
1. You do not need to print anything; it has already been taken care of. Just implement the given function.
2. You may return the answer in any order. In your answer, each value should occur at most once.
The first line of input contains a single integer ‘T’ denoting the number of test cases given. Then next ‘T’ lines follow:
The first line of each test case input contains three space-separated integers, where the first integer represents the ‘X’, the second one is ‘Y’ while the third one is ‘BOUND’.
Output Format:
For every test case, return the vector/list of all the required elements which are less than equal to the given ‘BOUND’ in any order.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraint :
1 <= T <= 1000
1 <= X, Y <= 100
0 <= BOUND <= 10^6
Time Limit: 1 sec
2
3 5 15
4 3 20
2 4 6 8 10 14
2 4 5 7 10 13 17 19
Test Case 1:
2 = 3^0 + 5^0
4 = 3^1 + 5^0
6 = 3^0 + 5^1
8 = 3^1 + 5^1
10 = 3^2 + 5^0
14 = 3^2 + 5^1
Output [2, 4, 6, 8, 10, 14] is the required set of elements.
Test Case 2:
2 = 4^0 + 3^0
4 = 4^0 + 3^1
5 = 4^1 + 3^0
7 = 4^1 + 3^1
10 = 4^0 + 3^2
13 = 4^1 + 3^2
17 = 4^2 + 3^1
19 = 4^2 + 3^1
Output [2, 4, 5, 7, 10, 13, 17, 19] is the required set of elements.
2
2 5 12
2 3 10
2 3 5 6 7 9
2 3 4 5 7 9 10
Try to use a simple brute force idea to solve the problem, calculating all the possible powerful integer combinations until ‘X’^i + ‘Y’^j is less than the given ‘BOUND’.
We can use a simple brute force idea to calculate the bounds for the powers ‘i’ and ‘j’.
We know that at the highest bound we need to have, X^i = 10^6. Therefore, taking log to base 2 of both sides we get,
log(X^i) = log(10^6)
=> i * log X = 6 * log (10) (Since log(10) to the base 2 is approx. ~ 3.321928)
=> i * log X ~ 20
=> i = 20 / (log X)
Now, highest value of i is possible at lowest value of ‘X’, as they are inversely proportional.
=> for ‘X’ = 2
=> maximum ‘i’ = 20
So, if ‘i’ <= 20 is not applied, it will give Time Limit Exceeded.
The algorithm to calculate the required will be:-
O(i * j), where ‘i’ and ‘j’ are the respective powers that would be used to calculate the powerful integers.
Since we are iterating over all the powers ‘i’ and for every ‘X’^‘i’ we are checking whether the sum of ‘Y’^’j’ and ‘X’^’i’ is less than the ‘BOUND’. Hence, the complexity here grows by O(i * j).
O(i * j), where ‘i’ and ‘j’ are the respective powers that would be used to calculate the powerful integers.
Since, here, we require O(i * j) amount of space for storing the respective powerful integers in set, therefore, the space complexity is O(i * j).