#P12381. Safe Lock Transformation
Safe Lock Transformation
Safe Lock Transformation
Blue has a safe whose lock is represented by an n-digit number. Blue can change any digit by either adding 1 or subtracting 1 in a single operation. However, special cascading rules apply:
- If you add 1 to a digit that is originally 9, the digit becomes 0 and a carry is generated to the digit immediately to the left.
- If you subtract 1 from a digit that is originally 0, the digit becomes 9 and a borrow is generated to the digit immediately to the left.
- This carry/borrow propagates to the left as long as the affected digit is at the boundary (i.e. 9 for addition and 0 for subtraction). When the operation finally changes the highest (leftmost) digit, any further propagation is ignored.
For example:
- 00000 → subtract 1 on the 5th digit: the 5th digit becomes -1 (treated as 9) and a borrow propagates through the 4th, 3rd, 2nd, and finally the 1st digit becomes 9 (propagation stops) resulting in 99999.
- 99999 → subtract 1 on the 5th digit: only the 5th digit changes from 9 to 8, resulting in 99998.
- 00000 → subtract 1 on the 4th digit: digits 1 to 4 get affected, and the result is 99990 (only positions 1–4 change).
- 97993 → add 1 on the 4th digit: since the 4th digit is 9, adding 1 produces 10 so it becomes 0 and carries to the 3rd digit, which is 9. This causes another carry to the 2nd digit (7 becomes 8), and the process stops. The final number is 98003.
- 99909 → add 1 on the 3rd digit: the 3rd digit 9 turns to 0 carrying to the 2nd digit; the 2nd digit 9 turns to 0 carrying to the 1st digit; the 1st digit 9 turns to 0 (carry ignored). The final result is 00009.
Initially the safe displays the number x, and Blue wishes to transform it into y using the minimum number of operations. Determine the least number of operations required.
Note on operations: When performing an operation on the digit at position i (1-indexed from the left), the procedure is:
$$\text{For addition: }\quad \text{Let } j = i \text{ and } carry = 1.\\ \textbf{while } j \ge 1 \text{ and } carry = 1:\\ \quad \text{if } a_j = 9:\\ \quad\quad a_j = 0, \quad j = j - 1, \quad \text{(carry remains 1)};\\ \quad \text{else}:\\ \quad\quad a_j = a_j + 1, \quad carry = 0;\\ \text{If } j = 0 \text{ (i.e. the highest digit was affected) the extra carry is ignored.} $$$$\text{For subtraction: }\quad \text{Let } j = i \text{ and } carry = 1.\\ \textbf{while } j \ge 1 \text{ and } carry = 1:\\ \quad \text{if } a_j = 0:\\ \quad\quad a_j = 9, \quad j = j - 1, \quad \text{(carry remains 1)};\\ \quad \text{else}:\\ \quad\quad a_j = a_j - 1, \quad carry = 0;\\ \text{If } j = 0 \text{ the extra borrow is ignored.} $$inputFormat
The input consists of three lines:
- An integer n representing the number of digits.
- A string x of length n representing the initial number (with possible leading zeros).
- A string y of length n representing the target number.
outputFormat
Output a single integer, the minimum number of operations required to transform x into y following the rules described above.
sample
5
00000
99999
1