One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learning something about matrix, so he decided to make a crazy problem for her.

Bob has a six-faced dice which has numbers 0, 1, 2, 3, 4 and 5 on each face. At first, he will choose a number N (4 <= N <= 1000), and for N times, he keeps throwing his dice for K times (2 <=K <= 6) and writes down its number on the top face to make an N*K matrix A, in which each element is not less than 0 and not greater than 5. Then he does similar thing again with a bit difference: he keeps throwing his dice for N times and each time repeat it for K times to write down a K*N matrix B, in which each element is not less than 0 and not greater than 5. With the two matrix A and B formed, Alice’s task is to perform the following 4-step calculation.

Step 1: Calculate a new N*N matrix C = A*B. 
Step 2: Calculate M = C^(N*N). 
Step 3: For each element x in M, calculate x % 6. All the remainders form a new matrix M’. 
Step 4: Calculate the sum of all the elements in M’.

Bob just made this problem for kidding but he sees Alice taking it serious, so he also wonders what the answer is. And then Bob turn to you for help because he is not good at math.

InputThe input contains several test cases. Each test case starts with two integer N and K, indicating the numbers N and K described above. Then N lines follow, and each line has K integers between 0 and 5, representing matrix A. Then K lines follow, and each line has N integers between 0 and 5, representing matrix B.

The end of input is indicated by N = K = 0.OutputFor each case, output the sum of all the elements in M’ in a line.Sample Input

  1. 4 2
  2. 5 5
  3. 4 4
  4. 5 4
  5. 0 0
  6. 4 2 5 5
  7. 1 3 1 5
  8. 6 3
  9. 1 2 3
  10. 0 3 0
  11. 2 3 4
  12. 4 3 2
  13. 2 5 5
  14. 0 5 0
  15. 3 4 5 1 1 0
  16. 5 3 2 3 3 2
  17. 3 1 5 4 5 2
  18. 0 0

Sample Output

  1. 14
  2. 56
  3.  
  4. C = A*B
    C^1000 = A*(B*A)^999*B
  5.  
  6. 简化到k*k上的矩阵计算方便
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<sstream>
  6. #include<algorithm>
  7. #include<queue>
  8. #include<deque>
  9. #include<iomanip>
  10. #include<vector>
  11. #include<cmath>
  12. #include<map>
  13. #include<stack>
  14. #include<set>
  15. #include<fstream>
  16. #include<memory>
  17. #include<list>
  18. #include<string>
  19. using namespace std;
  20. typedef long long LL;
  21. typedef unsigned long long ULL;
  22. #define MAXN 1007
  23. #define MOD 10000007
  24. #define INF 1000000009
  25. const double eps = 1e-9;
  26. int n, k;
  27. struct Mat
  28. {
  29. int a[10][10];
  30. Mat()
  31. {
  32. memset(a, 0, sizeof(a));
  33. }
  34. Mat operator * (const Mat& rhs)const
  35. {
  36. Mat ret;
  37. for (int i = 0; i < k; i++)
  38. {
  39. for (int j = 0; j < k; j++)
  40. {
  41. if (a[i][j])
  42. {
  43. for (int t = 0; t < k; t++)
  44. {
  45. ret.a[i][t] = (ret.a[i][t] + a[i][j] * rhs.a[j][t]) % 6;
  46. }
  47. }
  48. }
  49. }
  50. return ret;
  51. }
  52. };
  53. Mat fpow(Mat a, int b)
  54. {
  55. Mat ret;
  56. for (int i = 0; i < k; i++)
  57. ret.a[i][i] = 1;
  58. while (b != 0)
  59. {
  60. if (b & 1)
  61. ret = a*ret;
  62. a = a*a;
  63. b /= 2;
  64. }
  65. return ret;
  66. }
  67. int m1[MAXN][7], m2[7][MAXN], ans[MAXN][MAXN], tmp[MAXN][MAXN];
  68. int main()
  69. {
  70. while (scanf("%d%d", &n, &k), n + k)
  71. {
  72. for (int i = 0; i < n; i++)
  73. for (int j = 0; j < k; j++)
  74. scanf("%d", &m1[i][j]);
  75. for (int i = 0; i < k; i++)
  76. for (int j = 0; j < n; j++)
  77. scanf("%d", &m2[i][j]);
  78. Mat M;
  79. for (int i = 0; i < k; i++)
  80. {
  81. for (int j = 0; j < k; j++)
  82. {
  83. for (int t = 0; t < n; t++)
  84. {
  85. M.a[i][j] = (M.a[i][j] + m2[i][t] * m1[t][j])%6;
  86. }
  87. }
  88. }
  89. M = fpow(M, n*n - 1);
  90. memset(tmp, 0, sizeof(tmp));
  91. memset(ans, 0, sizeof(ans));
  92. for (int i = 0; i < n; i++)
  93. {
  94. for (int j = 0; j < k; j++)
  95. {
  96. for (int t = 0; t < k; t++)
  97. tmp[i][j] = (tmp[i][j] + m1[i][t] * M.a[t][j])%6;
  98. }
  99. }
  100. for (int i = 0; i < n; i++)
  101. {
  102. for (int j = 0; j < n; j++)
  103. {
  104. for (int t = 0; t < k; t++)
  105. ans[i][j] = (ans[i][j] + tmp[i][t] * m2[t][j])%6;
  106. }
  107. }
  108. int res = 0;
  109. for (int i = 0; i < n; i++)
  110. for (int j = 0; j < n; j++)
  111. res += ans[i][j]%6;
  112. printf("%d\n", res);
  113. }
  114. }
 

Fast Matrix Calculation 矩阵快速幂的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

  9. HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...

随机推荐

  1. 擅长排列的小明II

    先搜索 出来一点结果之后  看结果之间的 联系 得出  递推公式  . #include<stdio.h> #include<string.h> #include<mat ...

  2. Linux学习笔记之Linux常用命令剖析-cat/chmod/cd

    1.cat:用于连接文件并打印到标准输出设备上.(使用权限:所有使用者) 语法格式:cat [-AbeEnstTuv] [--help] [--version] fileName 参数说明: -n 或 ...

  3. SimpleDataFormat详解

    [转]SimpleDateFormat使用详解 public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方 ...

  4. IFormattable,ICustomFormatter, IFormatProvider接口

    定                 义 1.IFormattable   提供一种功能,用以将对象的值格式化为字符串表示形式. 2.IFormatProvider  提供用于检索控制格式化的对象的机制 ...

  5. 仿ofo单车做一个轮播效果

    github地址 首先我是利用swiper.js做的,因为这个很强大,哈哈~~,上代码 html很简单 <body> <div class="swiper-containe ...

  6. PSP需求分析文档

    PSP软件需求分析文档 刘杰 1.       引言 1.1  背景 开发项目经常延期不能按时提交,甚至不能给出明确的延迟时间 1.2  术语 PSP,数据库 2.       任务概述 2.1  目 ...

  7. day19-常用模块IV(re、typing)

    目录 re模块 typing模块 爬取音频 re模块 用来从字符串(文本)中查找特定的东西 1.元字符:有特殊意义的字符 ^ 从开头匹配 import re a = re.findall('^abc' ...

  8. logging,numpy,pandas,matplotlib模块

    logging模块 日志总共分为以下五个级别,这五个级别自下而上进行匹配debug->info->warning->error->critical,默认的最低级别warning ...

  9. CSS绝对定位模拟固定定位

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. arx代码片段

    ObjectARX代码片段二   转载自网络 一  在ARX中禁用AutoCAD的某个命令 以LINE命令为例,在程序中加入下面的一句即可禁用LINE命令: acedCommand(RTSTR, &q ...