poj 3318


随机一个行向量 P

判断 PAB=PC 是否正确即可。

当然有一定概率判断失误。。。

时间复杂度: O(n2)


蒙特·卡罗方法? 我不知道,先留坑。


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>

template<class Num>void read(Num &x)
{
    char c; int flag = 1;
    while((c = getchar()) < '0' || c > '9')
        if(c == '-') flag *= -1;
    x = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        x = (x<<3) + (x<<1) + (c-'0');
    x *= flag;
    return;
}
template<class Num>void write(Num x)
{
    if(x < 0) putchar('-'), x = -x;
    static char s[20];int sl = 0;
    while(x) s[sl++] = x%10 + '0',x /= 10;
    if(!sl) {putchar('0');return;}
    while(sl) putchar(s[--sl]);
}

const int maxn = 505, Base = 10;
typedef long long Array[maxn][maxn];
#define REP(__i,__start,__end) for(int __i = (__start); __i <= (__end); __i++)

int n;
Array A, B, C;
long long P[maxn], Q[maxn], AnsL[maxn], AnsR[maxn];

void init()
{
    REP(i, 1, n)
        REP(j, 1, n)
            read(A[i][j]);

    REP(i, 1, n)
        REP(j, 1, n)
            read(B[i][j]);

    REP(i, 1, n)
        REP(j, 1, n)
            read(C[i][j]);  
}
bool check()
{
    REP(i, 1, n)
    {
        P[i] = rand()%Base;
        Q[i] = AnsL[i] = AnsR[i] = 0; 
    }

    REP(i, 1, n)
        REP(k, 1, n)
            AnsR[i] += P[k] * C[k][i];

    REP(i, 1, n)
        REP(k, 1, n)
            Q[i] += P[k] * A[k][i];

    REP(i, 1, n)
        REP(k, 1, n)
            AnsL[i] += Q[k] * B[k][i];  

    REP(i, 1, n)
        if(AnsL[i] != AnsR[i]) return false;    

    return true;
}
int main()
{   
#ifndef ONLINE_JUDGE
    freopen("3318.in","r",stdin);
    freopen("3318.out","w",stdout);
#endif

    srand(998244353);

    while(scanf("%d", &n) != EOF)
        init(), puts(check()?"YES":"NO");

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值