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

后来发现会超时,超就超在  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. vim学习--usr03 moving around

    Word movement 小写的"w""ge""e""b" w表示向后移动到一个单词开头 ge表示向前移动到一个单词末 ...

  2. day15-Python运维开发基础(json序列化模块、random / time / zipfile / OS模块函数)

    1. json序列化模块 # ### json """ 所有编程语言都能够识别的数据格式叫做json,是字符串 json: 将数据类型序列化成字符串 pickle:将数据 ...

  3. GIMP

    1. 认识GIMP 2. GIMP与Photoshop的对比 3. GIMP官方手册教程 4. 2本GIMP的外文书下载 5. 2个外部入门教程 6. 其他相关软件 1. 认识GIMP GIMP是可用 ...

  4. Django 连接 Mysql (8.0.16) 失败

    首先,确认数据库配置正确无误: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # or use: mysql.con ...

  5. CH8 IO库

    8.1 #include <iostream> #include <string> #include <sstream> #include <fstream& ...

  6. vim的几种模式

    Normal Mode 普通模式 功能:在这种模式下可以移动光标等. 进入:默认进入vim之后,处于这种模式.在其他模式下狂按ESC后进入此模式. Visual Mode 可视模式 功能:在这种模式下 ...

  7. HihoCoder第一周与POJ3974:最长回文字串

    这个题目是hihoCoder第一周的题目,自己打算从第一周开始做起,不知道能追上多少,更不知道这一篇写完,下一篇会是什么时候... 题意很简单. 输入: abababa aaaabaa acacdas ...

  8. mysql日常小总结(其实就今天)

    联表查询: SELECT t1.user_Name  FROM t_user AS t1   ,  t_comment AS t2 WHERE t2.user_id=t1.id 结果如图: 加上GRO ...

  9. Selenium -- ActionChains().move_by_offset() 卡顿的解决方法

    测试运行时间 运行时间 发现每次0.5秒,此时需要修改默认的时间 打开Python安装目录下的Lib\site-packages\selenium\webdriver\common\actions\p ...

  10. JS中的Map对象

    1,js创建map对象 var map = new Map(); 2.将键值对放入map对象 map.set("key",value) map.set("key1&quo ...