统计斐波拉契数字

Problem E

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 11   Accepted Submission(s) : 5

Font: Times New Roman | Verdana | Georgia

Font Size:

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<=10^100. 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

#include<iostream>
#include<string>
using namespace std;
string lmx(string a,string b)
{
 string max,min;
 int i,len1,len2,j;
 if(a.length()>b.length())  {max=a;min=b;}
 else {max=b;min=a;}
 len1=max.length();
 len2=min.length();
 for(i=len1-1,j=len2-1;j>=0;j--,i--)  max[i]=max[i]+min[j]-'0';
 for(i=len1-1;i>=1;i--)
 {
        if(max[i]>'9')  {max[i-1]++;max[i]-=10;}
 }
 if(max[0]>'9')
 {
  max[0]-=10;max='1'+max;
 }
 return max;
}
bool gcd(string a,string b)
{
 int i;
 if(a.length()>b.length())  return true;
 else if(a.length()<b.length()) return false;
 else
 {
  for(i=0;i<a.length();i++)
  {
   if(a[i]>b[i])  return true;
   else if(a[i]<b[i]) return false;
  }
 }
 return true;
}
int main()
{
string mx[500],s1,s2;
int i,n;
mx[1]="1";
mx[2]="2";
for(i=3;i<=498;i++)
{
 mx[i]=lmx(mx[i-1],mx[i-2]);
}
while(cin>>s1>>s2)
{
 if(s1=="0"&&s2=="0") break;
 n=0;
 for(i=1;;i++)
 {
  if(gcd(mx[i],s1)&&gcd(s2,mx[i]))  n++;
  if(gcd(mx[i],s2))  break;
 }
 cout<<n<<endl;
}
 return 0;
}
posted @ 2012-11-25 11:34  forevermemory  阅读(171)  评论(0编辑  收藏  举报