hust1384---The value of F[n]

来源:互联网 发布:java中nio流详解 编辑:程序博客网 时间:2024/05/12 14:45

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 生完孩子肛门突出怎么办 生完孩子肋骨突出怎么办 蛙跳理蛙跳后腿疼怎么办 蛙跳两天后腿还疼怎么办 莲花菩提盘黑了怎么办 体育课蛙跳后肌肉拉伤怎么办 o型腿骨头弯了怎么办 小孩钢琴坐姿不对向后仰怎么办 小孩皮肤不好容易留疤怎么办 学游泳时站不稳怎么办 水呛到了不停打嗝怎么办 来月经前游泳了怎么办 快来完事游泳了怎么办 游泳时来月经了怎么办 经期第7天游泳了怎么办 来月经已经游了泳怎么办 月经来了要游泳怎么办 三个月宝宝趴着不会抬头怎么办 我的月经不完怎么办 游泳时怎么办能浮出水面 游泳时眼镜起雾怎么办 练瑜伽手臂变粗怎么办 孕妇喝了芬达怎么办 宫口开了但头高怎么办 整天坐着肚子越来越大怎么办 坐久了屁股变大怎么办 屁股久坐的黑印怎么办 练瑜伽小腿变粗怎么办 练瑜伽腿粗了怎么办 练完瑜伽腿粗怎么办 肚子大屁股大腿粗怎么办 甲状腺偏大弥漫性欠均匀怎么办 大腿后侧拉伤了怎么办 腰和臀部都疼怎么办 瑜伽后弯后腰疼怎么办? 吃了大蒜肝痛怎么办 练瑜伽前躯不行怎么办 月子里碰冷水了怎么办 月子手碰凉水了怎么办 月子里碰了冷水怎么办 顺产后下面会痛怎么办