【模板】矩阵乘法

template<int x, int y>
struct matrix {
    int s[x][y];
    matrix() {memset(s, 0, sizeof(s));}
    matrix(int a[x][y]) {memcpy(s, a, sizeof(a));}
    int* operator [] (int p) {return s[p];}
    template<int xy>
    matrix<x, xy> operator * (const matrix<y, xy> a)const {
        matrix<x, xy>c;
        for(int i = 0; i < x; i ++)
            for(int j = 0; j < xy; j ++)
                for(int k = 0; k < y; k ++)
                    c[i][j] += s[i][k] * a[k][j];
        return c;
    }
    template<int xy>
    matrix<x, xy> mul(const matrix<y, xy> a, const int mod)const {
        matrix<x, y>c;
        for(int i = 0; i < x; i ++)
            for(int j = 0; j < xy; j ++)
                for(int k = 0; k < y; k ++)
                    c[i][j] += s[i][k] * a[k][j], c[i][j] %= mod;
        return c;
    }
};

猜你喜欢

转载自www.cnblogs.com/akakw1/p/10019070.html