tjut 4686

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int V = 100000 + 50;
const int mod = 1000000000 + 7;
const int MaxN = 5;
struct Matrix{
    __int64 mat[MaxN][MaxN];
};
Matrix init, Pow;
__int64 n, A0, Ax, Ay, B0, Bx, By;
Matrix multi(Matrix a, Matrix b) {
    Matrix ans;
    for(int i = 0; i < MaxN; ++i)
    for(int j = 0; j < MaxN; ++j) {
        __int64 sum = 0;
        for(int k = 0; k < MaxN; ++k)
            sum = (sum + a.mat[i][k] * b.mat[k][j] % mod) % mod;
        ans.mat[i][j] = sum;
    }
    return ans;
}
Matrix MatrixQuickPow(Matrix a, __int64 b) {
    Matrix ans = init;
    while(b) {
        if(b & 1)
            ans = multi(ans, a);
        b /= 2;
        a = multi(a, a);
    }
    return ans;
}
int main() {
    while(~scanf("%I64d", &n)) {
        scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &A0, &Ax, &Ay, &B0, &Bx, &By);
        if(!n) {
            printf("0\n");
            continue;
        }
        init.mat[0][0] = 1;
        init.mat[0][1] = A0;
        init.mat[0][2] = B0;
        init.mat[0][3] = init.mat[0][4] = A0 * B0 % mod;
        Pow.mat[0][0] = Pow.mat[4][4] = 1;
        Pow.mat[0][1] = Ay;
        Pow.mat[0][2] = By;
        Pow.mat[0][3] = Pow.mat[0][4] = Ay * By % mod;
        Pow.mat[1][1] = Ax;
        Pow.mat[2][2] = Bx;
        Pow.mat[1][3] = Pow.mat[1][4] = Ax * By % mod;
        Pow.mat[2][3] = Pow.mat[2][4] = Ay * Bx % mod;
        Pow.mat[3][3] = Pow.mat[3][4] = Ax * Bx % mod;
        /*
for(int i = 0; i < MaxN; ++i) {
            for(int j = 0; j < MaxN; ++j)
                printf("%I64d ", Pow.mat[i][j]);
            printf("\n");
        }*/
        Matrix ans = MatrixQuickPow(Pow, n - 1);
        printf("%I64d\n", ans.mat[0][4]);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值