Matrix multiplication

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1289    Accepted Submission(s): 568

Problem Description
Given two matrices A and B of size n×n, find the product of them.

bobo hates big integers. So you are only asked to find the result modulo 3.

 
Input
The input consists of several tests. For each tests:

The first line contains n (1≤n≤800). Each of the following n lines
contain n integers -- the description of the matrix A. The j-th integer
in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤Aij,Bij≤109).

 
Output
For each tests:

Print n lines. Each of them contain n integers -- the matrix A×B in similar format.

 
Sample Input
1
0
1
2
0 1
2 3
4 5
6 7
 
Sample Output
0
0 1
2 1
 
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
#define MAX 810
int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX]; int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&a[i][j]);
a[i][j]=a[i][j]%;
}
}
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&b[i][j]);
b[i][j]=b[i][j]%;
}
} memset(c,,sizeof(c));
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(a[i][j]==) continue;
for(int k=;k<n;k++)
{
c[i][k]+=a[i][j]*b[j][k]; 如果这里 c[i][k]+=(a[i][j]*b[j][k])%3;就会超时
}
}
}
for(int i = ; i < n; i++)
{
for(int j = ; j < n; j++)
if(j == )
printf("%d", c[i][j]%);
else
printf(" %d", c[i][j]%);
printf("\n");
}
}
return ;
}
#include <iostream>
#include<stdio.h>
#include<vector>
///他把所有的0都忽略了,很巧妙的优化,aa[][], bb[][]里存储的是下一个不为0的位置:
#include<queue>
#include<stack>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
#define LL long long
#define gcd(a,b) (b==0?a:gcd(b,a%b))
#define lcm(a,b) (a*b/gcd(a,b))
//O(n)求素数,1-n的欧拉数
#define N 100010
//A^x = A^(x % Phi(C) + Phi(C)) (mod C)
int a[][];
int aa[][];
int b[][];
int bb[][]; int c[][];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(aa,,sizeof(aa));
memset(bb,,sizeof(bb));
memset(c,,sizeof(c));
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d",&a[i][j]);
a[i][j]%=;
/// cout<<"%%%"<<a[i][j]<<endl;
}
}
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d",&b[i][j]);
b[i][j]%=;
}
}
for(int i=; i<=n; i++)
{
int x=-;
for(int j=n; j>=; j--)
{
aa[i][j]=x; ///后退了一位 所以j:n~~0
if(a[i][j]) x=j;
//cout<<"~~~~"<<a[i][j]<<' '<<aa[i][j]<<endl;///记录矩阵中0的位置 赋值给aa【】【】
}
}
for(int i=; i<=n; i++)
{
int x=-;
for(int j=n; j>=; j--)
{
bb[i][j]=x;
if(b[i][j])x=j;
}
}
for(int i=; i<=n; i++)
{
for(int j=aa[i][]; j!=-; j=aa[i][j])
{
for(int k=bb[j][]; k!=-; k=bb[j][k]) ///a==0时 对应b那一列乘a==0 所以根据bb【j】来转变
c[i][k]+=a[i][j]*b[j][k];
}
}
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
printf("%d",c[i][j]%);
if(j!=n)printf(" ");
else printf("\n");
}
}
}
return ;
}

HDU 4920 矩阵乘积 优化的更多相关文章

  1. Hdu 4920矩阵乘法(内存访问的讲究)

    题目链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K ( ...

  2. hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场

    Matrix multiplication                                                                           Time ...

  3. hdu 5068 线段树维护矩阵乘积

    http://acm.hdu.edu.cn/showproblem.php?pid=5068 题意给的略不清晰 m个询问:从i层去j层的方法数(求连段乘积)或者修改从x层y门和x+1层z门的状态反转( ...

  4. HDU 5863 cjj's string game (矩阵乘法优化递推)

    题目大意:用k种字符构建两个长度为n的字符串(每种字符有无限多个),要求对应位置字符相同的连续子串最长长度为m,问方法数. 其中k,n,m是输入,n(1<=n<=1000000000), ...

  5. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  6. 卷积、矩阵乘积、高斯模糊滤波(降噪)、空域计算(2D卷积计算)、频域计算(FFT)的理解

    矩阵乘积:对应行列对应元素相乘的和组成新的矩阵 两个矩阵的乘法仅当第一个矩阵A的列数和另一个矩阵B的行数相等时才能定义.如A是m×n矩阵和B是n×p矩阵,它们的乘积C是一个m×p矩阵 并将此乘积记为: ...

  7. HDU 4920.Matrix multiplication-矩阵乘法

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  8. HDU 4920(杭电多校训练#5 1010 题) Matrix multiplication(不知道该挂个什么帽子。。。)

    题目地址:pid=4920">HDU 4920 对这个题简直无语到极点. . .竟然O(n^3)的复杂度能过....方法有三.. 1:进行输入优化和输出优化. . (前提是你的输入优化 ...

  9. HDU 4920 Matrix multiplication(bitset)

    HDU 4920 Matrix multiplication 题目链接 题意:给定两个矩阵,求这两个矩阵相乘mod 3 思路:没什么好的想法,就把0的位置不考虑.结果就过了.然后看了官方题解,上面是用 ...

随机推荐

  1. BZOJ 3007 [SDOI2012]拯救小云公主 - 对偶图 + 并查集

    Solution 答案具有单调性, 显然可以二分答案. 有两个注意点 : 英雄是可以随便走的, 也就是不是网格图... 还有坐标不能小于$1$ QAQ 开始时英雄在左下角, 公主在右上角, 我们反过来 ...

  2. mfc获取exe的版本信息

    CString GetFileVersion(const CString& sTargetFileName){ DWORD nInfoSize = 0, dwHandle = 0; nInfo ...

  3. eclipse中tomcat调试正确关联源码

    1.build path中jar包关联本地源码 2.tomcat中添加source关联工程lib下的jar包 以上两步即可. 可解决tomcat直接关联本地源码debug时无法计算表达式的情况. 错误 ...

  4. Spring Environment(三)生命周期

    Spring Environment(三)生命周期 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...

  5. Java中的逆变与协变 很直接不饶弯的讲出来了

    ```java 协变 extends只能new 辈分比自己低的家伙 List<? extends Number> list001 = new ArrayList<Integer> ...

  6. IOS初级:UIwindow

    AppDelegate.h @property (strong, nonatomic) UIWindow *window; AppDelegate.m - (BOOL)application:(UIA ...

  7. centos6.5虚拟机每次都要ifup eth0的解决办法

    修改文件/etc/sysconfig/network-scripts/ifcfg-eth0把ONBOOT=no改ONBOOT=yes

  8. windows server 2008 - 隐藏磁盘分区 (2)

    二 创建策略并设置给指定用户 1 打开“组策略管理”(在这里创建策略) cmd下输入GPMC.MSC 打开组策略管理   随后在域上右键选择“在这个域中创建GPO并在此处链接”,输入名字“磁盘管理策略 ...

  9. C# 使用 HttpPost 请求调用 WebService

    之前调用 WebService 都是直接添加服务引用,然后调用 WebService 方法的,最近发现还可以使用 Http 请求调用 WebService.这里还想说一句,还是 web api 的调用 ...

  10. NOIP水题测试(2017082401)

    哈,水题测试又来了! 上次的水题简单吧! 答案是以单题形式发布的(旅行家的预算随后发布). 下面来看今天的题,还是水题. 时间限制:5小时 题目一:看上去就很水 题目二:比上面一题还水 题目三:数的划 ...