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. Linux之线程相关命令及常用命令

    查进程 top命令:查看系统的资源状况.#top top -d 10     //指定系统更新进程的时间为10秒 ps:查看当前用户的活动进程.#ps -A ps命令查找与进程相关的PID号: ps ...

  2. 51nod1265判断四点共面

    1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点是否在同一个平面内(4点共 ...

  3. 264 Ugly Number II 丑数 II

    编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...

  4. 安装Kali里的应用程序或软件包

    安装Kali里的应用程序或软件包 安装额外的软件是apt-get命令最基本的功能了,命令非常简单易懂.安装软件包的语法如下: apt-get install 软件包名 比如,安装图像编辑软件gimp, ...

  5. how to do a mass update in Laravel5 ( 在Laravel 5里面怎么做大量数据更新 )

    Today, I had spent 3 hours to fix one problem, The old program has a bug, originally, when a user pr ...

  6. 2017-11-28 Html-浅谈如何正确给table加边框

    一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...

  7. [AC自动机模板]Keywords Search

    只是记录一下代码 AC自动机算法的教程请移步这里 还有这里 指针看着懵逼的还可以看一下这里 #include<iostream> #include<cstdio> #inclu ...

  8. VMware vCenter 6.5 安装及群集配置介绍

    一.介绍 VMware vCenter Server 提供了一个可伸缩.可扩展的平台,为虚拟化管理奠定了基础.可集中管理VMware vSphere环境,与其他管理平台相比,极大地提高了 IT 管理员 ...

  9. ThinkPHP---TP功能类之公文管理功能2----------继续完善

    [前言] 之前已经完成了公文的添加和列表展示功能,今天继续完善.做下公文的编辑和删除功能. [主体] (1)分析 控制器:DocController.class.php 方法:edit(将模板展示和数 ...

  10. ThinkPHP---thinkphp控制器、路由、分组设置(C)

    配置文件分3类:系统配置文件,分组配置文件,应用配置文件 ①系统配置文件ThinkPHP/Conf/convention.php: ②分组 / 模块 /平台配置文件Home/Conf/config.p ...