Codeforces gym 101341I Matrix God【思维】【hash】

Input file: standard input
Output file: standard output
Time limit: 2 seconds
Memory limit: 256 megabytes

Descirption

Matrices are square tables, containing integers in rows and columns. Do you know how to multiply matrices? The product of matrices A and B of size n×n is a matrix C = AB, such that
C (i,j) =n /∑ /k=1 A(i,k)·B (k,j)
Here the brackets contain numbers of row and column, where the corresponding element of matrix is located. As it is known, multiplying matrices is not the computationally simplest task. But not for Matrix god! He can easily multiply matrices using the number of operations of order O(n2). Here and now for some incomprehensible for mortals reason he is trying to determine, is it true that AB = C for three given matrices A, B and C. For him solving this problem has no difficulties. Can you compare with him? The Matrix god gives you an advantage: determine, at least, whether or not reminders of division of each C (i,j) by 109 + 7 are found correctly.

Input

The first line contains a single integer n (1 ≤ n ≤ 1000) — the size of all matrices. Then in n lines n integers aij, separated by a space, are given (0 ≤ ai,j ≤ 109 + 6) — the elements of matrix A. In the next n lines the elements of matrix B are given in the same way, and after that in n more lines — the reminders of division of elements of matrix C by 109 + 7.

Output

Output «YES» without quotes, if the reminders of division by 109 + 7 of each element of matrix C are found correctly, otherwise output «NO» without quotes.

Examples

Input:

2
1 2
5 6
7 8
19 22
43 50

Output:

YES

Input:

3
1 2 3
4 5 6
7 8 9
1 4 7
2 5 8
3 6 9
14 32 50
32 76 122
50 122 194

Output:

NO

Input:

1 1000000006 1000000006 1

Output:

YES

题意:

给你三个矩阵,让你判断前两个矩阵相乘是否等于第三个矩阵,结果%1e9 + 7。

思路:

此题直接矩阵相乘时间复杂度是o(n3)的,数据给到1000,2秒肯定会超时,所以要想办法优化时间复杂度。题目中给出提示让你优化到o(n2),结合矩阵的相关知识,我们可以想到,构造一个1 * n的随机矩阵,将这个矩阵先都左乘到等式两边,这样将n * n的矩阵可以压成1 * n的矩阵,每一步的矩阵乘法都优化到了o(n2),问题得到解决~
注意使用long long,int会爆数据!

ac代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
//#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e3 + 5;
ll a[maxn][maxn], b[maxn][maxn], c[maxn][maxn];
ll p[maxn];
ll rlt[maxn], rl[maxn], rr[maxn];
int main()
{
    srand(19260817);
    int n;
    while(scanf("%d", &n) != EOF)
    {
        memset(rl, 0, sizeof(rl));
        memset(rlt, 0, sizeof(rlt));
        memset(rr, 0, sizeof(rr));
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                scanf("%lld", &a[i][j]);
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                scanf("%lld", &b[i][j]);
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                scanf("%lld", &c[i][j]);
        for (int i = 0; i < n; ++i) //随机造个矩阵
            p[i] = (rand() * 13 % mod + rand() * 5 % mod + 997) % mod;
        for (int i = 0; i < n; ++i)         //与A乘
            for (int j = 0; j < n; ++j)
                rlt[i] = (rlt[i] + p[j] * a[j][i]) % mod;
        for (int i = 0; i < n; ++i)         //与B乘
            for (int j = 0; j < n; ++j)
                rl[i] = (rl[i] + rlt[j] * b[j][i]) % mod;
        for (int i = 0; i < n; ++i)         //与C乘
            for (int j = 0; j < n; ++j)
                rr[i] = (rr[i] + p[j] * c[j][i]) % mod;
        int flag = 1;
        for (int i = 0; i < n; ++i)
        {
            if(rr[i] != rl[i])
            {
                flag = 0;
                break;
            }
        }
        printf("%s\n", (flag == 1) ? "YES" : "NO");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值