UVALive 7455 Linear Ecosystem (高斯消元)
Linear Ecosystem
题目链接:
http://acm.hust.edu.cn/vjudge/contest/127401#problem/B
Description
http://7xjob4.com1.z0.glb.clouddn.com/99b0fe905e5bd89a24c882832c93cc09
Input
The first line of the input file contains an integer, n, which is the number of ecosystems. For each case,
the first line contains the integer k which is the number of comorgs. Followed by k lines, where the i-th
line contains, αi,1, αi,2, . . . , αi,k, the coefficients of the transition equation for ci.
Output
For each test case, output ‘1’ if the ecosystem is potentially stable, otherwise output ‘0’. Output only
5 answers per line. There should be a blank space between any two output answers.
Sample Input
6
2
4 -2
-6 5
2
2 2
0 0
3
0.3 0.2 0.5
0.4 0.4 0.2
0 0.8 0.2
3
0.3 0.2 0.5
0 0 0
0 0.8 0.2
2
4 2.0
-6 5
2
1 0
0 1
Sample Output
1 0 1 0 0
1
##题意:
对一个k元向量, 每次左乘一个k*k的矩阵得到新的向量.
问经过一定次数的左乘后,能否使得该向量不再变化. (同时要求此时向量非零)
##题解:
设初始向量为A,矩阵为P.
由于每次矩阵P都是左乘A, 那么可以把若干个P合并. 则题目的条件是:
化简为:  由于要求   所以 P-1 必须不可逆.
可以直接用高斯消元求P-1的秩,判断是否可逆(满秩即可逆).
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-6
#define maxn 50
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;
double a[maxn][maxn],x[maxn];
int equ,var;
int Gauss()
{
int i,j,k,col,max_r;
for(k=0,col=0;k<equ&&col<var;k++,col++){
max_r = k;
for(i=k+1;i<equ;i++)
if(fabs(a[i][col])>fabs(a[max_r][col]))
max_r = i;
if(fabs(a[max_r][col])<eps) return 0; //无解,有自由变元
if(k != max_r){
for(j=col;j<var;j++)
swap(a[k][j],a[max_r][j]);
swap(x[k],x[max_r]);
}
x[k]/=a[k][col];
for(j=col+1;j<var;j++)a[k][j]/=a[k][col];
a[k][col] = 1;
for(i=0;i<equ;i++)
if(i!=k){
x[i] -= x[k]a[i][k];
for(j=col+1;j<var;j++)a[i][j]-=a[k][j]a[i][col];
a[i][col]=0;
}
}
return 1;
}
vector ans;
int main(int argc, char const *argv[])
{
//IN;
int t; cin >> t;
while(t--)
{
    memset(a, 0, sizeof(a));
    cin >> equ; var = equ;
    for(int i=0; i<equ; i++) {
        for(int j=0; j<var; j++) {
            cin >> a[i][j];
        }
        a[i][i] -= 1.0;   /* P - 1 */
    }
    if(Gauss()) ans.push_back(0);
    else ans.push_back(1);
}
for(int i=0; i<ans.size(); i++) {
    printf("%d%c", ans[i], (i%5==4||i==ans.size()-1)?'\n':' ');
}
return 0;
}
UVALive 7455 Linear Ecosystem (高斯消元)的更多相关文章
- UVaLive 7455 Linear Ecosystem (Gaussi 消元)
		题意:对一个k元向量, 每次左乘一个k*k的矩阵得到新的向量.问经过一定次数的左乘后,能否使得该向量不再变化. (同时要求此时向量非零). 析:设初始向量为A,矩阵为P.由于每次矩阵P都是左乘A, 那 ... 
- UVALive 6449 IQ Test --高斯消元?
		题意:给你一串数字,问这串数字符合f[n] = a*f[n-1],f[n] = a*f[n-1]+b*f[n-2],f[n] = a*f[n-1]+b*f[n-2]+c*f[n-3]这几个方程中的哪个 ... 
- First Knight UVALive - 4297(优化高斯消元解概率dp)
		题意: 一个矩形区域被分成 m*n 个单元编号为 (1, 1)至 (m, n),左上为 (1, 1),右下为(m, n).给出P(k)i,j,其中 1 ≤ i ≤ m,1 ≤ j ≤ n,1 ≤ k ... 
- UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)
		题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ... 
- UVALive - 6185 Find the Outlier暴力填表+高斯消元+卡eps
		https://cn.vjudge.net/problem/UVALive-6185 我真的是服了orz eps 1e5,1e6过不了 开1e2 1e1都能过 题意:给你一个d阶多项式f的f(0),f ... 
- UVALive - 3490 Generator (AC自动机+高斯消元dp)
		初始有一个空串s,从前n个大写字母中不断随机取出一个字母添加到s的结尾,出现模式串t时停止,求停止时s的长度期望. 这道题解法不唯一,比较无脑的方法是对模式串t建一个单串AC自动机,设u为自动机上的一 ... 
- 【BZOJ-3143】游走     高斯消元 + 概率期望
		3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2264 Solved: 987[Submit][Status] ... 
- 【BZOJ-3270】博物馆      高斯消元 + 概率期望
		3270: 博物馆 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 292 Solved: 158[Submit][Status][Discuss] ... 
- *POJ 1222 高斯消元
		EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9612 Accepted: 62 ... 
随机推荐
- sgu 495. Kids and Prizes (简单概率dp 正推求期望)
			题目链接 495. Kids and Prizes Time limit per test: 0.25 second(s)Memory limit: 262144 kilobytes input: s ... 
- HTML表格标签
			table标签的用途: 在表格中放图片,或用于布局(已经淘汰掉了),存放数据 table制作过程: 1.先分析表格有多少行 2.分析有多少列 3.做好表格的基本之后再添加表格需要的一些属性 table ... 
- ORG 07C00H的意思
			简单说来,该指令用来修正该指令以后出现的(变量/标志的)内存地址,也就是说如果有ORG 0x12345h,那么在该指令以后的变量的地址将被修正为0x12345+old_addr.对于DOS中的COM文 ... 
- [ionic开源项目教程] - 第9讲 新闻详情页的实现
			目录 [ionic开源项目教程] 第1讲 前言,技术储备,环境搭建,常用命令 [ionic开源项目教程] 第2讲 新建项目,架构页面,配置app.js和controllers.js [ionic开源项 ... 
- XML中对特殊字符的处置
			str = str.replaceAll("‘", "‘"); str = str.replaceAll("’", "‘" ... 
- UVa 839 (递归方式读取二叉树) Not so Mobile
			题意: 递归的方式输入一个树状天平(一个天平下面挂的不一定是砝码还可能是一个子天平),判断这个天平是否能满足平衡条件,即W1 * D1 == W2 * D2. 递归的方式处理输入数据感觉很巧妙,我虽然 ... 
- 【英语】Bingo口语笔记(76) - 不知如何应答的场景对话
- 剑指offer—第二章算法之二分查找(旋转数组的最小值)
			旋转数组的最小数字 题目:把一个数组最开始的若干元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如:数组{3,4,5,1,2}为{1,2,3,4, ... 
- SmartWeatherAPI_Lite_WebAPI C# 获取key加密
			中国气象局面向网络媒体.手机厂商.第三方气象服务机构等用户,通过 web 方式提供数据气象服务的官方载体. 在一周前已经申请到appid,但是苦于没有C#版的key 的算法,一直验证不通过,经过几天查 ... 
- 安装sass时,gem在国内不能安装的解决
			最近在安装SASS的时候,用到gem命令,但是运行出行如下错误!(先声明,安装sass前,要保证自己电脑安装了ruby:ruby -v可以测试下有没有装) 原因是ruby 的gem被和谐了,现在淘宝的 ... 
