HDU 2045 不容易系列之(3)—— LELE的RPG难题

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2045

突然想起来,这种递推题都是可以用数学方法推出公式做出来的.<<算法分析>>书上有讲.数学方法好牛叉啊..把系数解出来就AC了..

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        if(n==1) cout<<3<<endl;
        else
            cout<<(__int64)(pow(2.0,n)+2*pow(-1.0,n))<<endl;
    }
    return 0;
}

递推解法

#include <math.h>
#include <stdio.h>

int main(void)
{
    int i;
    __int64 d[51] = {0, 3, 6, 6};

    for (i = 4; i < 51; i++)
        d[i] = d[i-1] + 2*d[i-2];
    while (scanf("%d", &i) != EOF)
        printf("%I64d\n", d[i]);

    return 0;
}

 

posted @ 2013-09-21 20:56  Destino74  阅读(143)  评论(0编辑  收藏  举报