数学 --- 高斯消元 POJ 1830
开关问题
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的更多相关文章
- [高斯消元] POJ 2345 Central heating
Central heating Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 614 Accepted: 286 Des ...
- [CSP-S模拟测试]:炼金术士的疑惑(模拟+数学+高斯消元)
题目传送门(内部题70) 输入格式 第一行一个正整数$n$,表示炼金术士已知的热化学方程式数量.接下来$n$行,每行一个炼金术士已知的热化学方程式.最后一行一个炼金术士想要求解的热化学方程式,末尾记为 ...
- 【POJ 1830】 开关问题 (高斯消元)
开关问题 Description 有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为 ...
- POJ 1830 开关问题(高斯消元)题解
思路:乍一看好像和线性代数没什么关系.我们用一个数组B表示第i个位置的灯变了没有,然后假设我用u[i] = 1表示动开关i,mp[i][j] = 1表示动了i之后j也会跟着动,那么第i个开关的最终状态 ...
- 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】1830 开关问题(高斯消元)
http://poj.org/problem?id=1830 高斯消元无解的条件:当存在非法的左式=0而右式不等于0的情况,即为非法.这个可以在消元后,对没有使用过的方程验证是否右式不等于0(此时因为 ...
- POJ 1830 开关问题 【01矩阵 高斯消元】
任意门:http://poj.org/problem?id=1830 开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...
- POJ 1830 开关问题(高斯消元求解的情况)
开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8714 Accepted: 3424 Description ...
- POJ 1830 开关问题 高斯消元,自由变量个数
http://poj.org/problem?id=1830 如果开关s1操作一次,则会有s1(记住自己也会变).和s1连接的开关都会做一次操作. 那么设矩阵a[i][j]表示按下了开关j,开关i会被 ...
随机推荐
- Android 解压boot.img
其实解压.打包boot.img没什么难度一看就会咯!! 1.先下附件:工具. 点击打开链接 6.0 KB, 下载次数: 60) 解压到bin文件夹里,方便以后使用. 2.解压boot ...
- 突然顿悟的Javascript中的this
一直对Javascript中的this都有一种似是而非的感觉,今天突然感觉豁然开朗,特此记录一下. 咱们先看个栗子: <!DOCTYPE html> <html> <he ...
- db2 中文表名和字段
建库语句 create db test on D: using codeset GBK territory CN 或者 territory cn codeset 和 territory 都是需要指定 ...
- embarcadero radstudio xe5 正式版 下载地址
http://altd.embarcadero.com/download/radstudio/xe5/delphicbuilder_xe5_win.iso
- 计算空间直线与平面的交点 (C#)
public class NGlbVec3d {// 三维点 public double x, y, z; public NGlbVec3d() { ...
- FFRPC应用之Client/Server
摘要: Ffrpc 进行了重构,精简了代码,代码更加清晰简洁,几乎完美的达到了我的预想.接下来将写几遍文章来介绍ffrpc可以做什么.简单总结ffrpc的特性是: Ffrpc是c++ 网络通信库 全异 ...
- JS - Cookie: getCookie, setCookie
JS function for Cookie 如果cookie未设置,判断时与空字符串‘’比较: function setCookie(cname, cvalue, exdays) { var d = ...
- Selenium Grid 学习笔记
Selenium Grid 学习笔记http://www.docin.com/p-765680298.html
- PowerPoint 打开文档发现.pptx中胡内容有问题
一.问题的提出 有一个文件,在window 7操作系统中通过邮箱地址保存到本地,结果打开的时候出现[PowerPoint 打开文档发现 文件.pptx中胡内容有问题] 然后提示[如果您信任此演示文稿的 ...
- Win7下安装git
1.下载并安装git for windows版本:msysgit http://msysgit.github.com/ 成功安装后,即可使用了,右键一个目录后可以看到菜单选项: 选择Git Gui ...