How Many Calls? UVA - 10518 矩阵快速幂

题目链接: 点我


题意:

给你fibonacci数列怎么求的,然后问你求f(n) = f(n - 1) + f(n - 2)需要多少次调用,并且这个数很大,取模一个进制的数。

思路:

我们要知道调用次数F(n) = f(n)* 2 - 1这个规律,那么这题就可以解了,矩阵快速幂跑一边即可.

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

typedef long long LL;
LL n, b;

struct mat {
    LL a[3][3];
    mat(){memset(a, 0,sizeof(a)); }
    mat operator *(const mat q){
        mat c;
        for(int i = 1; i <= 2; ++i)
            for(int j = 1; j <= 2; ++j)
            if(a[i][j])
        for(int k = 1;  k<= 2; ++k){
            c.a[i][k] += a[i][j] * q.a[j][k];
            if(c.a[i][j] >= b) c.a[i][k] %= b;
        }return c;
    }
};

mat qpow(mat x, LL n){
    mat ans;
    ans.a[1][1] = ans.a[2][2] = 1;
    while(n){
        if(n&1) ans =ans * x;
        x = x * x;
        n >>= 1;
    }return ans;
}

int main(){
    int kase = 0;
    while(scanf("%lld %lld", &n, &b) != EOF&&(n||b)){
        mat ans;
        ans.a[1][1] =ans.a[1][2] = ans.a[2][1] = 1;
        ans =qpow(ans, n+1);
        LL sum = (ans.a[2][1] * 2 - 1 + b) % b;
        printf("Case %d: %lld %lld %lld\n",++kase, n, b, sum);
    }return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值