26. Intelligence Agency Digit Sum
Difficulty: easy · Math
Problem
An intelligence agency is studying coded reports, each made of two numbers `N` and `R`. To decode a report: 1. Add up all the digits of `N`. 2. Repeat that sum `R` times, i.e. compute `R * (digit sum of N)`. 3. Keep adding the digits of the result until a single digit remains. Print that single digit. If `R` is `0`, print `0`. For example, `N = 99`, `R = 3`: the digit sum is 18, `3 * 18 = 54`, and `5 + 4 = 9`.
Input
- Line 1: the integer `N`. - Line 2: the integer `R`.
Output
Print the resulting single digit.
Example 1
Input: 99 3 Output: 9
Explanation: Digit sum 9+9 = 18; 18*3 = 54; 5+4 = 9.
Example 2
Input: 1234 2 Output: 2
Explanation: Digit sum 10; 10*2 = 20; 2+0 = 2.
Example 3
Input: 57 0 Output: 0
Explanation: R is 0, so the output is 0.
Constraints
- 1 <= N <= 10^18 - 0 <= R <= 10^9
Solutions are judged against 3 sample and 11 hidden tests. Sign in to solve it · All problems