HDU-3364 Lanterns(高斯消元)

                                                    Lanterns

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


 

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

 

题意:n个灯笼,m个开关,每个开关控制若干个灯笼,q次询问,每次询问给出n个灯笼的状态,问达到这种状态有多少种开关组合.

思路:列出n个方程,某开关控制此灯笼即为1,否则为0,然后根据询问就可以把方程列好,然后解方程,求自由变元的个数ans,答案即为2^ans.

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
const double eps = 1e-12;
const int inf = 0x3f3f3f3f;
map<int,int>::iterator it;

int n,m,q;
int a[52][52],om[52][52];
int x[52];//解集
bool free_x[52];//标记是否是不确定的变元

// 高斯消元法解方程组(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]!=0)
            {
                for(j=col; j<var+1; j++)
                {
                    a[i][j]^= a[k][j];
                }
            }
        }
    }

    // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).
    for (i = k; i < equ; i++)
    {
        // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.
        if (a[i][col] != 0) return -1;
    }
    // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.
    // 且出现的行数即为自由变元的个数.
    if (k < var)
        return var - k; // 自由变元有var - k个.
    // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.
    // 计算出Xn-1, Xn-2 ... X0.
    return 0;
}

int main()
{
	int t,cnt = 0;
	cin>>t;
	
	while(t--)
	{
		mem(a,0);
		mem(om,0);
		mem(x,0);
		mem(free_x,0);
		scanf("%d %d",&n,&m);
		for(int i = 1;i<= m;i++)
		{
			int k,x;
			scanf("%d",&k);
			for(int j = 1;j<= k;j++)
			{
				scanf("%d",&x);
				om[x-1][i-1] = 1;
			}
		}
		
		cin>>q;
		printf("Case %d:\n",++cnt);
		while(q--)
		{
			for(int i = 0;i< n;i++)
				for(int j = 0;j< m;j++)
					a[i][j] = om[i][j];
			
			for(int i = 0;i< n;i++)
			{
				int x;
				scanf("%d",&x);
				a[i][m] = x;
			}
			
			int ans = Gauss(n,m);
			if(ans == -1)
				printf("0\n");
			else
				printf("%lld\n",(ll)1<<ans);
		}
	}
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值