HDOJ 3364 Lanterns (高斯消元)

Lanterns

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


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
 

求多少种办法,也就是有多少种解,注意原始矩阵需要不断地赋值,所以用一个map数组记录原始,然后每一次都
要赋给a。。。


ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
int a[MAXN][MAXN];
int map[MAXN][MAXN]; 
int equ,var;
int x[MAXN];
int free_x[MAXN];
int free_num;
void debug()
{
	int i,j;
	printf("debug\n");
	for(i=0;i<equ;i++)
	{
		for(j=0;j<var;j++)
		printf("%d ",a[i][j]);
		printf("\n");
	}
}
ll Gauss()
{
	int i,j,col,k,max_r;
	free_num=0;
	for(k=0,col=0;k<equ&&col<var;k++,col++)
	{
		max_r=k;
		for(i=k+1;i<equ;i++)
		{
			if(abs(a[i][col])>abs(a[max_r][col]))
			max_r=i;
		}
		if(a[max_r][col]==0)
		{
			k--;
			free_x[free_num++]=col;
			continue;
		}
		if(max_r!=k)
		{
			for(j=col;j<var+1;j++)
			swap(a[k][j],a[max_r][j]);
		}
		for(i=k+1;i<equ;i++)
		{
			if(a[i][col])
			{
				int lcm=a[k][col]/gcd(a[k][col],a[i][col])*a[i][col];
                int ta=lcm/a[i][col],tb=lcm/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)%2-(a[k][j]*tb)%2+2)%2;
			}
		}
	}
	for(i=k;i<equ;i++)
	if(a[i][col])
	return -1;
	//printf("%d\n",var-k);
	return pow(2,var-k+0.0);
}
void init()
{
	int i,j;
	mem(x);mem(free_x);mem(map);
	scanf("%d%d",&equ,&var);
	for(i=0;i<var;i++)
	{
		int q,w;
		scanf("%d",&q);
		while(q--)
		{
			scanf("%d",&w);
			map[w-1][i]=1;
		}
	}
}
int main()
{
	int t,i,j,m;
	int cas=0;
	scanf("%d",&t);
	while(t--)
	{
		init();
		printf("Case %d:\n",++cas);
		scanf("%d",&m);
		while(m--)
		{
			for(i=0;i<equ;i++)
			for(j=0;j<var;j++)
			a[i][j]=map[i][j];
			for(i=0;i<equ;i++)
			scanf("%d",&a[i][var]);
			ll ans=Gauss();
			if(ans==-1)
			printf("0\n");
			else
			printf("%I64d\n",ans);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值