zoukankan      html  css  js  c++  java
  • FZU1683 纪念SlingShot

     Problem Description 题目链接

    已知 F(n)=3 * F(n-1)+2 * F(n-2)+7 * F(n-3),n>=3,其中F(0)=1,F(1)=3,F(2)=5,对于给定的每个n,输出F(0)+ F(1)+ …… + F(n) mod 2009。

     Input

    第一行是一整数m,代表总共有m个cases。

     Output

    对于每个case,输出一行。格式见样例,冒号后有一个空格。

     Sample Input

    2 3 6

     Sample Output

    Case 1: 37
    Case 2: 313
     
    #include <iostream>
    #include <stdio.h>
    #include <cstring>
    using namespace std;
    
    /* 矩阵快速幂问题,重点是找到矩阵
        Sn=Sn-1+f(n)=Sn-1+3*f(n-1)+2*f(n-2)+7*f(n-3)
        Base:Sn     | 1 1 0 0 | Sn-1
            f(n+1)  | 0 3 2 7 | f(n)
            f(n)    | 0 1 0 0 | f(n-1)
            f(n-1)  | 0 0 0 1 | f(n-2)
    
        Ori:S2    | 4 0 0 0 | 
            f(2)  | 5 0 0 0 | 
            f(1)  | 3 0 0 0 | 
            f(0)  | 1 0 0 0 | 
    */
    struct Matrix
    {
        int m[5][5];
    
        Matrix()
        {
            memset(m, 0, sizeof(m));
        }
    
        //初始化特征矩阵
        void init()
        {
            m[1][1] = 1;
            m[1][2] = 1;
            m[2][2] = 3;
            m[2][3] = 2;
            m[2][4] = 7;
            m[3][2] = 1;
            m[4][3] = 1;
        }
    
        //单位化
        void unit()
        {
            for (int i = 1; i <= 4; i++)
            {
                m[i][i] = 1;
            }
        }
    };
    
    //矩阵相乘
    Matrix mul(Matrix a, Matrix b)
    {
        Matrix c;
        for (int i = 1; i <= 4; i++)
        {
            for (int j = 1; j <= 4; j++)
            {
                for (int k = 1; k <= 4; k++)
                {
                    c.m[i][j] = (c.m[i][j] + a.m[i][k] * b.m[k][j]) % 2009;
                }
            }
        }
        return c;
    }
    
    //计算矩阵n次方
    Matrix pow(int n)
    {
        Matrix res;
        Matrix mt;
        mt.init();
        //化成单位矩阵
        res.unit();
    
        while (n)
        {
            if (n & 1)
                res = mul(res, mt);
            mt = mul(mt, mt);
            n = n >> 1;
        }
        return res;
    }
    
    int main()
    {
        int m, n, c = 1, ans;
        cin >> m;
        //初始矩阵
        Matrix oriM;
        oriM.m[1][1] = 4;
        oriM.m[2][1] = 5;
        oriM.m[3][1] = 3;
        oriM.m[4][1] = 1;
        while (m--)
        {
            cin >> n;
            if (n == 0)
            {
                ans = 1;
            }
            else
            {
                Matrix p = mul(pow(n - 1), oriM);
                ans = p.m[1][1];
            }
            printf("Case %d: %d
    ", c++, ans);
        }
    }

    参考链接:https://blog.csdn.net/weixin_34221276/article/details/86129443

  • 相关阅读:
    Mysql高级第一天(laojia)
    Mysql初级第三天(wangyun)
    Mysql初级第二天(wangyun)
    Mysql初级第一天(wangyun)
    Spring的源码解析
    JAVA8新特性
    java8
    JMM内存模型
    JAVA并发工具类
    mybatis
  • 原文地址:https://www.cnblogs.com/dlvguo/p/12735208.html
Copyright © 2011-2022 走看看