zoukankan      html  css  js  c++  java
  • hust1384---The value of F[n]

    Description

    For any integer i>=3 we have  F[i]=(F[i-1]+2*F[i-2]+3*F[i-3])%9901;
    
    Now give you F[0],F[1],F[2],can you tell me the value of F
    

    Input

    Fist Line, an integer Q(1<=Q<=100) represent the number of cases;
    
    Every case is a line of four integers:F[0],F[1],F[2],n;
    
    (0<=F[0],F[1],F[2]<9901,0<=n<100000000)
    

    Output

    For each case ,you are request to output one integer the value of F[n] in one line.
    

    Sample Input

    4
    1 2 3 3
    4 5 6 5
    2 3 4 2
    4 5 6 100000
    

    Sample Output

    10
    129
    4
    6086
    

    Hint
    Source
    Dongxu LI

    水题,非常easy推出转移矩阵

    /*************************************************************************
        > File Name: hust1384.cpp
        > Author: ALex
        > Mail: zchao1995@gmail.com 
        > Created Time: 2015年03月12日 星期四 13时50分41秒
     ************************************************************************/
    
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const double pi = acos(-1);
    const int inf = 0x3f3f3f3f;
    const double eps = 1e-15;
    typedef long long LL;
    typedef pair <int, int> PLL;
    
    const int mod = 9901;
    struct MARTIX
    {
        int mat[4][4];
        MARTIX();
        MARTIX operator * (const MARTIX &b)const;
        MARTIX& operator = (const MARTIX &b);
    };
    
    MARTIX :: MARTIX()
    {
        memset (mat, 0, sizeof(mat));
    }
    
    MARTIX MARTIX :: operator * (const MARTIX &b)const
    {
        MARTIX res;
        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                for (int k = 0; k < 3; ++k)
                {
                    res.mat[i][j] += this -> mat[i][k] * b.mat[k][j];
                    res.mat[i][j] %= mod;
                }
            }
        }
        return res;
    }
    
    MARTIX& MARTIX :: operator = (const MARTIX &b)
    {
        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                this -> mat[i][j] = b.mat[i][j];
            }
        }
        return *this;
    }
    
    MARTIX fastpow (MARTIX ret, int n)
    {
        MARTIX ans;
        ans.mat[0][0] = ans.mat[1][1] = ans.mat[2][2] = 1;
        while (n)
        {
            if (n & 1)
            {
                ans = ans * ret;
            }
            ret = ret * ret;
            n >>= 1;
        }
        return ans;
    }
    
    void Debug(MARTIX A)
    {
        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                printf("%d ", A.mat[i][j]);
            }
            printf("
    ");
        }
    }
    
    int F[3];
    
    int main ()
    {
        int t;
        scanf("%d", &t);
        while (t--)
        {
            int n;
            scanf("%d%d%d%d", &F[0], &F[1], &F[2], &n);
            if (n < 3)
            {
                printf("%d
    ", F[n]);
                continue;
            }
            MARTIX A;
            for (int i = 0; i < 3; ++i)
            {
                A.mat[i][0] = i + 1;
            }
            A.mat[0][1] = 1;
            A.mat[1][2] = 1;  
            MARTIX F1;
            for (int i = 0; i < 3; ++i)
            {
                F1.mat[0][i] = F[2 - i];
            }
            MARTIX ans = fastpow(A, n - 2);
            ans = F1 * ans;
            printf("%d
    ", ans.mat[0][0]);
        }
        return 0;
    }

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    SpringMvc的服务器端跳转和客户端跳转
    springMvc的一些简介 和基于xml的handlerMapping基本流程
    springMvc 的参数验证 BindingResult result 的使用
    SpringMVC 学习笔记(二) @RequestMapping、@PathVariable等注解
    springmvc处理ajax请求
    取maven copy部分
    maven scope含义的说明
    Maven依赖中的scope详解
    EasyMock 使用方法与原理剖析
    Maven:Generating Project in Batch mode 卡住问题
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4838269.html
Copyright © 2011-2022 走看看