HDU多校赛第9场 HDU 4965Fast Matrix Calculation【矩阵运算+数学小知识】
难度上。,,确实。。。不算难
问题是有个矩阵运算的优化
题目是说给个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【矩阵运算+数学小知识】的更多相关文章
- 【杂题总汇】HDU多校赛第十场 Videos
[HDU2018多校赛第十场]Videos 最后一场比赛也结束了…… +HDU传送门+ ◇ 题目 <简要翻译> 有n个人以及m部电影,每个人都有一个快乐值.每场电影都有它的开始.结束时间和 ...
- 牛客网多校赛第9场 E-Music Game【概率期望】【逆元】
链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...
- 2015 多校赛 第五场 1010 (hdu 5352)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5352 看题看得心好累. 题目大意: 给出 n 个点,依次执行 m 次操作:输入“1 x”时,表示将与 ...
- hdu 5373 The shortest problem(杭电多校赛第七场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5373 The shortest problem Time Limit: 3000/1500 MS (J ...
- hdu 5328 Problem Killer(杭电多校赛第四场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5328 题目大意:找到连续的最长的等差数列or等比数列. 解题思路:1.等差等比的性质有很多.其中比较重 ...
- hdu 5319 Painter(杭电多校赛第三场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5319 Painter Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 5326 Work(杭电多校赛第三场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5326 Work Time Limit: 2000/1000 MS (Java/Others) M ...
- 2015 多校赛 第五场 1006 (hdu 5348)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题目大意:给出一幅无向图,问是否存在一种方案,使得给每条边赋予方向后,每个点的入度与出度之差小于 ...
- 2015 多校赛 第三场 1002 (hdu 5317)
Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more i ...
随机推荐
- 91. ExtJS获取父子、兄弟容器元素方法
转自:https://blog.csdn.net/u014745818/article/details/44957341 1 1.当前对象的父对象(上级对象) this.ownerCt: 2.当前对象 ...
- 更新svn时出错,大概的意思是项目被锁定了
- 第16课 “远程 Git文档库” 的基础操作
16-1 “远程Git文档库” 的功能 如果 “本地Git文档库” 和 “远程Git文档库” 是存储在不同的计算机中,它们之前传送数据可以通过HTTP/HTTPS.SSH.GIT proto ...
- python中使用pip安装报错:Fatal error in launcher... 解决方法
python安装了2和3版本在 cmd 中用pip报的错误为:Fatal error in launcher:Unable to create process using 这是因为你安装了python ...
- python利用有道翻译实现“语言翻译器”的功能
import urllib.request import urllib.parse import json while True: content = input('请输入需要翻译的内容(退出输入Q) ...
- web前端利用HTML代码显示符号
HTML常用符号代码: ´ ´ © © > > µ µ ® ® & & ° ° ¡ ¡ » » ¦ ¦ ÷ ÷ ¿ ¿ ...
- Hibernate_01_初体验
hibernate开发的基本步骤: 编写配置文档hibernate.cfg.xml: 编写实体类: 生成对应实体类的映射文件并添加到配置文档中: 调用hibernate API进行测试. Hibern ...
- jQuery中容易让人困惑的东西
前言:jqueryt很灵活,太灵活了,可以说是他一个优点,也是他一个缺点,达到一种效果,十个人也许会用十种不同的方法来实现这个过程,结果一样,过程不一样,这到底是好,还是坏呢. 一,什么是jquery ...
- freemarker使用map替换字符串中的值demo2
package demo01; import java.io.IOException;import java.io.OutputStreamWriter;import java.io.StringWr ...
- Html-如何正确给table加边框
一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...