fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
Codeforces Round 1042-E (Div. 3) (C++) Adjacent XOR
최초 업로드: 2025-08-10 17:50:32
최근 수정 시간: 2025-08-30 14:07:00
게시자: rlatjwls3333
카테고리: Codeforces
조회수: 54
# E. Adjacent XOR [문제 링크](https://codeforces.com/contest/2131/problem/E) ## Problem Statement You're given an array $a$ of length $n$. For each index $i$ such that $1 \le i < n$, you can perform the following operation **at most once**: - Assign $a_i := a_i \oplus a_{i+1}$, where $\oplus$ denotes the bitwise XOR operation. You can choose indices and perform the operations in any sequential order. Given another array $b$ of length $n$, determine if it is possible to transform $a$ to $b$. ## 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 one integer $n$ ($2 \le n \le 2 \cdot 10^5$). The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < 2^{30}$). The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < 2^{30}$). It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. ## Output For each test case, output `"YES"` (quotes excluded) if $a$ can be transformed to $b$; otherwise, output `"NO"`. You can output the answer in any case (upper or lower). For example, the strings `"yEs"`, `"yes"`, `"Yes"`, and `"YES"` will be recognized as positive responses. ## 풀이 #### 그리디하게 앞에서부터 xor 해보고, 뒤에서부터 한번 xor 해본 후, A와 B가 같은지 확인하면 된다. ``` c++ #include<bits/stdc++.h> using namespace std; typedef vector<int> vi; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { int n; cin >> n; vi a(n), b(n); for(int i=0;i<n;i++) cin >> a[i]; for(int i=0;i<n;i++) cin >> b[i]; for(int i=0;i<n-1;i++) { if(a[i]!=b[i]) { if(b[i] == (a[i]^a[i+1])) { a[i] = a[i]^a[i+1]; } } } string ret = "YES\n"; if(a[n-1]!=b[n-1]) ret = "NO\n"; for(int i=n-2;i>=0;i--) { if(a[i]!=b[i]) { if(b[i] == (a[i]^a[i+1])) { a[i] = a[i]^a[i+1]; } else { ret="NO\n"; break; } } } cout << ret; } } ```