Each location affects only adjacent locations
Do the Gaussian elimination and then enumerate.
The code is as follows:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAXN usi
NG namespace Std;
int X[MAXN];
int FREE_X[MAXN];
int A[MAXN][MAXN];
void print () {for (Int. i=0; i<20; ++i) {for (int j=0; j<=20; ++j) printf ("%d", a[i][j]);
Puts ("");
}} int Gauss (int equ, int var) {int I, J, K, Col;
int TA, TB, LCM;
int tmp, FREE_INDEX;
int Free_x_num, Max_r;
int num = 0;
for (int i=0; i<=var; ++i) {x[i] = 0;
Free_x[i] = 0;
} col = 0;
for (k=0; k<equ&&col<var; ++k, ++col) {max_r = k;
for (i=k+1; i<equ; ++i) {if (ABS (A[i][col]) > abs (A[max_r][col])) Max_r = i;
} if (Max_r! = k) for (j=k; j<=var; ++j) Swap (A[k][j], a[max_r][j]);
if (a[k][col] = = 0) {--k;
free_x[num++] = col;
Continue
} For (i=k+1, i<equ; ++i) {if (A[i][col]) {for (j=col; j<=var; ++j)
A[I][J] ^= a[k][j];
}}} for (i=k; i<equ; ++i) {if (A[i][col]) return-1;
} return var-k;
} void Solve (int equ, int var) {int ans = Gauss (equ, Var);
if (ans = = 0) {int res = 0;
for (int i=0; i<equ; ++i) Res + = X[i];
printf ("%d\n", res);
return;
} else {int res = 0x3f3f3f3f;
int tot = (1<<ans);
for (int i=0; i<tot; ++i) {int cnt = 0;
for (int j=0; j<ans; ++j) {if (i& (1<<j)) {X[free_x[j]] = 1;
++cnt;
} else X[free_x[j]] = 0;
} for (int j=var-ans-1; j>=0;--j) {int tmp = A[j][var];
for (int l=j+1; l<var; ++l) if (A[j][l]) TMP ^= X[L];
X[J] = tmp;
if (X[j]) ++cnt;
} res = min (res, CNT);
} printf ("%d\n", res);
}} int main (void) {int nx;
while (~SCANF ("%d", &nx)) {memset (A, 0, sizeof (a));
A[0][20] = NX;
for (int i=1; i<20; ++i) scanf ("%d", &a[i][20]);
A[0][0] = a[0][1] = 1;
A[19][18] = a[19][19] = 1;
for (int i=1; i<19; ++i) {a[i][i] = a[i][i-1] = a[i][i+1] = 1;
}//print ();
Solve (20, 20);
Print ();
} return 0; }