HDU 3364 Lanterns(高斯消元)

来源:互联网 发布:matlab 知乎 编辑:程序博客网 时间:2024/06/05 08:50
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
23 22 1 22 1 320 1 11 1 13 300020 0 01 0 0
 

Sample Output
Case 1:10Case 2:80
 
分析:构造一个n*m的矩阵,然后消元,得到自由元的个数就行了
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;int t,n,m;int st[55],ed[55];int mp[55][55],mat[55][55];int equ,var;int X[55],free_x[55];int free_num;int Gauss(){    int max_r,col,k;    free_num=0;    for(k=0,col=0;k<equ&&col<var;k++,col++)    {        max_r=k;        for(int i=k+1;i<equ;i++)        {            if(abs(mp[i][col])>abs(mp[max_r][col]))                max_r=i;        }        if(mp[max_r][col]==0)        {            k--;            free_x[free_num++]=col;            continue;        }        if(max_r!=k)        {            for(int j=col;j<var+1;j++)                swap(mp[k][j],mp[max_r][j]);        }        for(int i=k+1;i<equ;i++)        {            if(mp[i][col]!=0)            {                for(int j=col;j<var+1;j++)                    mp[i][j]^=mp[k][j];            }        }    }    for(int i=k;i<equ;i++)        if(mp[i][col]!=0)  return -1;    return var-k;}int main(){    int x,y,k;    int cas=1;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        CLEAR(mat,0);//n*m的矩阵        REP(i,m)        {            scanf("%d",&k);            while(k--)            {                scanf("%d",&x);                mat[x-1][i]=1;            }        }        scanf("%d",&k);        printf("Case %d:\n",cas++);        equ=n;var=m;        while(k--)        {            REP(i,n)              scanf("%d",&mat[i][m]);//n*m的矩阵            memcpy(mp,mat,sizeof(mat));            int ans=Gauss();            if(ans==-1)                puts("0");            else                printf("%lld\n",1LL<<ans);        }    }    return 0;}


0 0
原创粉丝点击