Quality of Check Digits
Time limit1sMemory limit512 MB
Given a 10x10 operation table, count how many of the 10000 four-digit basic IDs let a single-digit change or an adjacent swap pass the check digit test.
- Level
Easy3 of 10
- Topics
- Implementation, Brute force, Simulation
- Solved
- No attempts yet
Problem
A city is introducing a five-digit social security number. The first four digits are the basic ID number, which runs from 0000 to 9999, and the last digit is a check digit that catches numbers written down wrong.
The check digit comes from an operation table. An operation table is a 10 x 10 table of decimal digits whose diagonal entries are all 0. Rows and columns are numbered from 0, and is the entry in row and column .
For a basic ID number , the check digit is
and the social security number is the five-digit sequence .
Operation Table 1 is shown below. The leftmost column holds the row number.
With Operation Table 1, the check digit of the basic ID number 2016 is
so the social security number is 20166.
The check digit depends on the table in use. Let Operation Table 2 be the table holding in row and column . It gives the check digit 3 for the same basic ID number 2016, so the social security number becomes 20163.
A five-digit sequence is tested by
A correct social security number satisfies , so check returns , which is 0 because the diagonal entries are 0. A non-zero value means the sequence is not a correct social security number. A wrong sequence can still return 0, and which mistakes the function catches depends on the table.
The city wants to catch two common mistakes on the five-digit sequence.
- writing one digit as a different digit
- swapping two adjacent digits
Both kinds happen on the check digit as well as on the four basic ID digits. Swapping two adjacent digits that are equal leaves the sequence unchanged, so it does not count as a mistake. Each basic ID number therefore yields 45 sequences with one digit changed, plus one sequence for each adjacent pair holding two different digits.
The table fails on a basic ID number if at least one of those sequences has check equal to 0. Operation Table 1 fails on no basic ID number. Operation Table 2 fails on 3439 of them: for example 20613, obtained by swapping the third and fourth digits of 20163, has check equal to 0.
Given an operation table, count the basic ID numbers from 0000 to 9999 on which the table fails.
Input
Ten lines describe the operation table. Line holds row as ten integers separated by single spaces. Every is between 0 and 9, and is 0.
Output
Print on one line the number of basic ID numbers for which the table fails to catch at least one of the two mistakes.