快速矩阵幂 csu1597 薛XX后代的IQ

裸快速矩阵的题,,主要来模板测试


说下这个模板用法,下标都是从0开始

Mat(行数,列数,填充矩阵(可填))

mat_mul(A,B)矩阵乘法

mat_pow(A,b)快速矩阵幂

this->S存放矩阵的数组


#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<functional>
#include<algorithm>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int matMX = 3;
const int MX = 200000 + 5;
const int INF = 0x3f3f3f3f;

LL mod;
LL power(LL a, LL b) {
    LL ret = 1;
    while(b) {
        if(b & 1) ret = ret * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ret;
}

struct Mat {
    int m, n;
    LL S[matMX][matMX];
    Mat(int a, int b) {
        m = a;
        n = b;
        memset(S, 0, sizeof(S));
    }
    Mat(int a, int b, LL w[][matMX]) {
        m = a;
        n = b;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                S[i][j] = w[i][j];
            }
        }
    }
};

Mat mat_mul(Mat A, Mat B) {
    Mat C(A.m, B.n);
    for(int i = 0; i < A.m; i++) {
        for(int j = 0; j < B.n; j++) {
            for(int k = 0; k < A.n; k++) {
                C.S[i][j] = (C.S[i][j] + A.S[i][k] * B.S[k][j]) % mod;
            }
        }
    }
    return C;
}

Mat Blank(int m, int n) {
    Mat ret(m, n);
    for(int i = 0; i < m; i++) {
        ret.S[i][i] = 1;
    }
    return ret;
}

Mat mat_pow(Mat A, LL b) {
    Mat ret = Blank(A.m, A.n);
    while(b) {
        if(b & 1) ret = mat_mul(ret, A);
        A = mat_mul(A, A);
        b >>= 1;
    }
    return ret;
}

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int X, Y, A, B, N;
        scanf("%d%d%d%d%lld%d", &X, &Y, &A, &B, &mod, &N);
        LL T1[][matMX] = {{0, 1}, {A, B}};
        LL T2[][matMX] = {{X}, {Y}};

        Mat s(2, 2, T1);
        Mat ans(2, 1, T2);

        Mat res = mat_mul(mat_pow(s, N), ans);
        printf("%lld\n", res.S[1][0]);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值