아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Sequence Conversion

시간 제한3초메모리 제한1024 MB

요약
인접한 두 원소에 같은 x를 xor하는 연산으로 배열 a를 b로 바꾸는 최소 연산 횟수를 구하고, 불가능하면 -1을 출력한다.
난이도

보통10점 중 7점

유형
그리디, 누적 합, 비트 연산
정답자
아직 제출이 없습니다

문제

You are given two arrays of non-negative integers a_1,a_2,…,a_Na\_1, a\_2, \dots, a\_N and b_1,b_2,…,b_Nb\_1, b\_2, \dots, b\_N.

You can perform the following operation several times:

  • Choose a non-negative integer xx and an index 1≤i<N1 \leq i < N. Then change a_ia\_i to a_i⊕xa\_i \oplus x and change a_i+1a\_{i+1} to a_i+1⊕xa\_{i+1} \oplus x.

Expression x⊕yx \oplus y means bitwise xor of two numbers xx and yy.
In binary representation, if the ii-th digit of x and y is equal, then the ii-th digit of x⊕yx \oplus y is 00, and if not, it is 11.
The given operation exists in all modern programming languages. For example, in C++ and Java, it is represented as x ∧ yx\ ^{\wedge}\ y.

You want to convert a_i\\{a\_i\\} to b_i\\{b\_i\\} by performing the minimum number of operations.

Find the minimum number of operations to convert a_i\\{a\_i\\} to b_i\\{b\_i\\}.

If you cannot convert a_i\\{a\_i\\} to b_i\\{b\_i\\} with the given operation, print −1-1.

입력

The first line contains an integer NN, where NN denotes the length of the two sequences.

The second line contains NN space-separated non-negative integers a_1,a_2,…,a_Na\_1, a\_2, \dots, a\_N.

The third line contains NN space-separated non-negative integers b_1,b_2,…,b_Nb\_1, b\_2, \dots, b\_N.

출력

Print −1-1, if it is impossible to change the sequence a_i\\{a\_i\\} to b_i\\{b\_i\\}.

Otherwise, print the minimum number of operations needed to change the sequence a_i\\{a\_i\\} to b_i\\{b\_i\\}.

제한

  • 1≤N≤1061 \leq N \leq 10^6
  • 0≤a_i,b_i<2300 \leq a\_i, b\_i < 2^{30} (1≤i≤N)(1 \le i \le N)

예제2

  1. 예제 1

    입력
    3
    1 2 3
    3 2 1
    
    예상 출력
    2
    
  2. 예제 2

    입력
    3
    1 5 3
    1 2 3
    
    예상 출력
    -1