HDU 3364 高斯消元

Lanterns

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


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
 

 解析  0表示关  1表示开    n个灯就表示n个方程 m个变元  第 i 个方程中可以控制第 i 个灯的开关所对应的系数就是1 否则就是0  可以构造出来一个增广矩阵。

   然后进行高斯消元解 0 1 异或方程组  若自由变元ans    答案就是1<<ans

#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("
");
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl;
using namespace std;
const int maxn= 50+10;
const int inf = 0x3f3f3f3f,mod=998244353;
typedef long long ll;
int A[maxn][maxn],b[maxn][maxn];//增广矩阵
int x[maxn];//解集
bool FreeX[maxn];//标记是否是不确定的变元
inline int gcd(int a,int b){int t;while(b!=0){t=b;b=a%b;a=t;}return a;}
inline int LCM(int a,int b){return a/gcd(a,b)*b;}//先除后乘防溢出
int Gauss(int Equ,int Var)
{
    int i,j,k;
    int MaxRow;
    int col = 0;
    int Lcm;
    int ta,tb;
    int temp;
    int FreeXNum;
    int FreeIndex;
    for(i = 0; i <= Var; ++i)
    {
        x[i] = 0;
        FreeX[i] = true;
    }
    for(k = 0; k < Equ && col < Var; ++k,++col)
    {
        MaxRow = k;
        for(i = k+1; i < Equ; ++i)
        {
            if(abs(A[i][col]) > abs(A[MaxRow][col]))
                MaxRow = i;
        }
        if(MaxRow != k)
        {
            for(j = k; j < Var+1; ++j)
                swap(A[k][j],A[MaxRow][j]);
        }
        if(A[k][col] == 0)
        {
            k--;
            continue;
        }
        for(i = k+1; i < Equ; ++i)
        {
            if(A[i][col])
            {
                // Lcm = LCM(abs(A[i][col]),abs(A[k][col]));
                // ta = Lcm / abs(A[i][col]);
                // tb = Lcm / abs(A[k][col]);
                // if(A[i][col] * A[k][col] < 0)
                //     tb = -tb;
                for(j = col; j < Var+1; ++j)
                {
                    A[i][j] ^= A[k][j]; //(A[i][j]*ta%2 - A[k][j]*tb%2 + 2) % 2;
                }
            }
        }
    }  

    for(i = k; i < Equ; ++i)
    {
        if(A[i][col])
            return -1;
    }
    if(k < Var)
        return Var - k;        //自由变元个数
    for(int i=Var-1;i>=0;i--)   //求唯一解
    {
        x[i]=A[i][Var];
        for(int j=i+1;j<Var;j++)
            x[i]^=(A[i][j]&&x[j]);
    }
    return 0;
}
int main()
{
    int t,kase=1;
    int equ,var,q;
    scanf("%d",&t);
    while(t--)
    {
        memset(b, 0, sizeof(b));
        scanf("%d %d", &equ,&var);
        for(int i=0;i<var;i++)
        {
            int k,x;
            scanf("%d",&k);
            while(k--)
            {
                scanf("%d",&x);
                b[x-1][i]=1;
            }
        }
        printf("Case %d:
",kase++);
        scanf("%d",&q);
        while(q--)
        {
            for(int i=0;i<maxn;i++)
                for(int j=0;j<maxn;j++)
                    A[i][j]=b[i][j];
            for(int i=0;i<equ;i++)
                scanf("%d",&A[i][var]);
            int ans=Gauss(equ,var);
            if(ans==-1)
                printf("0
");
            else
               printf("%lld
",1ll<<(ans));
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/stranger-/p/9526115.html