HDU - 3306 Another kind of Fibonacci 矩阵快速幂
来源:互联网 发布:codesys编程手册 教材 编辑:程序博客网 时间:2024/04/28 10:19
题目大意:A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2).
And we want to Calculate S(N) , S(N) = A(0) 2 +A(1) 2+……+A(n) 2.
解题思路:将An^2化开,得x * x * A(n-1) * A(n-1) + y * y * A(n-2) * A(n-2) + 2 * x * y * A(n-1) * A(n-2),由这个公式就可以得到矩阵了
#include<cstdio>typedef long long ll;const int N = 4;const ll mod = 10007;struct Matrix{ ll mat[N][N];}A, B, tmp;ll n, X, Y;void init() { for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) { A.mat[i][j] = B.mat[i][j] = 0; if(i == j) B.mat[i][j] = 1; } A.mat[0][0] = A.mat[1][2] = 1; A.mat[1][0] = A.mat[1][1] = (X * X) % mod; A.mat[2][0] = A.mat[2][1] = (Y * Y) % mod; A.mat[3][0] = A.mat[3][1] = (2 * X * Y) % mod; A.mat[1][3] = X % mod; A.mat[3][3] = Y % mod;}Matrix matMul(Matrix x, Matrix y) { for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) { tmp.mat[i][j] = 0; for(int k = 0; k < N; k++) tmp.mat[i][j] += (x.mat[i][k] * y.mat[k][j] ) % mod; } return tmp;}void solve() { while(n) { if(n & 1) B = matMul(B,A); A = matMul(A,A); n >>= 1; }}int main() { while(scanf("%I64d%I64d%I64d", &n, &X, &Y) != EOF) { if(n == 0) { printf("1\n"); continue; } else if(n == 1) { printf("2\n"); continue; } init(); n--; solve(); printf("%I64d\n", (2 * B.mat[0][0] + B.mat[1][0] + B.mat[2][0] + B.mat[3][0] ) % mod); } return 0;}
0 0
- hdu 3306 Another kind of Fibonacci 矩阵快速幂
- hdu 3306 Another kind of Fibonacci ---矩阵快速幂
- HDU 3306 Another kind of Fibonacci(矩阵快速幂)
- hdu 3306 Another kind of Fibonacci(矩阵快速幂)
- HDU - 3306 Another kind of Fibonacci 矩阵快速幂
- [HDU 3306] Another kind of Fibonacci · 矩阵快速幂
- HDU 3306 Another kind of Fibonacci (矩阵快速幂)
- hdu 3306 Another kind of Fibonacci(矩阵快速幂)
- hdoj 3306 Another kind of Fibonacci 【矩阵快速幂】
- hdu3306--Another kind of Fibonacci(矩阵快速幂)
- Another kind of Fibonacci(hdu3306)矩阵快速幂
- hdu 3306 Another kind of Fibonacci 矩阵连乘
- HDU 3306 Another kind of Fibonacci 矩阵连乘
- 矩阵十题【四】 HDU 3306 Another kind of Fibonacci
- hdu 3306 Another kind of Fibonacci(矩阵连乘)
- Another kind of Fibonacci(矩阵)
- hdu 3306 Another kind of Fibonacci
- hdu 3306 Another kind of Fibonacci
- 例题6-8 树 Tree UVa 548 中序遍历+后序遍历建树
- What is a cheek press event? 啥是“脸颊触碰”事件?
- eclipse 更改编辑器风格方法
- 南宁肛门处有一小块肉脱出
- 踏入ios
- HDU - 3306 Another kind of Fibonacci 矩阵快速幂
- Git相关配置
- 南宁肛门旁边长了个小疙瘩
- 南宁经常拉肚子是什么原因
- 南宁肛肠病手术需要住院吗
- 南宁哪里治疗肛肠病比较好
- gpu渲染性能调优
- 南宁治肛门瘙痒需要住院吗
- 南宁经常肛门痒是怎么回事