POJ 1830 开关问题(高斯消元求解的情况)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 8714 | Accepted: 3424 |
Description
Input
每组测试数据的格式如下:
第一行 一个数N(0 < N < 29)
第二行 N个0或者1的数,表示开始时N个开关状态。
第三行 N个0或者1的数,表示操作结束后N个开关的状态。
接下来 每行两个数I J,表示如果操作第 I 个开关,第J个开关的状态也会变化。每组数据以 0 0 结束。
Output
Sample Input
- 2
- 3
- 0 0 0
- 1 1 1
- 1 2
- 1 3
- 2 1
- 2 3
- 3 1
- 3 2
- 0 0
- 3
- 0 0 0
- 1 0 1
- 1 2
- 2 1
- 0 0
Sample Output
- 4
- Oh,it's impossible~!!
Hint
一共以下四种方法:
操作开关1
操作开关2
操作开关3
操作开关1、2、3 (不记顺序)
题目链接:POJ 1830
比较裸的一道题,记得开关自己跟自己是有关系的,即Mat[i][i]要恒为1,用高斯消元在判断了自由变量之后如果还出现非0系数,则说明无解,如果有解即有$freenum$个自由变量,那么显然每一个自由变量是随意取值的,每一个都取0或1,那么答案显然是$2^{freenum}$个
如何列方程呢?两边同时异或一下开始的状态即可以得到目标的状态,因为S->T的操作和0->(S xor T)的操作是一样的
代码:
- #include <stdio.h>
- #include <iostream>
- #include <algorithm>
- #include <cstdlib>
- #include <sstream>
- #include <numeric>
- #include <cstring>
- #include <bitset>
- #include <string>
- #include <deque>
- #include <stack>
- #include <cmath>
- #include <queue>
- #include <set>
- #include <map>
- using namespace std;
- #define INF 0x3f3f3f3f
- #define LC(x) (x<<1)
- #define RC(x) ((x<<1)+1)
- #define MID(x,y) ((x+y)>>1)
- #define fin(name) freopen(name,"r",stdin)
- #define fout(name) freopen(name,"w",stdout)
- #define CLR(arr,val) memset(arr,val,sizeof(arr))
- #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
- typedef pair<int, int> pii;
- typedef long long LL;
- const double PI = acos(-1.0);
- const int N = 31;
- int Mat[N][N];
- int Gaussian(int ne, int nv)
- {
- int ce, cv, i, j;
- for (ce = 1, cv = 1; ce <= ne && cv <= nv; ++ce, ++cv)
- {
- int te = ce;
- for (i = ce + 1; i <= ne; ++i)
- if (Mat[i][cv] > Mat[te][cv])
- te = i;
- if (te != ce)
- {
- for (i = cv; i <= nv + 1; ++i)
- swap(Mat[ce][i], Mat[te][i]);
- }
- if (!Mat[ce][cv])
- {
- --ce;
- continue;
- }
- for (i = ce + 1; i <= ne; ++i)
- {
- if (Mat[i][cv])
- {
- for (j = cv; j <= nv + 1; ++j)
- Mat[i][j] ^= Mat[ce][j];
- }
- }
- }
- for (i = ce; i <= ne; ++i)
- if (Mat[i][cv])
- return -1;
- return nv - (ce - 1);
- }
- int main(void)
- {
- int tcase, n, i;
- scanf("%d", &tcase);
- while (tcase--)
- {
- CLR(Mat, 0);
- scanf("%d", &n);
- for (i = 1; i <= n; ++i)
- {
- scanf("%d", &Mat[i][n + 1]);
- Mat[i][i] = 1;
- }
- for (i = 1; i <= n; ++i)
- {
- int x;
- scanf("%d", &x);
- Mat[i][n + 1] ^= x;
- }
- int a, b;
- while (~scanf("%d%d", &a, &b) && (a || b))
- Mat[b][a] = 1;
- int frnum = Gaussian(n, n);
- frnum == -1 ? puts("Oh,it's impossible~!!") : printf("%d\n", 1 << frnum);
- }
- return 0;
- }
POJ 1830 开关问题(高斯消元求解的情况)的更多相关文章
- POJ 1830 开关问题 高斯消元,自由变量个数
http://poj.org/problem?id=1830 如果开关s1操作一次,则会有s1(记住自己也会变).和s1连接的开关都会做一次操作. 那么设矩阵a[i][j]表示按下了开关j,开关i会被 ...
- POJ 1830 开关问题 (高斯消元)
题目链接 题意:中文题,和上篇博客POJ 1222是一类题. 题解:如果有解,解的个数便是2^(自由变元个数),因为每个变元都有两种选择. 代码: #include <iostream> ...
- POJ 1830 开关问题 [高斯消元XOR]
和上两题一样 Input 输入第一行有一个数K,表示以下有K组测试数据. 每组测试数据的格式如下: 第一行 一个数N(0 < N < 29) 第二行 N个0或者1的数,表示开始时N个开关状 ...
- POJ.1830.开关问题(高斯消元 异或方程组)
题目链接 显然我们需要使每个i满足\[( ∑_{j} X[j]*A[i][j] ) mod\ 2 = B[i]\] 求这个方程自由元Xi的个数ans,那么方案数便是\(2^{ans}\) %2可以用^ ...
- 【poj1830-开关问题】高斯消元求解异或方程组
第一道高斯消元题目~ 题目:有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关 ...
- 【poj2947】高斯消元求解同模方程组【没有AC,存代码】
题意: p start enda1,a2......ap (1<=ai<=n)第一行表示从星期start 到星期end 一共生产了p 件装饰物(工作的天数为end-start+1+7*x, ...
- 【zoj3645】高斯消元求解普通线性方程
题意: 给你一个方程组(含有12个方程),求(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11) 方程组的形式是一个二次方程组 (ai1-x1)^2 + (ai2-x2)^2 +( ...
- POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题
http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...
- POJ 3185 The Water Bowls 【一维开关问题 高斯消元】
任意门:http://poj.org/problem?id=3185 The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total S ...
随机推荐
- 【HDU4473】Exam(数学题)
点此看题面 大致题意: 设\(f(x)=\sum[(a*b)|x]\),求\(\sum_{x=1}^nf(x)\). 转化题意 将题意进行转换,我们就可以发现,我们要求的\(ans\)就是满足\(x* ...
- UIButton 加载网络图片
以后就可以 用这个分类 UIButton轻松加载网络图片了, UIButton+WebCache.h #import <UIKit/UIKit.h> @interface UIButt ...
- linux怎么进home目录下
可以使用cd命令,cd命令的功能是切换到指定的目录: 命令格式:cd [目录名] 有几个符号作为目录名有特殊的含义: “/”代表根目录.“..”代表上一级目录.“~”代表HOME目录.“-”代表前一目 ...
- 漫谈 Clustering (番外篇): Vector Quantization
在接下去说其他的聚类算法之前,让我们先插进来说一说一个有点跑题的东西:Vector Quantization.这项技术广泛地用在信号处理以及数据压缩等领域.事实上,在 JPEG 和 MPEG-4 等多 ...
- zabbix监控系统时间的问题
分类: 监控 2013-03-19 21:40:11 发现zabbix监控系统时间的一个问题!zabbix监控系统时间用的key是system.localtime,返回当前的系统时间,而配置tig ...
- 1.在Cisco Packet Tracer里交换机的初始配置
基本拓扑图: 点进交换机,会先进入交换机的用户模式,这个模式下交换机的名称后方会以‘>’显示 输入enable会进入交换机的特权模式,同样在交换机的名称后方以‘#’显示 在特权模式的环境下输入c ...
- Python编写一个程序求2的次方
#!/usr/bin/env python3 #-*- coding:utf-8 -*- #":"冒号后面为对参数注释,"->"为对整个函数注释 def ...
- 学习Pytbon第三天,用户输入
_username ='dream' #定义用户名 _password ='dream123'#定义用户密码username = input("username:")#请输入用户名 ...
- 格雷码Gray Code详解
格雷码简介 在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code),另外由于最大数与最小数之间也仅一位数不同,即“首尾相连”,因此又称循环码或反射码.格 ...
- Paper Folding UVA - 177 模拟+思路+找规律
题目:题目链接 思路:1到4是很容易写出来的,我们先考虑这四种情况的绘制顺序 1:ru 2:rulu 3:rululdlu 4:rululdluldrdldlu 不难发现,相较于前一行,每一次增加一倍 ...