Dumbear likes to play the Chinese Rings (Baguenaudier). It’s a game played with nine rings on a bar. The rules of this game are very simple: At first, the nine rings are all on the bar.
The first ring can be taken off or taken on with one step.
If the first k rings are all off and the (k + 1)th ring is on, then the (k + 2)th ring can be taken off or taken on with one step. (0 ≤ k ≤ 7)
Now consider a game with N (N ≤ 1,000,000,000) rings on a bar, Dumbear wants to make all the rings off the bar with least steps. But Dumbear is very dumb, so he wants you to help him.
The first ring can be taken off or taken on with one step.
If the first k rings are all off and the (k + 1)th ring is on, then the (k + 2)th ring can be taken off or taken on with one step. (0 ≤ k ≤ 7)
Now consider a game with N (N ≤ 1,000,000,000) rings on a bar, Dumbear wants to make all the rings off the bar with least steps. But Dumbear is very dumb, so he wants you to help him.
1 4 0
1 10
/*
题意:
大家都知道九连环吧,如果要拆第n个环,那么第n-1个环就必须在竿上,
前n-2个环都必须已经被拆下,题目是现在是一个n连环,求将n连环全部
拆掉需要的最少的步骤%200907.
题解:
如果要拆第n个环,那么第n-1个环就必须在竿上,前n-2个环都必须已经被
拆下;假设f(n)表示拆第n个环需要的最少步数,那么拆第n个环的时候,第
n-1个环在竿上,前n-2个环已经被拆下,那么f(n) = f(n-2)+1,加1是因为
拆环的时候需要一步,接下来只剩下第n-1个环了,拆第n-1个环时,第n-2个
环必须在竿上,那么就需要在将第n-2个环装在竿上,需要f(n-2)步,再拆第
n-1个环,又需要f(n-1)步,则得出递推公式:
f(n)= f(n-2)+1+ f(n-2)+f(n-1),即f(n) = 2*f(n-2)+f(n-1)+1;
所以构造矩阵为:| 1 2 1 | | f(n-1) | | f(n) |
| 1 0 0 | * | f(n-2) | = | f(n-1) |
| 0 0 1 | | 1 | | 1 |
*/
#include <stdio.h>
#include <string.h>
#include<iostream>
#include <algorithm>
using namespace std;
#define LL __int64
struct node
{
LL maxtri[6][6];
};
node res, roi;
LL n, kk = 200907;
node operator * (node &a, node &b)
{
node ans;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
ans.maxtri[i][j] = 0;
for (int k = 0; k < 3; k++)
{
ans.maxtri[i][j] += a.maxtri[i][k] * (b.maxtri[k][j] % kk) % kk;
ans.maxtri[i][j] %= kk;
}
}
}
return ans;
}
void quickmuti(LL nn)
{
node temp;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
temp.maxtri[i][j] = (i == j);
}
}
while (nn)
{
if (nn & 1)
{
temp = temp*roi;
}
nn >>= 1;
roi = roi*roi;
}
res = temp;
}
void init()
{
memset(roi.maxtri, 0, sizeof(roi.maxtri));
memset(res.maxtri, 0, sizeof(res.maxtri));
roi.maxtri[0][0] = roi.maxtri[0][2] = roi.maxtri[1][0] = roi.maxtri[2][2] = 1;
roi.maxtri[0][1] = 2;
res.maxtri[0][0] = 1;
res.maxtri[1][1] = 1;
res.maxtri[2][2] = 1;
}
int main()
{
while (scanf("%I64d", &n) != EOF)
{
if (n == 0)
return 0;
int num[3] = { 1,1,2 };
if (n <= 2)
{
cout << num[n] << endl;
continue;
}
init();
quickmuti(n - 2);
LL ans = 0;
for (int i = 0; i < 3; i++)
{
ans += res.maxtri[0][i] * num[2 - i] % kk;
ans %= kk;
}
printf("%I64d\n", ans);
}
return 0;
}