zoukankan      html  css  js  c++  java
  • poj3318

    题意:给定三个矩阵a,b,c,问a×b是否等于c?

    分析:只需要设置一个列向量x,求a*(b*x),求c*x,看是否相等即可。

    当然这不能保证正确,x是生成的随机向量,多次生成多次测试即可使错误概率大大减小。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <ctime>
    #include <algorithm>
    using namespace std;
    
    const int maxn = 510;
    
    long long  a[maxn][maxn], b[maxn][maxn], c[maxn][maxn], x[maxn];
    int n;
    
    void init(long long m[maxn][maxn])
    {
    	for (int i = 0; i < n; i++)
    		for (int j = 0; j < n; j++)
    			scanf("%I64d", &m[i][j]);
    }
    
    void mul(long long m[maxn][maxn], long long x[maxn], long long ans[maxn])
    {
    	for (int i = 0; i < n; i++)
    	{
    		ans[i] = 0;
    		for (int j = 0; j < n; j++)
    			ans[i] += x[j] * m[i][j];
    	}
    }
    
    bool check(long long ans1[maxn], long long ans2[maxn])
    {
    	for (int i = 0; i < n; i++)
    		if (ans1[i] != ans2[i])
    			return false;
    	return true;
    }
    int main()
    {
    	//freopen("D:\\t.txt", "r", stdin);
    	scanf("%d", &n);
    	init(a);
    	init(b);
    	init(c);
    	int t = 10;
    	while (t--)
    	{
    		srand(time(NULL));
    		for (int i = 0; i < n; i++)
    			x[i] = rand()%1000 + 1;
    		long long ans[maxn], ans1[maxn], ans2[maxn];
    		mul(b, x, ans);
    		mul(a, ans, ans1);
    		mul(c, x, ans2);
    		if (!check(ans1, ans2))
    		{
    			printf("NO\n");
    			return 0;
    		}
    	}
    	printf("YES\n");
    	return 0;
    }
    
    
  • 相关阅读:
    原根
    FFT
    bzoj3991[SDOI2015]寻宝游戏
    bzoj3990[SDOI2015]排序
    序列自动机
    bzoj4032[HEOI2015]最短不公共子串
    2015.8.28 字符串
    bzoj2821作诗
    bzoj2741【FOTILE模拟赛】L
    一个牛人给java初学者的建议
  • 原文地址:https://www.cnblogs.com/rainydays/p/1948629.html
Copyright © 2011-2022 走看看