Time Limit: 1000MS Memory Limit: 65536K

Problem Description

Recall the definition of the Fibonacci numbers: 

f1 := 1 

f2 := 2 

fn := fn-1 + fn-2     (n>=3) 


Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b].

Input

The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a=b=0. Otherwise, a<=b<=10100. The numbers a and b are given with no superfluous leading zeros.

Output

For each test case output on a single line the number of Fibonacci numbers fi with a<=fi<=b.

Sample Input

10 100
1234567890 9876543210
0 0

Sample Output

5
4

Solving Ideas

首先利用大数打500左右的fib数,然后遍历这些fib数是否在 [a, b]区间内。

#include <stdio.h>
#include <string.h>
int cmp(char *a, char *b)
{
    int lena, lenb;
    lena = strlen(a);
    lenb = strlen(b);
    if (lena > lenb)
        return 1;
    if (lena < lenb)
        return 0;
    for (int i = 0; i < lena; i++)
    {
        if (a[i] > b[i])
            return 1;
        if (a[i] < b[i])
            return 0;
    }
    return 1;
}
int main()
{
    char fib[500][110], a[110], b[110];
    int s[500][110], i, j, c, p, k, sum, t = 1;
    strcpy(fib[1], "1");
    strcpy(fib[2], "2");
    memset(s, 0, sizeof(s));
    s[1][0] = 1, s[2][0] = 2;
    for (i = 3; i < 500; i++)
    {
        c = 0;
        for (j = 0; j < t; j++)
        {
            s[i][j] = s[i - 1][j] + s[i - 2][j] + c;
            c = s[i][j] / 10;
            s[i][j] %= 10;
        }
        if (c)
            s[i][t++] = c;
        p = t - 1;
        k = 0;
        while (p >= 0)
            fib[i][k++] = s[i][p--] + '0';
        fib[i][k] = '\0';
    }
    while (scanf("%s%s", a, b), a[0] - '0' || b[0] - '0')
    {
        sum = 0;
        for (i = 1; i < 500; i++)
        {
            if (cmp(fib[i], a) && cmp(b, fib[i]))
                sum++;
            if (!cmp(b, fib[i]))
                break;
        }
        printf("%d\n", sum);
    }
    return 0;
}