Bank app~12 min2s · 256 MBtarget O(q) time, O(q) spacenumber-theory
You work on a banking app. Each statement gets a check digit: the last digit of base raised to the power exponent, where both numbers come from the statement. The base and the exponent can be as large as 1,000,000,000,000.
Your team lead asks: "What is the check digit of each statement?" A request holds up to 100,000 statements.
Write last_digits(bases, exponents).
bases[i] and exponents[i] describe statement i. The exponent is at least 1.i is the last decimal digit of bases[i] raised to the power exponents[i].Input: bases = [2, 3, 7, 10], exponents = [10, 5, 13, 100]
Output: [4, 3, 7, 0]Only the last digit of the base matters, and the last digits of its powers repeat in a cycle that divides 4. Here are the cycles, for the powers 1, 2, 3 and 4:
| Last digit | Last digits of the powers 1, 2, 3, 4 |
|---|---|
| 0 | 0, 0, 0, 0 |
| 1 | 1, 1, 1, 1 |
| 2 | 2, 4, 8, 6 |
| 3 | 3, 9, 7, 1 |
| 4 | 4, 6, 4, 6 |
| 5 | 5, 5, 5, 5 |
| 6 | 6, 6, 6, 6 |
| 7 | 7, 9, 3, 1 |
| 8 | 8, 4, 2, 6 |
| 9 | 9, 1, 9, 1 |
For the base 7 and the exponent 13, the exponent 13 acts like 1, 5, 9 or 13, which is power 1 in the cycle. The cycle for 7 starts at 7, so the answer is 7.
Input: bases = [9], exponents = [1]
Output: [9]The first power of 9 is 9.
Input: bases = [4, 4], exponents = [3, 4]
Output: [4, 6]4 cubed is 64, which ends in 4, and 4 to the fourth is 256, which ends in 6.
1 ≤ len(bases) = len(exponents) ≤ 100,0000 ≤ bases[i] ≤ 1,000,000,000,0001 ≤ exponents[i] ≤ 1,000,000,000,000You cannot multiply the base into itself 1,000,000,000,000 times, and you cannot build the power: it can have about 12,000,000,000,000 digits. The answer must come from a rule that does not depend on the size of the exponent.