E. And Constraint

Problem Statement

You are given a sequence aa of length n1n-1 and a sequence bb of length nn.

You can perform the following operation any number of times (possibly zero):

  • Choose an index 1in1 \leq i \leq n and increment bib_i by 1 (i.e., set bibi+1b_i \leftarrow b_i + 1).

Your goal is to perform the minimum number of operations such that for every 1in11 \leq i \leq n-1, the condition bi&bi+1=aib_i \And b_{i+1} = a_i holds, where &\And denotes the bitwise AND operation. If it is impossible to satisfy the condition, report it as well.

Input

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

The first line contains a single integer nn (2n1052 \leq n \leq 10^5).

The second line contains n1n-1 integers a1,a2,,an1a_1, a_2, \ldots, a_{n-1} (0ai<2290 \leq a_i < 2^{29}).

The third line contains nn integers b1,b2,,bnb_1, b_2, \ldots, b_n (0bi<2290 \leq b_i < 2^{29}).

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, you need to output only one integer.

If the goal can be achieved, output one integer — the minimum number of operations required. Otherwise, output 1-1.

Solution

submission link

Key Observations

Necessary Condition Analysis

The problem requires that bi&bi+1=aib_i \And b_{i+1} = a_i for all ii, which means that all bits of aia_i must be set in both bib_i and bi+1b_{i+1}.

Since we also have bi+1&bi+2=ai+1b_{i+1} \And b_{i+2} = a_{i+1}, the bits of ai+1a_{i+1} must be set in both bi+1b_{i+1} and bi+2b_{i+2}.

For bi+1b_{i+1} to satisfy both conditions simultaneously, it must have all bits set in both aia_i and ai+1a_{i+1}. Therefore, the minimum requirement is:

biaiai1b_i \geq a_i | a_{i-1}

Note that b1b_1 and bnb_n only need to satisfy one condition each.

Feasibility Check

This gives us a necessary condition: if we construct bb with bi=aiai1b_i = a_i | a_{i-1} and this doesn’t satisfy bi&bi+1=aib_i \And b_{i+1} = a_i, then no valid solution exists (output -1).

Conversely, if bb satisfies the necessary condition and bi&bi+1=aib_i \And b_{i+1} = a_i, then we can always find a valid bb that satisfies all constraints.

Optimized Dynamic Programming Solution

Problem Analysis

To find the minimum number of operations, we use dynamic programming since bib_i depends only on bi1b_{i-1}, ai1a_{i-1}, and aia_i.

The key insight is to determine which states of bib_i are useful for minimizing operations.

State Space Optimization

Consider the constraint that bib_i must contain all bits from aiai1a_i | a_{i-1}. For any additional bits beyond this minimum requirement, we only need to consider a limited set of useful values.

For example, if aiai1=0xaa_i | a_{i-1} = \text{0xa}, then bib_i must contain at least 0xa\text{0xa}. For additional bits, we only consider values that follow a specific pattern to avoid redundant computation.

The key observation is that if we have a higher bit value that costs more operations to reach, and the lower bit value will not be needed, we should set all of them to 0 (check the code below to better understand this). This means we only need to consider approximately 30 possible values for each bib_i.

Implementation Details

We precompute useful bit patterns using:

const int HIGHEST = 0x00ffffffffffffff;
p[0] = make_pair(0xffffffffffff, 0);
p[1] = make_pair(0xfffffffffffe, 1);
for (int i = 2; i < 40; i++) {
    p[i].first = p[i - 1].first << 1;
    p[i].second = p[i - 1].second << 1;
    p[i].first &= HIGHEST;
}

For each position ii, we compute the target value as:

int target_val = (b[i] & bits.first) | bits.second | a[i] | a[i - 1];

This formula ensures that:

  • We preserve the necessary bits from aiai1a_i | a_{i-1}
  • We consider only the useful bit patterns from our precomputed set
  • We maintain the minimum cost to reach each valid state

Time Complexity

The dynamic programming runs in O(nk)O(n \cdot k) time, where nn is the array length and k30k \approx 30 is the number of useful bit patterns per position. This is efficient enough for the given constraints.