POJ 1830-Switching problem (Gauss elimination) __poj

Source: Internet
Author: User

Switch problem

Time Limit: 1000MS Memory Limit: 30000K
Total submissions: 9787 accepted: 3930

Description has n the same switch, each switch is connected to some switches, and whenever you open or close a switch, other switches associated with this switch will change accordingly, that is, if the state of the associated switch is turned off, it becomes open if it turns off. Your goal is to make the last n switch reach a specific state after several switches. For any one switch, only one switch operation can be performed at most. Your task is to calculate how many methods are available to reach the specified state. (regardless of the order of the switch operation)

Input enters the first line with a number of K, which indicates that there are K-group test data below.
The format of each set of test data is as follows:
First row one number N (0 < n < 29)
The second line n 0 or 1, which indicates the N switch state at the start.
The third line, n 0 or 1, indicates the state of the n switch after the operation ends.
The next two number of each line I j, means that if the operation of the I switch, the state of the J switch will also change. Each group of data ends with 0 0.

Output "Oh,it ' s impossible~!!" does not include quotes if there is a feasible way to output the total number.

Sample Input

2
3
0 0 0 1 1 1 1 2 1 3 2 1 2 3 3 1 3 2 0 0 3 0 0 0 1 0 1 1
2< C14/>2 1
0 0

Sample Output

4
oh,it ' s impossible~!!

Description of the first set of data Hint:
Altogether the following four ways:
Operation Switch 1
Operation Switch 2
Operation Switch 3
Operation switches 1, 2, 3 (not in order)

Source Liangliang@poj

Output 1<< (number of free variables)


#include <iostream> #include <stdio.h> #include <algorithm> using namespace std;
const int MAXN = 33;
#define LL long-long int A[MAXN][MAXN];
		void f (int n) {for (int i=1;i<=n;i++) {for (int j=1;j<=n+1;j++) {printf ("%d", a[i][j]);
	printf ("\ n");
printf ("\ n");
	LL guass (int n,int m) {int x,y;
	X=y=1;
		for (; x<=n&&y<=m;x++,y++) {//F (n);
		int maxx=x;
		for (int i=x+1;i<=n;i++) {if (ABS (A[i][y]) >abs (a[maxx][y)) maxx=i;
		} if (maxx!=x) for (int i=y;i<=m+1;i++) swap (a[x][i],a[maxx][i]);
			if (a[x][y]==0) {x--;
		Continue
				for (int i=x+1;i<=n;i++) {if (A[i][y]) {for (int j=y;j<=m+1;j++) {a[i][j]=a[i][j]^a[x][j];
	for (int i=x;i<=n;i++) {if (a[i][m+1]!=0) return-1;

Return 1<< (m+1-x);
	int main () {int k;
	scanf ("%d", &k);
		while (k--) {memset (a,0,sizeof a);
		int n;
		scanf ("%d", &n);
		for (int i=1;i<=n;i++) scanf ("%d", &a[i][n+1]); For(int i=1;i<=n;i++)
			{int x;scanf ("%d", &x);
			A[i][n+1]^=x;
		A[i][i]=1;
		int u,v;
		scanf ("%d%d", &u,&v);
			while (u+v!=0) {a[v][u]=1;
		scanf ("%d%d", &u,&v);
		LL ans=guass (n, N); if (ans==-1) {printf ("Oh,it ' s impossible~!!
		\ n ");
		}else{printf ("%lld\n", ans); }

	}

}

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.