Problem Description
度熊面前有一个全是由1构成的字符串,被称为全1序列。你可以合并任意相邻的两个1,从而形成一个新的序列。对于给定的一个全1序列,请计算根据以上方法,可以构成多少种不同的序列。
Input
这里包括多组测试数据,每组测试数据包含一个正整数
N
,代表全1序列的长度。
1≤N≤200
1≤N≤200
Output
对于每组测试数据,输出一个整数,代表由题目中所给定的全1序列所能形成的新序列的数量。
Sample Input
1 3 5
Sample Output
1 3 8Hint如果序列是:(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; }