HDU 2256 Problem of Precision (矩阵快速幂)

题目链接: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 }
View Code

 

转载于:https://www.cnblogs.com/Recoder/p/5751455.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值