POJ 3318 - Matrix Multiplication 第一次用随机化解决问题...

     随机化还是很厉害的...印象最深的是以前手写快排~~一般加个随机化会使耗时不受输入数据的..时间更加稳定

     这个题是人品题了...开始交了好多遍都过不了..多交几次终于过了...


Program:

 

#include<iostream>
#include<stack>
#include<queue>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<time.h>
#include<cmath>
#define ll long long
#define oo 1000000007
#define MAXN 505
using namespace std; 
int n,M[3][MAXN][MAXN];
bool judge()
{
      int t,i,j,x,y,p,d; 
      for (t=1;t<=60000;t++)
      { 
             y=rand()%n;
             x=rand()%n;
             d=0;
             for (p=0;p<n;p++)
                d+=M[0][y][p]*M[1][p][x];
             if (d!=M[2][y][x]) return false;
      }
      return true;
} 
int main()
{
      int i,j,x; 
      scanf("%d",&n);
      for (x=0;x<3;x++)
         for (i=0;i<n;i++)
           for (j=0;j<n;j++)
              scanf("%d",&M[x][i][j]);
      srand((unsigned)time(NULL));
      if (judge()) printf("YES\n");
             else  printf("NO\n");
      return 0;
}


 

 

posted @ 2013-07-24 21:09  javawebsoa  Views(171)  Comments(0Edit  收藏  举报