HDU 5686 Problem B

Problem Description
  度熊面前有一个全是由1构成的字符串,被称为全1序列。你可以合并任意相邻的两个1,从而形成一个新的序列。对于给定的一个全1序列,请计算根据以上方法,可以构成多少种不同的序列。
 

Input
这里包括多组测试数据,每组测试数据包含一个正整数 N ,代表全1序列的长度。

1N200
 

Output
对于每组测试数据,输出一个整数,代表由题目中所给定的全1序列所能形成的新序列的数量。
 

Sample Input
  
  
1 3 5
 

Sample Output
  
  
1 3 8
Hint
如果序列是:(111)。可以构造出如下三个新序列:(111), (21), (12)。
总结一下其实就是斐波那契数列,用高精度搞一下即可,需要注意的是测试数据有0,输出为换行。
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 200 + 10;

struct bignum
{
    vector<int> p;
    void operator=(int x)
    {
        if (!x) p.push_back(0);
        while (x) { p.push_back(x % 10); x /= 10; }
    }
    void write()
    {
        for (int i = p.size(); i; i--) printf("%d", p[i - 1]);
        printf("\n");
    }
};

bignum operator+(bignum a, bignum b)
{
    bignum c;
    int j = 0;
    for (int i = 0; i < max(a.p.size(), b.p.size()); i++)
    {
        int l = i < a.p.size() ? a.p[i] : 0;
        int r = i < b.p.size() ? b.p[i] : 0;
        c.p.push_back((l + r + j) % 10);
        j = (l + r + j) / 10;
    }
    if (j) c.p.push_back(j);
    return c;
}

bignum ans[maxn];
int n;

int main()
{
    ans[1] = 1; ans[2] = 2;
    for (int i = 3; i < maxn; i++) ans[i] = ans[i - 1] + ans[i - 2];
    while (scanf("%d", &n) != EOF)
    {
        if (!n) printf("\n");
        else ans[n].write();
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值