#t131. 变长编码
变长编码
Variable-Length Encoding
Problem Description
(This problem is paired with a set of multiple-choice and true/false questions: https://ti.luogu.com.cn/problemset/1130.)
Xiaoming has just learned three ways of representing integers — sign-magnitude, ones' complement, and two's complement — and learned that computers usually store integers in two's complement. But it always bothers him that everyday numbers like still take a whole 4-byte two's-complement word, which feels wasteful.
After some searching, Xiaoming discovered a variable-length encoding for non-negative integers. The rule is as follows:
- Write the given non-negative integer in binary. For example, , and .
- Split the binary number into groups of bits, from the least significant bit to the most significant bit, padding the highest group with leading zeros if it is shorter than bits. For example, becomes a single group
0000000; becomes two groups0011110and0000111. - Starting from the low-order group, prepend one most-significant bit to each group. If the group is the last group, prepend a
0; otherwise prepend a1. Thus is encoded as00000000(one byte), and is encoded as10011110and00000111(two bytes).
This encoding uses fewer bytes for small numbers and can use many bytes for very large numbers. For example, the binary of is $(0001101\ 1011010\ 0110110\ 1001011\ 1110100\ 0100110\ 1001000\ 0010110\ 1001110)_2$, so its variable-length encoding (written in hexadecimal) is CE 96 C8 A6 F4 CB B6 DA 0D, bytes in total.
Can you write a program that computes the variable-length encoding of a non-negative integer?
Input Format
The first line contains a single non-negative integer . It is guaranteed that .
Output Format
Output one line: for each byte of the variable-length encoding of , print its value as a two-digit hexadecimal number (using uppercase letters A–F), with a single space between consecutive bytes.
Sample Input
Sample Input 1
0
Sample Input 2
926
Sample Input 3
987654321012345678
Sample Output
Sample Output 1
00
Sample Output 2
9E 07
Sample Output 3
CE 96 C8 A6 F4 CB B6 DA 0D
Sample Explanation
For input , its binary is , which is grouped by bits into 0000000. Since it is the last group, the most-significant bit is 0, giving 00000000. Its hexadecimal is 00, so we output 00.
Constraints
For all test data, .