hust1384---The value of F[n]

来源:互联网 发布:java中nio流详解 编辑:程序博客网 时间:2024/06/10 03:33

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

41 2 3 34 5 6 52 3 4 24 5 6 100000

Sample Output

1012946086

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
原创粉丝点击