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. [转]C语言/C++中如何产生随机数

    C语言/C++怎样产生随机数:这里要用到的是rand()函数, srand()函数,和time()函数. 需要说明的是,iostream头文件中就有srand函数的定义,不需要再额外引入stdlib. ...

  2. 数据传递-------ajaxJson------spring3mvc中使用ajax传json中文乱码解决

    参考来源:http://blog.csdn.net/dangerous_fire/article/details/25904225 第一种解决方法,适用所有情况 因为在controller中返回jso ...

  3. php 5.6 版本配置 oracle ddl

    1. Windows版PHP内置了Oracle驱动,在ext目录下:php_oci8.dllphp_oci8_11g.dllphp_pdo_oci.dllLinux上如果自己编译的话则添加下面的con ...

  4. MySQL与Sqlserver数据获取

    由于项目要求,一个.net mvc登录注册的东西网站必须放弃sqlserver数据去使用MySQL数据库,因此我遇到了一些问题,并找出相应的解决方法, 因为sqlserver跟MySQL的数据引擎不同 ...

  5. js实现左右点击图片层叠滚动特效

    需要加载js有 <script type="text/javascript" src="js/jquery-1.7.2.min.js"></s ...

  6. Python学习日记之文件读取操作

    Python内置了文件读写的函数open,read 用法示例: open('/home/root/files.txt ') 在打开文件后,操作完成后可以使用close()关闭文件,但比较好的文件读写方 ...

  7. 一个完整的网站记录(springmvc hibernate juery bootstrap)

    总述 该网站为了满足测试人员自主添加测试条目,编辑更新信息和删除信息,同时同步到后台数据库的基本功能. 关键技术:oracle数据库.tomcat8.5.springMVC.Hibernate.aja ...

  8. Sonar 规则

    bug类型: 1.".equals()" should not be used to test the values of "Atomic" classes. ...

  9. UML实例教程 解析UML建模分析与设计

    UML统一建模语言在软件开发过程中非常实用,UMl建模的分析与设计你是否熟悉,这里就通过实例向大家介绍,希望通过本文的学习,你对UML建模的分析与设计方法有一定的了解. 本节向大家介绍一下图书管理系统 ...

  10. Jmeter的面板介绍

    一.jmeter面板介绍,这里是5.1.1版本 1.jmeter面板大致分为菜单区,图标区,目录树以及详细配置区域. 2.大多数人用着英文的面板不太舒服,可以在options里切换jmeter的语言 ...