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. Objective-C中的命名前缀说明

    http://www.cnblogs.com/dhui69/p/6410134.html __kindof __kindof 这修饰符还是很实用的,解决了一个长期以来的小痛点,拿原来的 UITable ...

  2. k8s学习目录

    目录 K8S基础部分 基础部分 5 秒创建 k8s 集群[转] k8s 核心功能[转] k8s 重要概念[转] 部署 k8s Cluster(上)[转] 部署 k8s Cluster(下)[转] Ku ...

  3. BestCoder Round#15 1001-Love

    http://acm.hdu.edu.cn/showproblem.php?pid=5082 Love Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  4. ubuntu 升级到5.1kernel,打开bbr

    apt-get -f install wget -c https://kernel.ubuntu.com/~kernel-ppa/mainline/v5.1/linux-headers-5.1.0-0 ...

  5. PAT (Advanced Level) Practise - 1096. Consecutive Factors (20)

    http://www.patest.cn/contests/pat-a-practise/1096 Among all the factors of a positive integer N, the ...

  6. PAT (Basic Level) Practise (中文)-1036. 跟奥巴马一起编程(15)

    PAT (Basic Level) Practise (中文)-1036. 跟奥巴马一起编程(15)  http://www.patest.cn/contests/pat-b-practise/103 ...

  7. Fortran学习笔记5(数组Array)

    数组的声明方式 一维数组 二维数组 多维数组 数组索引值的改变 自定义类型的数组定义 对数组内容的设置 利用隐含式循环设置数组初值 对整个数组操作 对部分数组的操作 where函数 Forall函数 ...

  8. 【dp】数字游戏&寒假祭

    区间DP 题目描述 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易.游戏是这样的,在你面前有一圈整数(一共n个),你要按 ...

  9. 【Git版本控制】git---从已有分支拉出新的分支

    参考博文:git---从已有分支拉出新分支

  10. (30)zabbix Trapper 监控项配置

    概述 zabbix获取数据有超时时间,如果一些数据需要执行比较长的时间才能获取的话,那么zabbix会出现异常,考虑到这种情况,zabbix增加了Trapper功能,客户端自己提交数据给zabbix, ...