一开始看这个题目以为是个裸的矩阵快速幂的题目,

后来发现会超时,超就超在  M = C^(N*N). 这个操作,而C本身是个N*N的矩阵,N最大为1000。

但是这里有个巧妙的地方就是 C的来源其实 是= A*B, A为一个N*k的矩阵,B为一个k*N的矩阵,k最大为10,突破的就在这里,矩阵的结合律要用起来

即我先不把A*B结合,我先把B*A结合,这样M不是要C^N*N吗,就先把里面N*N个(B*A)算出来,就10*10再乘以logN*N即可。最后再两端乘一下A和B即可

也挺机智的,我没想到结合律。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL __int64
using namespace std;
LL matA[1010][10],matB[10][1010];
LL matC[1010][10];
LL MM[1010][1010];
int n,k;
struct Mat
{
LL mat[10][10];
}E;
Mat operator *(Mat a,Mat b)
{
Mat c;
for (int i=0;i<k;i++)
for (int j=0;j<k;j++){
c.mat[i][j]=0;
for (int w=0;w<k;w++){
c.mat[i][j]+=a.mat[i][w]*b.mat[w][j];
c.mat[i][j]%=6;
}
}
return c;
}
Mat operator ^(Mat a,int x)
{
Mat c=E;
for (int i=x;i;i>>=1){
if (i&1){
c=c*a;
}
a=a*a;
}
return c;
}
int main()
{
memset(E.mat,0,sizeof E.mat);
for (int i=0;i<10;i++) E.mat[i][i]=1;
while (scanf("%d%d",&n,&k)!=EOF)
{
if (n==0 && k==0) break;
for (int i=0;i<n;i++)
for (int j=0;j<k;j++) scanf("%I64d",&matA[i][j]);
for (int i=0;i<k;i++)
for (int j=0;j<n;j++) scanf("%I64d",&matB[i][j]);
Mat a;
for (int i=0;i<k;i++)
for (int j=0;j<k;j++){
a.mat[i][j]=0;
for (int w=0;w<n;w++){
a.mat[i][j]+=matB[i][w]*matA[w][j];
}
//cout<<i<<" aaa "<<j<<" "<<a.mat[i][j]<<endl;
} Mat M=a^(n*n-1); for (int i=0;i<n;i++)
for (int j=0;j<k;j++){
matC[i][j]=0;
for (int w=0;w<k;w++)
matC[i][j]+=matA[i][w]*M.mat[w][j];
// cout<<matC[i][j]<<" Matc "<<endl;
}
int ans=0;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++){
MM[i][j]=0;
for (int w=0;w<k;w++){
MM[i][j]+=matC[i][w]*matB[w][j];
}
// cout<<MM[i][j]<<" qwe "<<endl;
ans+=MM[i][j]%6;
}
printf("%d\n",ans);
}
return 0; }

  

HDU_4965 Fast Matrix Calculation 2014多校9 矩阵快速幂+机智的矩阵结合律的更多相关文章

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

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

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

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

  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. HDU 4965 Fast Matrix Calculation(矩阵高速幂)

    HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...

  5. Fast Matrix Calculation HDU - 4965

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

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

  7. hdu 4965 Fast Matrix Calculation

    题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...

  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 5015 233 Matrix(网络赛1009) 矩阵快速幂

    先贴四份矩阵快速幂的模板:http://www.cnblogs.com/shangyu/p/3620803.html http://www.cppblog.com/acronix/archive/20 ...

随机推荐

  1. java并发:初探消费者和生产者模式

    消费者和生产者模式 用继承Thread方式,用wait和notifyAll方法实现. 消费者和生产者模式的特点 1. 什么时候生产:仓库没有满的时候,生产者这可以生产,消费者也可以消费,仓库满的时候停 ...

  2. 题解:luogu P3909

    这个题拖了快三个月了,只因缺个快速乘(气愤.jpg). 题目链接:P3909 异或之积 你确定没人用前缀和,后缀和吗? 蒟蒻想法与众不同! 我们实验\(A[]={1,2,3,4}\). 这里计不乘6时 ...

  3. Ubuntu flatabulous 主题

    在终端输入以下指令 sudo apt-get update sudo apt-get upgrade sudo apt-get install unity-tweak-tool//安装unity tw ...

  4. 【Game学习随笔01】挑战任务01

    今天是2020年2月6日,时间过得好快,以至于我在写到时间会下意识写成2019年…… 看来全国肺炎情况进一步升温了,以至于我家所在的小区进行了命令封锁通知,所以出行不再像以前那么自由了,不管怎样,给战 ...

  5. Masonry与UITableView+FDTemplateLayoutCell搭配使用

    打个小广告:本人开发了一个宠物相关的App,欢迎大家下载体验~ 下载二维码: 进入正文: 之前发过一篇博客,也是对这两个的练习使用,但是之后遇到些问题,所以删除重写了.抱歉 Masonry是一款轻量级 ...

  6. 五 Hibernate的其他API,Query&Criteria&SQLQuery

    Query Criteria SQLQuery Query接口:用于接收HQL,用于查询多个对象 HQL:Hibernate Query Language  Query条件查询: Query分页查询: ...

  7. Matplotlib 多个图形

    章节 Matplotlib 安装 Matplotlib 入门 Matplotlib 基本概念 Matplotlib 图形绘制 Matplotlib 多个图形 Matplotlib 其他类型图形 Mat ...

  8. Uncaught Error: Bootstrap's JavaScript requires jQuery version 1.9.1 or higher, but lower than version 3

    前端导入静态页面的时候有一个报错,主要问题是冲突了 Uncaught Error: Bootstrap's JavaScript requires jQuery version 1.9.1 or hi ...

  9. NO19 优化Linux系统--重要开机自启动服务--关闭自启动项

    **如何优化Linux系统: 1   不用root,添加普通用户,通过sudo授权管理.2   更改默认的远程连接SSH服务端口及禁止root用户远程连接.3   定时自动更新服务器时间.4   配置 ...

  10. HDFS 命令行基本操作

    1.hdfs命令行 (1)查看帮助 hdfs dfs -help (2)查看当前目录信息 hdfs dfs -ls / (3)上传文件 hdfs dfs -put /本地路径 /hdfs路径 (4)剪 ...