Tất cả thử thách
Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Digit Sum of a Power of Two

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

0 ≤ n ≤ 2000

Ví dụ

Ví dụ 1
Đầu vào:
n = 10

Kết quả mong đợi:
expected = 7
Đầu vào:
long long n = 10;

Kết quả mong đợi:
long long expected = 7;
Đầu vào:
long n = 10;

Kết quả mong đợi:
long expected = 7;

Giải thích

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

Ví dụ 2
Đầu vào:
n = 0

Kết quả mong đợi:
expected = 1
Đầu vào:
long long n = 0;

Kết quả mong đợi:
long long expected = 1;
Đầu vào:
long n = 0;

Kết quả mong đợi:
long expected = 1;

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

2^n is 1 doubled n times.

Gợi ý 2

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

Gợi ý 3

Add the digits only once the doubling is finished.

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.