COCI '18 Contest 6 #1 Lun

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type

Mom Tihana wanted to buy her daughter Leda a plush toy via an online store. During the purchase process, the system asked for a credit card number to be entered. However, the purchase failed because Tihana mistakenly wrote one of the digits from that number. Internet research has revealed that such systems recognize the wrong number based on Luhn's algorithm.

This algorithm confirms the correctness of the number using a control digit which is always the last digit in the number. Steps to determine the validity of a number are:

  • Starting from the second digit from the right in the number (tens of the number), double the value of every second digit to the left. If this product is greater than nine, then the digits of that product should be summed up.
  • Calculate the sum of all values obtained in the previous step.
  • The sum thus obtained should be multiplied by nine and it should be determined the remainder of division by ten.
  • If the resulting remainder is equal to the last digit of the number (unit), the number is considered valid.

E.g. account number 79927398713 is considered valid because the end right digit 3 can be obtained from the remaining digits in the way described.

Account number79927398713
Double every other718947691672-
Sum79
(1+8)
947697
(1+6)
72= 67
(\text{Sum after intermediate step} \cdot 9) \bmod 10 = (67 \cdot 9) \bmod 10 = 603 \bmod 10 = 3

Write a program that loads the card number as an N-string that consists only of digits and exactly one sign x, and prints the smallest one-digit number which we can replace the sign x with so that the account number is valid.

Input Specification

In the first line there is an integer number N (1 \le N \le 100), the length of string from the task's test.

In the second line there is a string of length N consisting of just signs 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and exactly one sign x.

Output Specification

In the only line of the output it should be printed the required one-digit number.

Scoring

In the test samples totally worth 25 points, the sign x will always be in the last place in the string.

Sample Input 1

11
7992739871x

Sample Output 1

3

Sample Input 2

5
x2464

Sample Output 2

5

Sample Input 3

10
93380x1696

Sample Output 3

1

Comments


  • 6
    vsarca  commented on Sept. 7, 2024, 12:01 a.m.

    It took me 15 hours to solve this in Brainf***. My code is 978 characters long. At this time my submission is the 9th fastest on the leaderboard. I would rate this as comparable to a 17p solved in C++ in terms of implementation difficulty and a 9/10 in terms of fun. I do not recommend this for beginner Brainf*** programmers. Good luck!