hdu 2256 神奇的矩阵

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

感觉矩阵好神奇啊。。。

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

hdu 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 }

 

你可能感兴趣的:(HDU)