FZUOJ 1704 Turn off the light (高斯消元+大数高精度)

Problem 1704 Turn off the light

Accept: 84    Submit: 263
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

There are N lights and M switches in Peter’s bedroom. Every switch controls some lights among all. When press the switch, these lights will change their statuses (the lighting ones will be turn off, while the shutting one will be turn on.) Now some lights are lighting. Peter would like to know that under the premises of not pressing one switch twice, how many methods he could turn all the lights off. There is always at least one way to turn all the lights off.

Input

The first line of the input is an integer T which indicates the number of test cases.
For each test case, the first line are two integers N (1 ≤ N ≤ 100) and M (1 ≤ M ≤ 100).
Second line contains N number indicating that the ith light is on if the ith number is 1, or 0 otherwise.
The ith line of the following M lines contains a number D (1 ≤ D ≤ N) and D numbers, indicating the lights under this switch’s control. Lights are numbered from 1 to N.

Output

For each test case, print a line contains the answer.

Sample Input

12 31 12 1 21 11 2

Sample Output

2

还是一道开关问题,不同的是需要用大数高精度运算



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 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");
	}
}
int 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(int 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;
	if(k<var)
	return var-k;
	for(i=var-1;i>=0;i--)
	{
		int temp=a[i][var]%2;
		for(j=i+1;j<var;j++)
		if(a[i][j])
		temp=(temp-a[i][j]*x[j]%2+2)%2;
		x[i]=(temp/a[i][i]%2);
	}
	return 0;
}
void init()
{
	int i,j;
	mem(a);mem(x);mem(free_x);
	scanf("%d%d",&equ,&var);
	for(i=0;i<equ;i++)
	scanf("%d",&a[i][var]);
	for(i=0;i<var;i++)
	{
		int q,w;
		scanf("%d",&q);
		while(q--)
		{
			scanf("%d",&w);
			a[w-1][i]=1;
		}
	}
}
int main()
{
	int t,i,j;
	int cas=0;
	scanf("%d",&t);
	while(t--)
	{
		init();
		int ans=Gauss();
		if(ans==-1)
		printf("0\n");
		else
		{
			int s[50];
			mem(s);
			s[0]=1;
			int k=1;
			for(i=1;i<=ans;i++)
			{
				int kk=0;
				for(j=0;j<k;j++)
				{
					s[j]=s[j]*2+kk;
					kk=s[j]/10;
					s[j]=s[j]%10;
				}
				if(kk)
				s[k++]=kk;
			}
			for(i=k-1;i>=0;i--)
			printf("%d",s[i]);
			printf("\n");
		}
	}
	return 0;
}


你可能感兴趣的:(FZUOJ 1704 Turn off the light (高斯消元+大数高精度))