标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256
最重要的是构建递推式,下面的图是盗来的。貌似这种叫共轭数。
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 const int mod = 1024; 8 struct data { 9 int mat[3][3]; 10 data() {} 11 data(int x, int y, int z1 = 0, int z2 = 0) { 12 mat[1][1] = x, mat[1][2] = y; 13 mat[2][1] = z1, mat[2][2] = z2; 14 } 15 }; 16 17 data operator* (data a, data b) { 18 data ans; 19 for(int i = 1; i <= 2; ++i) { 20 for(int j = 1; j <= 2; ++j) { 21 ans.mat[i][j] = 0; 22 for(int k = 1; k <= 2; ++k) 23 ans.mat[i][j] = (ans.mat[i][j] + a.mat[i][k] * b.mat[k][j] % mod) % mod; 24 } 25 } 26 return ans; 27 } 28 29 data operator^ (data a, int n) { 30 data ans; 31 for(int i = 1; i <= 2; ++i) { 32 for(int j = 1; j <= 2; ++j) { 33 ans.mat[i][j] = (i == j); 34 } 35 } 36 while(n) { 37 if(n & 1) 38 ans = ans * a; 39 a = a * a; 40 n >>= 1; 41 } 42 return ans; 43 } 44 45 int main() 46 { 47 int t, n; 48 scanf("%d", &t); 49 while(t--) { 50 scanf("%d", &n); 51 data ans(5, 2); 52 data a(5, 2, 12, 5); 53 a = a ^ (n - 1); 54 ans = ans * a; 55 printf("%d\n", (ans.mat[1][1] * 2 - 1 + mod) % mod); 56 } 57 return 0; 58 }
HDU 2256 Problem of Precision (矩阵快速幂)
标签:
原文地址:http://www.cnblogs.com/Recoder/p/5751455.html