poj 3070 && nyoj 148 矩阵快速幂

poj 3070 && nyoj 148 矩阵快速幂

题目链接

poj: http://poj.org/problem?id=3070
nyoj: http://acm.nyist.net/JudgeOnline/problem.php?pid=148

思路:
  • 矩阵快速幂
  • 直接求取
    1143334-20171122184537930-676159752.png
代码:
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int mod = 10000;
struct mat {
    int a[2][2];
    mat() {
        a[0][1]=a[1][0]=1;
        a[1][1]=a[0][0]=0;
    }
};
mat qpow(mat& x, mat& y) {
    mat z;
    z.a[0][1]=z.a[1][0]=0;
    for(int i=0;i<=1;++i) {
        for(int j=0;j<=1;++j) {
            for(int k=0;k<=1;++k) {
                z.a[i][j]=(z.a[i][j]%mod+(x.a[i][k]%mod*y.a[k][j])%mod)%mod;
            }
        }
    }
    return z;
}
int slove(int n) {
    mat x,res;
    res.a[0][1]=res.a[1][0]=0;
    res.a[1][1]=res.a[0][0]=1;
    x.a[0][0]=1;
    while(n) {
        if(n&1) res=qpow(res,x);//二进制分解
        x=qpow(x,x);
        n>>=1;
    }
    return res.a[0][1];
}
int main() {
    int n;
    while(~scanf("%d",&n)&&n!=-1) {
        printf("%d\n",slove(n));
    }
    return 0;
}

转载于:https://www.cnblogs.com/lemonbiscuit/p/7880722.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值