HDU-2842(递推+矩阵快速幂)

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.
Input
Each line of the input file contains a number N indicates the number of the rings on the bar. The last line of the input file contains a number "0".
Output
For each line, output an integer S indicates the least steps. For the integers may be very large, output S mod 200907.
Sample Input
1
4
0
Sample Output
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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值