题意:给你一个矩阵A,求S=A+A^2+A^3+...+A^k。

  其实这个当时我看着毫无头绪,看了他们给的矩阵发现好!精!妙!

  我们这样看

       

         

   是不是有点思路!

   没错!就是右上角,我们以此类推可以得到A+A^2+A^3+...+A^k+E,我们只要再减去个单位矩阵就好了。

   但是!我矩阵里面怎么套矩阵!肿!么!办!其实很简单,一个n*n的矩阵,我们可以把它看成(2*n)*(2*n)的矩阵,就把他分成了四份,就如上图所示,就很简单了!

  注意下小坑点:减了可能就负了,后面减完要加个mod(ง •_•)ง

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<vector>
  6. #include<stack>
  7. #include<cstring>
  8. #include<queue>
  9. #include<set>
  10. #include<string>
  11. #include<map>
  12. #include <time.h>
  13. #define PI acos(-1)
  14. using namespace std;
  15. typedef long long ll;
  16. typedef double db;
  17. const int maxn = ;
  18. const int N = ;
  19. const ll maxm = 1e7;
  20. const int INF = 0x3f3f3f;
  21. const ll inf = 1e15 + ;
  22. const db eps = 1e-;
  23. ll n, k, mod;
  24. struct Matrix{
  25. ll mat[maxn][maxn];
  26. Matrix operator*(const Matrix& m)const{
  27. Matrix tmp;
  28. for (int i = ; i <= n; i++) {
  29. for (int j = ; j <= n; j++) {
  30. tmp.mat[i][j]=;
  31. for (int k = ; k <= n; k++) {
  32. tmp.mat[i][j] += mat[i][k]*m.mat[k][j]%mod;
  33. tmp.mat[i][j]+=mod;
  34. tmp.mat[i][j] %= mod;
  35. }
  36. }
  37. }
  38. return tmp;
  39. }
  40. };
  41.  
  42. int Pow(Matrix &m, int k) {
  43. Matrix ans;
  44. memset(ans.mat , , sizeof(ans.mat));
  45. for (int i=; i<=n; i++) {
  46. ans.mat[i][i]=;
  47. ans.mat[i+n][i+n]=;
  48. }
  49. n*=;
  50. while(k){
  51. if(k&)
  52. ans = ans*m;
  53. k >>= ;
  54. m = m*m;
  55. }
  56. n/=;
  57. for (int i=; i<=n; i++) {
  58. ans.mat[i][i+n]--;
  59. ans.mat[i][i+n]+=mod;
  60. ans.mat[i][i+n]%=mod;
  61. }
  62. for (int i=; i<=n; i++) {
  63. for (int j=; j<=n; j++) {
  64. if (j==n) printf("%d\n", ans.mat[i][j+n]);
  65. else printf("%d ", ans.mat[i][j+n]);
  66. }
  67. }
  68. }
  69.  
  70. void solve() {
  71. Matrix m; memset(m.mat, , sizeof(m.mat));
  72. scanf("%lld%lld%lld", &n, &k, &mod);
  73. for (int i = ; i <= n; i++) {
  74. for (int j = ; j <= n; j++) {
  75. scanf("%lld", &m.mat[i][j]);
  76. m.mat[i][i+n]=;
  77. m.mat[i+n][i+n]=;
  78. }
  79. }
  80. k++;
  81. Pow(m, k);
  82.  
  83. }
  84. int main() {
  85. int t = ;
  86. //freopen("in.txt", "r", stdin);
  87. // scanf("%d", &t);
  88. while(t--)
  89. solve();
  90. return ;
  91. }

      

经典矩阵快速幂之一-----poj3233(矩阵套矩阵的更多相关文章

  1. Luogu P3390 【模板】矩阵快速幂&&P1939 【模板】矩阵加速(数列)

    补一补之前的坑 因为上次关于矩阵的那篇blog写的内容太多太宽泛了,所以这次把一些板子和基本思路理一理 先看这道模板题:P3390 [模板]矩阵快速幂 首先我们知道矩阵乘法满足结合律而不满足交换律的一 ...

  2. 矩阵快速幂——将运算推广到矩阵上HDU 1575

    /* 本题的思路比较简单,就是将递推公式写出来,然后表达成为一个矩阵的形式 最后通过计算就可以得到一个符合题目要求的矩阵, 然后就是将矩阵上面所有的对角线元素相加 得到的结果即为所求的目标 */ #i ...

  3. 矩阵快速幂+二分 poj3233

    #include <iostream> #include <cstdio> #include <string> #include <cstring> # ...

  4. hdu 1005 Number Sequence(矩阵快速幂,找规律,模版更通用)

    题目 第一次做是看了大牛的找规律结果,如下: //显然我看了答案,循环节点是48,但是为什么是48,据说是高手打表出来的 #include<stdio.h> int main() { ], ...

  5. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  6. Count Numbers(矩阵快速幂)

    Count Numbers 时间限制: 8 Sec  内存限制: 128 MB提交: 43  解决: 19[提交] [状态] [讨论版] [命题人:admin] 题目描述 Now Alice want ...

  7. hdu 5451 Best Solver 矩阵循环群+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=5451 题意:给定x    求解 思路: 由斐波那契数列的两种表示方法, 之后可以转化为 线性表示 F[n] = ...

  8. 矩阵快速幂---BestCoder Round#8 1002

    当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n)   f(n-1)] = [f(n ...

  9. P3390 【模板】矩阵快速幂

    题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格式: 输出A^k ...

随机推荐

  1. 每月IT摘录201810

    技术 1.Redis.对于单机实例,我们采用原生主从(Master-Slave)模式实现高可用,常规模式下对外仅暴露 Master 节点.由于使用原生 Redis,所以单机实例支持所有 Redis 指 ...

  2. 扩展欧几里得 hdu 1576

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 不知道扩展欧几里得的同学可以参考:https://blog.csdn.net/zhjchengf ...

  3. Angular之模版引用变量

    A template reference variable is often a reference to a DOM element within a template. It can also b ...

  4. 【mysql】分区表

    分区表是什么? 分区表可以按照事先创建的规则,对mysql的记录进行分组,每一个组具有一个独立的逻辑单元来存储该组的数据.典型的如:按照创建时间的年份分组,按照id的顺序分组(每1000万条数据分一个 ...

  5. SpringBoot使用@Value从yml文件取值为空--注入静态变量

    SpringBoot使用@Value从yml文件取值为空--注入静态变量     1.application.yml中配置内容如下:   pcacmgr:   publicCertFilePath: ...

  6. CSS-calc 兼容写法

    width: 90%;/*写给不支持calc()的浏览器*/ width:-moz-calc(100% - (10px + 5px) * 2); width:-webkit-calc(100% - ( ...

  7. SQL删除重复数据只保留一条数据

    1.表结构与数据: CREATE TABLE tablezzl( id int, name ) ); 2.查询出重复的数据: 3.查询出要保留的重复数据: 4.最终的SQL: DELETE FROM ...

  8. devexpress WinForms MVVM

    WinForms MVVM This section is dedicated to the Model-View-ViewModel (MVVM) architectural pattern. Yo ...

  9. "cni0" already has an IP address different from 10.244.2.1/24。 Error while adding to cni network: failed to allocate for range 0: no IP addresses available in range set: 10.244.2.1-10.244.2.254

    "cni0" already has an IP address different from 10.244.2.1/24. Error while adding to cni n ...

  10. Centos7 开机启动命令行模式

    1.在图形界面下单击鼠标右键,选择“Konsole”: 2. 获取当前系统启动模式,输入:systemctl get-default 3.查看配置文件, cat /etc/inittab 4.通过以上 ...