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

4 2
5 5
4 4
5 4
0 0
4 2 5 5
1 3 1 5
6 3
1 2 3
0 3 0
2 3 4
4 3 2
2 5 5
0 5 0
3 4 5 1 1 0
5 3 2 3 3 2
3 1 5 4 5 2
0 0

Sample Output

14
56 C = A*B
C^1000 = A*(B*A)^999*B 简化到k*k上的矩阵计算方便
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 1007
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-9;
int n, k;
struct Mat
{
int a[10][10];
Mat()
{
memset(a, 0, sizeof(a));
}
Mat operator * (const Mat& rhs)const
{
Mat ret;
for (int i = 0; i < k; i++)
{
for (int j = 0; j < k; j++)
{
if (a[i][j])
{
for (int t = 0; t < k; t++)
{
ret.a[i][t] = (ret.a[i][t] + a[i][j] * rhs.a[j][t]) % 6;
}
}
}
}
return ret;
}
};
Mat fpow(Mat a, int b)
{
Mat ret;
for (int i = 0; i < k; i++)
ret.a[i][i] = 1;
while (b != 0)
{
if (b & 1)
ret = a*ret;
a = a*a;
b /= 2;
}
return ret;
}
int m1[MAXN][7], m2[7][MAXN], ans[MAXN][MAXN], tmp[MAXN][MAXN];
int main()
{
while (scanf("%d%d", &n, &k), n + k)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
scanf("%d", &m1[i][j]);
for (int i = 0; i < k; i++)
for (int j = 0; j < n; j++)
scanf("%d", &m2[i][j]);
Mat M;
for (int i = 0; i < k; i++)
{
for (int j = 0; j < k; j++)
{
for (int t = 0; t < n; t++)
{
M.a[i][j] = (M.a[i][j] + m2[i][t] * m1[t][j])%6;
}
}
}
M = fpow(M, n*n - 1);
memset(tmp, 0, sizeof(tmp));
memset(ans, 0, sizeof(ans));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < k; j++)
{
for (int t = 0; t < k; t++)
tmp[i][j] = (tmp[i][j] + m1[i][t] * M.a[t][j])%6;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int t = 0; t < k; t++)
ans[i][j] = (ans[i][j] + tmp[i][t] * m2[t][j])%6;
}
}
int res = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res += ans[i][j]%6;
printf("%d\n", res);
}
}
 

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. hdu61272017杭电多校第七场1008Hard challenge

    Hard challenge Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) ...

  2. PowerDesigner连接Oracle数据库(32位)反向生成物理数据模型

    PowerDesigner可以连接Oracle数据库进行反向生成物理数据模型,本文演示操作过程. 环境说明: 1)Windows8.1,Oracle11R2 32位. 2)PowerDesigner1 ...

  3. mysqladmin(MySQL管理工具)

    mysqladmin是一个执行管理操作的客户端程序.它可以用来检查服务器的配置和当前状态.创建和删除数据库等. 1.mysqladmin命令的语法: shell > mysqladmin [op ...

  4. 【日常总结】scrollTop、scrollHeight与clientHeight的重要关系

    前言 在做一个需求的时候涉及懒加载,百度了一下,发现scrollTop.scrollHeight与clientHeight这三个元素起到了重要作用,以前做过类似demo但是时间过太久忘记了,现在已经完 ...

  5. EasyUI系列学习(七)-Linkbutton(按钮)

    一.加载组件 1.使用class加载 <a href="#" class="easyui-linkbutton">按钮</a> 2.使用 ...

  6. FCC 基础JavaScript 练习1

    1.JavaScript中的注释方式有以下两种 // This is an in-line comment. /* This is a multi-line comment */ 2.avaScrip ...

  7. js文件中引用其他js文件

    这一个功能的作用是做自己的js包时,可以通过引入一个整体的js文件而引入其他js. 只需要在总体的js加上这一句话 document.write("<script type='text ...

  8. 【转】jvm内存结构

    JVM的基本结构 包括四部分:类加载器.执行引擎.内存区(运行时数据区).本地方法接口 类加载器:jvm启动时或类运行时将需要的class文件加载到JVM中. JVM内存申请过程如下: JVM 会试图 ...

  9. tensorboard在windows系统浏览器显示空白的解决

    一个简单的using_tensorboard.py程序,如下: #using_tensorboard.py import tensorflow as tf a = tf.constant(10,nam ...

  10. Laravel 网站项目目录结构规划

    最近在用Laravel这个PHP框架搭网站,大致了解这个框架的目录结构之后感觉学到了不少东西. 首先安装好包管理器: PHP部分当然用composer,安装在全局目录下方便一点. 前端部分,我没有选择 ...