Matrix Multiplication
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18173   Accepted: 3912

Description

You are given three n × n matrices AB and C. Does the equation A × B = C hold true?

Input

The first line of input contains a positive integer n (n ≤ 500) followed by the the three matrices AB and respectively. Each matrix's description is a block of n × n integers.

It guarantees that the elements of A and B are less than 100 in absolute value and elements of C are less than 10,000,000 in absolute value.

Output

Output "YES" if the equation holds true, otherwise "NO".

Sample Input

2
1 0
2 3
5 1
0 8
5 1
10 26

Sample Output

YES

Hint

Multiple inputs will be tested. So O(n3) algorithm will get TLE.
 
试题分析:
    首先,最暴力的方法显然就是求一遍A*B,然后与C对照,但是这样效率太低
    So O(n3) algorithm will get TLE.
    所以不可取
 
    那么我们设想一个随机生成的向量V,其每个元素都在1和0中选取
    考察A*(B*v)=? C
    但是我们说只执行一遍正确性是很可悲的
    设想如果A*B!=C那么就说明A*B-C!=0
    那么说明A*B-C完了的数组至少有一个非0
    但是如果所在的这列正好V为0呢?
    我们知道:若A=B,则A*N=B*N
    但是如果反过来说A*N=B*N那么A=B,这就不对了,因为N有可能是0
    我们继续思考这个简单的问题,那么如果将A*N=B*N这个N等概率地取几次任意值,那么基本上就不会出错了
    这里也一样,因为每个元素都在1和0中选取,所以算法正确性只有1/2
    我们可以采取多试几次或者扩大向量V的取值范围来提高算法的正确性
    由于在A*B!=C的情况下,A*B-C完了的数组至少有一个非0
    但这只是极端情况,所以个人认为还是多试几次好
 
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<time.h>
#include<stdlib.h>
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int A[501][501],B[501][501],C[501][501];
int N;
int Num[1001],Num2[1001];//压缩后的A*(B*V) 压缩后的V*C
int Fk[1001];//向量V
int tmp;
int main(){
N=read(); for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
A[i][j]=read();
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
B[i][j]=read();
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
C[i][j]=read(); for(tmp=1;tmp<=60;tmp++){
memset(Fk,0,sizeof(Fk));
memset(Num,0,sizeof(Num));
memset(Num2,0,sizeof(Num2));
for(int j=1;j<=N;j++) Fk[j]=(rand()*rand()+rand())%2;//随机
for(int j=1;j<=N;j++)
for(int k=1;k<=N;k++)
Num[j]+=C[j][k]*Fk[k];//压缩C*V
for(int j=1;j<=N;j++)
for(int k=1;k<=N;k++)
Num2[j]+=B[j][k]*Fk[k];//压缩(B*V)
for(int j=1;j<=N;j++) Fk[j]=Num2[j],Num2[j]=0;
for(int j=1;j<=N;j++)
for(int k=1;k<=N;k++)
Num2[j]+=A[j][k]*Fk[k];//(B*V)*A
int L=1;
for(L=1;L<=N;L++)
if(Num[L]!=Num2[L]) break;
if(L<=N) break;
} if(tmp<=60) cout<<"NO";
else cout<<"YES";
}

  

【数学】Matrix Multiplication的更多相关文章

  1. 数学(矩阵乘法,随机化算法):POJ 3318 Matrix Multiplication

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17783   Accepted: ...

  2. hdu 4920 Matrix multiplication bitset优化常数

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

  3. 矩阵乘法 --- hdu 4920 : Matrix multiplication

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

  4. hdu4920 Matrix multiplication 模3矩阵乘法

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

  5. acdeream Matrix Multiplication

    D - Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/O ...

  6. HDU 4920 Matrix multiplication 矩阵相乘。稀疏矩阵

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

  7. Matrix multiplication hdu4920

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

  8. HDU-4920 Matrix multiplication

    矩阵相乘,采用一行的去访问,比采用一列访问时间更短,根据数组是一行去储存的.神奇小代码. Matrix multiplication Time Limit: 4000/2000 MS (Java/Ot ...

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

    Matrix multiplication                                                                           Time ...

随机推荐

  1. log4j详解(一)

    >>>>1. 概述<<<< 1.1. 背景 在应用程序中添加日志记录总的来说基于三个目的:监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统 ...

  2. linux常用命令(一)

    1.linux文件命名规则 1.除了/之外,所有字符都是可以用的. 2.有些字符最好别用,如空格,制表符,退格,@#&-,命令行操作时候可能会出现混淆. 3.避免使用.作为文件开头,linux ...

  3. Xamarin.Android Binding

    0.要绑定的jar库,需要保证编译jar使用的jdk版本,与绑定时xamarin使用的jdk版本一致. 查看编译jar的jdk版本的方法:jar解压后,a.看MANIFEST.MF  b. javap ...

  4. wifi万能钥匙自媒体平台开放注册(付注册流程)

    12月13日,有网友爆料,wifi万能钥匙自媒体开放注册,看来自媒体还没有达到饱和阶段,也印证了自媒体时代才刚刚到来.现在这个自媒体的时代,几乎大多互联网企业都开通了自己的自媒体,比较知名的像今日头条 ...

  5. Go-Agent部署与FQ教程(2016-10-28)

    这篇文章是为了帮助使用go-agent-crack来FQ的同学,主要是几个想用该项目来FQ的同学对github主页的FQ文档大力吐槽,索性写一篇详细的文档来支持一下大家,其中包含了google gae ...

  6. [ZJOI2008]树的统计

    本题是一个树链剖分裸题,由于比较菜,老是RE,后来发现是因为使用了全局变量. /************************************************************ ...

  7. 简单的jquery tab

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 出售一套Unity + Lua热更新框架代码

    出售一套Unity + Lua的客户端框架代码,功能有资源管理.网络通信.配置文件解析.热更新.文件读写.Lua加密揭秘.UI框架.打包工具.编辑器工具等,已经在多个实际项目(已上线)中使用.代码优雅 ...

  9. MySQL 在 LIMIT 条件后注入

    from:https://rateip.com/blog/sql-injections-in-mysql-limit-clause/ 此方法适用于MySQL 5.x中,在limit语句后面的注入例如: ...

  10. Bittorrent Protocol Specification v1.0 中文

    翻译:小马哥 日期:2004-5-22 BitTorrent 是一种分发文件的协议.它通过URL来识别内容,并且可以无缝的和web进行交互.它基于HTTP协议,它的优势是:如果有多个下载者并发的下载同 ...