ง่าย 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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก