[Algo 1/1024]POJ3318 抽样检查(P值)

POJ3318 Matrix Multiplication

这是一道点名了需要实用抽样检查的题目, 分析可以得知单次误检概率为1/2, 0.5**15 < 6e-5

#include <stdio.h>
#include <vector>
#include <time.h>
using namespace std;

template<typename Type>
struct Matrix {
    Matrix(size_t rows, size_t cols): _data(rows*cols, 0), _rows(rows), _cols(cols) {

    }
    Type& operator ()(int i, int j) {
        return _data[i * _cols +j];
    }

    std::vector<Type> operator * (std::vector<Type>& x) {
        std::vector<Type> y(_rows, 0);
        for(int i = 0 ; i < _rows; i++) 
            for(int j = 0; j < _cols; j++)
                y[i] += 1LL * x[j] * _data[i * _cols +j];
        return y;        
    }

    std::vector<Type> operator * (std::vector<Type>&& x) {
        std::vector<Type> y(_rows, 0);
        for(int i = 0 ; i < _rows; i++) 
            for(int j = 0; j < _cols; j++)
                y[i] += 1LL * x[j] * _data[i * _cols +j];
        return y;        
    }

    std::vector<Type> _data;
    size_t _rows, _cols;
};

template<typename Type>
bool Compare(std::vector<Type>& x, std::vector<Type>& y) {
    bool result = true;
    for(int i = 0; i< x.size(); i++)
        result &= x[i] == y[i];
    return result;    
}

bool Solve(Matrix<long long>& A, Matrix<long long>& B, Matrix<long long>& C) {
    for(int i = 0; i < 15; i++) {
        std::vector<long long> x(C._cols);
        for(int _i = 0; _i < x.size(); _i++)
            x[_i] = rand()%101;
        std::vector<long long> ABx = A*(B*x);
        std::vector<long long> Cx = C*x;
        if(Compare(ABx, Cx) == false) return false;
    }
    return true;
}

int main() {
    int N, k;
    scanf("%d", &N);
    srand(time(NULL));
    Matrix<long long> A[3] = {Matrix<long long>(N,N), Matrix<long long>(N,N), Matrix<long long>(N,N)};
    for(int k = 0; k < 3;k++)
        for(int i = 0; i < N; i++) 
            for(int j = 0; j < N; j++)
                scanf("%lld", &A[k](i,j));

    printf(Solve(A[0], A[1], A[2]) ? "YES":"NO");
    putchar('\n');
}
posted @ 2020-03-13 15:13  xiconxi  阅读(99)  评论(0)    收藏  举报