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. RabbitMQ基本用法、消息分发模式、消息持久化、广播模式

    RabbitMQ基本用法 进程queue用于同一父进程创建的子进程间的通信 而RabbitMQ可以在不同父进程间通信(例如在word和QQ间通信) 示例代码 生产端(发送) import pika c ...

  2. MYSQL导入excel

    MYSQL使用navicat导入excel 第一步:首先需要准备好有数据的excel 第二步:选择"文件"->"另存为",保存为"CSV(逗号分 ...

  3. C语言总结的知识点

    什么是有效数字? ------------------------- 数据类型 运算符: 函数: 程序结构: 存储结构 内存管理 关键字: ------------------------- C语言: ...

  4. Selenium入门系列2 窗口大小控制

    selenium控制窗口最大化.适合手机的宽度.适合pad的宽度等尝试下实例,网站是否做了响应式布局 #coding=utf-8 # 改变浏览器窗口大小.前进后退 from selenium impo ...

  5. IOS transform的使用(移动,放大,旋转)

    @interface ViewController () - (IBAction)up; - (IBAction)big ; - (IBAction)leftRotate ; @property (n ...

  6. 一款带有CSS的单选框以及选中事件

    html <div class="radio radio-success"> <input type=" name="radioSingle1 ...

  7. NutDao配置多数据源

    首先,我必须声明,这是一个非常简单的方法,很多小菜没做出来,是因为把nutz想得太复杂 数据源(或者是数据库连接池),在Nutz.Ioc看来,是一个普通的Bean,没任何特别之处. 再强调一点,除了$ ...

  8. Android(java)学习笔记67:Android Studio新建工程中的build.gradle、settings.gradle

    随着信息化的快速发展,IT项目变得越来越复杂,通常都是由多个子系统共同协作完成.对于这种多系统.多项目的情况,很多构建工具都已经提供了不错的支持,像maven.ant.Gradle除了借鉴了ant或者 ...

  9. redis set类型

  10. jQuery实现轮播切换以及将其封装成插件(1)

    我们在网上经常会看到一些轮播切换的效果.轮播切换,就是在一个有限的空间中定时的像走马灯一样去播放一组图片,当然也可以通过鼠标悬停在小按钮上来切换显示.下面我们将一步一步的实现这一效果. 为保证效果,请 ...