Đề bài
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ụ
Đầ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.
Đầ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.