LightOJ1006---Hex-a-bonacci(矩阵快速幂)
来源:互联网 发布:淘宝创始人是谁 编辑:程序博客网 时间:2024/06/04 08:38
Given a code (not optimized), and necessary inputs, you have to find the output of the code for the inputs. The code is as follows:
int a, b, c, d, e, f;int fn( int n ) { if( n == 0 ) return a; if( n == 1 ) return b; if( n == 2 ) return c; if( n == 3 ) return d; if( n == 4 ) return e; if( n == 5 ) return f; return( fn(n-1) + fn(n-2) + fn(n-3) + fn(n-4) + fn(n-5) + fn(n-6) );}int main() { int n, caseno = 0, cases; scanf("%d", &cases); while( cases-- ) { scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &n); printf("Case %d: %d\n", ++caseno, fn(n) % 10000007); } return 0;}
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains seven integers, a, b, c, d, e, f and n. All integers will be non-negative and 0 ≤ n ≤ 10000 and the each of the others will be fit into a 32-bit integer.
Output
For each case, print the output of the given code. The given code may have integer overflow problem in the compiler, so be careful.
Sample Input
Output for Sample Input
5
0 1 2 3 4 5 20
3 2 1 5 0 1 9
4 12 9 4 5 6 15
9 8 7 6 5 4 3
3 4 3 2 54 5 4
Case 1: 216339
Case 2: 79
Case 3: 16636
Case 4: 6
Case 5: 54
很明显可以构造出一个6*6的矩阵
然后矩阵快速幂就行
/************************************************************************* > File Name: LightOJ1006.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年06月03日 星期三 21时17分35秒 ************************************************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace std;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;static const int mod = 10000007;class MARTIX { public: LL mat[6][6]; MARTIX(); MARTIX operator * (const MARTIX &ret)const; MARTIX& operator = (const MARTIX &ret);};MARTIX :: MARTIX() { memset(mat, 0, sizeof(mat));}MARTIX MARTIX :: operator * (const MARTIX &ret) const { MARTIX ans; for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { for (int k = 0; k < 6; ++k) { ans.mat[i][j] += this-> mat[i][k] * ret.mat[k][j]; ans.mat[i][j] %= mod; } } } return ans;}MARTIX& MARTIX :: operator = (const MARTIX &ret) { for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { this -> mat[i][j] = ret.mat[i][j]; } } return *this;}MARTIX fastpow(MARTIX A, int cnt) { MARTIX ans; for (int i = 0; i < 6; ++i) { ans.mat[i][i] = 1; } while (cnt) { if (cnt & 1) { ans = ans * A; } cnt >>= 1; A = A * A; } return ans;}LL f[10];int main() { int t, icase = 1; MARTIX Base; for (int i = 0; i < 6; ++i) { Base.mat[i][0] = 1; } for (int i = 1; i < 6; ++i) { Base.mat[i - 1][i] = 1; } scanf("%d", &t); while (t--) { int n; for (int i = 0; i < 6; ++i) { scanf("%lld", &f[i]); f[i] %= mod; } scanf("%d", &n); if (n <= 5) { printf("Case %d: %lld\n", icase++, f[n]); continue; } MARTIX ret; for (int i = 0; i < 6; ++i) { ret.mat[0][i] = f[5 - i]; } MARTIX B = fastpow(Base, n - 5); B = ret * B; printf("Case %d: %lld\n", icase++, B.mat[0][0]); } return 0;}
- LightOJ1006---Hex-a-bonacci(矩阵快速幂)
- lightOj1006 Hex-a-bonacci
- LightOj 1006 Hex-a-bonacci(矩阵快速幂)
- 【LightOJ】1006 - Hex-a-bonacci(矩阵快速幂)
- Lightoj1006——Hex-a-bonacci(递归转递推)
- LightOj 1006 - Hex-a-bonacci
- LightOJ 1006 Hex-a-bonacci
- LightOJ 1006 :Hex-a-bonacci
- LightOJ 1006 - Hex-a-bonacci
- LightOJ-1006-Hex-a-bonacci
- Light OJ 1006 Hex-a-bonacci
- lightoj-1006-Hex-a-bonacci【思维】
- LightOJ 1006 C Hex a bonacci (取模、水~)
- Light OJ:1006 Hex-a-bonacci(水题)
- LIGHT OJ 1006 - Hex-a-bonacci 【化简递推式(DP)】
- 【light-oj】-1006 - Hex-a-bonacci(思维)
- lightoj 1006 - Hex-a-bonacci (FOR循环)
- lightoj1006
- lua递归函数的编写,为了解决一个游戏当中遇到的复杂问题
- android-async-http开源框架的详细解释(附源码HTTP)
- 5.1-2
- 如何做个好员工
- ubuntu 15.04 sublime text 3
- LightOJ1006---Hex-a-bonacci(矩阵快速幂)
- Asp.Net alert弹出提示信息的5种方法
- db2常用的命令
- PHOTOSHOP图层混合模式的计算公式
- 面试题7: 静态变量和实例变量的区别?
- vmware 切换输入法
- Java 获取子网掩码
- lua实现一个小数取整数部分
- 谁是罪犯逻辑问题