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. [原创]Debian9 安装配置MariaDB

    序言 这次玩次狠得.除了编译器使用yum安装,其他全部手动编译.哼~ 看似就Nginx.PHP.MySql三个东东,但是它们太尼玛依赖别人了. 没办法,想用它们就得老老实实给它们提供想要的东西. 首先 ...

  2. git 获取领先落后的命令

    git --git-dir=/data/usr/local/gerrit-site/git/aixuexi-admin.git rev-list --left-right --count master ...

  3. C#多线程Thread

    在项目中经常用到线程Thread,先做个简单记录,后面再完善下,方便以后参考.本人技术有限,如有不同见解之处,欢迎博友批评指正. 执行的线程Thread分无参数的,一个参数,多个参数的.直接看代码吧. ...

  4. 类型信息(RTTI和反射)——反射

    运行时类型信息可以让你在程序运行时发现和使用类型信息. 在Java中运行时识别对象和类的信息有两种方式:传统的RTTI,以及反射.下面就来说说反射. 重点说说通过反射获取方法以及调用方法,即类方法提取 ...

  5. Suggestion: use tools:overrideLibrary="android.support.v17.leanback" to force usage

    Android Studio下修改方法: 在manifest中添加<uses-sdk tools:overrideLibrary="android.support.v17.leanba ...

  6. Spark核心组件

    Spark核心组件 1.RDD resilient distributed dataset, 弹性分布式数据集.逻辑上的组件,是spark的基本抽象,代表不可变,分区化的元素集合,可以进行并行操作.该 ...

  7. Altium_Designer-PCB中各层作用详解

    一直以来,对PCB中各层,比如:solder层.paste层.Top overlay层等等这些一知半解.今天仔细看了下,向大家介绍一下,有不对的地方还请指正. 1.mechanical机械层是定义整个 ...

  8. 如何处理用代码创建SD Sales order时遇到的错误消息KI 180

    错误消息KI 180:You must enter a company code for transaction Create sales document 代码: REPORT zcreate_so ...

  9. HashMap通过hashcode对其内容进行快速查找,而 TreeMap中所有的元素都保持着某种固定的顺序

    HashMap通过hashcode对其内容进行快速查找,而 TreeMap中所有的元素都保持着某种固定的顺序,如果你需要得到一个有序的结果你就应该使用TreeMap(HashMap中元素的排列顺序是不 ...

  10. 【CCPC-Wannafly Winter Camp Day4 (Div1) F】小小马(分类讨论)

    点此看题面 大致题意: 给你一张\(n*m\)的棋盘,问你一匹马在两个点中是否存在一条经过黑白格子数目相等的路径. 简化题目 首先,我们来简化一下题目. 考虑到马每次走的时候,所经过的格子的颜色必然发 ...