hdu2842---Chinese Rings(矩阵)

94 篇文章 0 订阅

Problem Description
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

Source
2009 Multi-University Training Contest 3 - Host by WHU

Recommend
gaojie | We have carefully selected several similar problems for you: 2841 2844 2843 2840 2839

如果前k个环被拆掉,第k+1个还被挂着,那么第k+2个就可以拿下或者装上
设f[n]表示拆掉前n个环需要的步数
显然要先把前n-2个拿掉:f[n-2]
拿掉第n个:1步
剩下第n-1个,如果要拆它,那么第n-2个必须挂着,根据题目意思,需要把前n-2个再次挂上,接下来的就是f[n-1]
f[n] = 2 * f[n - 2] + f[n - 1] + 1
然后推出系数矩阵,矩阵快速幂即可

/*************************************************************************
    > File Name: hdu2842.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月12日 星期四 15时42分57秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int mod = 200907;

class MARTIX
{
    public:
        LL mat[4][4];
        MARTIX();
        MARTIX operator * (const MARTIX &b)const;
        MARTIX& operator = (const MARTIX &b);
};

MARTIX :: MARTIX()
{
    memset (mat, 0, sizeof(mat));
}

MARTIX MARTIX :: operator * (const MARTIX &b)const
{
    MARTIX ret;
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            for (int k = 0; k < 3; ++k)
            {
                ret.mat[i][j] += this -> mat[i][k] * b.mat[k][j];
                ret.mat[i][j] %= mod;
            }
        }
    }
    return ret;
}

MARTIX& MARTIX :: operator = (const MARTIX &b)
{
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            this -> mat[i][j] = b.mat[i][j];
        }
    }
    return *this;
}

MARTIX fastpow(MARTIX A, int n)
{
    MARTIX ans;
    ans.mat[0][0] = ans.mat[1][1] = ans.mat[2][2] = 1;
    while (n)
    {
        if (n & 1)
        {
            ans = ans * A;
        }
        n >>= 1;
        A = A * A;
    }
    return ans;
}

void Debug(MARTIX A)
{
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            printf("%lld ", A.mat[i][j]);
        }
        printf("\n");
    }
}

int main ()
{
    int n;
    MARTIX A;
    A.mat[0][0] = 1;
    A.mat[0][1] = 1;
    A.mat[1][0] = 2;
    A.mat[2][0] = 1;
    A.mat[2][2] = 1;
//  Debug(A);
    while (~scanf("%d", &n))
    {
        if (!n)
        {
            break;
        }
        if (n < 3)
        {
            printf("%d\n", n);
            continue;
        }
        MARTIX ans = fastpow(A, n - 1);
        MARTIX F;
        F.mat[0][0] = 1;
        F.mat[0][2] = 1;
        ans = F * ans;
        printf("%lld\n", ans.mat[0][0]);
    }
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值