SP410 VHUFFM - Variable Radix Huffman Encoding

Description

Huffman encoding is a method of developing an optimal encoding of the symbols in a _source alphabet_ using symbols from a _target alphabet_ when the frequencies of each of the symbols in the source alphabet are known. Optimal means the average length of an encoded message will be minimized. In this problem you are to determine an encoding of the first _N_ uppercase letters (the source alphabet, ![tex2html_wrap_inline87](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/3122a084ae444f1cf5d0d838a7b575a21ee5e58e.png) through ![tex2html_wrap_inline89](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/f0d183168ef51175c11f482894edee7a8f5838b5.png) , with frequencies ![tex2html_wrap_inline91](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/ed521e8e7356fb5bd15196b3152ae22ed803397a.png) through ![tex2html_wrap_inline93](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/5847e9ea37e0837d2adeb4ed6974b1ec21edb151.png) ) into the first _R_ decimal digits (the target alphabet, ![tex2html_wrap_inline97](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/27507bbccfc8f512aa47df44d83b3799d9860c15.png) through ![tex2html_wrap_inline99](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/81c0ed6fecc0c3cb3cafe3f39b7a98e35173f2a6.png) ). Consider determining the encoding when _R_=2. Encoding proceeds in several passes. In each pass the two source symbols with the lowest frequencies, say ![tex2html_wrap_inline87](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/3122a084ae444f1cf5d0d838a7b575a21ee5e58e.png) and ![tex2html_wrap_inline105](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/6c49b728cdd633f62afe2403b3705fe46eb4f2de.png) , are grouped to form a new ``combination letter" whose frequency is the sum of ![tex2html_wrap_inline91](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/ed521e8e7356fb5bd15196b3152ae22ed803397a.png) and ![tex2html_wrap_inline109](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/190c644dd013a13946f6db37bd52d78e6878f851.png) . If there is a tie for the lowest or second lowest frequency, the letter occurring earlier in the alphabet is selected. After some number of passes only two letters remain to be combined. The letters combined in each pass are assigned one of the symbols from the target alphabet. The letter with the lower frequency is assigned the code 0, and the other letter is assigned the code 1. (If each letter in a combined group has the same frequency, then 0 is assigned to the one earliest in the alphabet. For the purpose of comparisons, the value of a ``combination letter" is the value of the earliest letter in the combination.) The final code sequence for a source symbol is formed by concatenating the target alphabet symbols assigned as each combination letter using the source symbol is formed. The target symbols are concatenated in the reverse order that they are assigned so that the first symbol in the final code sequence is the last target symbol assigned to a combination letter. The two illustrations below demonstrate the process for _R_=2. ![tabular23](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/6963c671eee6e8cc949f975ab0118f3e91d309d8.png) When _R_ is larger than 2, _R_ symbols are grouped in each pass. Since each pass effectively replaces _R_ letters or combination letters by 1 combination letter, and the last pass must combine _R_ letters or combination letters, the source alphabet must contain _k_\*(_R_-1)+_R_ letters, for some integer _k_. Since _N_ may not be this large, an appropriate number of fictitious letters with zero frequencies must be included. These fictitious letters are not to be included in the output. In making comparisons, the fictitious letters are later than any of the letters in the alphabet. Now the basic process of determining the Huffman encoding is the same as for the _R_=2 case. In each pass, the _R_ letters with the lowest frequencies are grouped, forming a new combination letter with a frequency equal to the sum of the letters included in the group. The letters that were grouped are assigned the target alphabet symbols 0 through _R_-1. 0 is assigned to the letter in the combination with the lowest frequency, 1 to the next lowest frequency, and so forth. If several of the letters in the group have the same frequency, the one earliest in the alphabet is assigned the smaller target symbol, and so forth. The illustration below demonstrates the process for _R_=3. ![tabular63](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/f6a8028eef7435ef4225dc9b9842576eef22c2fb.png)

Input Format

The input will contain one or more data sets, one per line. Each data set consists of an integer value for _R_ (between 2 and 10), an integer value for _N_ (between 2 and 26), and the integer frequencies ![tex2html_wrap_inline91](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/ed521e8e7356fb5bd15196b3152ae22ed803397a.png) through ![tex2html_wrap_inline93](https://cdn.luogu.com.cn/upload/vjudge_pic/SP410/5847e9ea37e0837d2adeb4ed6974b1ec21edb151.png) , each of which is between 1 and 999. The end of data for the entire input is the number 0 for _R_; it is not considered to be a separate data set.

Output Format

For each data set, display its number (numbering is sequential starting with 1) and the average target symbol length (rounded to two decimal places) on one line. Then display the _N_ letters of the source alphabet and the corresponding Huffman codes, one letter and code per line. Print a blank line after each test case. The examples below illustrate the required output format.