POJ3070-Fibonacci-矩阵快速幂

模板题。

#include <cstdio>
#include <cstring>

using namespace std;

const int MOD = 10000;
int N;

struct matrix
{
    int m[2][2];
}ans,base;

matrix multi(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;
}

int FastMi(int n)
{
    base.m[0][0] = base.m[0][1] = base.m[1][0] = 1;
    base.m[1][1] = 0;
    ans.m[0][0] = ans.m[1][1] =1;
    ans.m[0][1]=ans.m[1][0] = 0;
    while(n)
    {
        if(n&1)
            ans = multi(ans,base);
        base = multi(base,base);
        n >>= 1;
    }
    return ans.m[0][1];
}

int main()
{
    while(scanf("%d",&N) && N != -1)
    {
        printf("%d\n",FastMi(N));
    }
}

 

posted @ 2016-01-31 21:47  Helica  阅读(184)  评论(0编辑  收藏  举报