QOJ.ac

QOJ

実行時間制限: 2.0 s メモリ制限: 1024 MB 満点: 100 ハック可能 ✓

#15409. 紧凑编码

統計

二进制格式通常使用紧凑的整数表示方法。考虑将一个 32 位无符号整数写入文件:你必须总是保留 4 个字节来表示它(记住,8 个比特构成一个字节)。然而,在许多实际应用中,整数值往往较小。使用固定的 4 字节表示来写入这些较小的值会导致文件中大部分都充满了零字节。

为了使表示更加紧凑,我们引入了以下编码方案。每个值被表示为一个字节序列 $b_1, b_2, \dots, b_k$,其中每个 $b_i$ 是一个介于 0 到 255 之间(含)的整数。每个字节的最高有效位作为延续标志,而低 7 位承载实际数据。如果延续标志为 1,则表示后面还有更多字节;对于最后一个字节,该标志为 0。该表示采用大端序(big-endian),这意味着 $b_1$ 包含编码值的高位。

例如,以下是如何找到 $n = 112025$ 的紧凑表示。首先,我们找到它的二进制表示:

$$112025 = 11011010110011001_2$$

接下来,我们将其分割成 7 位的块,必要时在左侧补零:

$$0000110 / 1101011 / 0011001$$

前两个块后面还有其他块,因此对应字节的最高有效位设为 1。最后一个块后面没有其他块,因此其最高有效位为 0。这得到了:

$$b_1 = 10000110_2 = 134$$ $$b_2 = 11101011_2 = 235$$ $$b_3 = 00011001_2 = 25$$

给你一个整数 $n$,你的任务是求出它的紧凑表示。

输入格式

唯一的一行包含一个整数 $n$($0 \le n \le 2^{31} - 1$)。

输出格式

输出一个介于 0 到 255 之间(含)的整数序列,表示 $n$ 的紧凑编码。该编码不能包含数据位全为零的前导字节:也就是说,它不能以 128 开头。

样例

输入样例 1

112025

输出样例 1

134 235 25

输入样例 2

128

输出样例 2

129 0

输入样例 3

0

输出样例 3

0

输入样例 4

42

输出样例 4

42

输入样例 5

16384

输出样例 5

129 128 0

输入样例 6

2147483647

输出样例 6

135 255 255 255 127

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.