Question Link
Given n (n <29) switches, each switch will change when it is turned on or off. Originally, the switch will turn off,
Otherwise, it will be turned on. Given the initial and terminated states of N switches, and the associated switch relationships, determine the number of solutions in total from the initial state to the terminated state (excluding the order, and each switch can be operated only once ).
Analysis:
Since the switch only has two statuses: Open and Close, there are 2 ^ n cases in combination for each switch to open and close, and it is feasible to enumerate all cases, this data volume is unrealistic and needs to be optimized.
Start + X (I) * a (I) = end
X (I) * a (I) = start ^ end: When the start state and the end state are different, 1 is taken to change the state ..
The coefficient matrix A [I] [J] represents the joint relationship between switches:
1) if the operation on the J switch can affect the status of the I switch, a [I] [J] = 1;
2) If the operation on the J switch does not affect the status of the I switch, a [I] [J] = 0;
3) Special A [I] [I] = 1 (the switch operation will inevitably affect its current status );
But I still don't understand why it doesn't affect the backend ????
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cstdlib> 5 # include <cmath> 6 # include <algorithm> 7 # define LL _ int64 8 const int maxn = 30 + 10; 9 Using namespace STD; 10 int equ, VAR, FN; 11 int A [maxn] [maxn], X [maxn]; 12 13 int gcd (int A, int B) 14 {15 return B = 0? A: gcd (B, A % B); 16} 17 int lcm (int A, int B) 18 {19 return a * B/gcd (A, B ); 20} 21 int Gauss () 22 {23 int x_mo; 24 x_mo = 2; 25 int I, J, K, max_r, Col; 26 int Ta, TB, LCM; 27 Col = 0; 28 29 for (k = 0; k <equ & Col <var; k ++, Col ++) 30 {31 max_r = K; 32 For (I = k + 1; I <equ; I ++) 33 If (ABS (A [I] [col])> ABS (A [max_r] [col]) 34 max_r = I; 35 36 IF (max_r! = K) 37 for (j = K; j <var + 1; j ++) 38 swap (A [k] [J], a [max_r] [J]); 39 40 if (a [k] [col] = 0) 41 {42 k --; 43 continue; 44} 45 for (I = k + 1; I <equ; I ++) 46 {47 if (a [I] [col]! = 0) 48 {49 LCM = lcm (ABS (A [I] [col]), ABS (A [k] [col]); 50 TA = LCM/ABS (A [I] [col]); 51 TB = LCM/ABS (A [k] [col]); 52 if (a [I] [col] * A [k] [col] <0) TB =-TB; 53 54 for (j = Col; j <var + 1; j ++) 55 A [I] [J] = (A [I] [J] * Ta-A [k] [J] * TB) % x_mo + x_mo) % x_mo; 56} 57} 58} 59 for (I = K; I <equ; I ++) 60 if (a [I] [col]! = 0) 61 Return-1; 62 63 If (k <var) 64 return var-K; 65 return 0; 66} 67 int main () 68 {69 int I, t, n; 70 int B [maxn], E [maxn], c, d; 71 scanf ("% d", & T); 72 while (t --) 73 {74 CIN> N; 75 equ = N; 76 Var = N; 77 memset (A, 0, sizeof (a); 78 for (I = 0; I <n; I ++) 79 CIN> B [I]; 80 for (I = 0; I <n; I ++) 81 CIN> E [I]; 82 while (CIN> C> d) 83 {84 If (C = 0 & D = 0) break; 85 A [D-1] [C-1] = 1; // note that 1. Note that C is behind and later affects the front. But I still don't understand why 86} 87 for (I = 0; I <n; I ++) 88 A [I] [I] = 1; 89 for (I = 0; I <n; I ++) 90 A [I] [N] = (B [I] ^ e [I]); // when the start and end statuses are different, set 1 to change the state. 91 fn = Gauss (); 92 If (fn =-1) 93 printf ("Oh, it's impossible ~!! \ N "); 94 else 95 {96 fn = (1 <FN); // There are two states, therefore, the number of cases is 2 ^ fn. 97 cout <FN <Endl; 98} 99} 100 return 0; 101}