思路:
对于序列t,只要第一个数确定了,后续的数也随之确定了。枚举四种情况即可。
实现:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 int a[200005], b[200005]; 6 int get(int t, int a, int b) 7 { 8 int ans = 0, cnt = 0; 9 while (t || a || b) 10 { 11 int x = t & 1, y = a & 1, z = b & 1; 12 if (x == 0 && y == 1 && z == 0) ans |= 1 << cnt; 13 else if (x == 1 && y == 1 && z == 1) ans |= 1 << cnt; 14 else if (x == 0 && y == 1 && z == 1) return -1; 15 else if (x == 0 && y == 0 && z == 1) return -1; 16 else if (x == 1 && y == 0 && z == 0) return -1; 17 else if (x == 1 && y == 0 && z == 1) return -1; 18 t >>= 1; 19 a >>= 1; 20 b >>= 1; 21 cnt++; 22 } 23 return ans; 24 } 25 int main() 26 { 27 int n; 28 while (cin >> n) 29 { 30 for (int i = 0; i < n - 1; i++) cin >> a[i]; 31 for (int i = 0; i < n - 1; i++) cin >> b[i]; 32 bool ok = false; 33 for (int k = 0; k < 4; k++) 34 { 35 if (ok) break; 36 vector<int> v; 37 v.push_back(k); 38 bool flg = true; 39 for (int i = 0; i < n - 1; i++) 40 { 41 int tmp = get(v.back(), a[i], b[i]); 42 if (tmp == -1) { flg = false; break; } 43 v.push_back(tmp); 44 } 45 if (flg) 46 { 47 cout << "YES" << endl; 48 for (auto it: v) cout << it << " "; 49 cout << endl; 50 ok = true; 51 } 52 } 53 if (!ok) cout << "NO" << endl; 54 } 55 return 0; 56 }