题意:矩阵乘法,在乘的过程中每个元素都取模3
在比赛时,我一直在想怎么才能把O(n*n*n)的时间复杂度给降下来,可是一直没想到好的办法,后来看了题解后才知道原来时间复杂度还是O(n*n*n),只是优化了一点而已
代码如下:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<stdio.h>
#include<bitset>
#include<stdlib.h>
#include<math.h>
#define N 1000
#define inf 0x7ffffff
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
int a[N][N],b[N][N],c[N][N];
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int i,j,k;
int n;
while(scanf("%d",&n) != EOF)
{
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
{
scanf("%d",&a[i][j]);
a[i][j] %= 3;
}
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
{
scanf("%d",&b[i][j]);
b[i][j] %= 3;
}
memset(c,0,sizeof(c));
for(i = 0; i < n; i++)
for(k = 0; k < n; k++)
{
if(a[i][k] == 0)
continue;
for(j = 0; j < n; j++)
c[i][j] = (c[i][j] + a[i][k]*b[k][j])%3;
}
for(i = 0; i < n; i++){
int flag = 0;
for(j = 0; j < n; j++)
if(flag == 0)
{
printf("%d",c[i][j]);
flag = 1;
}
else printf(" %d",c[i][j]);
printf("\n");
}
}
return 0;
}