E. Prairie Partition

Codeforces
IDCF807E
Time2000ms
Memory256MB
Difficulty
binary searchgreedy
English · Original
Chinese · Translation
Formal · Original
It can be shown that any positive integer _x_ can be uniquely represented as _x_ = 1 + 2 + 4 + ... + 2_k_ - 1 + _r_, where _k_ and _r_ are integers, _k_ ≥ 0, 0 < _r_ ≤ 2_k_. Let's call that representation prairie partition of _x_. For example, the prairie partitions of 12, 17, 7 and 1 are: <center>12 = 1 + 2 + 4 + 5,17 = 1 + 2 + 4 + 8 + 2, 7 = 1 + 2 + 4, 1 = 1. </center>Alice took a sequence of positive integers (possibly with repeating elements), replaced every element with the sequence of summands in its prairie partition, arranged the resulting numbers in non-decreasing order and gave them to Borys. Now Borys wonders how many elements Alice's original sequence could contain. Find all possible options! ## Input The first line contains a single integer _n_ (1 ≤ _n_ ≤ 105) — the number of numbers given from Alice to Borys. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1 ≤ _a__i_ ≤ 1012; _a_1 ≤ _a_2 ≤ ... ≤ _a__n_) — the numbers given from Alice to Borys. ## Output Output, **in increasing order**, all possible values of _m_ such that there exists a sequence of positive integers of length _m_ such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input. If there are no such values of _m_, output a single integer _\-1_. [samples] ## Note In the first example, Alice could get the input sequence from \[6, 20\] as the original sequence. In the second example, Alice's original sequence could be either \[4, 5\] or \[3, 3, 3\].
可以证明,任何一个正整数 $x$ 都可以唯一地表示为 $x = 1 + 2 + 4 + \dots + 2^{k-1} + r$,其中 $k$ 和 $r$ 是整数,满足 $k \geq 0$ 且 $0 < r \leq 2^k$。我们将这种表示称为 $x$ 的 _prairie partition_。 例如,12、17、7 和 1 的 prairie partition 分别为: $17 = 1 + 2 + 4 + 8 + 2$, $7 = 1 + 2 + 4$, $1 = 1$。 Alice 取了一个正整数序列(可能包含重复元素),将每个元素替换为其 prairie partition 中的加数,然后将得到的所有数字按非降序排列,并将这个序列交给了 Borys。现在 Borys 想知道,Alice 原始序列可能包含多少个元素?请找出所有可能的取值! 第一行包含一个整数 $n$($1 \leq n \leq 10^5$)——Alice 交给 Borys 的数字个数。 第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$($1 \leq a_i \leq 10^{12}$;$a_1 \leq a_2 \leq \dots \leq a_n$)——Alice 交给 Borys 的数字。 请按**递增顺序**输出所有可能的 $m$ 值,使得存在一个长度为 $m$ 的正整数序列,当将每个元素替换为其 prairie partition 的加数,并将结果按非降序排列后,恰好得到输入的序列。 如果没有这样的 $m$ 值,请输出单个整数 _-1_。 在第一个例子中,Alice 可能的原始序列为 $[6, 20]$。 在第二个例子中,Alice 的原始序列可能是 $[4, 5]$ 或 $[3, 3, 3]$。 ## Input 第一行包含一个整数 $n$($1 \leq n \leq 10^5$)——Alice 交给 Borys 的数字个数。第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$($1 \leq a_i \leq 10^{12}$;$a_1 \leq a_2 \leq \dots \leq a_n$)——Alice 交给 Borys 的数字。 ## Output 请按**递增顺序**输出所有可能的 $m$ 值,使得存在一个长度为 $m$ 的正整数序列,当将每个元素替换为其 prairie partition 的加数,并将结果按非降序排列后,恰好得到输入的序列。如果没有这样的 $m$ 值,请输出单个整数 _-1_。 [samples] ## Note 在第一个例子中,Alice 可能的原始序列为 $[6, 20]$。在第二个例子中,Alice 的原始序列可能是 $[4, 5]$ 或 $[3, 3, 3]$。
**Definitions** Let $ \mathcal{P}(x) $ denote the *prairie partition* of a positive integer $ x $, uniquely defined as: $$ x = 1 + 2 + 4 + \dots + 2^{k-1} + r = (2^k - 1) + r, \quad \text{where } k \geq 0, \; 0 < r \leq 2^k. $$ The multiset of summands in $ \mathcal{P}(x) $ is $ \{1, 2, 4, \dots, 2^{k-1}, r\} $. Let $ A = (a_1, a_2, \dots, a_n) $ be the input sequence, sorted non-decreasingly, representing the multiset of all summands from the prairie partitions of some original sequence $ B = (b_1, \dots, b_m) $ of positive integers. **Constraints** 1. $ 1 \leq n \leq 10^5 $ 2. $ 1 \leq a_i \leq 10^{12} $ for all $ i $ 3. $ a_1 \leq a_2 \leq \dots \leq a_n $ **Objective** Find all possible values of $ m \in \mathbb{Z}^+ $ such that there exists a sequence $ B = (b_1, \dots, b_m) $ of positive integers where the multiset union of $ \mathcal{P}(b_1) \cup \mathcal{P}(b_2) \cup \dots \cup \mathcal{P}(b_m) $, when sorted, equals $ A $. Output all such $ m $ in increasing order; if none exist, output $-1$.
Samples
Input #1
8
1 1 2 2 3 4 5 8
Output #1
2
Input #2
6
1 1 1 2 2 2
Output #2
2 3
Input #3
5
1 2 4 4 4
Output #3
\-1
API Response (JSON)
{
  "problem": {
    "name": "E. Prairie Partition",
    "description": {
      "content": "It can be shown that any positive integer _x_ can be uniquely represented as _x_ = 1 + 2 + 4 + ... + 2_k_ - 1 + _r_, where _k_ and _r_ are integers, _k_ ≥ 0, 0 < _r_ ≤ 2_k_. Let's call that representa",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF807E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "It can be shown that any positive integer _x_ can be uniquely represented as _x_ = 1 + 2 + 4 + ... + 2_k_ - 1 + _r_, where _k_ and _r_ are integers, _k_ ≥ 0, 0 < _r_ ≤ 2_k_. Let's call that representa...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "可以证明,任何一个正整数 $x$ 都可以唯一地表示为 $x = 1 + 2 + 4 + \\dots + 2^{k-1} + r$,其中 $k$ 和 $r$ 是整数,满足 $k \\geq 0$ 且 $0 < r \\leq 2^k$。我们将这种表示称为 $x$ 的 _prairie partition_。\n\n例如,12、17、7 和 1 的 prairie partition 分别为:\n\n$17 = ...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ \\mathcal{P}(x) $ denote the *prairie partition* of a positive integer $ x $, uniquely defined as:  \n$$\nx = 1 + 2 + 4 + \\dots + 2^{k-1} + r = (2^k - 1) + r, \\quad \\text{where } ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments