hdu 1005 矩阵快速幂

矩阵快速幂算法解析
本文介绍了一种利用矩阵快速幂解决特定数学问题的方法。通过定义矩阵结构和实现矩阵乘法及快速幂运算,解决了斐波那契数列等递推序列的大数计算问题。文章详细展示了如何将递推关系转化为矩阵形式,并利用快速幂加速计算。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF ) return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
struct is
{
    ll a[10][10];
};
is juzhenmul(is a,is b,ll hang ,ll lie)
{
    int i,t,j;
    is ans;
    memset(ans.a,0,sizeof(ans.a));
    for(i=1;i<=hang;i++)
    for(t=1;t<=lie;t++)
    for(j=1;j<=lie;j++)
    {
        ans.a[i][t]+=(a.a[i][j]*b.a[j][t])%7;
    }
    return ans;
}
is quickpow(is ans,is a,ll x)
{
    while(x)
    {
        if(x&1)  ans=juzhenmul(ans,a,2,2);
        a=juzhenmul(a,a,2,2);
        x>>=1;
    }
    return ans;
}
int main()
{
    is ans,base;
    ll x,y,z,i,t;
    while(scanf("%lld%lld%lld",&x,&y,&z)!=EOF)
    {
        if(x==0&&y==0&&z==0)break;
        if(z<3)
        {printf("1\n");
        continue;}
        memset(ans.a,0,sizeof(ans.a));
        ans.a[1][1]=1;
        ans.a[2][2]=1;
        //printf("%lld %lld\n%lld %lld\n",ans.a[1][1],ans.a[1][2],ans.a[2][1],ans.a[2][2]);
        base.a[1][1]=x;
        base.a[1][2]=1;
        base.a[2][1]=y;
        base.a[2][2]=0;
        ans=quickpow(ans,base,z-2);
        printf("%lld\n",(ans.a[1][1]+ans.a[2][1])%7);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/jhz033/p/5422291.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值