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
- hust1384---The value of F[n]
- The Value of Time
- the value of ESP...
- The value of first company
- THE VALUE OF A SMILE
- The value given f…
- the most right digit of N^N.
- The Value of Forums - Finding Forums
- About the return value of SetTimer function
- Modify the value of Dictionary in circle
- swap two value of the same type
- DLL 调用错误 -The value of ESP
- The potential utilization value of vertical mill
- The value (166) of MAXTRANS parameter ignored.
- Mining the Network Value of Customers
- the sum of two fixed value
- TOJ 3758: The value of poetry
- The Definition of Key Value Coding and Key Value Observing
- Android中相册的数据结构
- eclipse下ruby调试环境整理
- 2.1 Socket系统调用
- 预热EBS卷Pre-Warming Amazon EBS Volumes
- Android下强制显示ActionBar的overflow按钮
- hust1384---The value of F[n]
- 【高效程序设计——贪心】UVa 10954 优先队列
- Swift from Scratch: Collections and Tuples
- Ajax缓存解决办法
- 字符串类型间的转化
- 《乐者为王——自由软件Linux之父李纳斯·托沃兹自述》
- 7款Android开发者常用的Android UI组件
- 【python学习笔记】网络爬虫的完整源代码
- oracle编程规范2