HDU6050(矩阵快速幂)

37 篇文章 0 订阅
8 篇文章 0 订阅

求: F(m,1)
公式:

(F(m,1)F(m,1))=(F(1,1)F(1,2))(AnB0)m1n

(F(m,1)F(m,1))=(F(1,1)F(1,2))(AnB1)m1n

其中
A=(0121)B0=(1001)B1=(1120)

证明参看大佬博客
直接矩阵快速幂求解
code:

#include<iostream>
#include"string.h"
#include<stdio.h>
using namespace std;
const int bc=2;
const int mod = 1000000007;
struct matrix
{
    long long x[bc][bc];
};
matrix mutimatrix(matrix a,matrix b)
{
    matrix temp;
    memset(temp.x,0,sizeof(temp.x));
    for(int i=0;i<bc;i++)    //答案的行
    {
        for(int j=0;j<bc;j++)   //答案的列
        {
            for(int k=0;k<bc;k++)
            {
                temp.x[i][j]+=a.x[i][k]*b.x[k][j];
                temp.x[i][j]%=mod;
                temp.x[i][j]+=mod;
                temp.x[i][j]%=mod;
            }
        }
    }
    return temp;
}
matrix sub(matrix a,matrix b)
{
    matrix temp;
    memset(temp.x,0,sizeof(temp.x));
    for(int i=0;i<bc;i++)    //答案的行
    {
        for(int j=0;j<bc;j++)   //答案的列
        {
            temp.x[i][j]=a.x[i][j]-b.x[i][j];
            temp.x[i][j]%=mod;
            temp.x[i][j]+=mod;
            temp.x[i][j]%=mod;
        }
    }
    return temp;
}
matrix powmatrix(matrix a,long long b)
{
    matrix temp;
    memset(temp.x,0,sizeof(temp.x));
    //初始化矩阵为单位阵
    for(int i=0;i<bc;i++)
        temp.x[i][i]=1;
    while(b)
    {
        if(b%2==1)
            temp=mutimatrix(temp,a);
        a=mutimatrix(a,a);
        b=b/2;
    }
    return temp;
}
int main()
{
    int kase;
    scanf("%d",&kase);
    long long n;
    long long m;
    while(kase--)
    {
        cin>>n>>m;
        matrix a,b;
        if(n%(2LL)==0)
        {
            b.x[0][0]=1;b.x[0][1]=0;
            b.x[1][0]=0;b.x[1][1]=1;
        }
        else{
            b.x[0][0]=-1;b.x[0][1]=2;
            b.x[1][0]=1;b.x[1][1]=0;
        }
        a.x[0][0]=0;a.x[0][1]=2;
        a.x[1][0]=1;a.x[1][1]=1;
        matrix temp=powmatrix(a,n);
        temp=sub(temp,b);
        matrix res=powmatrix(temp,m-1);
        long long ans=res.x[0][0]+res.x[1][0];
        ans%=mod;
        ans+=mod;
        ans%=mod;
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值