51nod 1358 浮波那契

構造新數列f(n)=f(n-10)+f(n-34),n>40,f(n)=1,n<=40,則FB(n)=f(10n)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
const int p=1e9+7;

struct Matrix
{
    LL a[35][35];
    Matrix(){memset(a,0,sizeof a);}
    Matrix operator*(const Matrix &m)
    {
        Matrix res;
        for(int i=0;i<35;i++)
        {
            for(int j=0;j<35;j++)
            {
                for(int k=0;k<35;k++)
                {
                    res.a[i][j]+=a[i][k]*m.a[k][j]%p;
                }
                res.a[i][j]%=p;
            }
        }
        return res;
    }
}ans,base;

Matrix pow(Matrix base,LL n)
{
    Matrix res;
    for(int i=0;i<35;i++) res.a[i][i]=1;
    while(n)
    {
        if(n&1) res=res*base;
        base=base*base;
        n>>=1;
    }
    return res;
}

int main()
{
    LL n;
    while(~scanf("%lld",&n))
    {
        if(n<=4) puts("1");
        else
        {
            for(int i=0;i<35;i++) ans.a[0][i]=1;
            base.a[9][0]=1,base.a[33][0]=1;
            for(int i=1;i<35;i++) base.a[i-1][i]=1;
            ans=ans*pow(base,10*n-40);
            printf("%lld\n",(ans.a[0][0]+p)%p);
        }
    }
    return 0;
}


發佈了57 篇原創文章 · 獲贊 35 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章