hdu 3326 Another kind of Fibonacci (矩阵构造)

题目大意:

描述了另外一种斐波那契

F[n] = x*F[n-1] + y*F[n-2];

求segma(F[i]^2);


思路分析:

构造矩阵的详细 请戳我

构造矩阵可以得到

中间矩阵为

1 1 0 0

0 x^2      y^2   2*x*y

0 1 0 0

0 x 0 y


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define N 30
typedef long long LL;
using namespace std;
const LL mod = 10007;

struct matrix
{
    int a[4][4];
}origin;


matrix multiply(matrix x,matrix y)
{
    matrix temp;
    memset(temp.a,0,sizeof(temp.a));
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            for(int k=0;k<4;k++)
            {
                temp.a[i][j]+=x.a[i][k]*y.a[k][j];
                temp.a[i][j]=(temp.a[i][j])%mod;
            }
        }
    }
    return temp;
}

matrix matmod(matrix a,LL k)
{
    matrix res;

    memset(res.a,0,sizeof res.a);
    for(int i=0;i<4;i++)res.a[i][i]=1;

    while(k)
    {
        if(k&1)
        res=multiply(res,a);
        k>>=1;
        a=multiply(a,a);
    }
    return res;
}

int main()
{
    int  n,x,y;
    while(scanf("%d%d%d",&n,&x,&y)!=EOF)
    {
        matrix st;

        x%=mod;
        y%=mod;

        st.a[0][0]=st.a[0][1]=1;
        st.a[0][2]=st.a[0][3]=0;

        st.a[1][0]=0;
        st.a[1][1]=(x*x)%mod;
        st.a[1][2]=(y*y)%mod;
        st.a[1][3]=(2*x*y)%mod;

        st.a[2][0]=st.a[2][2]=st.a[2][3]=0;
        st.a[2][1]=1;

        st.a[3][0]=st.a[3][2]=0;
        st.a[3][1]=x%mod;
        st.a[3][3]=y%mod;

        matrix end = matmod(st,n);

        int ans=0;

        for(int i=0;i<4;i++)
        {
            ans+=end.a[0][i];
            ans%=mod;
        }
        printf("%d\n",ans%mod);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值