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 ;
}

最新文章

  1. maven 环境的配置 JAVA_HOME not found in your envirnment
  2. Java 线程异常处理器
  3. java 获取系统当前时间
  4. P2P资料
  5. BI就是报表?
  6. php 分词 —— PHPAnalysis无组件分词系统
  7. java jdbc数据库操作
  8. Ubuntu 14.04 安装 Xilinx ISE 14.7 全过程
  9. as3判断XML是否合法
  10. liunx系统磁盘满的时候如何排查
  11. Android完全退出activity
  12. 自学elastic search
  13. php常用函数搜集
  14. linux 下程序员专用搜索源码用来替代grep的软件ack(后来发现一个更快的: ag), 且有vim插件的
  15. 识别同音字词pypinyin, 分词 jieba
  16. LightOj 1248 - Dice (III)(几何分布+期望)
  17. delphi 路径函数
  18. 修改ubuntu计算机名
  19. Java Integer Cache
  20. Windows 下安装 tensorflow &amp; keras &amp; opencv 的避坑指南!

热门文章

  1. tiny cc 编译器,tinycc,变种
  2. How To Configure SAMBA Server And Transfer Files Between Linux &amp; Windows
  3. 转:Struts2返回JSON数据的具体应用范例
  4. 接口没添加@responseBody注解
  5. openssl编译安装-各种蛋疼
  6. Windows 8风格应用-触控输入
  7. C#中的String类2
  8. unity延时函数
  9. 走进JDK(三)------AbstractStringBuilder、StringBuffer、StringBuilder
  10. linux利用命令将一个盘上的所有复制到另一个盘上