#C8298. Digital Root and Magical Number

    ID: 52264 Type: Default 1000ms 256MiB

Digital Root and Magical Number

Digital Root and Magical Number

You are given a number or a list of numbers. The digital root of a nonnegative integer \(n\) is obtained by repeatedly summing its digits until a single-digit number is produced. For example, the digital root of 942 is computed as \(9+4+2=15\), and then \(1+5=6\); hence, the digital root is 6.

The problem has two modes:

  • If the input contains a single integer, compute its digital root.
  • If the input contains more than one integer, the first integer indicates \(N\), the number of elements in the list. Then follow \(N\) integers that represent ages (or any numbers). In this case, first compute the digital root of each number, sum these values, and finally compute the digital root of the sum. This final value is called the magical number.

Examples:

  • Input: 5 → Output: 5 (since the digital root of 5 is 5)
  • Input: 942 → Output: 6
  • Input: 1\n942 → Output: 6 (magical number for a single number is the same as its digital root)
  • Input: 4\n942 123 456 789 → Output: 6
  • Input: 10\n1 1 1 1 1 1 1 1 1 1 → Output: 1

inputFormat

The input is read from standard input. It consists of one or more integers separated by whitespace. If there is only one integer, it represents a number and you should compute its digital root.

If there are more integers, the first integer \(N\) denotes the number of subsequent integers. These \(N\) integers form a list. Compute the digital root of each number, sum these digital roots, then compute and output the digital root of the sum (i.e. the magical number).

outputFormat

Output a single integer to the standard output, which is either the digital root (in the single integer case) or the magical number (in the list case).

## sample
5
5