Codeforces Round 1008 (Div. 1)
F
AND x OR
Codeforces
3300
原题
← EG →
翻译中…

Suppose you have two arrays $c$ and $d$, each of length $k$. The pair $(c, d)$ is called good if $c$ can be changed to $d$ by performing the following operation any number of times.

翻译中…
  • Select two distinct indices $i$ and $j$ ($1 \leq i, j \leq k$, $i \neq j$) and a nonnegative integer $x$ ($0 \leq x \lt 2^{30}$). Then, apply the following transformations:
  • $c_i := c_i \mathbin{\&} x$, where $\&$ denotes the bitwise AND operation.
翻译中…

You are given two arrays $a$ and $b$, both of length $n$, containing nonnegative integers not exceeding $m$.

翻译中…

You can perform two types of moves on these arrays any number of times:

翻译中…
  1. Select an index $i$ ($1 \leq i \leq n$) and set $a_i := a_i + 1$.
  2. Select an index $i$ ($1 \leq i \leq n$) and set $b_i := b_i + 1$.
翻译中…

Note that the elements of $a$ and $b$ may exceed $m$ at some point while performing the moves.

翻译中…

Find the minimum number of moves required to make the pair $(a, b)$ good.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($1 \leq n, m \leq 2 \cdot 10^6$) — the length of arrays $a$ and $b$, and the maximum possible value in these arrays, respectively.

翻译中…

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq m$) — denoting the array $a$.

翻译中…

The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($0 \leq b_i \leq m$) — denoting the array $b$.

翻译中…

Additionally, it is guaranteed that the sum of all values of $n$ and the sum of all values of $m$ across all test cases do not exceed $2 \cdot 10^6$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the minimum number of moves required to make the pair $(a, b)$ good.

翻译中…

Note

翻译中…

In the first case, we already have $a = b$.

翻译中…

In the second case, we can perform move $2$ on index $i = 2$ twice. The array $b$ becomes $[8, 8, 32]$. We can see that $(a, b)$ is now good.

翻译中…

In the third case, we can perform move $2$ on index $i = 1$, then perform move $1$ on index $i = 2$. It can be proven that you cannot make the pair $(a, b)$ good in fewer than $2$ moves.

翻译中…

样例

Input #1
5
4 3
0 1 2 3
0 1 2 3
3 32
8 9 32
8 6 32
5 64
5 7 16 32 64
4 8 16 32 64
4 11
9 1 4 3
8 11 6 2
5 10
7 9 5 4 2
3 10 6 5 9
Output #1
0
2
2
0
1
InputCopy OutputCopy
题目信息
题号
2077F
时间限制
5 s
内存限制
1024 MB
标签
位运算
构造
动态规划

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。