POJ 3318-matrix multiplication (compression matrix with O (n^2) method to find matrix equality)

Source: Internet
Author: User

Title Address: POJ 3318
Test instructions: There are 3 n*n matrix a,b,c, q AB is equal to C.
Idea: The topic description is very simple, is to use matrix multiplication, but it is obvious that the time complexity of matrix multiplication is O (n^3), obviously timed out. How does that improve? is to use the compression matrix method:
Set matrix R is a matrix of 1*n, according to the nature of the matrix, if a*b*r=c*r, then a*b=c. As can be seen, although more than a matrix, but the time complexity of the O (n^2). So the question is how the determinant of this r is set, the random algorithm used by someone, but the random algorithm may have errors on the key points, you can set R as an ascending sequence {... }。

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>//#pragma comment (linker, "/stack:102400000,102400000")using namespace STD;typedef Long LongLL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-6;intmaxn=510; LL a[510][510]; LL b[510][510]; LL ra[510]; LL c[510][510]; LL rab[510]; LL rc[510];intCheckintN) {intI,j; for(i=0; i<n;i++) for(j=0; j<n;j++) ra[i]+=a[j][i]* (j+1); for(i=0; i<n;i++) for(j=0; j<n;j++) Rab[i]+=ra[j]*b[j][i]; for(i=0; i<n;i++) for(j=0; j<n;j++) rc[i]+=c[j][i]* (j+1);/*for (i=0;i<n;i++) printf ("%lld", Rab[i]);        printf ("\ n");        for (i=0;i<n;i++) printf ("%lld", Rc[i]);        printf ("\ n"); */     for(i=0; i<n;i++) {if(Rab[i]!=rc[i])return 0; }return 1;}intMain () {intN while(~scanf("%d", &n)) {memset(RA,0,sizeof(RA));memset(Rab,0,sizeof(RAB));memset(RC,0,sizeof(RC)); for(intI=0; i<n;i++) for(intj=0; j<n;j++)scanf("%lld", &a[i][j]); for(intI=0; i<n;i++) for(intj=0; j<n;j++)scanf("%lld", &b[i][j]); for(intI=0; i<n;i++) for(intj=0; j<n;j++)scanf("%lld", &c[i][j]);if(check (n))puts("YES");Else            puts("NO"); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3318-matrix multiplication (compression matrix with O (n^2) method to find matrix equality)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.