1sting 大数 递推

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);
    }
}
时间: 2024-10-13 15:10:53

1sting 大数 递推的相关文章

POJ 1737 Connected Graph (大数+递推)

题目链接: http://poj.org/problem?id=1737 题意: 求 \(n\) 个点的无向简单连通图的个数.\((n<=50)\) 题解: 这题你甚至能OEIS. http://oeis.org/A001187 但不支持这样做.TAT 间接做. 总方案数减去不合法方案. 因为\(n\)个点的完全图有 \(C(n,2)={n(n-1) \over 2}\) 条边,显然就有 \(2^{C(n,2)}\) 种子图,即枚举每条边是否选择. 设$ f[i]$ 表示每个点都和点 \(1\)

UVA 10254-The Priest Mathematician(大数+递推)

题目大意:在原本的汉诺塔游戏基础上加一根柱子,移动策略是:要移动N个汉诺塔,先用4根柱子把K个到一个柱子,然后用其余3根把剩下的N-K个移动到目标柱子,再用4根把初始的K个移动到目标柱子. 关键的问题是找到每个N的K是多少,观察可以发现规律是:随着K的递增,其实移动的次数Fn(K)先递增后递减,然后F1(K),F2(K),...的最大值随着K的增大递增.要形式化证明似乎比较困难..不过在题目的范围内这是可以AC的.程序中维护这个当前使得Fi(K)最大的K,然后递推. import java.ut

HDU 1865 1sting (递推、大数)

1sting Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7573    Accepted Submission(s): 2945 Problem Description You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’

UVa 10328 Coin Toss(Java大数+递推)

https://vjudge.net/problem/UVA-10328 题意: 有H和T两个字符,现在要排成n位的字符串,求至少有k个字符连续的方案数. 思路:这道题目和ZOJ3747是差不多的,具体做法可以参考另一篇博客http://www.cnblogs.com/zyb993963526/p/7203833.html 但是这道题目的话是要用大数来做的,c++感觉不太好写,就学了下java的做法. 1 import java.math.BigInteger; 2 import java.ut

CodeVs 3150 (大数 + 递推)

#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<map> #include<set> #include<vector> #include<queue> #include<stack> //#in

poj2506 大数递推

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8205   Accepted: 3974 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

2506Tiling(大数递推)

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8200   Accepted: 3970 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

题目链接:uva 1478 - Delta Wave 题目大意:对于每个位置来说,可以向上,水平,向下,坐标不能位负,每次上下移动最多为1, 给定n问说有多少种不同的图.结果对10100取模. 解题思路:因为最后都要落回y=0的位置,所以上升的次数和下降的次数是相同的,并且上升下降的关系满足出栈入栈的关系.即卡特兰数. 所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和. C(2?in)?f(i)=C(2?i?2n)?f(i?1)?(n?2?i+1)?(n?2?i+2)

ACM学习历程—HDU1041 Computer Transformation(递推 &amp;&amp; 大数)

Description A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, afte