您的位置:首页 > 其它

hdu 3364(异或方程的高斯消元)

2016-01-23 17:53 351 查看

Lanterns

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)



[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

Case 1:
1
0
Case 2:
8
0

 
解题思路:n条方程,等式的右边是最终灯的状态,m个未知量,每个未知量的系数表示该开关能否影响灯的状态。最后只要求出方程组的自由变量即可,最后的答案是2^free。
这里的方程组是XOR不是简单的+,但采用的方式也是高斯消元,只是把加减法换成xor。

我自己写的代码一直WA,暂时没找到原因:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int n,m,A[50][50];
int q,B[50][50];

int Gauss()
{
int i = 0,j = 0,k,u,r;
while(i < n && j < m)	//处理第i个方程,第j个变量
{
r = i;
for(k = i+1; k < n; k++)
{
if(A[k][j])
{
r = k;
break;
}
}
if(A[r][j])
{
if(r != i)
{
for(k = 0; k <= m; k++)
swap(A[r][k],A[i][k]);
}
for(k = i+1; k < n; k++)	//进行异或消元
if(A[k][j])
{
for(u = j; u <= m; u++)
A[k][u] ^= A[i][u];
}
i++;
}
j++;
}
//判断解的情况
for(k = i; k < n; k++)
if(A[i][m]) return -1;
return i;
}

int main()
{
int t,cas = 1;
cin>>t;
while(t--)
{
cin>>n>>m;
memset(B,0,sizeof(B));
for(int i = 0; i < m; i++)
{
int k;
cin>>k;
while(k--)
{
int var;
cin>>var;
B[var-1][i] = 1;
}
}
cin>>q;
printf("Case %d:\n",cas++);
while(q--)
{
memcpy(A,B,sizeof(B));
for(int i = 0; i < n; i++)
cin>>A[i][m];
int ans = Gauss();
if(ans == -1) printf("0\n");
else cout<<(1LL<<(m-ans))<<endl;
}
}
return 0;
}


别人AC的代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=55;

struct matrix{
int f[maxn][maxn];
}e,g;

int Gauss(matrix a,int m,int n)//高斯消元
{
int i=1,j=1,k,r,u;
while(i<=m&&j<=n)//处理第i个方程,第j个变量
{
r=i;
for(k=i;k<=m;k++)
if(a.f[k][j]){r=k;break;}
if(a.f[r][j])
{
if(r!=i)for(k=0;k<=n+1;k++)swap(a.f[r][k],a.f[i][k]);
for(u=i+1;u<=m;u++)if(a.f[u][j])
for(k=i;k<=n+1;k++)a.f[u][k]^=a.f[i][k];
i++;
}
j++;
}
for(u=i;u<=m;u++)//判断无解
if(a.f[u][n+1])return -1;
return i-1;
}
int main()
{
int T,tt=0;
cin>>T;
while(T--)
{
int i,j,k,n,m,a,b,q,r;
cin>>n>>m;
memset(e.f,0,sizeof(e.f));
for(i=1;i<=m;i++)
{
cin>>k;
for(j=0;j<k;j++)
{
cin>>a;
e.f[a][i]=1;
}
}
cout<<"Case "<<++tt<<":"<<endl;
cin>>q;
while(q--)
{
for(i=1;i<=n;i++)
{
cin>>a;
e.f[i][m+1]=a;
}
r=Gauss(e,n,m);//求出有界遍历个数r
if(r==-1)cout<<0<<endl;
else cout<<(1LL<<(m-r))<<endl;//注意范围,超int
}
}
return  0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数学