すべてのチャレンジ
簡単 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 つずつ開いてみましょう。

ヒント 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用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

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