ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Digit Sum of a Power of Two

Add up the decimal digits of 2 raised to the power n.

สิ่งที่คุณจะได้เรียนรู้

  • Double a number stored as digits
  • Sum the digits of a large number
  • Handle the n = 0 case

โจทย์

ดูฉบับแปล

Powers of two grow fast: 2^64 already has 20 digits and 2^2000 has over 600. Compute 2^n exactly and add up its decimal digits. Without big integers, keep the number as an array of digits and double it n times.

Implement digit_sum_power_of_two(n).

Input

n: The exponent.

Output

Return the sum of the decimal digits of 2^n.

เงื่อนไข

0 ≤ n ≤ 2000

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
n = 10

ผลลัพธ์ที่คาดหวัง:
expected = 7
อินพุต:
long long n = 10;

ผลลัพธ์ที่คาดหวัง:
long long expected = 7;
อินพุต:
long n = 10;

ผลลัพธ์ที่คาดหวัง:
long expected = 7;

คำอธิบาย

2^10 = 1024 and 1 + 0 + 2 + 4 = 7.

ตัวอย่างที่ 2
อินพุต:
n = 0

ผลลัพธ์ที่คาดหวัง:
expected = 1
อินพุต:
long long n = 0;

ผลลัพธ์ที่คาดหวัง:
long long expected = 1;
อินพุต:
long n = 0;

ผลลัพธ์ที่คาดหวัง:
long expected = 1;

คำอธิบาย

2^0 = 1, so the digit sum is 1.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

2^n is 1 doubled n times.

คำใบ้ 2

Store digits with the lowest digit first so carries move toward the end of the array.

คำใบ้ 3

Add the digits only once the doubling is finished.

ความซับซ้อนที่คาดหวัง

O(n²) time, O(n) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

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

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

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

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

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