#C3584. Convergence of Digit Square Sum Sequence

    ID: 47027 Type: Default 1000ms 256MiB

Convergence of Digit Square Sum Sequence

Convergence of Digit Square Sum Sequence

Given an integer \(a_0\) and an integer \(K\), define a sequence \(a_0, a_1, a_2, \ldots\) where each subsequent term is the sum of the squares of the digits of the previous term. In particular, the transformation can be written in LaTeX as:

\( a_{n+1} = \sum_{d \in D(a_n)} d^2 \),

where \(D(a_n)\) denotes the set of digits of \(a_n\). Your task is to determine if the sequence reaches the value 1 within \(K\) steps (including the possibility that \(a_0 = 1\)). If the sequence reaches 1 within \(K\) steps, print YES, otherwise print NO.

Note: The operation should be applied at most \(K\) times. For example, if \(a_0 = 19\) and \(K = 10\), the sequence eventually reaches 1, so the answer is YES.

inputFormat

The input is given via standard input (stdin) and is formatted as follows:

  • The first line contains a single integer \(T\) representing the number of test cases.
  • The following \(T\) lines each contain two space-separated integers \(a_0\) and \(K\), where \(a_0\) is the starting integer and \(K\) is the maximum number of steps allowed.

outputFormat

For each test case, output a single line containing either YES if the sequence reaches 1 within \(K\) steps or NO otherwise. The output should be printed via standard output (stdout).

## sample
3
19 10
82 5
7 2
YES

YES NO

</p>