G. Build an Array

G. Build an Array

Yesterday, Dima found an empty array and decided to add some integers to it. He can perform the following operation an unlimited number of times:

  • add any integer to the left or right end of the array.
  • then, as long as there is a pair of identical adjacent elements in the array, they will be replaced by their sum.

It can be shown that there can be at most one such pair in the array at the same time.

For example, if the array is $[3, 6, 4]$ and we add the number $3$ to the left, the array will first become $[3, 3, 6, 4]$, then the first two elements will be replaced by $6$, and the array will become $[6, 6, 4]$, and then — $[12, 4]$.

After performing the operation exactly $k$ times, he thinks he has obtained an array $a$ of length $n$, but he does not remember which operations he applied. Determine if there exists a sequence of $k$ operations that could result in the given array $a$ from an empty array, or determine that it is impossible.

Input

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case description contains two integers $n$ and $k$ ($1 \le n \le 10^5$, $n \le k \le 10^6$) — the length of the resulting array and the number of operations.

The second line contains $n$ integers $a_i$ ($1 \le a_i \le 10^9, a_{i - 1} \ne a_i$) — the elements of the resulting array.

It is guaranteed that the sum of the values of $n$ across all test cases does not exceed $10^5$.

Output

For each test case, if there is no suitable sequence of operations of length $k$, output "NO". Otherwise, output "YES".

You may output "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Example

Input

8
3 3
2 1 4
3 7
2 1 4
2 15
2 16
3 10
256 32 1
3 289
768 96 1
3 290
768 96 1
5 7
5 1 6 3 10
4 6
6 8 5 10

Output

YES
NO
YES
YES
YES
NO
YES
YES

 

解题思路

  感觉这题比 D. Come a Little Closer 还简单...

  我们反过来考虑,给定一个序列 $a$,能否通过将元素进行拆分使得 $a$ 的长度至少为 $k$。对于一个数 $x$,显然如果 $x$ 是奇数则无法拆分。否则我们可以将其拆成 $\frac{x}{2},\frac{x}{2}$,进一步的如果 $\frac{x}{2}$ 还是偶数,可以继续拆分成 $\frac{x}{2},\frac{x}{4},\frac{x}{4}$,或 $\frac{x}{4},\frac{x}{4},\frac{x}{4},\frac{x}{4}$。为此对于每个元素 $x$ 我们都将其看作 $x = q \cdot 2^k$ 的形式,其中 $q$ 是奇数,$k$ 实际上就是 $x$ 在二进制下末尾连续 $0$ 的个数。所以一个数最多会被拆分成 $2^k$ 个数($2^k$ 个 $q$)。

  但很显然我们不能随意将其拆分,因为还要考虑到在序列中 $x$ 左右相邻的元素,确保拆分出来的元素在合并成 $x$ 的过程中不会与 $x$ 原本相邻的元素合并。不妨假设与 $x$ 左邻的元素是 $y$(暂时不考虑右邻的元素,此时我们会依次从序列右端添加拆分出来的值)。如果 $y > x$,$x$ 确实可以随意拆分,因为拆分出来的值在合并的过程中都严格小于 $y$ 进而不会与 $y$ 发生合并。另一种可以随意拆分的情况是 $\frac{y}{2^{\text{ctz}(y)}} \ne \frac{x}{2^{\text{ctz}(x)}}$($\text{ctz}(x)$ 表示 $x$ 在二进制下末尾 $0$ 的个数),因为 $x$ 拆分出来的所有值都不可能等于 $y$。

  否则考虑 $y < x$ 且 $\frac{y}{2^{\text{ctz}(y)}} = \frac{x}{2^{\text{ctz}(x)}}$ 的情况。首先拆分出与 $y$ 相邻的值必须严格大于 $y$(即至少为 $2y$),否则拆分出的值在合并的过程中会与 $y$ 合并。由于我们希望拆分出来的元素尽可能多,因此先把 $x$ 拆分成 $2^{\text{ctz}(x) - (\text{ctz}(y) + 1)}$ 个 $2y$,然后除了最左边与 $y$ 相邻的那个 $2y$ 外,其余每个的 $2y$ 都拆分成 $2^{\text{ctz}(y) + 1}$ 个 $q$。例如 $y = 6$,$x = 48$,先将 $x$ 拆分成 $12,12,12,12$,再拆分成 $12, \; 3,3,3,3, \; 3,3,3,3, \; 3,3,3,3$。合并的时候从左往右依次向序列右端添加这些值,这样就能合并出 $x$ 且不会与 $y$ 发生合并了。

  类似的方式考虑 $x$ 存在右邻元素的情况(无左邻元素)。为了将上述结果方便表示,这里先定义 $f(x,y)$ 表示当 $x$ 与只 $y$ 相邻时,$x$ 能拆分出的最多数量,有 $\displaylines{f(x,y)= \begin{cases} 2^{\text{ctz}(x)}, &y < x \text{ or } \frac{y}{2^{\text{ctz}(y)}} \ne \frac{x}{2^{\text{ctz}(x)}} \\ 1 + \left(2^{\text{ctz}(x) - (\text{ctz}(y) + 1)} - 1\right) \cdot 2^{\text{ctz}(y) + 1}, &\text{else} \end{cases}}$。

  最后考虑向序列添加的第一个元素,可以是 $a_i$ 中的任意一个,并且可以将 $a_i$ 拆分成 $2^{\text{ctz}(a_i)}$ 个值进行添加(因为此时不存在相邻元素)。当确定第一个添加的是 $a_i$ 后,只能在序列左端依次添加 $a_{i-1} \sim a_1$,在序列右端依次添加 $a_{i+1} \sim a_n$,且两个过程是独立的。其中往左端依次添加 $a_{i-1} \sim a_1$ 时,拆分时只需考虑右邻的元素。往右端依次添加 $a_{i+1} \sim a_n$ 时,拆分时只需考虑左邻的元素。

  定义 $l_i$ 表示当序列中存在 $a_{i+1}$ 时,依次添加 $a_i \sim a_1$ 能拆分出的最多数量,有 $l_i = l_{i-1} + f(a_{i}, a_{i+1})$。同理,定义 $r_i$ 表示当序列中存在 $a_{i-1}$ 时,依次添加 $a_{i+1} \sim a_n$ 能拆分出的最多数量,有 $r_i = r_{i+1} + f(a_i,a_{i-1})$。

  最后就是枚举每一个 $a_i$,如果存在某个 $i$ 满足 $2^{\text{ctz}(a_i)} + l_{i-1} + r_{i+1} \geq k$,则有解。

  AC 代码如下,时间复杂度为 $O(n)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 1e5 + 5;

int a[N], c[N];
LL l[N], r[N];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        c[i] = __builtin_ctz(a[i]);
    }
    for (int i = 1; i < n; i++) {
        l[i] = l[i - 1];
        if (a[i] < a[i + 1] || a[i] >> c[i] != a[i + 1] >> c[i + 1]) l[i] += 1 << c[i];
        else l[i] += (1 << c[i]) - (1 << c[i + 1] + 1) + 1;
    }
    r[n + 1] = 0;
    for (int i = n; i > 1; i--) {
        r[i] = r[i + 1];
        if (a[i] < a[i - 1] || a[i] >> c[i] != a[i - 1] >> c[i - 1]) r[i] += 1 << c[i];
        else r[i] += (1 << c[i]) - (1 << c[i - 1] + 1) + 1;
    }
    for (int i = 1; i <= n; i++) {
        if ((1 << c[i]) + l[i - 1] + r[i + 1] >= m) {
            cout << "YES\n";
            return;
        }
    }
    cout << "NO\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round #1027 (Div. 3) Editorial:https://codeforces.com/blog/entry/143268

posted @ 2025-05-27 21:41  onlyblues  阅读(56)  评论(0)    收藏  举报
Web Analytics