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: [email protected].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;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
时间: 2024-10-08 07:36:17