题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=4965

题意

给出两个矩阵 一个A: n * k 一个B: k * n

C = A * B

M = (A * B) ^ (n * n)

然后将M中所有的元素对6取余后求和

思路

矩阵结合律。。

M = (A * B) * (A * B) * (A * B) * (A * B) * (A * B) * (A * B) * (A * B) * (A * B) ……

其实也等价于

M = A * (B * A) * (B * A) * (B * A) * (B * A) * (B * A) * (B * A) * B

因为 n 比较大 k 比较小

如果用 (A * B) 做矩阵快速幂的话,会T

所有 用 B * A 做矩阵快速幂 最后左乘A 再右乘B 就可以了

AC代码

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <ctype.h>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <climits>
  7. #include <ctime>
  8. #include <iostream>
  9. #include <algorithm>
  10. #include <deque>
  11. #include <vector>
  12. #include <queue>
  13. #include <string>
  14. #include <map>
  15. #include <stack>
  16. #include <set>
  17. #include <list>
  18. #include <numeric>
  19. #include <sstream>
  20. #include <iomanip>
  21. #include <limits>
  22. #define CLR(a, b) memset(a, (b), sizeof(a))
  23. #define pb push_back
  24. #define bug puts("***bug***");
  25. #define fi first
  26. #define se second
  27. //#define bug
  28. //#define gets gets_s
  29. using namespace std;
  30. typedef long long ll;
  31. typedef long double ld;
  32. typedef unsigned long long ull;
  33. typedef pair <int, int> pii;
  34. typedef pair <ll, ll> pll;
  35. typedef pair <string, int> psi;
  36. typedef pair <string, string> pss;
  37. typedef pair <double, int> pdi;
  38. const double PI = acos(-1.0);
  39. const double EI = exp(1.0);
  40. const double eps = 1e-8;
  41. const int INF = 0x3f3f3f3f;
  42. const int maxn = 1e3 + 10;
  43. const int MOD = 6;
  44. struct Matrix
  45. {
  46. int a[10][10];
  47. int n;
  48. Matrix() {}
  49. Matrix operator * (Matrix const &b)const
  50. {
  51. Matrix res;
  52. res.n = n;
  53. CLR(res.a, 0);
  54. for (int i = 0; i < n; i++)
  55. for (int j = 0; j < n; j++)
  56. for (int k = 0; k < n; k++)
  57. res.a[i][j] = (res.a[i][j] + this->a[i][k] * b.a[k][j]) % MOD;
  58. return res;
  59. }
  60. };
  61. Matrix pow_mod(Matrix base, int a, int n)
  62. {
  63. Matrix ans;
  64. CLR(ans.a, 0);
  65. for (int i = 0; i < n; i++)
  66. ans.a[i][i] = 1;
  67. ans.n = n;
  68. while (a > 0)
  69. {
  70. if (a & 1)
  71. ans = ans * base;
  72. base = base * base;
  73. a >>= 1;
  74. }
  75. return ans;
  76. }
  77. int A[maxn][maxn], B[maxn][maxn], ans[maxn][maxn];
  78. int main()
  79. {
  80. int n, k;
  81. while (scanf("%d%d", &n, &k) && (n || k))
  82. {
  83. for (int i = 0; i < n; i++)
  84. for (int j = 0; j < k; j++)
  85. scanf("%d", &A[i][j]);
  86. for (int i = 0; i < k; i++)
  87. for (int j = 0; j < n; j++)
  88. scanf("%d", &B[i][j]);
  89. Matrix base;
  90. base.n = k;
  91. CLR(base.a, 0);
  92. for (int i = 0; i < k; i++)
  93. for (int j = 0; j < k; j++)
  94. for (int l = 0; l < n; l++)
  95. base.a[i][j] = (base.a[i][j] + B[i][l] * A[l][j]) % MOD;
  96. base = pow_mod(base, n * n - 1, k);
  97. CLR(ans, 0);
  98. for (int i = 0; i < k; i++)
  99. for (int j = 0; j < n; j++)
  100. for (int l = 0; l < k; l++)
  101. ans[i][j] = (ans[i][j] + base.a[i][l] * B[l][j]) % MOD;
  102. CLR(B, 0);
  103. for (int i = 0; i < n; i++)
  104. for (int j = 0; j < n; j++)
  105. for (int l = 0; l < k; l++)
  106. B[i][j] = (B[i][j] + A[i][l] * ans[l][j]) % MOD;
  107. int tot = 0;
  108. for (int i = 0; i < n; i++)
  109. for (int j = 0; j < n; j++)
  110. tot += B[i][j];
  111. cout << tot << endl;
  112. }
  113. }

HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】的更多相关文章

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

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

  2. hdu 4965 Fast Matrix Calculation(矩阵高速幂)

    题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...

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

  4. Fast Matrix Calculation 矩阵快速幂

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

  5. HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律

    一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...

  6. HDU 4965 Fast Matrix Calculation(矩阵高速幂)

    HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...

  7. hdu 4965 Fast Matrix Calculation

    题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...

  8. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  9. HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂

    题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. CSS字体中英文名称对照表(转)

      在css文件中,我们常看到有些字体名称变成了乱码,这是由于网页开发者将中文字体的名字直接写成了中文,而css文件本身没有声明字符编码方式,查看时就出现了乱码.为了避免这种乱码状况出现,可以将css ...

  2. ABP框架EF6链接Oracle数据库手动迁移

    环境:VS2017 + ABP官方模板(不含Zero) +Oracle 11Gx64DB  + Oracle 11Gx32Client(PLSQL工具访问) 一.Abp项目的下载以及运行 1.创建ab ...

  3. dispatch_after中时间的计算

    dispatch_after中用的时间是纳秒,所以需要进行转换:desDelayInSeconds(目标时间,比如2s)* NSEC_PER_SEC double delayInSeconds = 0 ...

  4. The TTY demystified

    http://www.linusakesson.net/programming/tty/index.php The TTY demystified Real teletypes in the 1940 ...

  5. IOS与安卓的远程调试

    本地调试H5页面方案总结 http://www.jianshu.com/p/a43417b28280 Fiddler 手机抓包 http://blog.csdn.net/gld824125233/ar ...

  6. linux安全组配置

    万网的是这样子配置的:

  7. [译]GLUT教程 - 移动镜头3

    Lighthouse3d.com >> GLUT Tutorial >> Input >> Moving the Camera III 上一节的示例中我们用键盘更改 ...

  8. CentOS: Make Command not Found and linux xinetd 服务不能启动

    在centos 安装编译器 yum -y install gcc automake autoconf libtool make linux xinetd 服务不能启动: [root@capaa xin ...

  9. UIWebView的全屏截图

    项目开发中,我们可能会遇到如下的应用场景:将一篇文章,进行截屏(需要全屏截取,包括滚动部分)后,分享到新浪微博.邮箱等等.前段时间,我在应用开发中实现了该功能,代码也是从网上找到的,自己整理了一下.主 ...

  10. 将普通用户添加至sudoers列表

    编辑/etc/sudoers文件,在尾部添加如下内容: myusername ALL=(ALL) ALL myusername ALL=(ALL) NOPASSWD: ALL 其中需要将红色部分替换成 ...