Gym

来源:互联网 发布:sqlserver cast函数 编辑:程序博客网 时间:2024/06/06 04:05

题意:

          给出 n * n 的 A , B ,C 三个矩阵,问 A * B 是否等于

思路:

         n 太大,不能暴力。O(n^3)

         我们可以将矩阵变化压缩一下。转化为O(n^2)。

代码:

#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <ctime>using namespace std;const int maxn=1e3+100;const int mod=1e9+7;int n;int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],c1[maxn],b1[maxn];void rd(int a[][maxn]){    for (int i=0; i<n; ++i) {        for (int j=0; j<n; ++j) {            scanf("%d",&a[i][j]);        }    }}void mul(int a[][maxn],int d[]){    int tmp[maxn];    for (int i=0; i<n; ++i) {        tmp[i]=0;        for(int j=0;j<n;++j)            tmp[i]=(tmp[i]+1ll*a[i][j]*d[j])%mod;    }    for (int i=0; i<n; ++i) {        d[i]=tmp[i];    }}bool pd(int a[],int b[]){    for (int i=0; i<n; ++i) {        if (a[i]!=b[i]) {            return false;        }    }    return true;}int main() {    srand((unsigned)time(NULL));    scanf("%d",&n);                for (int i=0; i<n; ++i)            c1[i]=b1[i]=rand();        rd(a);rd(b);rd(c);        mul(b, b1);mul(c, c1);        mul(a, b1);        if (pd(b1,c1)) {            puts("YES");        }        else puts("NO");        return 0;}


0 0
原创粉丝点击