Условие
Take every a and every b from lo to hi and compute a^b. Some values repeat, like 2^4 = 4^2 = 16. Count how many different values there are. The values are huge, but you never need them: write a as r^k where r is not itself a perfect power, and a^b = r^(k × b). Two powers are equal exactly when they share the same r and the same exponent k × b.
Implement distinct_powers(lo, hi).
Input
lo: The smallest base and exponent.
hi: The largest base and exponent.
Output
Return the number of distinct values of a^b with lo ≤ a ≤ hi and lo ≤ b ≤ hi.
Ограничения
2 ≤ lo ≤ hi ≤ 100
Примеры
Ввод:
lo = 2
hi = 4
Ожидаемый вывод:
expected = 8
Ввод:
long long lo = 2;
long long hi = 4;
Ожидаемый вывод:
long long expected = 8;
Ввод:
long lo = 2;
long hi = 4;
Ожидаемый вывод:
long expected = 8;
Пояснение
The nine powers are 4, 8, 16, 9, 27, 81, 16, 64, 256. 16 appears twice, so there are 8 distinct values.
Ввод:
lo = 7
hi = 7
Ожидаемый вывод:
expected = 1
Ввод:
long long lo = 7;
long long hi = 7;
Ожидаемый вывод:
long long expected = 1;
Ввод:
long lo = 7;
long hi = 7;
Ожидаемый вывод:
long expected = 1;
Пояснение
Only 7^7, so 1.
Как подступиться
Сначала попробуйте сами, затем открывайте подсказки по одной.
Подсказка 1
Find the smallest r and largest k with r^k = a.
Подсказка 2
a^b equals r^(k × b), so the pair (r, k × b) identifies the value.
Подсказка 3
Put the pairs in a set and count them.
Ожидаемая сложность
O((hi − lo)² log hi) time, O((hi − lo)²) extra space
Ссылки
Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.