#B3996. Digit Transformation Puzzle
Digit Transformation Puzzle
Digit Transformation Puzzle
Given a positive integer \(n\), you are allowed to perform at most \(q\) operations. In each operation, perform the following two steps:
- Remove the last digit (least significant digit) of \(n\) and denote it as \(x\). Let the remaining part of \(n\) be \(r\) (if \(n\) is a single digit, then \(r = 0\)).
- Compute \(x^2\) and take its last digit, denoted by \(d\). Then, if \(d \neq 0\), prepend it before \(r\) (i.e. insert \(d\) as the new most significant digit). If \(d = 0\), do not include it as a leading digit, and the new number will just be \(r\).
This entire process counts as one operation. For example, starting with \(142\):
- Operation 1: \(x = 2\), so \(x^2 = 4\) and \(r = 14\). Prepending 4 to 14 yields \(414\).
- Operation 2: Now with \(414\), \(x = 4\), so \(x^2 = 16\) and \(d = 6\), \(r = 41\). Prepending 6 to 41 yields \(641\).
- And so on…
Your task is to determine whether it is possible to transform \(n\) into another positive integer \(m\) using no more than \(q\) operations.
inputFormat
The input consists of a single line with three positive integers: \(n\), \(m\), and \(q\), separated by spaces.
\(n\) is the starting number, \(m\) is the target number, and \(q\) is the maximum number of allowed operations.
outputFormat
Output a single line: Yes
if it is possible to obtain \(m\) from \(n\) in at most \(q\) operations, otherwise output No
.
sample
142 616 4
Yes