首页 > 其他 > 详细

矩阵快速幂 poj 3070

时间:2015-05-02 13:55:06      阅读:110      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <cstdio>

using namespace std;
const int MOD = 10000;

struct Matrix
{
    int m[2][2];
};

Matrix Mul(Matrix a, Matrix b)
{
    Matrix tmp;
    for(int i=0; i<2; i++)
        for(int j=0; j<2; j++)
    {
        tmp.m[i][j] = 0;
        for(int k=0; k<2; k++)
            tmp.m[i][j] = (tmp.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD;
    }

    return tmp;
}

Matrix fast_mod(Matrix a, int m)
{
    if(m == 1) return a;
    Matrix tmp = fast_mod(a, m>>1);
    tmp = Mul(tmp, tmp);
    if(m & 1) return Mul(a, tmp);
    else return tmp;
}

int main()
{
    int n;
    while(~scanf("%d", &n) && n != -1)
    {
        if(!n) {
                cout<<0<<endl;
                continue;
        }
        Matrix ans;
        ans.m[0][0] = ans.m[0][1] = ans.m[1][0] = 1, ans.m[1][1] = 0;
        ans = fast_mod(ans, n);
        cout<<ans.m[0][1]<<endl;
    }
    return 0;
}

矩阵快速幂 poj 3070

原文:http://blog.csdn.net/dojintian/article/details/45438781

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!