矩阵运算 快速幂

  1. 矩阵乘法必须满足第一个矩阵的列等于第二个矩阵的行。
  2. 结果矩阵的行是第一个矩阵的行,列是第二个矩阵的列。
  3. 矩阵乘法行列各元素相乘。
  4. 矩阵快速幂利用幂的二分优化。
  5. 利用运算符重载进行一系列更方便的操作。
// 整数快速幂取模
template <typename Type>
long long ipow(Type x, Type n, Type mod){
    long long result = 1LL;
    while(n) {
        if(n & 1)
            result = (result*x) % mod;
        x = ((long long)x*x) % mod;
        n >>= 1;
    }

    return result;
}

//矩阵运算类
struct mat{
    int r = 0;
    int c = 0;
    vector<vector<long long> >rect;
    //construct
    mat(int r, int c) : r(r),c(c){
        rect.resize(r);
        for(int i = 0; i < r; ++i)
            rect[i].resize(c);
    }
    mat() = default;
    //input and output
    void in(){
        for(int i = 0; i < r; ++i)
            for(int j = 0; j < c; ++j)
                scanf("%I64d",&rect[i][j]);
    }
    void out(){
        for(int i = 0; i < r; ++i){
            for(int j = 0; j < c; ++j){
                if(j == 0)
                    printf("%I64d",rect[i][0]);
                else
                    printf(" %I64d",rect[i][j]);
            }
            printf("\n");
        }

    }
    //init
    void init(){
        for(int i = 0; i < r; ++i)
            for(int j = 0; j < c; ++j)
                rect[i][j] = (i == j);
    }
    //overload
    mat operator * (mat& rhs){
        mat ans(r,rhs.c);
        long long res;
        if(c == rhs.r){
            for(int i = 0; i < r; ++i){
                for(int j = 0; j < rhs.c; ++j){
                    res = 0LL;
                    for(int k = 0; k < c; ++k){//k means c or rhs.r
                        res += rect[i][k] * rhs.rect[k][j];
                    }
                    ans.rect[i][j] = res;
                }
            }
        }
        return ans;
    }
    void operator *= (mat& rhs){
        mat ans(r,rhs.c);
        long long res;
        if(c == rhs.r){
            for(int i = 0; i < r; ++i){
                for(int j = 0; j < rhs.c; ++j){
                    res = 0LL;
                    for(int k = 0; k < c; ++k){//k means c or rhs.r
                        res += rect[i][k] * rhs.rect[k][j];
                    }
                    ans.rect[i][j] = res;
                }
            }
        }
        rect = ans.rect;
    }
    mat operator ^ (int k){//r == c
        mat ans(r,c);
        ans.init();
        for(;k;k >>= 1){
            if(k & 1)
                ans *= (*this);
            (*this) *= (*this);
        }
        return ans;
    }
    void operator ^= (int k){//r == c
        mat ans(r,c);
        ans.init();
        for(;k;k >>= 1){
            if(k & 1)
                ans *= (*this);
            (*this) *= (*this);
        }
        (*this) = ans;
    }
};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值