• Recursive sequence HDU


    矩阵加速的线形递推的裸题,难点就在于构造矩阵。


    代码:

    using namespace std;
    const int MAXN=7;
    int T;
    ll a,b,n;
    typedef struct{
        ll mp[MAXN][MAXN];
        void init(){
            mem(mp,0);
            for(int i=0;i<MAXN;i++){
                 mp[i][i]=1;
             }
        }
    }matrix;
    matrix pp={
        1,1,0,0,0,0,0,
        2,0,0,0,0,0,0,
        1,0,1,0,0,0,0,
        4,0,4,1,0,0,0,
        6,0,6,3,1,0,0,
        4,0,4,3,2,1,0,
        1,0,1,1,1,1,1
    };
    matrix multi(matrix a,matrix b)
    {
        matrix res;
        for(int i=0;i<MAXN;i++){
           for(int j=0;j<MAXN;j++){
               res.mp[i][j]=0;
               for(int k=0;k<MAXN;k++){
                   res.mp[i][j]+=(a.mp[i][k]*b.mp[k][j])%MOD;
                   res.mp[i][j]=res.mp[i][j]%MOD;
               }
           }
        }
        return res;
    }
    matrix fastm(matrix a,ll x)
    {
        matrix res;
        res.init();
        while(x){
            if(x&1){
                res=multi(res,a);
            }
            x>>=1;
            a=multi(a,a);
        }
        return res;
    }
    int main()
    {
        cin>>T;
        while(T--){
            scanf("%lld%lld%lld",&n,&a,&b);
            if(n==1){
                printf("%lld
    ",a);
            }else if(n==2){
                printf("%lld
    ",b);
            }else{
                matrix now=fastm(pp,n-2);
                ll num;
                num=(b*now.mp[0][0])%MOD;
                num=(num+a*now.mp[1][0]%MOD)%MOD;
                num=(num+16*now.mp[2][0]%MOD)%MOD;
                num=(num+8*now.mp[3][0]%MOD)%MOD;
                num=(num+4*now.mp[4][0]%MOD)%MOD;
                num=(num+2*now.mp[5][0]%MOD)%MOD;
                num=(num+now.mp[6][0]%MOD)%MOD;
                printf("%lld
    ",num);
            }
        }
    
        return 0;
    }
    
    
    越自律,越自由
  • 相关阅读:
    结对编程(阶段二)
    结对编程 第一阶段
    Git实验
    webpack02
    webpack01
    实验五 单元测试
    实验四 代码审评
    UML建模工具的安装和使用
    实验二 结对编程(阶段二)
    博客园背景的修改
  • 原文地址:https://www.cnblogs.com/ha-chuochuo/p/13646607.html
Copyright © 2020-2023  润新知