ZOJ 2853 Evolution 【简单矩阵快速幂】
这道题目第二次看的时候才彻底理解了是什么意思
把题目转化为数学模型分析后就是 有一个初始序列, 有一个进化率矩阵
求的是初始序列 与进化率矩阵进行 m 次运算后, 初始序列最后一位的答案
那么显然,可以对进化率矩阵进行快速幂计算
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.
这个栗子看懂了这题就会懂了。
Source Code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ; int n, m; struct Mat{
double mat[N][N];
}; Mat operator * (Mat a, Mat b){
Mat c;
memset(c.mat, , sizeof(c.mat));
for(int k = ; k < n; ++k){
for(int i = ; i < n; ++i){
if(a.mat[i][k] <= ) continue; //
for(int j = ; j < n; ++j){
if(b.mat[k][j] <= ) continue; //
c.mat[i][j] += a.mat[i][k] * b.mat[k][j];
}
}
}
return c;
} Mat operator ^ (Mat a, int k){
Mat c;
for(int i = ; i < n; ++i){
for(int j = ; j < n; ++j){
c.mat[i][j] = (i == j); //init
}
}
for(; k; k >>= ){
if(k & ) c = c * a; //key
a = a * a;
}
return c;
} int main(){
int i, j, t, k, u, v, numCase = ;
while(EOF != scanf("%d%d",&n,&m)){
if( == n && == m) break;
double val;
Mat a, b;
memset(a.mat, , sizeof(a.mat));
memset(b.mat, , sizeof(b.mat));
for(i = ; i < n; ++i) b.mat[i][i] = ;
for(i = ; i < n; ++i) scanf("%lf", &a.mat[i][i]);
scanf("%d",&t);
while(t--){
scanf("%d%d%lf",&u,&v,&val);
b.mat[u][u] -= val; //
b.mat[u][v] = val; //
}
b = b ^ m;
double cur = ;
for(i = ; i < n; ++i){
cur += b.mat[i][n - ] * a.mat[i][i];
}
/*
for(i = 0; i < n; ++i){
for(j = 0; j < n; ++j){
cout << c.mat[i][j] << ' ';
}
cout << endl;
}
*/
printf("%.0lf\n",cur);
}
return ;
} /*
3 1
40 20 10
2
0 1 1.0
1 2 1.0
*/
ZOJ 2853 Evolution 【简单矩阵快速幂】的更多相关文章
- HDU 1575 Tr A( 简单矩阵快速幂 )
链接:传送门 思路:简单矩阵快速幂,算完 A^k 后再求一遍主对角线上的和取个模 /********************************************************** ...
- UVA10870—Recurrences(简单矩阵快速幂)
题目链接:https://vjudge.net/problem/UVA-10870 题目意思: 给出a1,a2,a3,a4,a5………………ad,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第 ...
- HDU 4990 Reading comprehension 简单矩阵快速幂
Problem Description Read the program below carefully then answer the question.#pragma comment(linker ...
- Evolution(矩阵快速幂)zoj2853
Evolution Time Limit: 5 Seconds Memory Limit: 32768 KB Description Evolution is a long, long pr ...
- 简单矩阵快速幂(HDU Tr A 1575)
题目中所给的方阵就是一个矩阵,而就是只要将题目所给矩阵不断进行相乘即可,本题中我采用的是直接重载运算符*,使矩阵每一个都进行运算,可以简化为只对对角线上的元素进行运算.最后所得结果就只需将最终的矩阵上 ...
- zoj2893 Evolution(矩阵快速幂)
题意:就是说物种进化,有N种物种,编号是0——N-1,M次进化后,问你编号为N-1的物种有多少数量:其中要注意的就是i物种进化到j物种的概率是p:(那么剩下的不要忘了):所以单位矩阵初始化对角线的值为 ...
- Codeforces - 185A 简单矩阵快速幂
题意:求第n个三角形内部的上三角形个数 对每个三角形分别维护上下三角形个数,记为\(dp[1][i],dp[2][i]\) 规律很明显是 \(dp[1][i+1]=3*dp[1][i]+dp[2][i ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- POJ3070矩阵快速幂简单题
题意: 求斐波那契后四位,n <= 1,000,000,000. 思路: 简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...
随机推荐
- 编写带参数decorator
无参的@log装饰器: def log(f): def fn(x): print 'call ' + f.__name__ + '()...' return f(x) return fn 发现对于被装 ...
- web开发注意的一些事
js命名不要包含"-",在chrome浏览器是测试发现,如果文件包含"-",即使指定js本地缓存了,还会向服务器发送请求. cookie path 区分大小写
- WebAppScaner
https://www.ohloh.net/p/simple-scan/ https://code.google.com/p/skipfish/ http://code.google.com/p/wa ...
- MCE遥控---用遥控器玩电脑
实现功能:利用Vista/Windows7的Media Center或者iMCE的支持,配上电脑遥控器,就可以在电视上用遥控器玩电脑,看高清.听音乐.看照片.录电视等.遥控器比鼠标操作起来更加自然,家 ...
- HDU 3350 #define is unsafe
题目大意:给定一个只含有MAX和+操作的式子,求加法运行了多少次,其中MAX使用宏定义. 题解:注意一个规律,对于MAX(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b ...
- 【转】Configuring VM Acceleration on Linux
Configuring VM Acceleration on Linux Linux-based systems support virtual machine acceleration throug ...
- 【转】 利用spring的profile切换不同的环境
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- C++对C语言的非面向对象特性扩充(1)
我将分3篇来介绍C++相对于C在非对象特性上的扩充,今天要讲的是C++在注释,输入输出,局部变量说明的扩充,以及const修饰符与C中的#define的比较. 1.C++注释除了包括原有C的块注释/* ...
- 补全aaz288 可能有问题的过程 P_COMPL_AAZ288
补全aaz288 可能有问题的过程: /* add by weiyongle 20160623 失地农民补足aaz288,针对早期导出的数据(只适用于江安县) 经测试:江安县 江安县个体劳动者 这个单 ...
- select into from 和 insert into select 的用法和区别(转)
转自:http://www.studyofnet.com/news/182.html select into from 和 insert into select都是用来复制表,两者的主要区别为: se ...