关于 矩阵在ACM中的应用
关于矩阵在ACM中的应用
1、矩阵运算法则
重点说说矩阵与矩阵的乘法,不说加减法。
支持:
- 结合律 (AB)C = A(BC)
- 分配律 A(B+C) = AB + AB
- $\left( \lambda A\right) B=\lambda \left( AB\right) =A\left( \lambda B\right) $
2、矩阵乘法的程序实现:
struct matrix
{
double a[][];
} ans, base; matrix multiply(matrix x, matrix y)
{
matrix tmp;
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
{
tmp.a[i][j] = ;
for (int k = ; k < n; k++) tmp.a[i][j] += x.a[i][k] * y.a[k][j];
}
return tmp;
}
3、矩阵的幂运算可以进行快速幂
因为矩阵的乘法支持结合律,所以B*A*A*A = B(A*A*A) = B*$A^{3}$
程序实现:
void fast_mod(int k)
{
while (k)
{
if (k & ) ans = multiply(ans, base);
base = multiply(base, base);
k >>= ;
}
}
4、将行列式的变换动作构造成一个矩阵(如 平移 旋转 翻转 等操作)
5、将一个一次递推式 构造出 矩阵。
例如:fibonacci数列的递推关系 f(n) = f(n-1) + f(n-2)
我们可以将矩阵构造为:$\left( \begin{matrix} 0& 1\\ 1& 1\end{matrix} \right) \left( \begin{matrix} a\\ b\end{matrix} \right) =\left( \begin{matrix} b\\ a+b\end{matrix} \right) $
比如今天做的一个题目 zoj 2853 Evolution
它就是先进行构造“进化”这个动作的矩阵。然后有多少次进化就等于"初始物种状态矩阵" 乘 "进化矩阵”n
题目 见文末
附上我的代码:
#include<cstdio>
#include<cstring>
int n;
struct matrix
{
double a[][];
} ans, base; matrix multiply(matrix x, matrix y)
{
matrix tmp;
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
{
tmp.a[i][j] = ;
for (int k = ; k < n; k++) tmp.a[i][j] += x.a[i][k] * y.a[k][j];
}
return tmp;
}
void fast_mod(int k)
{
while (k)
{
if (k & ) ans = multiply(ans, base);
base = multiply(base, base);
k >>= ;
}
}
int main()
{
int m, t, xi, yi;
double s, zi, fin;
int num[];
while (~scanf("%d%d", &n, &m) && !(n == && m == ))
{
for (int i = ; i < n; i++) scanf("%d", &num[i]);
scanf("%d", &t);
memset(base.a, , sizeof(base.a));
for (int i = ; i < n; i++) base.a[i][i] = ; //初始为进化成自己
while (t--)
{
scanf("%d%d%lf", &xi, &yi, &zi);
base.a[xi][yi] += zi;
base.a[xi][xi] -= zi;
}
memset(ans.a, , sizeof(ans.a));
for (int i = ; i < n; i++) ans.a[i][i] = ;
fast_mod(m);
fin = ;
for (int i = ; i < n; i++)
fin += num[i] * ans.a[i][n-];
printf("%.0f\n", fin);
}
return ;
}
6、矩阵在图的邻接矩阵中的应用
暂时还不会, = =、 以后补上。
Evolution
Time Limit: 5 Seconds Memory Limit: 32768 KB
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 0Sample Output
120
0
关于 矩阵在ACM中的应用的更多相关文章
- 矩阵快速幂在ACM中的应用
矩阵快速幂在ACM中的应用 16计算机2黄睿博 首发于个人博客http://www.cnblogs.com/BobHuang/ 作为一个acmer,矩阵在这个算法竞赛中还是蛮多的,一个优秀的算法可以影 ...
- ACM 中 矩阵数据的预处理 && 求子矩阵元素和问题
我们考虑一个$N\times M$的矩阵数据,若要对矩阵中的部分数据进行读取,比如求某个$a\times b$的子矩阵的元素和,通常我们可以想到$O(ab)$的遍历那个子矩阵,对它的各 ...
- [ACM训练] ACM中巧用文件的输入输出来改写acm程序的输入输出 + ACM中八大输入输出格式
ACM中巧用文件的输入输出来改写acm程序的输入输出 经常有见大神们使用文件来代替ACM程序中的IO,尤其是当程序IO比较复杂时,可以使自己能够更专注于代码的测试,而不是怎样敲输入. C/C++代码中 ...
- C++ STL泛型编程——在ACM中的运用
学习过C++的朋友们应该对STL和泛型编程这两个名词不会陌生.两者之间的关系不言而喻,泛型编程的思想促使了STL的诞生,而STL则很好地体现了泛型编程这种思想.这次想简单说一下STL在ACM中的一些应 ...
- Java在ACM中的应用
Java在ACM中的应用 —. 在java中的基本头文件(java中叫包) import java.io.*; import java.util.*; //输入Scanner import java. ...
- IO/ACM中来自浮点数的陷阱(收集向)
OI/ACM中经常要用到小数来解决问题(概率.计算几何等),但是小数在计算机中的存储方式是浮点数而不是我们在作数学运算中的数,有精度的限制. 以下以GUN C++为准,其他语言(或编译器)也差不了多少 ...
- ACM中的浮点数精度处理
在ACM中,精度问题非常常见.其中计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模板一般就不成问题了.精度问题则不好说,有时候一个精度问题就可能成为一道题的瓶颈,让你debu ...
- ACM 中常用的算法有哪些? 2014-08-21 21:15 40人阅读 评论(0) 收藏
ACM 中常用的算法有哪些?作者: 张俊Michael 网络上流传的答案有很多,估计提问者也曾经去网上搜过.所以根据自己微薄的经验提点看法. 我ACM初期是训练编码能力,以水题为主(就是没有任何算法, ...
- ACM中Java的应用
先说一下Java对于ACM的一些优点吧: (1) 对于熟悉C/C++的程序员来说Java 并不难学,两周时间基本可以搞定一般的编程,再用些时间了解一下Java库就行了.Java的语法和C++非常类似, ...
随机推荐
- Adobe 软件防止联网激活更改Hosts文件
来自为知笔记(Wiz) 附件列表
- dispatch_set_target_queue 说明
参照:http://blog.csdn.net/growinggiant/article/details/41077221 http://codingobjc.com/blog/2013/05/07/ ...
- Linux Epoll相关知识
其实在Linux下设计并发网络程序,向来不缺少方法,比如典型的Apache模型(Process Per Connection,简称PPC),TPC(Thread PerConnection)模型,以及 ...
- mysql connector 和 sqlserver ado.net 的区别
1,虽然同样是实现了IDataReader接口,但是 对于 MySql.Data.MySqlClient.MySqlDataReader 和 System.Data.SqlClient.SqlDat ...
- Disconf
通常我们会做如下配置:(disconf 2.6.21) <!-- 一次扫描 --> <bean id="disconfMgrBean" class="c ...
- vrrp
https://zhidao.baidu.com/question/1498182981731748379.html
- UEFI+GPT引导实践篇(二):UEFI引导安装64位Win7/Win8
下文是在联想Y480笔记本上以UEFI方式启动安装Windows8的全过程,安装Windows7过程基本相同.注意,如果你的电脑硬盘是MBR分区结构,安装过程中将要删除硬盘上所有数据,请安装前备份硬盘 ...
- vs2010连接postgresql数据库
Windows环境C/C++访问PostgreSQL主要有两种方式:利用Qt封装的数据库访问组件.利用PostgreSQL的API函数.使用Qt平台访问PostgreSQL的局限性很大,一旦脱离了访问 ...
- JavaScript笔记:变量及其作用域
一.变量的定义及声明 在javascript中变量仅仅是用来保存值的一个占位符而已,定义变量时要使用关键字var后跟一个变量名,如下所示: var message; //定义一个变量message,像 ...
- 【SSM 8】spring集成Mybatis通用Mapper
上篇博客中介绍了关于Mybatis底层封装的思路问题,那么这篇博客,就介绍一下怎么引入通用的mapper插件. 备注:本项目通过maven管理 关键版本说明: spring:4.1.3.RELEASE ...