jiulianhuan 快速幂--矩阵快速幂

题目信息:

1471: Jiulianhuan

时间限制: 1 Sec   内存限制: 128 MB
提交: 95   解决: 22

题目描述

  For each data set in the input print on a separate line, on the standa  I think that you might have played the traditional Chinese ring game: The Chinese Linking Rings (here we call its nickname Jiulianhuan —— “九连环”). Well, you say you haven’t played it before? Then you must have seen it before, right? If not seen, come to borrow mine to have a good look at it and enjoy it!d output, the integer that represents the maximal amount of overtaking.

jiulianhuan 快速幂--矩阵快速幂_第1张图片

Now, I would like to mention the rules or common sense of Jiulianhuan again.
1) The first ring can put on or down the handles at any time. That is, when the first ring is under the handle, it can climb up the handle within one step, and vice versa.
2) At any moment, you can only operate one ring, on the condition that the ring is operable.
3) If the first k-2 rings are under the handle, and the (k-1)th ring is on the handle, then if the k-th ring is under the handle, you can put it on the handle, and if it is not under the handle, you can put it down the handle.
Seems complicated? But I tried my simplest explanation to you, and I hope its not hard for you to understand. Maybe you have played the game before,  and the above is what actually a “step” means in the game. 

输入

  Given n (not bigger than 10^8), you are to output the minimum steps it needs to down n well-put rings. There are no more than 100 test cases.

输出

  A number a line. Because the number are so huge ,you are to output the result after it mod prime 10007.

样例输入

1
2
9
1005

样例输出

1
2
341
4260

提示


/*
由题意可推知:
a(1)=1;
a(2)=2;
a(3)=5;
a(4)=10;
a(n) = a(n-1)+2*a(n-2)+1;
求得通项公式:
    a(1) = 1;
    a(2) = 2;
    奇数:a(n) = a1+3/4*(2^(n-1)-1);
    偶数:a(n) = a2+3/8*(2^(n-2)-1);

*/

三种方法实现该题:
/*********************************************************************/
  1 //直接用通项公式求答案
  2 #include "stdio.h"
  3 #include "string.h"
  4 #define MOD 10007
  5 #define MOD_D 30021
  6 
  7 int mypow(int a,int n)  //快速幂
  8 {
  9     int y;
 10     if(n==0)
 11         return 1;
 12     y = mypow(a,n/2);
 13     y = (y*y)%MOD_D;
 14     if(n%2==1)
 15         y *= a;
 16     return y%MOD_D;
 17 }
 18 
 19 int main()
 20 {
 21     int n;
 22     while(scanf("%d",&n)!=EOF)
 23     {
 24         if(n==1) { printf("1\n"); continue; }
 25         if(n==2) { printf("2\n"); continue; }
 26         if(n%2==1)
 27             printf("%d\n",(1+(4*((mypow(2,n-1)-1)%MOD_D)/3)%MOD)%MOD);
 28         else
 29             printf("%d\n",(2+(8*((mypow(2,n-2)-1)%MOD_D)/3)%MOD)%MOD);
 30     }
 31     return 0;
 32 }
 33 
 34 /***********************************************************************/
 35 //找循环节
 36 #include "stdio.h"
 37 #include "string.h"
 38 
 39 #define N 1000007
 40 #define MOD 10007
 41 int p[N] = {0,1,2,5};
 42 
 43 int main()
 44 {
 45     int n;
 46     for(n=4; n<=N; ++n)
 47     {
 48         p[n] = (p[n-1] + 2*p[n-2] + 1)%MOD;
 49         if(p[n]==p[2] && p[n-1]==p[1])
 50             break;
 51     }
 52     int k = n-2;
 53     while(scanf("%d",&n)!=EOF)
 54     {
 55         n = (n-1)%k+1;
 56         printf("%d\n",p[n]);
 57     }
 58     return 0;
 59 }
 60 
 61 /******************************************************************/
 62 //矩阵快速幂算法
 63 #include "stdio.h"
 64 #include "string.h"
 65 #define MOD 10007
 66 
 67 struct Matrix
 68 {
 69     int n,m;
 70     int a[3][3];
 71 }p0;
 72 
 73 Matrix Mult_mod(Matrix a,Matrix b)
 74 {
 75     Matrix c;
 76     c.n = a.n;
 77     c.m = b.m;
 78     int i,j,k;
 79     for(i=0; i<a.n; ++i)
 80     {
 81         for(j=0; j<a.m; ++j)
 82         {
 83             c.a[i][j] = 0;
 84             for(k=0; k<a.m; ++k)
 85                 c.a[i][j] += (a.a[i][k]*b.a[k][j])%MOD;
 86         }
 87     }
 88     return c;
 89 }
 90 
 91 Matrix power_mod(Matrix a,int n)
 92 {
 93     if(n==1)
 94         return p0;
 95     Matrix y = power_mod(a,n/2);
 96     y = Mult_mod(y,y);
 97     if(n%2==1)
 98         y = Mult_mod(y,p0);
 99     return y;
100 }
101 
102 int main()
103 {
104     int n;
105     memset(p0.a,0,sizeof(p0.a));
106     p0.n = 3;
107     p0.m = 3;
108     p0.a[0][0] = 1;
109     p0.a[0][1] = 1;
110     p0.a[1][0] = 2;
111     p0.a[2][0] = 1;
112     p0.a[2][2] = 1;
113     Matrix ans;
114     while(scanf("%d",&n)!=EOF)
115     {
116         if(n==1) printf("1\n");
117         else if(n==2) printf("2\n");
118         else
119         {
120             ans = power_mod(p0,n-2);
121             printf("%d\n",(2*ans.a[0][0]+ans.a[1][0]+ans.a[2][0])%MOD);
122         }
123 
124     }
125     return 0;
126 }

 




 

你可能感兴趣的:(矩阵)