开关问题

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. IE8以下版本iframe出现滚动条和内容空白问题

    在网页中使用iframe引用了第三方的页面 $("#tianqi").html('<iframesrc="http://i.tianqi.com/index.php ...

  2. Codeforces Round #382 (Div. 2) A. Ostap and Grasshopper bfs

    A. Ostap and Grasshopper 题面 On the way to Rio de Janeiro Ostap kills time playing with a grasshopper ...

  3. [转]说说C#的async和await

    C# 5.0中引入了async 和 await.这两个关键字可以让你更方便的写出异步代码. 看个例子: public class MyClass { public MyClass() { Displa ...

  4. 文件系统管理 之 Linux 查看磁盘分区、文件系统、使用情况的命令和相关工具介绍

    一.df 命令:df 是来自于coreutils 软件包,系统安装时,就自带的:我们通过这个命令可以查看磁盘的使用情况以及文件系统被挂载的位置: 举例: [root@localhost beinan] ...

  5. 百度Web富文本编辑器ueditor在ASP.NET MVC3项目中的使用说明

    ====================================================================== [百度Web富文本编辑器ueditor在ASP.NET M ...

  6. 深入剖析 redis RDB 持久化策略

    简介 redis 持久化 RDB.AOF redis 提供两种持久化方式:RDB 和 AOF.redis 允许两者结合,也允许两者同时关闭. RDB 可以定时备份内存中的数据集.服务器启动的时候,可以 ...

  7. D3 & Data Visualization in Ext JS

    通过适配器可以在ExtJs中轻松的集成D3的展示能力 http://video.sencha.com/watch/zvUjnFJ91xVvuwdTh2zjqP?mkt_tok=eyJpIjoiWm1a ...

  8. ubuntu-15.04-server-i386.iso 安装 Oracle 11gR2 数据库

    特点: 需要重新安装老版本的 libaio1_0.3.109-2ubuntu?_i386.deb.默认的libaio库有问题,和其默认libaio的编译方式有关! 默认的gcc 4.9 需要使用 -W ...

  9. Versions 出现 SVN Working Copy xxx locked

    Versions处于选中状态,Finder的导航栏就是Versions的导航栏,如下图,Action - Cleanup...,就可以解锁了

  10. UFT\QTP 12 新特性

    UFT\QTP 12 新特性 http://blog.csdn.net/testing_is_believing/article/details/22310297