POJ 3070 Fibonacci 矩阵快速幂

5 篇文章 0 订阅

题意很明确,求第m个斐波那契数MOD10000的结果

题目连矩阵都构造好了,就是 

1   1

1   0

然后对这个求幂就行了

/*
ID: sdj22251
PROG: subset
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAXN 305
#define INF 100000000
#define eps 1e-7
#define PI 3.1415926535898
using namespace std;
int n = 2, m;
int tt[2][2]={{1, 1}, {1, 0}};
struct wwj
{
    int r, c;
    int mat[3][3];
} need, ready;
void init()
{
    memset(need.mat, 0, sizeof(need.mat));
    need.r = n;
    need.c = n;
    for(int i = 1; i <= n; i++)
    {
        need.mat[i][i] = 1;
    }
    ready.c = n;
    ready.r = n;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        ready.mat[i][j] = tt[i - 1][j - 1];
    }
}
wwj multi(wwj x, wwj y)
{
    wwj t;
    int i, j, k;
    memset(t.mat, 0, sizeof(t.mat));
    t.r = x.r;
    t.c = y.c;
    for(i = 1; i <= x.r; i++)
    {
        for(k = 1; k <= x.c; k++)
            if(x.mat[i][k])
            {
                for(j = 1; j <= y.c; j++)
                {
                    t.mat[i][j] += (x.mat[i][k] * y.mat[k][j]) % 10000;
                    t.mat[i][j] %= 10000;
                }
            }
    }
    return t;
}
int main()
{
    int m;
    while(scanf("%d", &m) != EOF)
    {
        if(m == -1) break;
        init();
        while(m)
        {
            if(m & 1)
            {
                need = multi(ready, need);
            }
            ready = multi(ready, ready);
            m = m >> 1;
        }
        printf("%d\n", need.mat[1][2] % 10000);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值