hdu 3364(异或方程的高斯消元)
来源:互联网 发布:程序员密码 编辑:程序博客网 时间:2024/06/02 04:20
Lanterns
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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.
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.
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.
Please follow the format of the sample output.
Sample Input
23 22 1 22 1 320 1 11 1 13 300020 0 01 0 0
Sample Output
Case 1:10Case 2:80
解题思路:n条方程,等式的右边是最终灯的状态,m个未知量,每个未知量的系数表示该开关能否影响灯的状态。最后只要求出方程组的自由变量即可,最后的答案是2^free。
这里的方程组是XOR不是简单的+,但采用的方式也是高斯消元,只是把加减法换成xor。
我自己写的代码一直WA,暂时没找到原因:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int n,m,A[50][50];int q,B[50][50];int Gauss(){int i = 0,j = 0,k,u,r;while(i < n && j < m)//处理第i个方程,第j个变量 {r = i;for(k = i+1; k < n; k++){if(A[k][j]){r = k;break;}}if(A[r][j]){if(r != i){for(k = 0; k <= m; k++)swap(A[r][k],A[i][k]);}for(k = i+1; k < n; k++)//进行异或消元if(A[k][j]){for(u = j; u <= m; u++)A[k][u] ^= A[i][u];}i++;}j++;}//判断解的情况for(k = i; k < n; k++)if(A[i][m]) return -1;return i;}int main(){int t,cas = 1;cin>>t;while(t--){cin>>n>>m;memset(B,0,sizeof(B));for(int i = 0; i < m; i++){int k;cin>>k;while(k--){int var;cin>>var;B[var-1][i] = 1;}}cin>>q;printf("Case %d:\n",cas++);while(q--){memcpy(A,B,sizeof(B));for(int i = 0; i < n; i++)cin>>A[i][m];int ans = Gauss();if(ans == -1) printf("0\n");else cout<<(1LL<<(m-ans))<<endl;}}return 0;}
别人AC的代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=55;struct matrix{ int f[maxn][maxn];}e,g;int Gauss(matrix a,int m,int n)//高斯消元{ int i=1,j=1,k,r,u; while(i<=m&&j<=n)//处理第i个方程,第j个变量 { r=i; for(k=i;k<=m;k++) if(a.f[k][j]){r=k;break;} if(a.f[r][j]) { if(r!=i)for(k=0;k<=n+1;k++)swap(a.f[r][k],a.f[i][k]); for(u=i+1;u<=m;u++)if(a.f[u][j]) for(k=i;k<=n+1;k++)a.f[u][k]^=a.f[i][k]; i++; } j++; } for(u=i;u<=m;u++)//判断无解 if(a.f[u][n+1])return -1; return i-1;}int main(){ int T,tt=0; cin>>T; while(T--) { int i,j,k,n,m,a,b,q,r; cin>>n>>m; memset(e.f,0,sizeof(e.f)); for(i=1;i<=m;i++) { cin>>k; for(j=0;j<k;j++) { cin>>a; e.f[a][i]=1; } } cout<<"Case "<<++tt<<":"<<endl; cin>>q; while(q--) { for(i=1;i<=n;i++) { cin>>a; e.f[i][m+1]=a; } r=Gauss(e,n,m);//求出有界遍历个数r if(r==-1)cout<<0<<endl; else cout<<(1LL<<(m-r))<<endl;//注意范围,超int } } return 0;}
0 0
- hdu 3364(异或方程的高斯消元)
- HDU 3364 Lanterns 高斯消元(水 异或方程
- hdu 3364 Lanterns(高斯消元模板题+亦或方程)
- HDU-5833 Zhu and 772002(异或方程高斯消元)
- 高斯消元 开关类问题(异或方程)
- HDU - 5833 Zhu and 772002(高斯消元解异或方程)
- POJ 1830 开关问题 高斯消元 异或方程
- POJ 1222 EXTENDED LIGHTS OUT 高斯消元 异或方程 (水
- poj 1222 EXTENDED LIGHTS OUT 高斯消元 异或方程
- HDU 3976 (高斯消元 KCL方程)
- HDU 3364 - Lanterns (高斯消元 + 异或方程组)
- 解方程(按位或)
- hdu 1496 hash 方程解的情况
- hdu 2199 二分法求方程的根
- HDU 4579 Random Walk (解方程)
- 二分专攻:HDU 2199(解方程)
- hdu 2199 解方程(二分法)
- hdu 2899(方程题,二分查找)
- DP------Unique Paths
- 2、[置顶] CAS单点登录源码解析之【服务器端】
- UINavigationItem和UItabBarItem的区别详解
- Linux学习—xShell连接vmware虚拟机设置Ip
- Xcode 调试与快捷键
- hdu 3364(异或方程的高斯消元)
- XMPP介绍三:Socket
- 3、 CAS单点登录源码解析之【单点登出】
- 4、CAS单点登录源码解析之【用户认证】
- phpcms某处储存型XSS(demo+本地演示)
- hdu1128 Self Numbers
- windows2003 iis6配置文件和win2008/2012 iis7.5配置文件
- ecshop hash登录 + wordpress mysql盲注字段
- SQLmap简介以及防火墙绕过方法