모든 챌린지
쉬움 0명 해결 정답률 0%

Distinct Powers

Count the different values of a to the power b for a and b in a range.

배우게 될 내용

  • Reduce a base to its smallest root
  • Compare huge powers without computing them
  • Count unique values with a set

문제

번역본 보기

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

예시

예시 1
입력:
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.

예시 2
입력:
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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.