Poj 1830 Gaussian elimination

Source: Internet
Author: User
Switch Problems
Time limit:1000 ms Memory limit:30000 K
Total submissions:5418 Accepted:2022

Description

There are n identical switches, each of which is associated with some switches. Whenever you turn a switch on or off, other switches associated with this switch will also change accordingly, that is, the status of these associated switches will change to off if it is turned on, and if it is turned off, it will change to on. Your goal is to make the last n switches to a specific State after several switching operations. For any switch, you can only perform one switch operation at most. Your task is to calculate how many methods can reach the specified state. (Excluding the switch operation order)

Input

The number of K in the first line indicates that there are K groups of test data below.
The format of each group of test data is as follows:
N (0 <n <29) of the first row)
The number of N 0 or 1 in the second line indicates the n switch states at the beginning.
The number of N 0 or 1 in the third row indicates the status of N switches after the operation is complete.
The next line contains two I j numbers, indicating that if the I switch is operated, the status of the J switch also changes. Each group of data ends at 0.

Output

If there is a feasible method, the total number of outputs; otherwise, "Oh, it's impossible ~!" Is output ~!!" Excluding quotation marks

Sample Input

230 0 01 1 11 21 32 12 33 13 20 030 0 01 0 11 22 10 0

Sample output

4Oh,it‘s impossible~!!

Hint

Description of the first group of data:
There are four methods in total:
Operation switch 1
Operation switch 2
Operation Switch 3
Operation switch 1, 2, 3 (do not remember the order)
1 /************************************** * *********************************** 2> File Name: poj_1830.cpp 3> author: stomach_ache 4> mail: [email protected] 5> created time: Thursday, July 10, 2014 40 seconds 6> propose: 7 *************************************** * *******************************/8 9 # include <cmath> 10 # include <string> 11 # include <cstdio> 12 # include <vector> 13 # include <fstream> 14 # inclu De <cstring> 15 # include <iostream> 16 # include <algorithm> 17 using namespace STD; 18 19 const int max_n = 35; 20 const double EPS = 1e-8; 21 22 int A [max_n] [max_n], s [max_n], E [max_n]; 23 24 int gauss_jordan (int n, int m) {25 int I, J; // I maintain the rank of the matrix 26 for (I = 0, j = 0; I <n & J <m; I ++, J ++) {27 int rows = I; 28 for (int K = I + 1; k <n; k ++) {29 If (ABS (A [k] [J]> ABS (A [distinct] [J]) rows = K; 30} 31 if (ABS (A [pipeline] [J]) <EPS) {32 I --; 33 continue; 34} // discard this line and directly process the next line 35 if (pipeline! = I) for (int K = 0; k <= m; k ++) Swap (A [I] [K], a [distinct] [k]); 36 37 // change the coefficient of the unknown number being processed to 138 for (int K = I + 1; k <= m; k ++) A [I] [k]/= A [I] [J]; 39 for (INT r = 0; r <n; r ++) if (I! = R) {40 for (int K = J + 1; k <= m; k ++) 41 // A [J] [k]-= A [J] [I] * A [I] [k]; 42 A [r] [k] = (a [r] [k]-A [r] [J] * A [I] [k] + 2) % 2; 43} 44} 45 // no solution 46 for (int K = I; k <n; k ++) if (a [k] [m]! = 0) Return-1; 47 if (I = N) return 1; 48 // The number of free variables is n-I, each variable has two states: 49 return 1 <(n-I); 50} 51 52 int main (void) {53 int K, N; 54 scanf ("% d", & K); 55 while (k --) {56 scanf ("% d", & N); 57 for (INT I = 0; I <n; I ++) scanf ("% d", S + I); 58 for (INT I = 0; I <n; I ++) scanf ("% d", e + I); 59 int I, j; 60 memset (A, 0, sizeof ()); 61 while (scanf ("% d", & I, & J) & I + J) A [J-1] [I-1] = 1; 62 // construct the Augmented Matrix 63 for (INT I = 0; I <n; I ++) {64 A [I] [N] = s [I] ^ e [I]; 65 A [I] [I] = 1; 66} 67 int ans = gauss_jordan (n, n); 68 if (ANS =-1) puts ("oh, it's impossible ~!! "); 69 else printf (" % d \ n ", ANS); 70} 71 72 return 0; 73}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.