2203B.md (1337B)
1 Source: https://codeforces.com/problemset/problem/2203/B 2 3 B. Beautiful Numbers 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 512 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Let's define $F(x)$ as the sum of the digits of $x$. An integer $x$ is considered beautiful if $F(F(x)) = F(x)$. 22 23 You are given an integer $x$. In one move, you can choose any digit in the number and replace it with another. The resulting number cannot have leading zeros. 24 25 Your task is to calculate the minimum number of moves (possibly zero) required to make the given number beautiful. 26 27 Input 28 29 The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. 30 31 The only line of each test case contains a single integer $x$ ($1 \le x \le 10^{18}$). 32 33 Output 34 35 For each test case, print a single integer — the minimum number of moves (possibly zero) required to make the given number beautiful. 36 37 Example 38 39 Input 40 41 ``` 42 4 43 44 1 45 46 37 47 48 645 49 50 2374236843276813 51 ``` 52 53 Output 54 55 ``` 56 0 57 58 1 59 60 2 61 62 12 63 ``` 64 65 Note 66 67 In the first example, the given number is already beautiful. 68 69 In the second example, in one move, we can get the beautiful number $3\underline{3}$ (the changed digit is underlined). 70 71 In the third example, in two moves, we can get the beautiful number $\underline{1}4\underline{0}$ (the changed digits are underlined).