关于矩阵在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、矩阵乘法的程序实现:

  1. struct matrix
  2. {
  3. double a[][];
  4. } ans, base;
  5.  
  6. matrix multiply(matrix x, matrix y)
  7. {
  8. matrix tmp;
  9. for (int i = ; i < n; i++)
  10. for (int j = ; j < n; j++)
  11. {
  12. tmp.a[i][j] = ;
  13. for (int k = ; k < n; k++) tmp.a[i][j] += x.a[i][k] * y.a[k][j];
  14. }
  15. return tmp;
  16. }

3、矩阵的幂运算可以进行快速幂

  因为矩阵的乘法支持结合律,所以B*A*A*A = B(A*A*A) = B*$A^{3}$

程序实现:

  1. void fast_mod(int k)
  2. {
  3. while (k)
  4. {
  5. if (k & ) ans = multiply(ans, base);
  6. base = multiply(base, base);
  7. k >>= ;
  8. }
  9. }

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

  它就是先进行构造“进化”这个动作的矩阵。然后有多少次进化就等于"初始物种状态矩阵" 乘 "进化矩阵”

题目 见文末

附上我的代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. int n;
  4. struct matrix
  5. {
  6. double a[][];
  7. } ans, base;
  8.  
  9. matrix multiply(matrix x, matrix y)
  10. {
  11. matrix tmp;
  12. for (int i = ; i < n; i++)
  13. for (int j = ; j < n; j++)
  14. {
  15. tmp.a[i][j] = ;
  16. for (int k = ; k < n; k++) tmp.a[i][j] += x.a[i][k] * y.a[k][j];
  17. }
  18. return tmp;
  19. }
  20. void fast_mod(int k)
  21. {
  22. while (k)
  23. {
  24. if (k & ) ans = multiply(ans, base);
  25. base = multiply(base, base);
  26. k >>= ;
  27. }
  28. }
  29. int main()
  30. {
  31. int m, t, xi, yi;
  32. double s, zi, fin;
  33. int num[];
  34. while (~scanf("%d%d", &n, &m) && !(n == && m == ))
  35. {
  36. for (int i = ; i < n; i++) scanf("%d", &num[i]);
  37. scanf("%d", &t);
  38. memset(base.a, , sizeof(base.a));
  39. for (int i = ; i < n; i++) base.a[i][i] = ; //初始为进化成自己
  40. while (t--)
  41. {
  42. scanf("%d%d%lf", &xi, &yi, &zi);
  43. base.a[xi][yi] += zi;
  44. base.a[xi][xi] -= zi;
  45. }
  46. memset(ans.a, , sizeof(ans.a));
  47. for (int i = ; i < n; i++) ans.a[i][i] = ;
  48. fast_mod(m);
  49. fin = ;
  50. for (int i = ; i < n; i++)
  51. fin += num[i] * ans.a[i][n-];
  52. printf("%.0f\n", fin);
  53. }
  54. return ;
  55. }

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 NM. 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

关于 矩阵在ACM中的应用的更多相关文章

  1. 矩阵快速幂在ACM中的应用

    矩阵快速幂在ACM中的应用 16计算机2黄睿博 首发于个人博客http://www.cnblogs.com/BobHuang/ 作为一个acmer,矩阵在这个算法竞赛中还是蛮多的,一个优秀的算法可以影 ...

  2. ACM 中 矩阵数据的预处理 && 求子矩阵元素和问题

            我们考虑一个$N\times M$的矩阵数据,若要对矩阵中的部分数据进行读取,比如求某个$a\times b$的子矩阵的元素和,通常我们可以想到$O(ab)$的遍历那个子矩阵,对它的各 ...

  3. [ACM训练] ACM中巧用文件的输入输出来改写acm程序的输入输出 + ACM中八大输入输出格式

    ACM中巧用文件的输入输出来改写acm程序的输入输出 经常有见大神们使用文件来代替ACM程序中的IO,尤其是当程序IO比较复杂时,可以使自己能够更专注于代码的测试,而不是怎样敲输入. C/C++代码中 ...

  4. C++ STL泛型编程——在ACM中的运用

    学习过C++的朋友们应该对STL和泛型编程这两个名词不会陌生.两者之间的关系不言而喻,泛型编程的思想促使了STL的诞生,而STL则很好地体现了泛型编程这种思想.这次想简单说一下STL在ACM中的一些应 ...

  5. Java在ACM中的应用

    Java在ACM中的应用 —. 在java中的基本头文件(java中叫包) import java.io.*; import java.util.*; //输入Scanner import java. ...

  6. IO/ACM中来自浮点数的陷阱(收集向)

    OI/ACM中经常要用到小数来解决问题(概率.计算几何等),但是小数在计算机中的存储方式是浮点数而不是我们在作数学运算中的数,有精度的限制. 以下以GUN C++为准,其他语言(或编译器)也差不了多少 ...

  7. ACM中的浮点数精度处理

    在ACM中,精度问题非常常见.其中计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模板一般就不成问题了.精度问题则不好说,有时候一个精度问题就可能成为一道题的瓶颈,让你debu ...

  8. ACM 中常用的算法有哪些? 2014-08-21 21:15 40人阅读 评论(0) 收藏

    ACM 中常用的算法有哪些?作者: 张俊Michael 网络上流传的答案有很多,估计提问者也曾经去网上搜过.所以根据自己微薄的经验提点看法. 我ACM初期是训练编码能力,以水题为主(就是没有任何算法, ...

  9. ACM中Java的应用

    先说一下Java对于ACM的一些优点吧: (1) 对于熟悉C/C++的程序员来说Java 并不难学,两周时间基本可以搞定一般的编程,再用些时间了解一下Java库就行了.Java的语法和C++非常类似, ...

随机推荐

  1. Events with Dojo(翻译)

    In this tutorial, we will be exploring dojo/on and how Dojo makes it easy to connect to DOM events. ...

  2. 去掉mac终端里面hostname提示处的bogon

    打开终端,如果在命令提示$前面显示的主机名变成了bogon,则可以通过命令行修改: sudo scutil --set hostname 新主机名

  3. 为什么在ucos向stm32f103移植时说os_cpu_c.c中有三个函数如OS_CPU_SysTickInit()需要注释掉

    我在看os_cpu_c.c代码时对下面这段话困惑了半天总是在百度的帮助下找到了答案 /*  申明几个函数,这里要注意最后三个函数需要注释掉,为什么呢?    OS_CPU_SysTickHandler ...

  4. React组件的分类

    * 1.statelessComponent 不包含任何state的组件 例如:AntDesign的 :Button,Input组件 * 2.viewComponent 包含少量ui state的组件 ...

  5. nyoj 881 小M的区间公约数

    点击打开链接 首先给的范围很大,是10^9.暴力解肯定超时(单用for循环到10^9都大约要2s-3s),首先写了个程序暴力的把两个数所有的约数都打印出来,最后发现所有的公约数都是最大公约数的约数,并 ...

  6. ubuntu14.04计划任务无法执行

    在/etc/crontab中添加了任务1,并确认执行时间设置没有错.发现任务没有执行,而/var/log/cron.log日志文件中没有该计划任务的执行信息.另一个计划任务却能正确,通过修改任务1的执 ...

  7. web页面设计基本原则

    1.对齐. 不对齐看起来糟糕极了.简直一片混乱,会给人一种很不专业的印象.对齐让页面更漂亮一些,更重要的是便于更好的传达信息. 2.去掉糟糕的边框.边框会产生一种分离感. 3.亲密性. 元素相互靠近看 ...

  8. arc下dealloc得处理

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  9. RabbitMQ小结

    1.帮助文档 rabbitmq官网:http://www.rabbitmq.com/ rabbitmq谷歌论坛:https://groups.google.com/forum/#!forum/rabb ...

  10. petapoco存储过程

    db.ExecuteScalar<string>("exec P_GetCode @0,@1,@2,@3,@4,@5",); using (var db = new D ...