开关问题

Problem's Link: http://poj.org/problem?id=1830


Mean:

analyse:

增广矩阵:con[i][j]:若操作j,i的状态改变则con[i][j]=1,否则con[i][j]=0。

最后的增广矩阵应该是N*(N+1),最后一列:对比开光的始末状态,若相同则为0,若不同则为1;

最后的解共有三种:
1.无解,既出现了一行中前面N个数为0,第N+1的值非0;
2.没有第1种情况出现,存在X行数值全为0,则解的个数为2^X;
3,没有1,2 两种情况出现,唯一解,输出1。

Time complexity: O(n)

Source code: 

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-06-17-22.36
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std;
const int p=;
int con[p][p];
int N;
int beg[p],fin[p];
int function()
{
int i,j,k,t,row,col,temp,count=;
for(row=col=; row<=N&&col<=N; row++,col++)
{
if(con[row][col]==)
{
for(i=row+; i<=N; i++)
{
if(con[i][col]!=)
{
for(j=; j<=N+; j++)
{
swap(con[row][j],con[i][j]);
}
break;
}
}
}
if(con[row][col]==)
{
row--;
continue;
}
for(k=; k<=N; k++)
{
if(con[k][col]!=&&k!=row)
{
temp=-(con[k][col]/con[row][col]);
for(t=col; t<=N+; t++)
{
con[k][t]=(temp*con[row][t])+con[k][t];
}
}
}
}
for(k=row; k<N+; k++)
if(con[k][N+]!=) { return ; }
if(row==N+) { return ; }
else
{
return <<(N-row+);
}
}
int main()
{
int T,i,j,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
for(i=; i<=N; i++)
{
scanf("%d",&beg[i]);
}
for(i=; i<=N; i++)
{
scanf("%d",&fin[i]);
}
scanf("%d%d",&x,&y);
memset(con,,sizeof(con));
while(x!=&&y!=)
{
con[y][x]=;
scanf("%d%d",&x,&y);
}
for(i=; i<=N; i++)
{
con[i][i]=;
}
for(i=; i<=N; i++)
{
if(beg[i]==fin[i])
{
con[i][N+]=;
}
else
{
con[i][N+]=;
}
}
int pp = function();
if(pp)
{
printf("%d\n",pp);
}
else
{
printf("Oh,it's impossible~!!\n");
}
}
}

数学 --- 高斯消元 POJ 1830的更多相关文章

  1. [高斯消元] POJ 2345 Central heating

    Central heating Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 614   Accepted: 286 Des ...

  2. [CSP-S模拟测试]:炼金术士的疑惑(模拟+数学+高斯消元)

    题目传送门(内部题70) 输入格式 第一行一个正整数$n$,表示炼金术士已知的热化学方程式数量.接下来$n$行,每行一个炼金术士已知的热化学方程式.最后一行一个炼金术士想要求解的热化学方程式,末尾记为 ...

  3. 【POJ 1830】 开关问题 (高斯消元)

    开关问题   Description 有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为 ...

  4. POJ 1830 开关问题(高斯消元)题解

    思路:乍一看好像和线性代数没什么关系.我们用一个数组B表示第i个位置的灯变了没有,然后假设我用u[i] = 1表示动开关i,mp[i][j] = 1表示动了i之后j也会跟着动,那么第i个开关的最终状态 ...

  5. 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:// ...

  6. 【POJ】1830 开关问题(高斯消元)

    http://poj.org/problem?id=1830 高斯消元无解的条件:当存在非法的左式=0而右式不等于0的情况,即为非法.这个可以在消元后,对没有使用过的方程验证是否右式不等于0(此时因为 ...

  7. POJ 1830 开关问题 【01矩阵 高斯消元】

    任意门:http://poj.org/problem?id=1830 开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...

  8. POJ 1830 开关问题(高斯消元求解的情况)

    开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8714   Accepted: 3424 Description ...

  9. POJ 1830 开关问题 高斯消元,自由变量个数

    http://poj.org/problem?id=1830 如果开关s1操作一次,则会有s1(记住自己也会变).和s1连接的开关都会做一次操作. 那么设矩阵a[i][j]表示按下了开关j,开关i会被 ...

随机推荐

  1. 每日英语:The Secret About Online Ad Traffic: One-Third Is Bogus

    Billions of dollars are flowing into online advertising. But marketers also are confronting an uncom ...

  2. HDU 4759 Poker Shuffle

    Poker Shuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  3. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】【实验一】流水灯模块

    实验一:流水灯模块 对于发展商而言,动土仪式无疑是最重要的任务.为此,流水灯实验作为低级建模II的动土仪式再适合不过了.废话少说,我们还是开始实验吧. 图1.1 实验一建模图. 如图1.1 所示,实验 ...

  4. ExtJs 可查询的下拉框

    最近项目中有个需求,就是有四个模块需要加载一个主表的内容,比如说这个表叫项目表(比如项目表里有两个字段一个是项目ID--projCd,还有一个是项目名称--projNm).主表的内容的要放在一个下拉框 ...

  5. Spring3系列3 -- JavaConfig

    Spring3系列3-JavaConfig-1 从Spring3开始,加入了JavaConfig特性,JavaConfig特性允许开发者不必在Spring的xml配置文件中定义bean,可以在Java ...

  6. 读写文本(.txt)文件 .NET

    http://www.cnblogs.com/jx270/archive/2013/04/14/3020456.html (一) 读取文件 如果你要读取的文件内容不是很多,可以使用 File.Read ...

  7. Day One studying english

    I start study english lately,but the is no basis for english.Only i use baidu translation,google tra ...

  8. 微信、qq时间格式模板

    产品近来蛋疼,时间格式从做完到现在改了四遍了 ,最新的要求如下: * 2分钟内 无显示 * 2分钟-24小时 HH:mm * 昨天 昨天 HH:mm * 前天 前天 HH:mm * 今年 MM:DD ...

  9. pdf嵌入字体

    论文提交时,要求所有的字体都是嵌入的,为这个问题折腾了很久,发现了一个很好的答案,记一下: http://stackoverflow.com/questions/4231656/how-do-i-em ...

  10. SQL Server 问题 1 - SQL Server encountered error 0x80070422/0x8007042d

    今天执行SQL Server 2014的full-text search 查询操作:select * from table where contains(summary, 'smith') 报出如下错 ...