hust1384---The value of F[n]

2 篇文章 0 订阅

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

水题,很容易推出转移矩阵

/*************************************************************************
    > 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("\n");
    }
}

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\n", 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\n", ans.mat[0][0]);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值