QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 128 MB 總分: 100

#580. 1的个数

统计

令 $x$ 为一个由 0 和 1 组成的序列。序列 $x$ 中的一个“极度孤立的 1”(UFO)是指位于序列两端(即第一个或最后一个)且不与任何其他 1 相邻的 1。例如,序列 10001010 有两个 UFO,序列 1101011000 没有 UFO,而序列 1000 只有一个 UFO。

令 $sks(n)$ 表示从 $1$ 到 $n$ 的所有整数的二进制表示中 UFO 的总数。例如,$sks(5)=5$,$sks(64)=59$,$sks(128)=122$,$sks(256)=249$。

我们将处理非常大的数字。因此,我们将以一种简洁的方式表示它们。假设 $x$ 是一个正整数,$x_2$ 是其二进制表示(以 1 开头)。那么 $x$ 的简洁表示是序列 $REP(x)$,由表示连续相同数字块长度的正整数组成。例如:

$$REP(460288)=REP(1110000011000000000_2)=(3,5,2,9)$$

$$REP(408)=REP(110011000_2)=(2,2,2,3)$$

你的任务是编写一个程序,在给定 $REP(n)$ 的情况下,求出 $REP(sks(n))$。

输入格式

第一行包含一个整数 $k$ ($1 \le k \le 1\,000\,000$),表示正整数 $n$ 的简洁表示的长度。第二行包含 $k$ 个整数 $x_1, x_2, \dots, x_k$ ($0 < x_i \le 1\,000\,000\,000$),以空格分隔。序列 $x_1, x_2, \dots, x_k$ 构成了数字 $n$ 的简洁表示。你可以假设 $x_1+x_2+\dots+x_k \le 1\,000\,000\,000$,即 $0 < n < 2^{1\,000\,000\,000}$。

输出格式

你的程序需要向标准输出打印两行。第一行应包含一个正整数 $l$。第二行应包含 $l$ 个正整数 $y_1, y_2, \dots, y_l$,以空格分隔。序列 $y_1, y_2, \dots, y_l$ 构成了 $sks(n)$ 的简洁表示。

样例

样例输入 1

6
1 1 1 1 1 1

样例输出 1

5
1 1 2 1 1

说明

序列 1,1,1,1,1,1 构成了 $101010_2=42$ 的简洁表示,$sks(42)=45$,而 $45=101101_2$ 的简洁表示为 1,1,2,1,1。

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.