Fast Matrix Calculation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 170    Accepted Submission(s): 99

Problem Description
   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.
 
Input
   The 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.
 
Output
   For 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
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  4970 4968 4967 4966 4964 
 
题解:
 (4 <= N <= 1000), (2 <=K <= 6)
N*K matrix A,K*N matrix B
A*B是N*N,但是B*A为k*k,于是。。。
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 1005
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,k;
int a[N][],b[][N],d[][],f[N][],g[N][N],h[N][N];
int ans; typedef struct{
int m[][];
} Matrix; Matrix e,P; Matrix I = {,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
}; Matrix matrixmul(Matrix aa,Matrix bb)
{
int i,j,kk;
Matrix c;
for (i = ; i <= k; i++)
for (j = ; j <= k;j++)
{
c.m[i][j] = ;
for (kk = ; kk <= k; kk++)
c.m[i][j] += (aa.m[i][kk] * bb.m[kk][j])%mod;
c.m[i][j] %= mod;
}
return c;
} Matrix quickpow(int num)
{
Matrix m = P, q = I;
while (num >= )
{
if (num & )
q = matrixmul(q,m);
num = num >> ;
m = matrixmul(m,m);
}
return q;
} int main()
{
int i,j,o;
//freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d%d",&n,&k)!=EOF)
{
if(n== && k==) break;
memset(d,,sizeof(d));
memset(f,,sizeof(f));
memset(g,,sizeof(g));
memset(h,,sizeof(h));
ans=;
for(i=;i<=n;i++){
for(j=;j<=k;j++){
scanf("%d",&a[i][j]);
}
} for(i=;i<=k;i++){
for(j=;j<=n;j++){
scanf("%d",&b[i][j]);
}
} for(i=;i<=k;i++){
for(o=;o<=k;o++){
for(j=;j<=n;j++){
d[i][o]+=(b[i][j]*a[j][o])%;
}
d[i][o]%=;
P.m[i][o]=d[i][o];
}
} e=quickpow(n*n-); for(i=;i<=n;i++){
for(o=;o<=k;o++){
for(j=;j<=k;j++){
f[i][o]+=(a[i][j]*e.m[j][o])%;
}
f[i][o]%=;
}
} for(i=;i<=n;i++){
for(o=;o<=n;o++){
for(j=;j<=k;j++){
g[i][o]+=(f[i][j]*b[j][o])%;
}
g[i][o]%=;
}
}
/*
for(i=1;i<=n;i++){
for(o=1;o<=n;o++){
for(j=1;j<=n;j++){
h[i][o]+=(g[i][j]*g[j][o])%6;
}
h[i][o]%=6;
}
} */ for(i=;i<=n;i++){
for(o=;o<=n;o++){
ans+=g[i][o];
}
}
printf("%d\n",ans); } return ;
}

hdu 4965 矩阵快速幂 矩阵相乘性质的更多相关文章

  1. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  2. POJ 3734 Blocks(矩阵快速幂+矩阵递推式)

    题意:个n个方块涂色, 只能涂红黄蓝绿四种颜色,求最终红色和绿色都为偶数的方案数. 该题我们可以想到一个递推式 .   设a[i]表示到第i个方块为止红绿是偶数的方案数, b[i]为红绿恰有一个是偶数 ...

  3. 矩阵快速幂/矩阵加速线性数列 By cellur925

    讲快速幂的时候就提到矩阵快速幂了啊,知道是个好东西,但是因为当时太蒟(现在依然)没听懂.现在把它补上. 一.矩阵快速幂 首先我们来说说矩阵.在计算机中,矩阵通常都是用二维数组来存的.矩阵加减法比较简单 ...

  4. POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 27277   Accepted:  ...

  5. 培训补坑(day10:双指针扫描+矩阵快速幂)

    这是一个神奇的课题,其实我觉得用一个词来形容这个算法挺合适的:暴力. 是啊,就是循环+暴力.没什么难的... 先来看一道裸题. 那么对于这道题,显然我们的暴力算法就是枚举区间的左右端点,然后通过前缀和 ...

  6. 快速幂 & 矩阵快速幂

    目录 快速幂 实数快速幂 矩阵快速幂 快速幂 实数快速幂 普通求幂的方法为 O(n) .在一些要求比较严格的题目上很有可能会超时.所以下面来介绍一下快速幂. 快速幂的思想其实是将数分解,即a^b可以分 ...

  7. 矩阵快速幂模板(pascal)

    洛谷P3390 题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格 ...

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

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

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

随机推荐

  1. 关于用终端运行php来测试推送的问题

    照网上的方法,合并好了证书的pem,密码也是对的,然后也写好了推送用的php文件,在终端里php这个文件,报错报错内容是:Warning: stream_socket_client(): SSL op ...

  2. 与调试器共舞 - LLDB 的华尔兹

    你是否曾经苦恼于理解你的代码,而去尝试打印一个变量的值? 1 NSLog(@"%@", whatIsInsideThisThing); 或者跳过一个函数调用来简化程序的行为? 1 ...

  3. 创建一个文件夹用于写入UTF-8编码的文件

    实现效果: 知识运用: File类的CreateText方法 StreamWriter类的WriteLine方法 实现代码: private void button2_Click(object sen ...

  4. webpack打包性能分析

    1. 如何定位webpack打包速度慢的原因 首先需要定位webpack打包速度慢的原因,才能因地制宜采取合适的方案,我们可以在终端输入: webpack --profile --json > ...

  5. 【page-monitor 前端自动化 上篇】初步调研

    转载文章:来源(靠谱崔小拽) 前端自动化测试主要在于:变化快,不稳定,兼容性复杂:故而,想通过较低的成本维护较为通用的自动化case比较困难.本文旨在通过page-monitor获取和分析dom结构, ...

  6. mysql启动提示mysql.host 不存在,启动失败的解决方法

    图示: 日志: 190625 10:48:42 InnoDB: Started; log sequence number 0 130207190625 10:48:42 [ERROR] Fatal e ...

  7. bootstrap table 保留翻页选中数据

    $(function () { $('#exampleTable').on('uncheck.bs.table check.bs.table check-all.bs.table uncheck-al ...

  8. 初涉k-d tree

    听说k-d tree是一个骗分的好东西?(但是复杂度差评??? 还听说绍一的kdt常数特别小? KDT是什么 KDT的全称是k-degree tree,顾名思义,这是一种处理多维空间的数据结构. 例如 ...

  9. [LUOGU] P2543 [AHOI2004]奇怪的字符串

    LCS //Writer:GhostCai && His Yellow Duck #include<iostream> #include<cstring> #d ...

  10. DNS服务-了解篇

    简介 DNS是用来名字解析的,名字解析成IP地址,IP地址解析成名字,正反操作,有服务器端和客户端即 S/C DNS是应用层协议,基于UDP/53.TCP/53端口,缺一不可 分为正向解析和反向解析/ ...