|
1 | 1 | """
|
2 |
| -We define super digit of an integer x using the following rules: |
| 2 | +The super digit problem is defined as follows: |
| 3 | +Given an integer represented as a string and an integer k, |
| 4 | +the goal is to find the super digit of the number formed by concatenating |
| 5 | +the integer n k times. |
3 | 6 |
|
4 |
| -Given an integer, we need to find the super digit of the integer. |
| 7 | +The super digit of a number is defined recursively: |
| 8 | +- If the number has only one digit, that digit is the super digit. |
| 9 | +- Otherwise, the super digit is the super digit of the sum of its digits. |
5 | 10 |
|
6 |
| -If has only 1 digit, then its super digit is x . |
7 |
| -Otherwise, the super digit of x is equal to the super digit of the sum of the digits of . |
8 |
| -For example, the super digit 9875 of will be calculated as: |
9 |
| -
|
10 |
| - super_digit(9875) 9+8+7+5 = 29 |
11 |
| - super_digit(29) 2 + 9 = 11 |
12 |
| - super_digit(11) 1 + 1 = 2 |
13 |
| - super_digit(2) = 2 |
14 |
| -
|
15 |
| -
|
16 |
| - ex -2: |
17 |
| - Here n=148 and k=3 , so p=148148148 . |
18 |
| -
|
19 |
| -super_digit(P) = super_digit(148148148) |
20 |
| - = super_digit(1+4+8+1+4+8+1+4+8) |
21 |
| - = super_digit(39) |
22 |
| - = super_digit(3+9) |
23 |
| - = super_digit(12) |
24 |
| - = super_digit(1+2) |
25 |
| - = super_digit(3) |
26 |
| - = 3 |
| 11 | +For example, for n = "9875" and k = 4, the concatenated number is: |
| 12 | +super_digit(9875987598759875), which can be reduced by summing its digits. |
27 | 13 | """
|
28 | 14 |
|
29 |
| -""" |
30 |
| -
|
31 |
| -Sample Input 0 |
32 |
| -148 3 |
| 15 | +from __future__ import annotations |
33 | 16 |
|
34 |
| -Sample Output 0 |
| 17 | +def superDigit(n: str, k: int) -> int: |
| 18 | + """ |
| 19 | + Computes the super digit of a number formed by concatenating n k times. |
35 | 20 |
|
36 |
| -3 |
| 21 | + Parameters: |
| 22 | + n (str): The string representation of the integer. |
| 23 | + k (int): The number of times to concatenate n. |
37 | 24 |
|
38 |
| -""" |
| 25 | + Returns: |
| 26 | + int: The super digit of the concatenated number. |
39 | 27 |
|
| 28 | + >>> superDigit("148", 3) |
| 29 | + 3 |
| 30 | + >>> superDigit("9875", 4) |
| 31 | + 8 |
| 32 | + >>> superDigit("123", 3) |
| 33 | + 9 |
| 34 | + """ |
40 | 35 |
|
41 |
| -def superDigit(n, k): |
42 | 36 | # Calculate the initial sum of the digits in n
|
43 | 37 | digit_sum = sum(int(digit) for digit in n)
|
44 |
| - |
| 38 | + |
45 | 39 | # Multiply the sum by k
|
46 | 40 | total_sum = digit_sum * k
|
47 |
| - |
| 41 | + |
48 | 42 | # Recursive function to find the super digit
|
49 | 43 | while total_sum >= 10:
|
50 | 44 | total_sum = sum(int(digit) for digit in str(total_sum))
|
51 |
| - |
| 45 | + |
52 | 46 | return total_sum
|
53 | 47 |
|
54 |
| - |
55 |
| -if __name__ == "__main__": |
| 48 | +if __name__ == '__main__': |
| 49 | + # Read input and split it into n and k |
56 | 50 | first_multiple_input = input().rstrip().split()
|
57 |
| - |
58 |
| - n = first_multiple_input[0] |
59 |
| - k = int(first_multiple_input[1]) |
60 |
| - |
| 51 | + |
| 52 | + n = first_multiple_input[0] # n as a string |
| 53 | + k = int(first_multiple_input[1]) # k as an integer |
| 54 | + |
| 55 | + # Call the superDigit function and print the result |
61 | 56 | result = superDigit(n, k)
|
62 |
| - |
63 | 57 | print(result)
|
0 commit comments