สมมติว่าเรามีอาร์เรย์ A ที่มีองค์ประกอบ N และสตริงไบนารี่ S อีกอันหนึ่ง ลองพิจารณาว่าผู้เล่นสองคนกำลังเล่นเกม มีตัวเลขเป็น 0 และ 1 มีตัวแปร x ตัวหนึ่งซึ่งมีค่าเริ่มต้นเป็น 0 เกมมี N รอบ ในบุคคลที่กลม S[i] ทำสิ่งใดสิ่งหนึ่งต่อไปนี้:แทนที่ x ด้วย x XOR A[i] มิฉะนั้นจะไม่ทำอะไรเลย บุคคลที่ 0 ต้องการ 0 ในตอนท้ายของเกมนี้ แต่บุคคลที่ 1 ต้องการไม่ใช่ศูนย์ เราต้องเช็คก่อนว่า x กลายเป็น 0 ต่อท้ายหรือไม่
ดังนั้น ถ้าอินพุตเป็น A =[1, 2]; S ="10" ดังนั้นผลลัพธ์จะเป็น 1 เนื่องจาก person1 เปลี่ยน x ด้วย 0 XOR 1 =1 ดังนั้นมันจะเป็น 1 เสมอโดยไม่คำนึงถึงตัวเลือกของ person0
ขั้นตอน
เพื่อแก้ปัญหานี้ เราจะทำตามขั้นตอนเหล่านี้ -
N := size of A Define an array judge of size: 60. z := 0 fill judge with 0 for initialize n := N - 1, when 0 <= n, update (decrease n by 1), do: x := A[n] loop through the following unconditionally, do: if x is same as 0, then: Come out from the loop y := x I := -1 for initialize i := 0, when i < 60, update (increase i by 1), do: if y mod 2 is same as 1, then: I := i y := y / 2 if judge[I] is same as 0, then: judge[I] := x Come out from the loop x := x XOR judge[I] if S[n] is not equal to '0', then: if x is not equal to 0, then: z := 1 return z
ตัวอย่าง
ให้เราดูการใช้งานต่อไปนี้เพื่อความเข้าใจที่ดีขึ้น -
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A, string S){
int N = A.size();
int judge[60];
int z = 0;
fill(judge, judge + 60, 0);
for (int n = N - 1; 0 <= n; n--){
int x = A[n];
while (1){
if (x == 0)
break;
int y = x;
int I = -1;
for (int i = 0; i < 60; i++){
if (y % 2 == 1)
I = i;
y /= 2;
}
if (judge[I] == 0){
judge[I] = x;
break;
}
x ^= judge[I];
}
if (S[n] != '0'){
if (x != 0)
z = 1;
}
}
return z;
}
int main(){
vector<int> A = { 1, 2 };
string S = "10";
cout << solve(A, S) << endl;
} อินพุต
{ 1, 2 }, "10" ผลลัพธ์
1