zoukankan      html  css  js  c++  java
  • [蓝桥杯][算法提高VIP]递推求值

    [[F(n,1),F(n,2),F(n-1,1),F(n-1,2),F(n-2,1),F(n-2,2),1]=\ [F(n-1,1),F(n-1,2),F(n-2,1),F(n-2,2),F(n-3,1),F(n-3,2),1] egin{bmatrix} 0 & 1 & 1 & 0 & 0 & 0 & 0\ 1 & 0 & 0 & 1 & 0 & 0 & 0\ 0 & 0 & 0 & 0 & 1 & 0 & 0\ 0 & 0 & 0 & 0 & 0 & 1 & 0\ 2 & 3 & 0 & 0 & 0 & 0 & 0\ 0 & 2 & 0 & 0 & 0 & 0 & 0\ 5 & 3 & 0 & 0 & 0 & 0 & 1\ end{bmatrix} \ =[F(3,1),F(3,2),F(2,1),F(2,2),F(1,1),F(1,2),1]egin{bmatrix} 0 & 1 & 1 & 0 & 0 & 0 & 0\ 1 & 0 & 0 & 1 & 0 & 0 & 0\ 0 & 0 & 0 & 0 & 1 & 0 & 0\ 0 & 0 & 0 & 0 & 0 & 1 & 0\ 2 & 3 & 0 & 0 & 0 & 0 & 0\ 0 & 2 & 0 & 0 & 0 & 0 & 0\ 5 & 3 & 0 & 0 & 0 & 0 & 1\ end{bmatrix}^{n-3} ]

    LL n;
    
    void mul(int f[7],int m[7][7])
    {
        int c[7]={0};
        for(int i=0;i<7;i++)
            for(int j=0;j<7;j++)
                c[i]=(c[i]+(LL)f[j]*m[j][i])%mod;
        memcpy(f,c,sizeof c);
    }
    
    void mulself(int m[7][7])
    {
        int c[7][7]={0};
        for(int i=0;i<7;i++)
            for(int j=0;j<7;j++)
                for(int k=0;k<7;k++)
                    c[i][j]=(c[i][j]+(LL)m[i][k]*m[k][j])%mod;
        memcpy(m,c,sizeof c);
    }
    
    int main()
    {
        int f[7]={6,5,1,4,2,3,1};
        int m[7][7]={{0,1,1,0,0,0,0},
                     {1,0,0,1,0,0,0},
                     {0,0,0,0,1,0,0},
                     {0,0,0,0,0,1,0},
                     {2,3,0,0,0,0,0},
                     {0,2,0,0,0,0,0},
                     {5,3,0,0,0,0,1}};
        cin>>n;
    
        if(n == 1)
            cout<<2<<endl<<3<<endl;
        else if(n == 2)
            cout<<1<<endl<<4<<endl;
        else if(n == 3)
            cout<<6<<endl<<5<<endl;
        else
        {
            n-=3;
            while(n)
            {
                if(n & 1) mul(f,m);
                mulself(m);
                n>>=1;
            }
    
            cout<<f[0]<<endl<<f[1]<<endl;
        }
        //system("pause");
        return 0;
    }
    
  • 相关阅读:
    一个完整的移动端项目的构建步骤——框架搭构1
    简单日历,纯js
    javascript语句语义大全(7)
    微软笔试Highway问题解析
    中国电信翼支付2014编程大赛决赛
    海岛问题
    大数计算
    Dijkstra算法
    Android测试之Keycode
    字符串解析
  • 原文地址:https://www.cnblogs.com/fxh0707/p/14618177.html
Copyright © 2011-2022 走看看