Question:
In some dependent lights (that is, this is changed), we need to change the status of several lights to reach the target State from the initial state. Each lamp can be changed once at most, total number of solutions.
Analysis:
Since each lamp can only be changed once, the final state is only related to the change of the lamp related to the lamp, and it is easy to think of setting up a equations to solve the problem. As for solving the equations, Gaussian daniu has already paved the way for us, and pays high respect to him.
Algorithm:
0 indicates that no change is made to the I lamp, and 1 indicates that no change is made to the I lamp. Therefore, the change of all the lights related to the lamp (including the lamp itself) is the final change, the final change can be obtained from the initial and final state, and then the Gaussian elimination element is used to solve the equations. The final answer is 2 ^ The number of unsolvable elements.
Code:
Program syj; {XOR equation} <br/> var task, I, j, n, x, y, FJ, M: longint; <br/> NO: Boolean; <br/> F, Z: array [1 .. 30] of longint; <br/> begin <br/> assign (input, 'pku1830. in '); reset (input); <br/> assign (output, 'pku1830. out'); rewrite (output); <br/> readln (task); <br/> for task: = 1 to task do begin <br/> readln (N ); <br/> for I: = 1 to n do read (Z [I]); <br/> for I: = 1 to n do begin <br/> Read (x); F [I]: = Z [I] XOR X or (1 Shl I); <br/> end; <br/> readln (I, j ); <br/> while I <> 0 do begin <br/> F [J]: = f [J] Or (1 shl I); <br/> readln (I, j); <br/> end; <br/> M: = 0; <br/> for I: = 1 to n do begin <br/> FJ: = 0; <br/> for J: = I to n do <br/> If Odd (F [J] shr I) then begin <br/> FJ: = J; break; <br/> end; <br/> If Fj = 0 then Inc (m) <br/> else begin <br/> X: = f [I]; f [I]: = f [FJ]; F [FJ]: = x; <br/> for J: = I + 1 to n do <br/> If Odd (F [J] shr I) N f [J]: = f [J] XOR f [I]; <br/> end; <br/> NO: = false; <br/> for I: = n downto 1 do <br/> If Odd (F [I]) Then <br/> if not odd (F [I] shr I) then begin no: = true; break; end <br/> else <br/> for J: = 1 to I-1 do f [J]: = f [J] XOR (f [J] shr I) and 1); <br/> if no then writeln ('oh, it', CHR (39 ),'s impossible ~!! ') <Br/> else writeln (1 SHL m); <br/> end; <br/> close (input); <br/> close (output ); <br/> end. <br/>