POJ 3070 Fibonacci(矩阵快速幂)

题目地址:http://poj.org/problem?id=3070

思路:矩阵快速幂和快速幂的思想差不多,快速幂详解地址:http://blog.csdn.net/qq_25605637/article/details/47357119,按照下面2张图来就行


AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
const int maxn = 40000;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
struct node
{
    int a[2][2];
    void init()
    {
        a[0][0] = a[0][1] = a[1][0] = 1;
        a[1][1] = 0;
    }
};
node matrixmul(node a,node b)
{
    node c;
    for(int i=0; i<2; i++)
    {
        for(int j=0; j<2; j++)
        {
            c.a[i][j] = 0;
            for(int k=0; k<2; k++)
            {
                c.a[i][j] += a.a[i][k] * b.a[k][j];
            }
            c.a[i][j] %= 10000;
        }
    }
    return c;
}
node quickmod(node s,int k)
{
    node ans;
    ans.init();
    while(k)
    {
        if(k & 1)
            ans = matrixmul(ans,s);
        k = k >> 1;
        s = matrixmul(s,s);
    }
    return ans;
}
int main()
{
    int n;
    while(scanf("%d",&n))
    {
        if(n == -1)
            break;
        if(n == 0)
        {
            printf("0\n");
            continue;
        }
        node s;
        s.init();
        s = quickmod(s,n-1);
        printf("%d\n",s.a[0][1]%10000);
    }
    return 0;
}


大神地址:http://www.cnblogs.com/ACShiryu/archive/2011/08/09/poj3070.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值