Universal Cup Judging System

Universal Cup

Time Limit: 4 s Memory Limit: 512 MB Total points: 100
Statistics

给定一个长度为 $n$ 的二进制序列 $a_1, a_2, \dots, a_n$,其中 $n$ 是 2 的幂。在一次操作中,你可以选择序列中的任意元素并将其变为相反数(即选择区间 $[1, n]$ 中的索引 $i$,将 $a_i$ 变为 $1-a_i$)。此外,如果序列在某一时刻是回文的(即对于所有合法的索引 $i$,满足 $a_i = a_{n+1-i}$),你可以切掉序列的右半部分,仅保留 $a_1, a_2, \dots, a_{n/2}$,并将 $n$ 更新为新序列的长度(即除以 2)。这种操作不计入移动次数——我们所说的移动次数仅指元素改变的次数。你可以自由地交替进行元素修改和序列减半操作。当然,如果序列只剩下一个元素,则无法继续缩短。

请确定将序列缩短为仅剩一个元素所需的最少移动次数。

此外,$n$ 可能非常大,序列通过其中 1 的位置列表给出(其余元素均为 0)。

输入格式

第一行包含两个整数 $n$ 和 $m$ ($1 \le n \le 10^{18}$; $1 \le m \le 100\,000$; $m \le n$; $n$ 是 2 的非负整数次幂),分别表示序列的长度和其中 1 的个数。

第二行包含 $m$ 个整数 $p_1, p_2, \dots, p_m$ ($1 \le p_i \le n$; $p_i < p_{i+1}$),表示序列 $a_1, a_2, \dots, a_n$ 中 1 的位置。

输出格式

输出一个整数,表示将序列 $a_1, a_2, \dots, a_n$ 缩短为单元素序列所需的最少移动次数。

样例

输入 1

8 3
1 5 8

输出 1

2

说明

在样例测试中,给定的序列是 10001001。有三种方法可以通过两次移动将其缩短为一个元素:

  • 10001001 $\xrightarrow{+1}$ 10011001 $\to$ 1001 $\to$ 10 $\xrightarrow{+1}$ 11 $\to$ 1
  • 10001001 $\xrightarrow{+1}$ 10011001 $\to$ 1001 $\to$ 10 $\xrightarrow{+1}$ 00 $\to$ 0
  • 10001001 $\xrightarrow{+1}$ 10000001 $\to$ 1000 $\xrightarrow{+1}$ 0000 $\to$ 00 $\to$ 0

没有办法用少于两次的移动将序列缩短为一个元素,因此结果为 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.