Quality of Check Digits

Time limit1sMemory limit512 MB

Summary
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 i⊗ji \otimes j is the entry in row ii and column jj.

For a basic ID number abcdabcd, the check digit ee is

e=(((0⊗a)⊗b)⊗c)⊗de = (((0 \otimes a) \otimes b) \otimes c) \otimes d

and the social security number is the five-digit sequence abcdeabcde.

Operation Table 1 is shown below. The leftmost column holds the row number.

row0123456789
00317598642
17092154863
24206871359
31750983426
46123045978
53674209581
65869720134
78945362017
89438617205
92581436790

With Operation Table 1, the check digit of the basic ID number 2016 is

e=(((0⊗2)⊗0)⊗1)⊗6=((1⊗0)⊗1)⊗6=(7⊗1)⊗6=9⊗6=6e = (((0 \otimes 2) \otimes 0) \otimes 1) \otimes 6 = ((1 \otimes 0) \otimes 1) \otimes 6 = (7 \otimes 1) \otimes 6 = 9 \otimes 6 = 6

so the social security number is 20166.

The check digit depends on the table in use. Let Operation Table 2 be the table holding (j−i) mod 10(j - i) \bmod 10 in row ii and column jj. It gives the check digit 3 for the same basic ID number 2016, so the social security number becomes 20163.

A five-digit sequence abcdeabcde is tested by

check(abcde)=((((0⊗a)⊗b)⊗c)⊗d)⊗e\mathrm{check}(abcde) = ((((0 \otimes a) \otimes b) \otimes c) \otimes d) \otimes e

A correct social security number satisfies e=(((0⊗a)⊗b)⊗c)⊗de = (((0 \otimes a) \otimes b) \otimes c) \otimes d, so check returns e⊗ee \otimes e, 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 ii holds row ii as ten integers xi0,xi1,…,xi9x_{i0}, x_{i1}, \ldots, x_{i9} separated by single spaces. Every xijx_{ij} is between 0 and 9, and xiix_{ii} 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.

Examples4

  1. Example 1

    Input
    0 3 1 7 5 9 8 6 4 2
    7 0 9 2 1 5 4 8 6 3
    4 2 0 6 8 7 1 3 5 9
    1 7 5 0 9 8 3 4 2 6
    6 1 2 3 0 4 5 9 7 8
    3 6 7 4 2 0 9 5 8 1
    5 8 6 9 7 2 0 1 3 4
    8 9 4 5 3 6 2 0 1 7
    9 4 3 8 6 1 7 2 0 5
    2 5 8 1 4 3 6 7 9 0
    
    Expected output
    0
    
  2. Example 2

    Input
    0 1 2 3 4 5 6 7 8 9
    9 0 1 2 3 4 5 6 7 8
    8 9 0 1 2 3 4 5 6 7
    7 8 9 0 1 2 3 4 5 6
    6 7 8 9 0 1 2 3 4 5
    5 6 7 8 9 0 1 2 3 4
    4 5 6 7 8 9 0 1 2 3
    3 4 5 6 7 8 9 0 1 2
    2 3 4 5 6 7 8 9 0 1
    1 2 3 4 5 6 7 8 9 0
    
    Expected output
    3439
    
  3. Example 3

    Input
    0 9 8 7 6 5 4 3 2 1
    1 0 9 8 7 6 5 4 3 2
    2 1 0 9 8 7 6 5 4 3
    3 2 1 0 9 8 7 6 5 4
    4 3 2 1 0 9 8 7 6 5
    5 4 3 2 1 0 9 8 7 6
    6 5 4 3 2 1 0 9 8 7
    7 6 5 4 3 2 1 0 9 8
    8 7 6 5 4 3 2 1 0 9
    9 8 7 6 5 4 3 2 1 0
    
    Expected output
    9995
    
  4. Example 4

    Input
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    
    Expected output
    10000