#P16061. [CSPro 24] 登机牌条码

[CSPro 24] 登机牌条码

Background

The testdata on Luogu is only for non-official communication and is not official testdata. Official judging link: https://www.cspro.org/.

Xixiaifu Island has beautiful scenery and is crowded with tourists. However, because transportation to and from the outside world relies only on ferries, the inconvenience of transportation has seriously limited the development of the island’s tourism industry. After persistent effort, the Xixiaifu Island administrative committee secured an investment and built a general aviation airport. After three years of intensive construction of the main facilities, Xixiaifu Island General Aviation Airport has finally started installing and debugging the software and hardware systems inside the terminal building. Xiao C is a development engineer in the airport operating company’s IT department. Recently, an important task for the IT department is to develop a self-service boarding pass printing system. The following figure shows a boarding pass sample designed by the design department according to the industry standards of the International Civil Aviation Organization.

The most important part of the boarding pass is the machine-readable barcode at the very bottom. Xiao C is responsible for developing the algorithm to generate this machine-readable barcode. From the data to be encoded to the barcode, there are many steps in between. Xiao C asks you to help handle the data encoding part.

Problem Description

The barcode on the boarding pass is a PDF417 code. The structure of a PDF417 code is shown below.

:::align{center} :::

The basic element of a PDF417 code is a Module. All modules are rectangles of the same size, filled with either black or white. Modules first form rows, and multiple rows are stacked to form the entire PDF417 code. In each row, every 1717 modules represent one Code word. A code word is the smallest data unit in PDF417 encoding. In each code word pattern, there are four black rectangles and four white rectangles arranged alternately, which is where “417” comes from. Each row begins and ends with fixed start and stop patterns. Adjacent to them are the left and right row indicators, which represent information such as the row number and the number of code words in the row. The middle part is the data area. The encoding process is: first, according to the encoding rules, convert the data to be encoded into code words; then compute error correction code words based on the chosen width of the PDF417 code (i.e., the number of code words per row) and the redundancy level; finally, convert the code words into corresponding patterns by rules, and fill them into the data area in order from left to right and from top to bottom, and combine them with the start/stop patterns and the left/right row indicators to form a complete PDF417 code.

Each code word is a number from 00 to 928928, and each code word can encode two input characters. For the input data to be encoded, encode it according to the table below. The encoder has three modes: uppercase letter mode, lowercase letter mode, and digit mode. At the beginning of encoding, the encoder is in uppercase letter mode. When the encoder is in a certain mode, it can only encode the corresponding type of characters. If you need to encode other types of characters, you must switch to the corresponding mode using special values. There can be multiple ways to switch modes. For example, to switch from uppercase mode to lowercase mode, you can switch directly using 2727, or you can first switch to digit mode using 2828 and then immediately switch to lowercase mode using 2727. You need to choose the shortest way to switch, so only the former method is correct. Note that from lowercase mode you cannot switch directly to uppercase mode; you must go through digit mode as a transition.

Value Uppercase Mode Lowercase Mode Digit Mode
00 A a 00
11 B b 11
22 C c 22
33 D d 33
44 E e 44
55 F f 55
66 G g 66
77 H h 77
88 I i 88
99 J j 99
1010 K k
1111 L l
1212 M m
1313 N n
1414 O o
1515 P p
1616 Q q
1717 R r
1818 S s
1919 T t
2020 U u
2121 V v
2222 W w
2323 X x
2424 Y y
2525 Z z
2727 Lowercase
2828 Digit Uppercase
2929 Padding

Using this method, you can obtain a sequence of numbers not exceeding 3030. If there is an odd number of such numbers, append a 2929 at the end to make it an even number. Group them into pairs. Suppose HH and LL are two consecutive numbers in a pair, then the resulting code word is:

30×H+L\begin{aligned} 30 \times H + L \end{aligned}

For example, to encode “HE1lo\text{HE1lo}”, first generate the number sequence according to the alphabet:

H E    1    l  o
7 4 28 1 27 11 14

Since there is an odd number of numbers, append 2929 at the end, and then group them into pairs:

(7, 4), (28, 1), (27, 11), (14, 29)

Finally compute the code words. For example, 30×7+4=21430 \times 7 + 4 = 214, and so on, obtaining the code words:

214, 841, 821, 449

Next, compute the error correction codes. The number of error correction code words is determined by the error correction level. Suppose the error correction level is s(0≤s≤8)s(0 \leq s \leq 8), then the number of error correction code words is k=2s+1k = 2^{s+1}. In particular, if s=−1s = -1 is specified, it means no error correction code words are needed. To compute error correction code words, first determine the data code words. The data code words are formed by concatenating the following data in order (as shown in the figure):

:::align{center} :::

  • One length code word, representing the total number of data code words nn, including this length code word, the data code words, and the padding code words.
  • Several data code words, which are the code word sequence computed above.
  • Zero or more padding code words, each being a repeated 900900, so that the total number of code words (including the error correction code words) is exactly divisible by the row width of the data area.

Let all data code words be dn−1,dn−2,…,d0d_{n-1}, d_{n-2}, \dots, d_0 in order, and the error correction code words be ck−1,ck−2,…,c0c_{k-1}, c_{k-2}, \dots, c_0 in order. Then the error correction code words are computed as follows:

Take the degree-kk polynomial g(x)=(x−3)(x−32)…(x−3k)g(x) = (x - 3)(x - 3^2)\dots(x - 3^k), and the degree-(n−1)(n - 1) polynomial $d(x) = d_{n-1}x^{n-1} + \dots + d_{n-2}x^{n-2} + \dots + d_1x + d_0$. Find a polynomial q(x)q(x) and a polynomial r(x)r(x) of degree not exceeding (k−1)(k - 1) such that

$$\begin{aligned} x^k d(x) &\equiv q(x) g(x) - r(x) \end{aligned}$$

Then, for each coefficient of the xix^i term in r(x)r(x), take it modulo 929929 (take the positive value). The resulting number is the error correction code word cic_i.

For example, if you want to encode HE1lo\text{HE1lo} into a PDF417 barcode, and the row width of the data area is 44 code words (i.e., 6868 modules), and the error correction level is 00. Then there are two error correction code words. From the previous encoding result, there are 44 data code words. Adding one length code word gives 77 code words in total. Therefore, you need to add one padding code word so that the total number of code words including the error correction code words can be divisible by 44. In this way, there are 66 data code words used to compute the error correction code words:

6, 214, 841, 821, 449, 900

Therefore, g(x)=x2−12x+27g(x) = x^2 - 12x + 27, $d(x) = 6x^5 + 214x^4 + 841x^3 + 821x^2 + 449x + 900$. It is not hard to get r(x)=−32902164x+98246277r(x) = -32902164x + 98246277, so we can compute:

$$\begin{aligned} c_1 &= 229 \equiv -32902164 \mod 929, \\ c_0 &= 811 \equiv 98246277 \mod 929. \end{aligned}$$

Thus, the complete code word sequence is:

6, 214, 841, 821, 449, 900, 229, 811

In this problem, the task you need to help Xiao C complete is: given the data to be encoded, compute the code word sequence that needs to be filled into the data area. The processed data contains only uppercase letters, lowercase letters, and digits.

Input Format

Read input from standard input.

The first line contains two integers ww and ss separated by a space, representing the number of code words each row of the data area can hold and the error correction level. It is guaranteed that 0<w<9290 < w < 929 and −1≤s≤8-1 \leq s \leq 8. In particular, when s=−1s = -1, it means no error correction code words are needed.

The second line is a non-empty string containing only uppercase letters, lowercase letters, and digits. Its length guarantees that after encoding, the total number of data code words is less than 929929.

Output Format

Write to standard output.

Output several lines, one number per line, representing the complete encoded code word sequence.

5 -1
HELLO
5
214
341
449
900
4 0
HE1lo
6
214
841
821
449
900
229
811

Hint

Explanation for Sample 1

The data to be encoded is HELLO. First, look it up in the table and map it to numbers. Note that since the encoder starts in uppercase letter mode, no extra mode switching is needed. Therefore the numbers are: 7,4,11,11,147, 4, 11, 11, 14. Since there is an odd number of numbers, append 2929 to form the sequence 7,4,11,11,14,297, 4, 11, 11, 14, 29. Then group them into pairs and compute the code words: 7×30+4=2147 \times 30 + 4 = 214, and so on, getting 214,341,449214, 341, 449. This input does not require generating error correction code words, and the width of the data area is 55 code words. Currently there are 33 data code words, and adding the length code word at the beginning gives 44 code words. Therefore, one padding code word is needed so that the total number of code words reaches 55 and fills one row. Note that the length in the length code word includes all data code words, so the length code word is 55 rather than 44. Finally, the code word sequence is 5,214,341,449,9005, 214, 341, 449, 900.

Explanation for Sample 2

This test case is the example previously used to illustrate the encoding process.

Subtasks

For 20%20\% of the data, s=−1s = -1, and the input string contains only uppercase letters or only lowercase letters.

For 40%40\% of the data, s=−1s = -1.

For 80%80\% of the data, s≤2s \leq 2.

For 100%100\% of the data, all input requirements are satisfied.

Translated by ChatGPT 5