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. 思路: 简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...
随机推荐
- centos6.5安装pysider遇见的坑
一.服务器自带pyhon2.7.9 安装pip, # yum -y install python-devel python-setuptool wget http://pypi.python.org/ ...
- c++ 简单的词法分析
scanner.h #include<iostream> #include<fstream> #include<string> using namespace st ...
- poj 2007 Scrambled Polygon 极角排序
/** 极角排序输出,,, 主要atan2(y,x) 容易失精度,,用 bool cmp(point a,point b){ 5 if(cross(a-tmp,b-tmp)>0) 6 retur ...
- 阿里云ECS每天一件事D1:配置SSH
近期因为项目需求,采购了两台阿里云ECS,选择的系统为CentOS 6.3 X64 安全加固版,额外买了160G的硬盘,应该够应付此项目的需求了. ECS默认已经配置好了sshd服务,可以使用root ...
- 模拟美萍加密狗--Rockey2虚拟狗(一)
目录(?)[+] 最近受朋友之托做了一个美萍智能电源控制的插件.美萍茶楼从2010版开始支持智能电源控制设备,就是开单.结账时自动开关相应房间的电器,不过官方的设备是有线的.朋友的店已经开了一段时 ...
- 基于Visual C++2013拆解世界五百强面试题--题16-进制分析
清写出下列代码的输出内容 #include <stdio.h> int main() { int a = -1, b = -12, c = -123, d = -1234; printf( ...
- 制作cdlinux u盘启动
U盘一个 CDlinux的iso镜像文件 UltraISO grub4dos grubinst 方法/步骤1 1 [第一步]:用UltraISO把CDlinux的镜像刻录进U盘. 打开UltraISO ...
- 解决https无法缓存的问题
火狐弃用http,转而大力推广https的动作一石激起千层浪,非常多没有安装安全证书的站点使用新版火狐浏览器已经打不开了. 之前我们站点仅仅有涉及须要加密的部分连接为https协议.眼下看来不得不将整 ...
- nvarchar and nchar
Same: 1.store unicode charactor. 2. A charactor is stroed with 2 bytes. Different. 1. nchar 会自动填充数据 ...
- C# 日期格式转换 string类型 20150329 转换为 2015/03/29
DateTime.ParseExact("20150329", "yyyyMMdd", System.Globalization.CultureInfo.Cur ...