HDU 3364

来源:互联网 发布:到底谁的心,谁人知 编辑:程序博客网 时间:2024/05/03 15:12

Lanterns

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


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个开关,每个开关控制数个灯的状态改变,给出k条询问,问使灯的状态变为询问中的状态有多少种发法。
思路:同余高斯消元法,模板题,将每个开关控制每个灯列成行列式,最终状态是结果列,同余高斯消元,如果无解就是0,否则结果就是1<<(自由变元的个数),为了学习各种高斯消元的模板。将一整套的高斯消元模板弄来了,包括非同余的,以供相互学习。
#include<stdio.h>#include<algorithm>#include<iostream>#include<string.h>#include<math.h>using namespace std;const int maxn = 110;const int mod = 2;int a[maxn][maxn];//增广矩阵int b[maxn][maxn];int x[maxn];//解集bool free_x[maxn];//标记是否是不确定的变元int gcd(int a,int b){    return a == 0?b:gcd(b%a,a);}int lcm(int a,int b){    return a*b/gcd(a,b);//先除后乘防溢出}// 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,//-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)//有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var.int Gauss(int equ,int var){    int i,j,k;    int max_r;// 当前这列绝对值最大的行.    int col;//当前处理的列    int ta,tb;    int LCM;    int temp;    int free_x_num;    int free_index;//    for(int i=0;i<=var;i++)//    {//        x[i]=0;//        free_x[i]=true;//    }    //转换为阶梯阵.    col=0; // 当前处理的列    for(k = 0;k < equ && col < var;k++,col++)    {// 枚举当前处理的行.// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)        max_r=k;        for(i=k+1;i<equ;i++)        {            if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;        }        if(max_r!=k)        {// 与第k行交换.            for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);        }        if(a[k][col]==0)        {// 说明该col列第k行以下全是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[i][j]*ta-a[k][j]*tb)%mod+mod)%mod;//如果不是同模取余则改为a[i][j] = (a[i][j]*ta-a[k][j]*tb;                }            }        }    }    // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).    for (i = k; i < equ; i++)    { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.        if (a[i][col]) return -1;    }    // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.    // 且出现的行数即为自由变元的个数.    if (k < var)    {        //首先,自由变元有var - k个,即不确定的变元至少有var - k个.//        for (i = k - 1; i >= 0; i--)//        {//            // 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第equ行.//            // 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的.//            free_x_num = 0; // 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元.//            for (j = 0; j < var; j++)//            {//                if (a[i][j] != 0 && free_x[j]) free_x_num++, free_index = j;//            }//            if (free_x_num > 1) continue; // 无法求解出确定的变元.//            // 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的.//            temp = a[i][var];//            for (j = 0; j < var; j++)//            {//                if (a[i][j] != 0 && j != free_index) temp -= a[i][j] * x[j];//            }//            x[free_index] = temp / a[i][free_index]; // 求出该变元.//            free_x[free_index] = 0; // 该变元是确定的.//        }        return var - k; // 自由变元有var - k个.    }    // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.    // 计算出Xn-1, Xn-2 ... X0.    //需要解得时候就用下面的循环来解。//    for (i = var - 1; i >= 0; i--)//    {//        temp = a[i][var];//        for (j = i + 1; j < var; j++)//        {//            if (a[i][j] != 0) temp = ((temp - a[i][j]*x[j])%mod+mod)%mod;//temp -= a[i][j] * x[j];//        }//        while(temp % a[i][i] != 0) temp+=mod;//如果不是同模取余,则temp % a[i][i] != 0时产生浮点数解//        x[i] = (temp / a[i][i])%mod;//         if(x[i]<0)x[i]+=mod;//    }    return 0;}int main(){    int i, j;    int n,m,var;    int t,kase = 1;    scanf("%d",&t);    while (t--)    {        memset(b,0,sizeof(b));        scanf("%d%d", &n, &var);        for(int i = 0;i < var;i++)        {            int num;            scanf("%d",&num);            for(int j = 0;j < num;j++)            {                int nn;                scanf("%d",&nn);                b[nn-1][i] = 1;            }        }        scanf("%d",&m);        printf("Case %d:\n",kase++);        while(m--)        {            memcpy(a,b,sizeof(b));            for(int i = 0;i < n;i++)            {                scanf("%d",&a[i][var]);            }            int ans = Gauss(n,var);            if(ans == -1)                printf("0\n");            else if(ans == 0)                printf("1\n");            else                printf("%I64d\n",1LL<<ans);        }    }    return 0;}



0 0
原创粉丝点击