HDU_4965 Fast Matrix Calculation 2014多校9 矩阵快速幂+机智的矩阵结合律
一开始看这个题目以为是个裸的矩阵快速幂的题目,
后来发现会超时,超就超在 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 矩阵快速幂+机智的矩阵结合律的更多相关文章
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
- HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂
题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...
- hdu 4965 Fast Matrix Calculation(矩阵高速幂)
题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...
- HDU 4965 Fast Matrix Calculation(矩阵高速幂)
HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...
- 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 ...
- 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 ...
- hdu 4965 Fast Matrix Calculation
题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...
- 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 ...
- HDU 5015 233 Matrix(网络赛1009) 矩阵快速幂
先贴四份矩阵快速幂的模板:http://www.cnblogs.com/shangyu/p/3620803.html http://www.cppblog.com/acronix/archive/20 ...
随机推荐
- Screen命令让Linux shell在后台运行
#screen ping ychack.com //挂置后台ping本站 #screen ping baidu.com //挂置后台ping百度 #screen -ls //列出进程 #screen ...
- JS控制输入框和文本框字数
文本框限制字数: HTML结构: JS: $('.advert-title').each(function(){ var TXTlength = $(this).text().length; // 当 ...
- Day3-L-Cup HDU2289
The WHU ACM Team has a big cup, with which every member drinks water. Now, we know the volume of the ...
- Day2-O-Coloring a Tree CodeForces-902B
You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the ...
- win10下python3安装深度学习一般要用的库
matplotlib :绘图库 seaborn:基于matplotlib的图形可视化包 numpy:函数.矩阵运算库 pandas :基于numpy的结构化数据分析库 首先看一下cmd能不能使用pip ...
- JAVA地址栏重写很详细
这几天蛋疼.看看别人url重写是怎么搞的..1.解释下什么事url重写,以及它的优缺点: URL重写,其实就是把带一大堆参数的url,变成一个看上去很规矩的url.例:/viewthread.jsp? ...
- git/github error: failed to push some refs to 'https://github.com/shenhaha/cloudletter.git'
git 提交代码到github上报如下错误 报错分析: 解决方法: 关闭这两个设置 再次提交代码 success
- Vue项目中v-for无法渲染数据
在Vue项目中,我们想要实现下面的布局效果 后端返回的数据格式如下,可以看出产品列表五张图的数据位于同一个数组中 而我的html结构如下: 我希望直接渲染左边一张大图,然后右边的四张小图通过v-for ...
- OBU设备非接触式读卡方案:SI522
传统收费站将成历史!全部转为ETC系统 当高速人工收费已经成为我们驾驶出行的习惯后,我们发现,高速人工收费带来低效率.长等待以及落后性等缺点逐渐给人们出行带来不便.伴随着我国汽车保有量的逐年递增,高速 ...
- 清北学堂模拟赛2 T2 ball
题目大意: 多组数据,每组给定n,m,表示将n个小球放进m个箱子,每个小球均有两个箱子(可能相同)可放,求所有小球均放好的方案mod998244353的总数. 思路: 算是我和题解思路肥肠相近的一道题 ...