QOJ.ac

QOJ

Limite de temps : 2.0 s Limite de mémoire : 1024 MB Points totaux : 100 Hackable ✓

#18027. 长出蘑菇了!!

Statistiques

“キノコが生えてる!!” (长蘑菇了!!)

京畿科学高中的宿舍“友正2馆”里共存着京畿科学高中的学生、蜜蜂、蟑螂、在宇从未见过的虫子、鸡蛋等各种生物。因此,友正2馆以环境友好而闻名。

然而有一天,因为环境实在太“友好”了,友正2馆的淋浴间里竟然开始长蘑菇了!

讨厌蘑菇的奥托马奇·皮克尔(Automachi Pickle)决定把这些蘑菇全部消灭掉。

淋浴间里有 $N$ 个蘑菇排成一列,第 $i$ $(1 \le i \le N)$ 个蘑菇的大小为 $a_i$。

皮克尔使用一种特殊的工具来减小蘑菇的大小。对第 $i$ 个蘑菇及其相邻的第 $j$ 个蘑菇使用该工具,可以获得以下效果:

  • 第 $i$ 个蘑菇的大小变为 $a_i\, \& \, a_j$。 ($\&$ 运算是按位与运算,定义在下方的说明中。)

由于皮克尔想尽快消灭蘑菇,去阅读轻小说《关于让主张“竞技编程绝对不可能”的韩星在一百天内彻底填满 Streak 的 PS 故事》,他希望以最少的工具使用次数将所有蘑菇的大小变为 $0$。

为了懒惰的皮克尔,请帮他计算出最少的工具使用次数!

输入格式

第一行给定蘑菇的数量 $N$。

第二行给定 $N$ 个整数 $a_1,\,a_2,\,\cdots,\,a_N$,以空格分隔。

$1 \le N \le 10^6$

$0 \le a_i \le 2^{31}-1 \,(1 \le i \le N,\, a_i$ 为整数$)$

输出格式

如果无法将所有蘑菇的大小变为 $0$,则输出 $-1$;如果可以,则输出将所有蘑菇变为 $0$ 所需的最少工具使用次数。

子任务

编号 分值 限制
1 2 $a_1\ \&\ a_2\ \&\ \cdots\ \&\ a_N \neq 0$
2 10 $N=3$
3 18 $a_1=0$
4 30 $n\leq5000$
5 40 无额外限制。

样例

输入 1

7
1 7 4 6 3 5 9

输出 1

8

说明

即使第 $i$ 个蘑菇的大小变为 $0$,该蘑菇也不会消失。也就是说,即使第 $i$ 个蘑菇的大小变为 $0$,第 $i-1$ 个蘑菇和第 $i+1$ 个蘑菇也不会变得相邻。

按位与(bitwise and)运算是对两个二进制数值按位进行的运算。首先将给定的两个操作数表示为二进制。然后比较两个数值的每一位,只有当两个数值在该位上均为 $1$ 时,结果才为 $1$,其余情况均为 $0$。

例如,$13 \& 7$ 的结果为 $5$。$13$ 的二进制表示为 $1101_{(2)}$,$7$ 的二进制表示为 $111_{(2)}$。计算过程如下:

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.