#P7379. Restore Missing Digit in Bank Card Number Using Checksum Algorithm

    ID: 20575 Type: Default 1000ms 256MiB

Restore Missing Digit in Bank Card Number Using Checksum Algorithm

Restore Missing Digit in Bank Card Number Using Checksum Algorithm

Given a bank card number with exactly one missing digit (represented by a question mark '?' in the input), your task is to determine the missing digit such that the card becomes valid under the checksum algorithm described below.

The checksum algorithm is defined as follows:

  1. Starting from the second-to-last digit and moving left, every alternate digit is multiplied by 22. The remaining digits are left unchanged.
  2. For each digit that was multiplied by 22, calculate the sum of its digits (i.e. if the product is greater than or equal to 1010, subtract 99 from it).
  3. Sum all the resulting digits from step 1 and 2 (excluding the check digit, which is the last digit). Then multiply the sum by 99 and take the result modulo 1010.
  4. The computed value from step 3 must equal the check digit (i.e. the last digit of the card number).

    For example, to validate the card number 7992739871379927398713, the computation is organized as follows:

    [ \begin{array}{|c|c|c|c|c|c|c|c|c|c|c|} \hline \text{Card Digits} & 7 & 9 & 9 & 2 & 7 & 3 & 9 & 8 & 7 & 1 & 3 \ \hline \text{Multiply Every Other Digit} & 7 & \red{18} & 9 & \red{4} & 7 & \red{6} & 9 & \red{16} & 7 & \red{2} & - \ \hline \text{After Summing Digits} & 7 & \green{9} & 9 & 4 & 7 & 6 & 9 & \green{7} & 7 & 2 & =67 \ \hline \end{array} ] [ 67 \times 9 \bmod 10 = 3 ] Since the result equals the last digit (33), the card number is valid.

    When one digit is missing from the input card number, fill in the appropriate digit so that the complete card number satisfies the checksum condition described above.

inputFormat

Input is a single string representing the bank card number with exactly one missing digit. The missing digit is denoted by a question mark '?' in the string. For instance, a valid input could be:
7992739871?

outputFormat

Output the full valid bank card number after replacing the '?' with the correct digit so that the checksum algorithm holds.

sample

7992739871?
79927398713