簡単 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 つずつ開いてみましょう。

ヒント 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 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。