难度上。,,确实。。。不算难

问题是有个矩阵运算的优化

题目是说给个N*K的矩阵A给个K*N的矩阵B(1<=N<=1000 && 1=<K<=6),先把他们乘起来乘为C矩阵。然后算C^(N*N)

相当于

ABABABABABABAB...=(AB)^(N*N)

不如

A(BA)^(N*N-1)B

由于BA乘得K*K的矩阵,K是比較小的

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
int a[1111][1111];
int b[1111][1111];
int c[1111][1111];
int tmp[1111][1111];
int sum[1111][1111];
void TIMES(int K,int N,int M,int aa[][1111],int bb[][1111],int cc[][1111]){
memset(tmp,0,sizeof(tmp));
for(int i=1;i<=K;i++){
for(int j=1;j<=M;j++){
for(int k=1;k<=N;k++){
tmp[i][j]=(tmp[i][j]+aa[i][k]*bb[k][j]%6)%6;
}
}
}
for(int i=1;i<=K;i++)
for(int j=1;j<=M;j++)
cc[i][j]=tmp[i][j]%6;
}
void quick(int cc[][1111],int mi,int n){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i==j) sum[i][j]=1;
else sum[i][j]=0;
while(mi){
if(mi&1)
TIMES(n,n,n,cc,sum,sum);
TIMES(n,n,n,cc,cc,cc);
mi>>=1;
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("G:/in.txt","r",stdin);
//freopen("G:/myout.txt","w",stdout);
#endif
int N,K;
while(~scanf("%d%d",&N,&K)){
if(N==0 && K==0) return 0;
for(int i=1;i<=N;i++){
for(int j=1;j<=K;j++){
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<=K;i++){
for(int j=1;j<=N;j++){
scanf("%d",&b[i][j]);
}
}
TIMES(K,N,K,b,a,c);
quick(c,N*N-1,K);
TIMES(N,K,K,a,sum,sum);
TIMES(N,K,N,sum,b,sum);
int ans=0;
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
ans+=sum[i][j];
cout<<ans<<endl;
}
}

HDU多校赛第9场 HDU 4965Fast Matrix Calculation【矩阵运算+数学小知识】的更多相关文章

  1. 【杂题总汇】HDU多校赛第十场 Videos

    [HDU2018多校赛第十场]Videos 最后一场比赛也结束了…… +HDU传送门+ ◇ 题目 <简要翻译> 有n个人以及m部电影,每个人都有一个快乐值.每场电影都有它的开始.结束时间和 ...

  2. 牛客网多校赛第9场 E-Music Game【概率期望】【逆元】

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  3. 2015 多校赛 第五场 1010 (hdu 5352)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5352 看题看得心好累. 题目大意: 给出 n 个点,依次执行 m 次操作:输入“1 x”时,表示将与 ...

  4. hdu 5373 The shortest problem(杭电多校赛第七场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5373 The shortest problem Time Limit: 3000/1500 MS (J ...

  5. hdu 5328 Problem Killer(杭电多校赛第四场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5328 题目大意:找到连续的最长的等差数列or等比数列. 解题思路:1.等差等比的性质有很多.其中比较重 ...

  6. hdu 5319 Painter(杭电多校赛第三场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5319 Painter Time Limit: 2000/1000 MS (Java/Others)   ...

  7. hdu 5326 Work(杭电多校赛第三场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5326 Work Time Limit: 2000/1000 MS (Java/Others)    M ...

  8. 2015 多校赛 第五场 1006 (hdu 5348)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题目大意:给出一幅无向图,问是否存在一种方案,使得给每条边赋予方向后,每个点的入度与出度之差小于 ...

  9. 2015 多校赛 第三场 1002 (hdu 5317)

    Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more i ...

随机推荐

  1. 第14章 Wi-Fi系统应用 14.1 了解Wi-Fi系统的结构

    Android平台中Wi-Fi系统从上到下主要包括Java框架类.Android适配器库.wpa_supplicant守护进程.驱动程序和协议,这几部分的系统结构如图14-3所示. (1)Wi-Fi用 ...

  2. python-day01 pip 在线安装,标识符规则,注释,变量名,类型

    1.python第三方库安装: 在线安装:pip install 库名 pip install 库名 -i 国内源网站地址 离线安装:xxx.tar.gz/rar/zip 解压安装 2.标识符规则: ...

  3. Python 42 mysql用户管理 、pymysql模块

    一:mysql用户管理 什么是mysql用户管理 mysql是一个tcp服务器,应用于操作服务器上的文件数据,接收用户端发送的指令,接收指令时需要考虑到安全问题, ATM购物车中的用户认证和mysql ...

  4. BZOJ 4525 二分

    思路: 满足二分性质... 二分一下      就完了 //By SiriusRen #include <cstdio> #include <algorithm> using ...

  5. itext 生成pdf文档 小结(自己备忘)

    1.引入maven <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf& ...

  6. jquery.validate验证text,checkbox,radio,selected

    index.cshtml <form id="formLogin" method="post"> <div> <label for ...

  7. (转)JavaScript深入之从原型到原型链

    构造函数创建对象 我们先使用构造函数创建一个对象: function Person() { } var person = new Person(); person.name = 'Kevin'; co ...

  8. 4、scala数组

    1.Array 2.ArrayBuffer 3.遍历Array和ArrayBuffer 4.数组常见操作 1.  Array Scala中,array代表的含义与java类似,也是长度不可改变的数组. ...

  9. Ceph 文件系统的安装

    yum install -y wget wget https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz#md5=01026f87 ...

  10. ASP.NET 页面验证cookie

    using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary ...