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.

矩阵快速幂

 #include<stdio.h>
#include<string.h>
#include<math.h>
typedef long long ll;
const int mod=; struct mat{
int r,c;
int m[][]; //经测试最大开成590*590的 ll 型矩阵
mat(){}
mat(int r,int c):r(r),c(c){}
void clear(){
memset(m,,sizeof(m));
} mat operator+(mat a)const{
mat ans(r,c);
for(int i=;i<=r;i++){
for(int j=;j<=c;j++){
ans.m[i][j]=(m[i][j]+a.m[i][j])%mod;
}
}
return ans;
} mat operator*(mat a)const{
mat tmp(r,a.c);
int i,j,k;
for(i=;i<=tmp.r;i++){
for(j=;j<=tmp.c;j++){
tmp.m[i][j]=;
for(k=;k<=c;k++){
tmp.m[i][j]=(tmp.m[i][j]+(m[i][k]*a.m[k][j])%mod)%mod;
}
}
}
return tmp;
} mat operator^(int n)const{ //需要时可以用 ll n,注意运算符优先级比较低,多用括号;
mat ans(r,r),tmp(r,r);
memcpy(tmp.m,m,sizeof(tmp.m));
ans.clear();
for(int i=;i<=ans.r;i++){
ans.m[i][i]=;
}
while(n){
if(n&)ans=ans*tmp;
n>>=;
tmp=tmp*tmp;
}
return ans;
} void print()const{
for(int i=;i<=r;i++){
for(int j=;j<=c;j++){
printf("%d",m[i][j]);
if(j==c)printf("\n");
else printf(" ");
}
}
} }; int m1[][],m2[][],tmp[][],tmp2[][],tmp3[][]; int main(){
int n,k;
while(scanf("%d%d",&n,&k)!=EOF&&n+k){
int i,j,p;
for(i=;i<=n;i++){
for(j=;j<=k;j++)scanf("%d",&m1[i][j]);
}
for(i=;i<=k;i++){
for(j=;j<=n;j++)scanf("%d",&m2[i][j]);
}
for(i=;i<=k;i++){
for(j=;j<=k;j++){
tmp[i][j]=;
for(p=;p<=n;p++){
tmp[i][j]+=m2[i][p]*m1[p][j];
}
tmp[i][j]%=;
}
}
mat a(k,k);
memcpy(a.m,tmp,sizeof(tmp));
a=(a^(n*n-));
memcpy(tmp,a.m,sizeof(tmp));
for(i=;i<=n;i++){
for(j=;j<=k;j++){
tmp2[i][j]=;
for(p=;p<=k;p++){
tmp2[i][j]+=m1[i][p]*tmp[p][j];
}
tmp2[i][j]%=;
}
}
for(i=;i<=n;i++){
for(j=;j<=n;j++){
tmp3[i][j]=;
for(p=;p<=k;p++){
tmp3[i][j]+=tmp2[i][p]*m2[p][j];
}
tmp3[i][j]%=;
}
}
int ans=;
for(i=;i<=n;i++){
for(j=;j<=n;j++)ans+=tmp3[i][j];
}
printf("%d\n",ans);
}
return ;
}

hdu4965 Fast Matrix Calculation 矩阵快速幂的更多相关文章

  1. HDU 4965 Fast Matrix Calculation 矩阵快速幂

    题意: 给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\) ...

  2. Fast Matrix Calculation 矩阵快速幂

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...

  3. HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂

    题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...

  4. hdu 4965 Fast Matrix Calculation(矩阵高速幂)

    题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...

  5. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

  6. ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)

    Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...

  7. bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希

    题目 给定矩阵A, B和模数p,求最小的正整数x满足 A^x = B(mod p). 分析 与整数的离散对数类似,只不过普通乘法换乘了矩阵乘法. 由于矩阵的求逆麻烦,使用 $A^{km-t} = B( ...

  8. HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律

    一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...

  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. 服务注册和发现(Consul)

    使用Consul提供注册和发现服务 什么是 Consul Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其它分布式服务注册与发现的方案,Consul ...

  2. Android 音视频深入 五 完美的录视频(附源码下载)

    本篇项目地址,名字是录视频,求star https://github.com/979451341/Audio-and-video-learning-materials 这一次的代码录视频在各个播放器都 ...

  3. es6新增的math函数有哪些

    Math.trunc():用于去除一个数的小数部分,返回整数部分. Math.sign():用来判断一个数到底是正数.负数.还是零. Math.cbrt():用于计算一个数的立方根. Math.hyp ...

  4. 一个canvas的demo

    该demo放于tomcat下运行,否则出现跨域错误 <!DOCTYPE html> <html> <head> <meta charset="utf ...

  5. Saiku缓存处理(七)

    Saiku缓存处理方案 Saiku默认是从缓存中读取数据的(如果缓存中有数据的话),所以用户看到的数据不一定是最新的,如果需要看到最新的的数据需要手动刷新数据或者更改配置信息. Saiku获取实时数据 ...

  6. SqlServer2008备份与还原(完整图示版)

    一.备份 1.在需要备份的数据库上,右键——任务——备份,如下: 2.选择备份到哪个路径和备份名字: 点击“添加”,如下, 3.上面点击“确定”后,回到第一个页面,选中刚才添加的路径和文件名 4.左上 ...

  7. RabbitMQ 消息应答机制(message acknowledgments)

    消息应答机制是一个很重要的功能,它能保证消息队列中的某个消息是否被成功处理.如果RabbitMQ server收到Consumer端发来的应答信号,就会将Consumer刚才处理的消息删除,并发送下一 ...

  8. AVL平衡二叉树实现,图解分析,C++描述,完整可执行代码

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  9. translclude

    .transclude:true 启用transclude,启用以后,有两个地方会发生变化: ①.使用指令的元素内部的所有内容都会被保存起来.不妨先把这一段内容称为一坨. 比如指令元素是这样的: &l ...

  10. win10下安装scala流程及问题

    第一步:Java 设置 检测方法前文已说明,这里不再描述. 如果还为安装,可以参考我们的Java 开发环境配置. 接下来,我们可以从 Scala 官网地址 http://www.scala-lang. ...