码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4686(矩阵快速幂)

时间:2015-05-29 10:05:59      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

题意:如下公式
技术分享
其中
a0 = A0
ai = a(i-1)*AX+AY
b0 = B0
bi = b(i-1)*BX+BY
题解:构造矩阵
矩阵x:
| 1 a0 b0 a0*b0 s0 |
| 0 0 0 0 0 |
| 0 0 0 0 0 |
| 0 0 0 0 0 |
| 0 0 0 0 0 |
矩阵y:
| 1 ay by ay*by ay*by |
| 0 ax bx ax*by ax*by |
| 0 0 0 bx*ay bx*ay |
| 0 0 0 ax*bx ax*bx |
| 0 0 0 0 1 |

#include <stdio.h>
#include <string.h>
#define ll long long
const int MOD = 1000000007;
struct Mat {
    ll g[5][5];
}res, ori;
ll n, a0, ax, ay, b0, bx, by;

Mat multiply(Mat x, Mat y) {
    Mat temp;
    memset(temp.g, 0, sizeof(temp.g));
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++)
            for (int k = 0; k < 5; k++)
                temp.g[i][j] = (temp.g[i][j] + x.g[i][k] * y.g[k][j]) % MOD;
    return temp;
}

void calc(ll n) {
    while (n) {
        if (n & 1)
            ori = multiply(ori, res);
        n >>= 1;
        res = multiply(res, res);
    }
}

int main() {
    while (scanf("%lld", &n) == 1) {
        scanf("%lld%lld%lld%lld%lld%lld", &a0, &ax, &ay, &b0, &bx, &by);
        if (n == 0) {
            printf("0\n");
            continue;
        }
        memset(res.g, 0, sizeof(res.g));
        ori.g[0][0] = res.g[0][0] = 1;
        ori.g[0][1] = a0 % MOD;
        ori.g[0][2] = b0 % MOD;
        ori.g[0][3] = (a0 * b0) % MOD;
        ori.g[0][4] = (a0 * b0) % MOD;
        res.g[0][1] = ay % MOD;
        res.g[1][1] = ax % MOD;
        res.g[0][2] = by % MOD;
        res.g[2][2] = bx % MOD;
        res.g[0][3] = res.g[0][4] = (ay * by) % MOD;
        res.g[1][3] = res.g[1][4] = (ax * by) % MOD;
        res.g[2][3] = res.g[2][4] = (bx * ay) % MOD;
        res.g[3][3] = res.g[3][4] = (ax * bx) % MOD;
        res.g[4][4] = 1;
        calc(n - 1);
        printf("%lld\n", ori.g[0][4]);
    }
    return 0;
}

hdu 4686(矩阵快速幂)

标签:

原文地址:http://blog.csdn.net/hyczms/article/details/46136959

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!