题目信息:
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.
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(1) = 1;
a(2) = 2;
奇数:a(n) = a1+3/4*(2^(n-1)-1);
偶数:a(n) = a2+3/8*(2^(n-2)-1);
*/
/*********************************************************************/
//直接用通项公式求答案
#include "stdio.h"
#include "string.h"
#define MOD 10007
#define MOD_D 30021 int mypow(int a,int n) //快速幂
{
int y;
if(n==)
return ;
y = mypow(a,n/);
y = (y*y)%MOD_D;
if(n%==)
y *= a;
return y%MOD_D;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==) { printf("1\n"); continue; }
if(n==) { printf("2\n"); continue; }
if(n%==)
printf("%d\n",(+(*((mypow(,n-)-)%MOD_D)/)%MOD)%MOD);
else
printf("%d\n",(+(*((mypow(,n-)-)%MOD_D)/)%MOD)%MOD);
}
return ;
} /***********************************************************************/
//找循环节
#include "stdio.h"
#include "string.h" #define N 1000007
#define MOD 10007
int p[N] = {,,,}; int main()
{
int n;
for(n=; n<=N; ++n)
{
p[n] = (p[n-] + *p[n-] + )%MOD;
if(p[n]==p[] && p[n-]==p[])
break;
}
int k = n-;
while(scanf("%d",&n)!=EOF)
{
n = (n-)%k+;
printf("%d\n",p[n]);
}
return ;
} /******************************************************************/
//矩阵快速幂算法
#include "stdio.h"
#include "string.h"
#define MOD 10007 struct Matrix
{
int n,m;
int a[][];
}p0; Matrix Mult_mod(Matrix a,Matrix b)
{
Matrix c;
c.n = a.n;
c.m = b.m;
int i,j,k;
for(i=; i<a.n; ++i)
{
for(j=; j<a.m; ++j)
{
c.a[i][j] = ;
for(k=; k<a.m; ++k)
c.a[i][j] += (a.a[i][k]*b.a[k][j])%MOD;
}
}
return c;
} Matrix power_mod(Matrix a,int n)
{
if(n==)
return p0;
Matrix y = power_mod(a,n/);
y = Mult_mod(y,y);
if(n%==)
y = Mult_mod(y,p0);
return y;
} int main()
{
int n;
memset(p0.a,,sizeof(p0.a));
p0.n = ;
p0.m = ;
p0.a[][] = ;
p0.a[][] = ;
p0.a[][] = ;
p0.a[][] = ;
p0.a[][] = ;
Matrix ans;
while(scanf("%d",&n)!=EOF)
{
if(n==) printf("1\n");
else if(n==) printf("2\n");
else
{
ans = power_mod(p0,n-);
printf("%d\n",(*ans.a[][]+ans.a[][]+ans.a[][])%MOD);
} }
return ;
}