Lanterns

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

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
 
Source

题意:

就是给了 n 个灯 m 个开关,每个开关能够控制很多灯,当然了每个灯也可以由多种开关控制。现在给了你每个开关能够控制的灯的标号,

然后给你一个最终状态,让你求的是能够达到这个最终状态的方法数(初始状态都是关着的,开着是 1,关着是 0).

思路:

解题思路:

有 n 个灯也就意味着我们要列 n 个方程, m 个开关就是 m 个未知数,首先通过输入的开关控制的灯可以确定初始的 a(系数矩阵),注意的是 a 矩阵的列和行的变化。

然后通过给定的最终状态来确定 a∗x = b 的 列矩阵 b,然后就正常高消就行了,还有需要注意的一点是:它给定的 Q 个询问的时候,我们要提前将 a 矩阵保存

因为高消之后 a 矩阵就变了,所以我们就将 a 矩阵保存。

代码:

 //#include"bits/stdc++.h"
#include<sstream>
#include<iomanip>
#include"cstdio"
#include"map"
#include"set"
#include"cmath"
#include"queue"
#include"vector"
#include"string"
#include"cstring"
#include"time.h"
#include"iostream"
#include"stdlib.h"
#include"algorithm"
#define db double
#define ll long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i, n) for(int i=0;i<n;i++)
const int N = 1e2+ ;
//const int mod = 1e9 + 7;
//const int MOD = mod - 1;
const int inf = 0x3f3f3f3f;
const db PI = acos(-1.0);
const db eps = 1e-;
using namespace std;
int equ,var;//equ个方程,var个变量,增广矩阵行数为equ,列数为var+1,分别为0到var
int a[N][N];//增广矩阵
int x[N];//存储自由变元
int f_x[N];
int free_x;//自由变元个数
void swap(int &x,int &y){
int t;
t=x,x=y,y=t;
}
int Gauss()
{
int ma_r,col,k;
free_x=;
for(k=,col=;k<equ&&col<var;k++,col++){
ma_r = k;
for (int i = k + ; i < equ; i++) if (abs(a[i][col] > abs(a[ma_r][col]))) ma_r = i;//取系数最大的一行
if (!a[ma_r][col]) {
k--;
f_x[free_x++] = col;
continue;
}
if (ma_r != k)
for (int j = col; j < var + ; j++) swap(a[k][j], a[ma_r][j]);//与当前行交换 for (int i = k + ; i < equ; i++)
if (a[i][col] != )
for (int j = col; j < var + ; j++) a[i][j] ^= a[k][j];//消除其他行第col列的变量
}
for(int i=k;i<equ;i++) if(a[i][col]!=) return -;//没被消除则无解 if(k<var) return var-k;//自由变元个数
//唯一解,回代
for(int i=var-;i>=;i--){
x[i]=a[i][var];
for(int j=i+;j<var;j++) x[i]^=(a[i][j]&&x[j]);//自下而上
}
return ;
}
int b[N][N];
int n,m;
int main()
{
int t;
ci(t);
for(int I=;I<=t;I++)
{
ci(n),ci(m);
memset(a,, sizeof(a));
for(int i=;i<m;i++){
int k,c;
ci(k);
for(int j=;j<k;j++) ci(c),a[c-][i]=;
}
equ=n,var=m;
for(int i=;i<equ;i++){
for(int j=;j<var;j++){
b[i][j]=a[i][j];
}
}
int q;
ci(q);
printf("Case %d:\n",I);
while(q--)
{
for(int i=;i<equ;i++)
for(int j=;j<var;j++)
a[i][j]=b[i][j];
for(int i=;i<n;i++) ci(a[i][var]);
int ans=Gauss();
if(ans==-) puts("");
else pl(1ll<<ans);
}
}
return ;
}

最新文章

  1. .NET 垃圾回收与内存泄漏
  2. 如何搭建SVN服务器,详细安装步骤。
  3. 374&amp;375. Guess Number Higher or Lower 1&amp;2
  4. Ubuntu 配置有线网 IP
  5. SSH配置中出现问题
  6. 日本DARTS 支撑的一系列应用项目
  7. PHP-CS-Fixer:格式化你的PHP代码
  8. 代码生成AnimatorController
  9. python 守护进程 daemon
  10. ASP.NET几种页面数据绑定的用法及区别: &lt;%#、 &lt;%=、 &lt;% 、&lt;%@
  11. WP7 MD5加密
  12. Oracle基础 exp/imp 数据泵导入/导出 命令
  13. 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇04:碰撞检测》
  14. POJ 1797 Heavy Transportation (dijkstra 最小边最大)
  15. 设计模式——java
  16. Android之ExpandableListView的属性(Group不展开)
  17. Android icons集合
  18. 完整的 HTML 4 + HTML 5 实体参考手册
  19. mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)
  20. UVa10474

热门文章

  1. SublimeText插件autoprefixer : css 添加前续
  2. Android AS升级3.1 编译报错:The SourceSet &#39;instrumentTest&#39; is not recognized by the Android Gradle Plugin.
  3. Android setUserVisibleHint-- fragment真正的onResume和onPause方法
  4. python模块详解 re
  5. ansible使用9-Playbooks: Special Topics
  6. java:错误Error,异常Excepition
  7. ztree的数据绑定
  8. Linux与Windows区别——总结中
  9. 如何更换vim-airline的theme
  10. 双击易语言没有反应,按住shift再双击可解决