码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 题目3318 Matrix Multiplication(快速判断矩阵乘是否正确)

时间:2015-05-18 09:07:56      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

Matrix Multiplication
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17132   Accepted: 3732

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.

Source

POJ Monthly--2007.08.05, qzc

题目大意:就是给你三个矩阵a,b,c,问是否a*b=c

ac代码

#include<stdio.h>
#include<string.h>
__int64 a[1010][1010],b[1010][1010],c[1010][1010],e[1010],temp[1010],f[1010];
int check(int n)
{
	int i,j;
	memset(temp,0,sizeof(temp));
	memset(f,0,sizeof(f));
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			temp[i]+=a[j][i]*(j+1);
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			f[i]+=b[j][i]*temp[j];
	for(i=0;i<n;i++)
		if(e[i]!=f[i])
			return 0;
	return 1;
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int i,j;
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				scanf("%I64d",&a[i][j]);
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				scanf("%I64d",&b[i][j]);
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				scanf("%I64d",&c[i][j]);
		memset(e,0,sizeof(e));
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				e[i]+=c[j][i]*(j+1);
		if(check(n))
		{
			printf("YES\n");
		}
		else
			printf("NO\n");
	}
}


POJ 题目3318 Matrix Multiplication(快速判断矩阵乘是否正确)

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/45796823

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!