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 ;
}

HDU 3364 高斯消元的更多相关文章

  1. HDU 2827 高斯消元

    模板的高斯消元.... /** @Date : 2017-09-26 18:05:03 * @FileName: HDU 2827 高斯消元.cpp * @Platform: Windows * @A ...

  2. hdu 3915 高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=3915 这道题目是和博弈论挂钩的高斯消元.本题涉及的博弈是nim博弈,结论是:当先手处于奇异局势时(几堆石子数相互 ...

  3. HDU 3359 高斯消元模板题,

    http://acm.hdu.edu.cn/showproblem.php?pid=3359 题目的意思是,由矩阵A生成矩阵B的方法是: 以a[i][j]为中心的,哈曼顿距离不大于dis的数字的总和 ...

  4. [置顶] hdu 4418 高斯消元解方程求期望

    题意:  一个人在一条线段来回走(遇到线段端点就转变方向),现在他从起点出发,并有一个初始方向, 每次都可以走1, 2, 3 ..... m步,都有对应着一个概率.问你他走到终点的概率 思路: 方向问 ...

  5. HDU 4418 高斯消元解决概率期望

    题目大意: 一个人在n长的路径上走到底再往回,走i步停下来的概率为Pi , 求从起点开始到自己所希望的终点所走步数的数学期望 因为每个位置都跟后m个位置的数学期望有关 E[i] = sigma((E[ ...

  6. hdu 5088 高斯消元n堆石子取k堆石子使剩余异或值为0

    http://acm.hdu.edu.cn/showproblem.php?pid=5088 求能否去掉几堆石子使得nim游戏胜利 我们可以把题目转化成求n堆石子中的k堆石子数异或为0的情况数.使用x ...

  7. hdu 2262 高斯消元求期望

    Where is the canteen Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  8. hdu 4418 高斯消元求期望

    Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. HDU 3364 Lanterns (高斯消元)

    题意:有n个灯和m个开关,每个开关控制数个灯的状态改变,给出k条询问,问使灯的状态变为询问中的状态有多少种发法. 析:同余高斯消元法,模板题,将每个开关控制每个灯列成行列式,最终状态是结果列,同余高斯 ...

随机推荐

  1. The thirteen day

    Well begun is hanlf done 良好的开端是成功的一半.(此句是省略句,Something that is well begun is something that is half ...

  2. sharepoint2007就地升级2010系列(四)升级数据库

    上一篇我们完成了系统的升级,今天我们来看一下SQL2005X64是如何升级到SQL2008X64的. 首先,我们先停掉所有sharepoint的服务 其实网上的文档并没有写到这一步,但是我个人觉得,要 ...

  3. One special dictionary

    由于项目一个功能需要,可以将关键字的值叠加加来,最终可以获取对这些关键字都做了些什么操作. Generic Programming is very powerful. /// <summary& ...

  4. (C# 基础) 类访问修饰符

    C# 中有5个权限修饰符,用于控制对对象的访问权限. 1. public:   访问不受限制. namespace, enum成员,interface成员 隐式的具有public 修饰符,不能在显式添 ...

  5. formvalidator插件

    一.引用jquery 二.引用formValidator.js //================================================================== ...

  6. Azure IOT 设备固件更新技巧,看这一篇就够了

    嫌长不看版 今天为大家准备的硬菜是:在 Azure IoT 中心创建 Node.js 控制台应用,进行端到端模拟固件更新,为基于 Intel Edison 的设备安装新版固件的流程.通过创建模拟设备应 ...

  7. sql分组数据去重

    #分组获得每个机柜里服务器占用的机架总数,如552807e6-b428-4184-b219-ae368c68ddb3占用4个 mysql> select cabinet_uuid, count( ...

  8. 关于VisualStudio2010发布项目问题

    VisualStudio2010速度还是很给力的,VS2015打开机器就双100%了:VS2010机器上跑起来还是很好用的. 今天编译一个MVC3.0项目,发布时候出现诡异现象:Content文件夹里 ...

  9. powershell解决win10开始菜单和通知中心无法打开

    然后通过 Ctrl + Shift + Esc 弹出任务管理器点击文件-->运行新任务 在打开的填写框里面输入 "powershell"同时勾选下方的"以管理员身份 ...

  10. Raknet—视频会议系统最佳的数据传输引擎

    RakNet是一个跨平台的C++和C#的游戏引擎,它主要是为高效的数据传输而设计,使用者可以通过它进行游戏和其他的程序的开发.RakNet虽然是一个游戏引擎,但同样也是一个非常好的视频会议系统传输引擎 ...