经典问题:开关灯问题(高斯消元)(3364)

Lanterns

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1393    Accepted Submission(s): 551


Problem Description
Alice has received a beautiful present from Bob. The present contains n lanterns and m switches. Each switch controls some lanterns and pushing the switch will change the state of all lanterns it controls from off to on or from on to off. A lantern may be controlled by many switches. At the beginning, all the lanterns are off.

Alice wants to change the state of the lanterns to some specific configurations and she knows that pushing a switch more than once is pointless. Help Alice to find out the number of ways she can achieve the goal. Two ways are different if and only if the sets (including the empty set) of the switches been pushed are different.
 

Input
The first line contains an integer T (T<=5) indicating the number of test cases.
The first line of each test case contains an integer n (1<=n<=50) and m (1<=m<=50).
Then m lines follow. Each line contains an integer k (k<=n) indicating the number of lanterns this switch controls.
Then k integers follow between 1 and n inclusive indicating the lantern controlled by this switch.
The next line contains an integer Q (1<=Q<=1000) represent the number of queries of this test case.
Q lines follows. Each line contains n integers and the i-th integer indicating that the state (1 for on and 0 for off) of the i-th lantern of this query.
 

Output
For each test case, print the case number in the first line. Then output one line containing the answer for each query.
Please follow the format of the sample output.
 

Sample Input
  
  
2 3 2 2 1 2 2 1 3 2 0 1 1 1 1 1 3 3 0 0 0 2 0 0 0 1 0 0
 

Sample Output
  
  
Case 1: 1 0 Case 2: 8 0


利用线性方程组的增广矩阵不断进行初等行变换,每次都要选择绝对值最大的元素,这样可以让误差尽量小(虽然这道题并没有体现出来)。最后所求的答案是2^自由元的个数。


/*------------------Header Files------------------*/
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <vector>
#include <set>
#include <limits.h>
using namespace std;
/*------------------Definitions-------------------*/
#define LL long long
#define uLL unsigned long long
#define PI acos(-1.0)
#define INF 0x3F3F3F3F
#define MOD 1000000007
#define MAX 60
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
/*---------------------Work-----------------------*/
int rec[MAX][MAX],cop[MAX][MAX],n,m;
double gauss() //n行m+1列的增广矩阵
{
	int i,j,k,t;
	for(i=0,j=0;i<n&&j<m;i++,j++)
	{
		int id=i;
		for(k=i+1;k<n;k++) if(abs(rec[k][j])>abs(rec[id][j])) id=k; //选绝对值最大的
		if(id!=i) //交换行,避免精度误差
		{
			for(t=j;t<=m;t++) swap(rec[i][t],rec[id][t]);
		}
		if(rec[i][j]==0) //说明第j列第i行以下全是0,于是处理下一列
		{
			i--;
			continue;
		}
		for(k=i+1;k<n;k++) //处理下面每一行
		{
			if(rec[k][j]==0) continue;
			for(t=j;t<=m;t++) rec[k][t]=rec[k][t]^rec[i][t]; //注意这一步,要取异或
		}
	}
	for(k=i;k<n;k++) if(rec[k][m]!=0) return 0; //无解
	if(i<=m) return pow(2.0,m-i); //返回2次方的自由元
}
void work()
{
	int T; cin>>T;
	for(int Case=1;Case<=T;Case++)
	{
		printf("Case %d:\n",Case);
		memset(rec,0,sizeof(rec));
		memset(cop,0,sizeof(cop));
		scanf("%d%d",&n,&m);
		int ans1,ans2;
		for(int i=1;i<=m;i++)
		{
			scanf("%d",&ans1);
			for(int j=1;j<=ans1;j++)
			{
				scanf("%d",&ans2);
				rec[ans2-1][i-1]=1;
				cop[ans2-1][i-1]=1;
			}
		}
		int Q;
		scanf("%d",&Q);
		for(int i=1;i<=Q;i++)
		{
			for(int j=0;j<n;j++)
				for(int k=0;k<m;k++)
					rec[j][k]=cop[j][k];
			for(int j=0;j<n;j++) //增广矩阵最后一列
				scanf("%d",&rec[j][m]);
			double ans=gauss(); //数据可能很大用double存储
			printf("%.0lf\n",ans);
		}
	}
}
/*------------------Main Function------------------*/
int main()
{
	//freopen("test.txt","r",stdin);
	work();
	return 0;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值