POJ 3318 Matrix Multiplication(随机算法)

时间:2021-07-30 16:27:27

题目链接

随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机。

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <ctime>
#include <cmath>
using namespace std;
#define LL __int64
int a[][],b[][],c[][];
int main()
{
int i,j,k,flag,n;
srand((unsigned)time());
while(scanf("%d",&n)!=EOF)
{
for(i = ; i <= n; i ++)
{
for(j = ; j <= n; j ++)
scanf("%d",&a[i][j]);
}
for(i = ; i <= n; i ++)
{
for(j = ; j <= n; j ++)
scanf("%d",&b[i][j]);
}
for(i = ; i <= n; i ++)
{
for(j = ; j <= n; j ++)
scanf("%d",&c[i][j]);
}
flag = ;
for(i = ; i <= &&flag; i ++)
{
LL temp = ;
int x,y;
x = rand()%n + ;
y = rand()%n + ;
for(k = ; k <= n; k ++)
{
temp += a[x][k]*b[k][y];
}
if(temp != c[x][y])
flag = ;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return ;
}