Evolution(矩阵快速幂)zoj2853
Time Limit: 5 Seconds Memory Limit: 32768 KB
Description
Evolution is a long, long process with extreme complexity and involves many species. Dr. C. P. Lottery is currently investigating a simplified model of evolution: consider that we have N (2 <= N <= 200) species in the whole process of evolution, indexed from 0 to N-1, and there is exactly one ultimate species indexed as N-1. In addition, Dr. Lottery divides the whole evolution process into M (2 <= M<= 100000) sub-processes. Dr. Lottery also gives an 'evolution rate' P(i, j) for 2 species i and j, where i and j are not the same, which means that in an evolution sub-process, P(i, j) of the population of species i will transform to species j, while the other part remains unchanged.
Given the initial population of all species, write a program for Dr. Lottery to determine the population of the ultimate species after the evolution process. Round your final result to an integer.
Input
The input contains multiple test cases!
Each test case begins with a line with two integers N, M. After that, there will be a line with N numbers, indicating the initial population of each species, then there will be a number T and T lines follow, each line is in format "i j P(i,j)" (0 <= P(i,j) <=1).
A line with N = 0 and M = 0 signals the end of the input, which should not be proceed.
Output
For each test case, output the rounded-to-integer population of the ultimate species after the whole evolution process. Write your answer to each test case in a single line.
Notes
- There will be no 'circle's in the evolution process.
- E.g. for each species i, there will never be a path i, s1, s2, ..., st, i, such that P(i,s1) <> 0, P(sx,sx+1) <> 0 and P(st, i) <> 0.
- The initial population of each species will not exceed 100,000,000.
- There're totally about 5 large (N >= 150) test cases in the input.
Example
Let's assume that P(0, 1) = P(1, 2) = 1, and at the beginning of a sub-process, the populations of 0, 1, 2 are 40, 20 and 10 respectively, then at the end of the sub-process, the populations are 0, 40 and 30 respectively.
Sample Input
2 3
100 20
1
0 1 1.0
4 100
1000 2000 3000 0
3
0 1 0.19
1 2 0.05
0 2 0.67
0 0
Sample Output
120
0
题意:就是说物种进化,有N种物种,编号是0-----N-1,M次进化后,问你编号为N-1的物种有多少数量;
其中要注意的就是i物种进化到j物种的概率是p;(那么剩下的不要忘了);所以单位矩阵初始化对角线的值为1,
然后根据题目进化的概率进行加减;比如P(i, j) = 0.3,则mat[i][j] += 0.3,mat[i][i] -= 0.3;
把题目转化为数学模型,分析后就是 有一个初始序列, 有一个进化率矩阵,求的是初始序列 与进化率矩阵进
行 m 次运算后, 初始序列最后一位的答案,那么显然,可以对进化率矩阵进行快速幂计算
这里,有一点是要注意的 由于矩阵的大小是200*200的但是一般的笔记本内存不够,至少我的笔记本不
行。。。。。以至于本人纠结了N久,,,谁让题目是服务器改的呢。。。。!<Memory Limit: 32768 KB>
内存就是大!
不说了,其他的都是比较基础的。
转载请注明出处:http://www.cnblogs.com/yuyixingkong/p/4326818.html
题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1853
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std; int N,M;struct matrix
{
double mat[210][210];
}; double num[]; matrix multiply(matrix x,matrix y)//乘
{
matrix temp;
memset(temp.mat,,sizeof(temp.mat));
for(int i=; i<N; i++)
{
for(int j=; j<N; j++)
{
if(x.mat[i][j]==) continue;
for(int k=; k<N; k++)
{
if(y.mat[j][k]==) continue;
temp.mat[i][k]+=x.mat[i][j]*y.mat[j][k];
}
}
}
return temp;
} matrix quicklymod(matrix a,int n)
{
matrix res;//单位阵
memset(res.mat,,sizeof(res.mat));
for(int i=;i<N;i++) res.mat[i][i]=;
while(n)
{
if(n&)
res=multiply(res,a);
n>>=;
a=multiply(a,a);
}
/*for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
printf("%5.2lf",a.mat[i][j]);
printf("\n");
}
printf("\n");*/
return res;
}
int main()
{
int i,j,T;
double p,ans;
matrix ant;
while(scanf("%d%d",&N,&M),(N||M))
{
for(i=;i<N;i++)
{
scanf("%lf",&num[i]);
for(j=;j<N;j++)
{
if(i==j)
ant.mat[i][j]=;
else
ant.mat[i][j]=;
}
} scanf("%d",&T);
while(T--)
{
scanf("%d%d%lf",&i,&j,&p);
ant.mat[i][j]+=p;
ant.mat[i][i]-=p;
} ant=quicklymod(ant,M); ans=;
for(i=;i<N;i++)
{
ans+=num[i]*ant.mat[i][N-];
}
printf("%.0lf\n",ans);
}
return ;
} /*
2 3
100 20
1
0 1 1.0
4 100
1000 2000 3000 0
3
0 1 0.19
1 2 0.05
0 2 0.67
0 0
*/
Evolution(矩阵快速幂)zoj2853的更多相关文章
- ZOJ - 2853 Evolution 线性变换变成矩阵快速幂
题意:给你N个数,1~N分别为num[i], 以及T个 (i,j,P) 对于每组(i,j,P),让你将 num[i] 减去 P*num[i] 再把 P*num[i] 加到 num[j] 上.T个 ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...
- 51nod 1126 矩阵快速幂 水
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
- hdu2604(递推,矩阵快速幂)
题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...
- 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b * A B = a*A+b*C a*c+b*D c d ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
随机推荐
- dubbo-admin 出现警告(不影响使用)
<dubbo:application name="pyg-sellergoods-s" />. <dubbo:application name="pyg ...
- ThinkPHP5代码执行的简单分析
漏洞影响版本: ThinkPHP 5.0.5-5.0.22 ThinkPHP 5.1.0-5.1.30 漏洞复现: 一.mac的debug环境搭建. 一键化环境搭建工具: mamp pro ,调试工具 ...
- postgresql-脏页和缓存失效
脏页和缓存失效 https://www.cnblogs.com/flying-tiger/p/7885478.html Dirty pages and cache invalidation 我们一直在 ...
- mysql之视图,存储过程,触发器,事务
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的临时 ...
- Python中对矩阵的洗牌操作
[code] import numpy as np # 创建随机交换的索引 permutation = list(np.random.permutation(3)) # 创建矩阵X,Y X = np. ...
- 如何用ajax下载文件
引子 在HTML5没来之前,浏览器想要下载文件,可能有这么几种方式: 借助a标签,<a href="学习资料.xlsx"></a> window.locat ...
- kmp模式串匹配
大二的时候百度看不懂,现在大三下学期了百度也看不懂.(实在是不能理解这个next数组究竟是怎么样工作的,为什么会得出那样的结果.)如果有好心人知道的话希望可以联系我告诉我(邮箱:4609019410@ ...
- Sublime Text3 一些实用设置
字体大小 "font_size": 14 高亮编辑中的那一行 "highlight_line": true 当你把脑袋扭过到显示器以外的地方后再回头看编辑器,光 ...
- Andrew Ng机器学习课程笔记(六)之 机器学习系统的设计
Andrew Ng机器学习课程笔记(六)之 机器学习系统的设计 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7392408.h ...
- AutowireCapableBeanFactory源码详解
一.概述 对于想要拥有自动装配能力,并且想要把这种能力暴露给外部应用BeanFactory类需要实现此接口. 正常情况下不要使用此接口,应该更倾向于使用BeanFactory或者ListableBea ...