Power of Matrix

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Appoint description: 
System Crawler  (2015-03-15)

Description

 

Problem B : Power of Matrix

Time limit: 10 seconds

Consider an n-by-n matrix A. We define Ak = A * A * ... * A (k times). Here, * denotes the usual matrix multiplication.

You are to write a program that computes the matrix A + A2 + A3 + ... + Ak.

Example

Suppose A = . Then A2 =  = , thus:

Such computation has various applications. For instance, the above example actually counts all the paths in the following graph:

Input

Input consists of no more than 20 test cases. The first line for each case contains two positive integers n (≤ 40) and k (≤ 1000000). This is followed by n lines, each containing n non-negative integers, giving the matrix A.

Input is terminated by a case where n = 0. This case need NOT be processed.

Output

For each case, your program should compute the matrix A + A2 + A3 + ... + Ak. Since the values may be very large, you only need to print their last digit. Print a blank line after each case.

Sample Input

  1. 3 2
  2. 0 2 0
  3. 0 0 2
  4. 0 0 0
  5. 0 0

Sample Output

  1. 0 2 4
  2. 0 0 2
  3. 0 0 0

首先我们来想一下计算A+A^2+A^3...+A^k。

如果A=2,k=6。那你怎么算

2+22+23+24+25+26 = ?= (2+22+23)*(1+23)

如果A=2,k=7。那你怎么算

2+22+23+24+25+26+2= ?= (2+22+23)*(1+23)+27

so....同理:

当k是偶数,A+A^2+A^3...+A^k=(E+A^(k/2))*(A+A^2...+A^(k/2))。

当k是奇数,A+A^2+A^3...+A^k=(E+A^(k/2))*(A+A^2...+A^(k/2))+A^k。

转载请注明出处:寻找&星空の孩子

题目链接:UVA 11149

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. #define LL __int64
  7. #define mmax 45
  8.  
  9. struct matrix
  10. {
  11. int mat[mmax][mmax];
  12. };
  13.  
  14. int N;
  15.  
  16. matrix multiply(matrix a,matrix b)
  17. {
  18. matrix c;
  19. memset(c.mat,,sizeof(c.mat));
  20. for(int i=; i<N; i++)
  21. {
  22. for(int j=; j<N; j++)
  23. {
  24. if(a.mat[i][j]==)continue;
  25. for(int k=; k<N; k++)
  26. {
  27. if(b.mat[j][k]==)continue;
  28. c.mat[i][k]=(c.mat[i][k]+a.mat[i][j]*b.mat[j][k])%;
  29.  
  30. }
  31. }
  32. }
  33. return c;
  34. }
  35.  
  36. matrix quickmod(matrix a,int n)
  37. {
  38. matrix res;
  39. for(int i=; i<N; i++) //单位阵
  40. for(int j=; j<N; j++)
  41. res.mat[i][j]=(i==j);
  42. while(n)
  43. {
  44. if(n&)
  45. res=multiply(a,res);
  46. a=multiply(a,a);
  47. n>>=;
  48. }
  49. return res;
  50. }
  51. matrix add (matrix a,matrix b)
  52. {
  53. matrix ret;
  54. for(int i=; i<N; i++)
  55. for(int j=; j<N; j++)
  56. ret.mat[i][j]=(a.mat[i][j]+b.mat[i][j])%;
  57. return ret;
  58. }
  59. matrix solve(matrix a,int k)
  60. {
  61. if(k==) return a;
  62. matrix ans;
  63. for(int i=; i<N; i++)
  64. for(int j=; j<N; j++)
  65. ans.mat[i][j]=(i==j);
  66. if(k==) return ans;
  67. ans=multiply((add(quickmod(a,(k>>)),ans)),solve(a,(k>>)));
  68. if(k%) ans=add(quickmod(a,k),ans);
  69. return ans;
  70. }
  71.  
  72. int main()
  73. {
  74. int k;
  75. while(scanf("%d%d",&N,&k)!=EOF)
  76. {
  77. if(!N)break;
  78. matrix ans;
  79. for(int i=;i<N;i++)
  80. {
  81. for(int j=;j<N;j++)
  82. {
  83. int temp;
  84. scanf("%d",&temp);
  85. ans.mat[i][j]=temp%;
  86. }
  87. }
  88.  
  89. ans=solve(ans,k);
  90.  
  91. for(int i=;i<N;i++)
  92. {
  93. for(int j=;j<N-;j++)
  94. {
  95. printf("%d ",ans.mat[i][j]);
  96. }
  97. printf("%d\n",ans.mat[i][N-]);
  98. }
  99. printf("\n");
  100. }
  101. return ;
  102. }

Power of Matrix(uva11149+矩阵快速幂)的更多相关文章

  1. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

  2. hdu4965 Fast Matrix Calculation 矩阵快速幂

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...

  3. ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)

    Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...

  4. HDU 4965 Fast Matrix Calculation 矩阵快速幂

    题意: 给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\) ...

  5. bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希

    题目 给定矩阵A, B和模数p,求最小的正整数x满足 A^x = B(mod p). 分析 与整数的离散对数类似,只不过普通乘法换乘了矩阵乘法. 由于矩阵的求逆麻烦,使用 $A^{km-t} = B( ...

  6. UVA-11149 Power of Matrix(矩阵二分幂)

    题目大意:给一个n阶方阵,求A1+A2+A3+......Ak. 题目分析:令F(k)=A1+A2+A3+......Ak.当k为偶数时,F(k)=F(k/2)*(E+Ak/2),k为奇数时,F(k) ...

  7. Fast Matrix Calculation 矩阵快速幂

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...

  8. UVA11149 矩阵快速幂

    首先我们来想一下计算A+A^2+A^3...+A^k. 如果A=2,k=6.那你怎么算 2+22+23+24+25+26 = ?= (2+22+23)*(1+23) 如果A=2,k=7.那你怎么算 2 ...

  9. uva11149矩阵快速幂

    求A+A^1+...+A^n 转换一下变成|A  E|,的n+1次方就是|A^(n+1)  A^n+...+A+E| |0  E|                       |    0       ...

随机推荐

  1. 利用App漏洞获利2800多万元,企业该如何避免类似事件?

    上个月,上海警方抓捕了一个利用网上银行漏洞非法获利的犯罪团伙,该团伙利用银行App漏洞非法获利2800多万元. 据悉,该团伙使用技术软件成倍放大定期存单金额,从而非法获利.理财邦的一篇文章分析了犯罪嫌 ...

  2. 【计算机网络】数据交换技术和多路复用技术的正(nao)确(can)打开方式

    交换的作用   数据交换是计算机网络中两个终端进行数据传输的方式,它又可以分成两种类型:电路交换和分组交换.很显然,问题的核心在于“交换”,那么我们首先要思考的是:交换的作用是什么?   “交换”的作 ...

  3. cad.net之ACAD移植到GCAD的自动加载问题

    将acad.pgp,lsp,fas,vlx,名称增加一份gcad.pgp,lsp,fas,vlx.涉及系统加载用. Lisp的拖拉加载在gcad无法通过lastprompt获取命令历史栏最后一行(含路 ...

  4. 利用CVE-2018-0950漏洞自动窃取Windows密码

    i春秋作家:浅安 0×00 前言 记得还在2016年的时候,我的一个同事那时还在使用CERT BFF(自动化模糊测试工具),他向我询问到如何将微软办公软件中看起来可利用的漏洞转化成可以运行的病毒程序并 ...

  5. 转---移动端 h5开发相关内容总结——CSS篇

    作者:伯乐在线专栏作者 - zhiqiang21 如有好文章投稿,请点击 → 这里了解详情 如需转载,发送「转载」二字查看说明 1.移动端开发视窗口的添加 h5端开发下面这段话是必须配置的 meta ...

  6. C#导出HTML到PDF组件 Pechkin

    C#导出PDF功能是开发中经常遇到的功能,我们采用第三方的组件,比如 iTextSharp, aspose等,还能搜到一些开源的类库, 但是对于一些内容复杂样式丰富的PDF,我们希望通过传入一个URL ...

  7. Eclipse 导入本地 Git 项目

    File -->  Open Projects From File System 选择项目路径 Finish

  8. 05-创建kubectl-kubeconfig文件

    本文档介绍创建 kubeconfig 文件 下载 kubectl $ wget https://dl.k8s.io/v1.6.0/kubernetes-client-linux-amd64.tar.g ...

  9. sql 数据库日志收缩

    SQL2008 的收缩日志 由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消:(SQL2005)Backup Log DNName w ...

  10. Oracle VM VirtualBox启动后莫名奇妙的报错

    VirtualBox软件无法启动: 参考解决:http://blog.csdn.net/a_ssimi/article/details/52002939 修改兼容性:http://blog.csdn. ...