E - 1sting (递推+大数加法)

来源:互联网 发布:天猫淘宝棉拖鞋 编辑:程序博客网 时间:2024/04/30 21:54

根据题意递推构造类fibnacci数列,由于要算到第200项,已经远远超出long long 所能表示的范围。

要用大数加法。即用数组模拟。


Description

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. 
 

Input

The 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. 
 

Output

The output contain n lines, each line output the number of result you can get . 
 

Sample Input

311111111
 

Sample Output

128
 

#include<cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;typedef long long ll;char s[210];char f[210][210];char s1[210],s2[210];void init(){    int l1,l2;    memset(f,0,sizeof(f));    strcpy(f[1],"1");    strcpy(f[2],"2");    strcpy(f[3],"3");    for(int k=4;k<=200;k++)    {        strcpy(s1,f[k-1]);        strcpy(s2,f[k-2]);        l1 = strlen(s1);        l2 = strlen(s2);        int i=0,c = 0,x = 0;        while(i<l1 && i<l2)        {            int t = s1[i]-'0'+s2[i]-'0'+c;            if(t>=10)            {                c = t/10;    //这里不是简单的c=1                f[k][x++] = t%10+'0';            }            else            {                f[k][x++] = t+'0';                c = 0;     //这里忘记清0            }            i++;        }        while(i<l1)        {            if(c)            {                int t = (s1[i]-'0'+c);                f[k][x++] = t%10+'0';                i++;                c = t/10;            }            else                f[k][x++] = s1[i++];        }        while(i<l2)        {            if(c)            {                int t = (s2[i]-'0'+c);                f[k][x++] = t%10+'0';                i++;                c = t/10;            }            else f[k][x++] = s2[i++];        }        if(c)            f[k][x++] = c+'0';    }}int main(){    int T;    init();    scanf("%d",&T);    while(T--)    {        scanf("%s",s);        int len = strlen(s);        int p = strlen(f[len]);        for(int i=p-1;i>=0;i--)            printf("%c",f[len][i]);        printf("\n");    }    return 0;}




0 0