1. 程式人生 > >1sting 大數 遞推

1sting 大數 遞推

span i++ ive pri sstream class num sizeof name

You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ‘2’, or leave the ‘1’ there. Surly, you may get many different results. For example, given 1111 , you can get 1111, 121, 112,211,22. Now, your work is to find the total number of result you can get.

InputThe first line is a number n refers to the number of test cases. Then n lines follows, each line has a string made up of ‘1’ . The maximum length of the sequence is 200.
OutputThe output contain n lines, each line output the number of result you can get .
Sample Input

3
1
11
11111

Sample Output

1
2
8

如果最後一個數字和前一個合並。。f(n-2)
不合並 f(n-1)
fn = fn-2 + fn-1
大數!
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include
<map> #include<stack> #include<fstream> #include<set> #include<memory> #include<bitset> #include<string> #include<functional> using namespace std; typedef long long LL; const int MAXN = 1e6 + 9; #define INF 0x3f3f3f3f #define MAXN 203 int a[MAXN][MAXN];
char str[MAXN]; void add(int to[], int add[]) { if (to[0] < add[0]) to[0] = add[0]; for (int i = 1; i <= to[0]; i++) to[i] += add[i]; for (int i = 1; i <= to[0]; i++) { to[i + 1] += to[i] / 10; to[i] %= 10; } if (to[to[0] + 1] > 0) to[0]++; } void Init() { memset(a, 0, sizeof(0)); a[1][0] = 1, a[1][1] = 1; a[2][0] = 1, a[2][1] = 2; for (int i = 3; i < MAXN; i++) { add(a[i], a[i - 1]); add(a[i], a[i - 2]); } } void print(int p) { for (int i = a[p][0]; i > 0; i--) printf("%d", a[p][i]); printf("\n"); } int main() { int n, t; scanf("%d", &n); Init(); while (n--) { scanf("%s", &str); t = strlen(str); print(t); } }

1sting 大數 遞推