QOJ.ac

QOJ

시간 제한: 1 s 메모리 제한: 512 MB 총점: 100 난이도: [표시] 해킹 가능 ✓

#174. 檢查碼的品質

통계

你所居住的小城市計畫引入一套新的社會安全號碼(SSN)系統。每位市民都將由一個五位數的 SSN 唯一識別。其前四位數字表示基本 ID 號碼(0000–9999),最後一位數字則是用於偵測錯誤的檢查碼。

為了計算檢查碼,該市決定使用一張運算表。運算表是一張由十進位數字組成的 $10 \times 10$ 表格,其主對角線上的元素全部為 $0$。以下是兩張運算表示例。

運算表 1

$0$ $1$ $2$ $3$ $4$ $5$ $6$ $7$ $8$ $9$
$0$ $0$ $3$ $1$ $7$ $5$ $9$ $8$ $6$ $4$ $2$
$1$ $7$ $0$ $9$ $2$ $1$ $5$ $4$ $8$ $6$ $3$
$2$ $4$ $2$ $0$ $6$ $8$ $7$ $1$ $3$ $5$ $9$
$3$ $1$ $7$ $5$ $0$ $9$ $8$ $3$ $4$ $2$ $6$
$4$ $6$ $1$ $2$ $3$ $0$ $4$ $5$ $9$ $7$ $8$
$5$ $3$ $6$ $7$ $4$ $2$ $0$ $9$ $5$ $8$ $1$
$6$ $5$ $8$ $6$ $9$ $7$ $2$ $0$ $1$ $3$ $4$
$7$ $8$ $9$ $4$ $5$ $3$ $6$ $2$ $0$ $1$ $7$
$8$ $9$ $4$ $3$ $8$ $6$ $1$ $7$ $2$ $0$ $5$
$9$ $2$ $5$ $8$ $1$ $4$ $3$ $6$ $7$ $9$ $0$

運算表 2

$0$ $1$ $2$ $3$ $4$ $5$ $6$ $7$ $8$ $9$
$0$ $0$ $1$ $2$ $3$ $4$ $5$ $6$ $7$ $8$ $9$
$1$ $9$ $0$ $1$ $2$ $3$ $4$ $5$ $6$ $7$ $8$
$2$ $8$ $9$ $0$ $1$ $2$ $3$ $4$ $5$ $6$ $7$
$3$ $7$ $8$ $9$ $0$ $1$ $2$ $3$ $4$ $5$ $6$
$4$ $6$ $7$ $8$ $9$ $0$ $1$ $2$ $3$ $4$ $5$
$5$ $5$ $6$ $7$ $8$ $9$ $0$ $1$ $2$ $3$ $4$
$6$ $4$ $5$ $6$ $7$ $8$ $9$ $0$ $1$ $2$ $3$
$7$ $3$ $4$ $5$ $6$ $7$ $8$ $9$ $0$ $1$ $2$
$8$ $2$ $3$ $4$ $5$ $6$ $7$ $8$ $9$ $0$ $1$
$9$ $1$ $2$ $3$ $4$ $5$ $6$ $7$ $8$ $9$ $0$

使用運算表時,四位基本 ID 號碼 $abcd$ 的檢查碼 $e$ 按如下公式計算。此處,$i \otimes j$ 表示表中第 $i$ 列第 $j$ 行的元素。

$$e = (((0 \otimes a) \otimes b) \otimes c) \otimes d$$

例如,使用運算表 1 時,基本 ID 號碼 $abcd = 2016$ 的檢查碼 $e$ 按如下方式計算。

$$\begin{aligned} e &= (((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 \end{aligned}$$

因此,對應的 SSN 為 $20166$。

注意,檢查碼取決於所使用的運算表。若使用運算表 2,對於同一個基本 ID 號碼 $2016$,我們得到 $e = 3$,完整的 SSN 將為 $20163$。

$$\begin{array}{c} \begin{aligned} 2016 & \\ \Downarrow & \; \text{+使用運算表 2 計算檢查碼} \\ 20163 & \\ \Downarrow & \; \text{人為錯誤} \end{aligned} \\[6pt] \boxed{\begin{array}{c} \overbrace{\mathbf{0}0163 \quad \mathbf{1}0163 \quad \dots \quad \mathbf{9}0163 \quad 2\mathbf{1}163 \quad 2\mathbf{2}163 \quad \dots \quad 2016\mathbf{8} \quad 2016\mathbf{9}}^{\text{改寫單一數字}} \\[10pt] \overbrace{\mathbf{02}163 \quad 2\mathbf{10}63 \quad 20\mathbf{61}3 \quad 201\mathbf{36}}^{\text{交換相鄰兩個數字}} \end{array}} \end{array}$$

圖 B.1. 兩種常見的人為錯誤

加上檢查碼的目的是偵測書寫或輸入 SSN 時發生的人為錯誤。下面的 check 函式可以偵測某些人為錯誤。對於一個五位數 $abcde$,check 函式定義如下。

$$\operatorname{check}(abcde) = ((((0 \otimes a) \otimes b) \otimes c) \otimes d) \otimes e$$

對於正確的 SSN,此函式會回傳 $0$。這是因為運算表中主對角線上的每個元素都是 $0$,且對於正確的 SSN 有 $e = (((0 \otimes a) \otimes b) \otimes c) \otimes d$:

$$\operatorname{check}(abcde) = ((((0 \otimes a) \otimes b) \otimes c) \otimes d) \otimes e = e \otimes e = 0.$$

另一方面,若 check 回傳非零值,則表示給定的號碼不可能是正確的 SSN。注意,依據所使用的運算表不同,check 函式對於錯誤的 SSN 也可能回傳 $0$。能夠偵測出哪些類型的錯誤取決於所使用的運算表;運算表決定了錯誤偵測的品質。

市政當局希望偵測數字序列上的兩種常見人為錯誤:改寫單一數字,以及交換相鄰兩個數字,如圖 B.1 所示。

如果一張運算表能夠對由四位基本 ID 號碼 0000–9999 產生的所有 SSN 偵測出全部這兩種常見錯誤,則稱該運算表是好的。注意,發生在檢查碼上的錯誤與發生在四位基本 ID 數字上的錯誤一樣,都應當被偵測出來。例如,運算表 1 是好的。運算表 2 不是好的,因為對於將正確的 SSN $20163$ 的第 3 位和第 4 位數字交換後得到的 $20613$,有 $\operatorname{check}(20613) = 0$。事實上,在全部 $10000$ 個基本 ID 號碼中,運算表 2 對多達 $3439$ 個基本 ID 號碼無法偵測出一個或多個常見錯誤。

給定一張運算表,請統計有多少個基本 ID 號碼滿足:該運算表無法偵測出其對應的一個或多個常見錯誤,以此判斷這張運算表的品質。

輸入格式

輸入包含單一測試資料,格式如下。

$$\begin{array}{cccc} x_{00} & x_{01} & \cdots & x_{09} \\ \vdots & & & \\ x_{90} & x_{91} & \cdots & x_{99} \end{array}$$

輸入描述一張運算表,其中 $x_{ij}$ 表示第 $i$ 列第 $j$ 行的十進位數字。輸入的每一行對應表格中的一列,同一列中的元素之間以單一空白字元分隔。主對角線元素 $x_{ii}$($i = 0, \ldots, 9$)恆為 $0$。

輸出格式

輸出給定的運算表無法偵測出一個或多個常見人為錯誤的基本 ID 號碼個數。

範例

輸入格式 1

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

輸出格式 1

0

輸入格式 2

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

輸出格式 2

3439

輸入格式 3

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

輸出格式 3

9995

輸入格式 4

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

輸出格式 4

10000

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.