hdu 2256 神奇的矩阵

本文提供了一种使用矩阵快速幂解决HDU 2256问题的方法,通过初始化特定矩阵并利用矩阵乘法及快速幂运算求解。代码实现了题目要求的功能,并给出了解题思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256

感觉矩阵好神奇啊。。。

盗张图,看了就知道怎么做了。。。

好吧,直接上代码了,问题转化是难点

View Code
 1 #include<iostream>
 2 const int m=1024;
 3 using namespace std;
 4 int n;
 5 struct Matrix{
 6     int map[2][2];
 7 };
 8 Matrix mat;
 9 
10 void Initiate(){
11     mat.map[0][0]=5;
12     mat.map[0][1]=12;
13     mat.map[1][0]=2;
14     mat.map[1][1]=5;
15 }
16 
17 //矩阵相乘
18 Matrix Mul(Matrix &a,Matrix &b){
19     Matrix c;
20     for(int i=0;i<2;i++){
21         for(int j=0;j<2;j++){
22             c.map[i][j]=0;
23             for(int k=0;k<2;k++){
24                 c.map[i][j]+=a.map[i][k]*b.map[k][j];
25                 c.map[i][j]%=m;
26             }    
27         }
28     }
29     return c;
30 }
31 
32 //矩阵快速幂
33 Matrix Pow(int n){
34     if(n==1)return mat;
35     else if(n&1){
36         return Mul(mat,Pow(n-1));
37     }else {
38         Matrix temp=Pow(n>>1);
39         return Mul(temp,temp);
40     }
41 }
42 
43 int main(){
44     int _case;
45     scanf("%d",&_case);
46     while(_case--){
47         scanf("%d",&n);
48         Initiate();
49         mat=Pow(n);
50         int ans=(mat.map[0][0]*2-1)%m;
51         printf("%d\n",ans);
52     }
53     return 0;
54 }

 

转载于:https://www.cnblogs.com/wally/archive/2013/03/01/2939318.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值