HDU 3364 Lanterns(高斯消元)
2015-09-25 14:07
531 查看
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
Sample Output
分析:构造一个n*m的矩阵,然后消元,得到自由元的个数就行了
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的矩阵,然后消元,得到自由元的个数就行了
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int INF=0x3f3f3f3f; typedef long long LL; int t,n,m; int st[55],ed[55]; int mp[55][55],mat[55][55]; int equ,var; int X[55],free_x[55]; int free_num; int Gauss() { int max_r,col,k; free_num=0; for(k=0,col=0;k<equ&&col<var;k++,col++) { max_r=k; for(int i=k+1;i<equ;i++) { if(abs(mp[i][col])>abs(mp[max_r][col])) max_r=i; } if(mp[max_r][col]==0) { k--; free_x[free_num++]=col; continue; } if(max_r!=k) { for(int j=col;j<var+1;j++) swap(mp[k][j],mp[max_r][j]); } for(int i=k+1;i<equ;i++) { if(mp[i][col]!=0) { for(int j=col;j<var+1;j++) mp[i][j]^=mp[k][j]; } } } for(int i=k;i<equ;i++) if(mp[i][col]!=0) return -1; return var-k; } int main() { int x,y,k; int cas=1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); CLEAR(mat,0);//n*m的矩阵 REP(i,m) { scanf("%d",&k); while(k--) { scanf("%d",&x); mat[x-1][i]=1; } } scanf("%d",&k); printf("Case %d:\n",cas++); equ=n;var=m; while(k--) { REP(i,n) scanf("%d",&mat[i][m]);//n*m的矩阵 memcpy(mp,mat,sizeof(mat)); int ans=Gauss(); if(ans==-1) puts("0"); else printf("%lld\n",1LL<<ans); } } return 0; }
相关文章推荐
- [.Net码农].Net中的json操作类
- unix的计划任务时间设置
- 解决:对COM 组件的调用返回了错误 HRESULT E_FAIL
- 对像转成 和 byte 互转类库方法
- DIY Ruby CPU 分析——Part I
- C程序设计实验1
- 关于iOS9系统对App应用的几个修改
- ios7 UITableViewCell高度自适应
- 转:表单中Readonly和Disabled的区别(HTML中使用javascript解除禁止input输入框代)
- haskell读写文件相关(含二进制)
- python模拟登录新浪微博
- 我必须改变2
- Beginner’s Guide nginx初学者手册
- 大话Philm的从无到有(二)满月篇
- 窗体使用空布局管理器后,组件不显示的问题
- symfony学习笔记--第二步,创建Bundle
- iOS 蓝牙开发(二)iOS 连接外设的代码实现
- Oracle实例和Oracle数据库(Oracle体系结构)
- 黑马程序员---java高新技术----反射技术
- 浅淡java类加载机制