HDOJ 3364 Lanterns (高斯消元)
2016-01-31 19:09
381 查看
Lanterns
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1338 Accepted Submission(s): 525
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
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; }
相关文章推荐
- 相关矩阵到相似矩阵--Ochiia系数
- Delphi中始终用WebBrowser打开网页
- 交叉编译OpenWRT应用程序
- hdu 2131 Probability
- mysql-5.6.27-winx64 achieve 配置
- Lua数据结构 — TString(二)
- POJ3273-Monthly Expense-二分答案
- 离线安装 Ambari Hadoop
- 习题3-1 得分 UVa1585
- 杭电2391Filthy Rich(动态规划)
- POJ3258-River Hopscotch-二分答案
- C#成神之路<2> 关于基本类对象的创建操作
- 分布式架构
- NGUI ScrollView 最彻底优化方案 加载大量(百万级)Item不卡的()
- 查找 4
- 掌握需求过程
- P27 (**) Group the elements of a set into disjoint subsets.
- P27 (**) Group the elements of a set into disjoint subsets.
- 开源库链接收藏
- delphi中webbrowser的用法