Social feed~25 min2s · 256 MBtarget O(e^2) time, O(e) spacenumber-theory
A social app lets a post be shared. In each round, every person who received the post in the previous round shares it with a fixed number of new people, called the base. So the reach of a round, the number of people who receive the post in it, is the base multiplied by itself once for each round.
After a few dozen rounds, the reach no longer fits any integer type. To check the figures, the app prints a checksum: the sum of the digits of the reach.
Write digit_sum_of_power(base, exponent).
exponent is base to the power exponent. Round 0 is the author alone, so its reach is 1.Input: base = 2, exponent = 15
Output: 26In round 15 with the base 2, the reach is 2^15 = 32768. Its digits are 3, 2, 7, 6 and 8.
| Digit | 3 | 2 | 7 | 6 | 8 | Sum |
|---|---|---|---|---|---|---|
| Value | 3 | 2 | 7 | 6 | 8 | 26 |
Input: base = 3, exponent = 5
Output: 93^5 = 243, and 2 + 4 + 3 = 9.
Input: base = 7, exponent = 0
Output: 1Round 0 is the author alone: the reach is 1, so the digit sum is 1.
2 ≤ base ≤ 90 ≤ exponent ≤ 10^4The number 9^10000 has 9,543 digits. No 64-bit integer holds it, so the number must be stored as a list of digits or cells.
Multiplying a list of d digits by the base takes d steps. Doing that once for each of 10^4 rounds takes about 5 x 10^7 steps. Storing 9 digits in one cell, and multiplying several rounds at once, cuts the work for the base 9 by a factor of about 54.