How many Fibs?
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12413 | Accepted: 4431 |
Description
Recall the definition of the Fibonacci numbers:
Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b].
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
Source
Ulm Local 2000
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<fstream>
const int N=1000;
using namespace std;
ofstream out;
struct bignumber{
int len;
int c[N];
void setnum(char s[]){
len=0;
memset(c,0,sizeof(c));
for(int i=strlen(s)-1;i>=0;i--){
c[len++]=s[i]-'0';
}
}
bool iszero() {
return len==1&&c[0]==0;
}
}big[1000];
void add(bignumber &a,bignumber &b,bignumber &c){
int i, carry=0;
c.len=a.len+b.len-1;
for (i=0;i<c.len;i++){
c.c[i] = carry + a.c[i] + b.c[i];
carry = c.c[i] / 10;
c.c[i] %= 10;
}
if(carry){
c.c[c.len++]=carry;
}else{
while(!c.c[c.len]){
c.len--;
}c.len++;
}
}
char s[N],t[N];
bignumber a,b;
bool cmp(bignumber n,bignumber m){
if(n.len==m.len){
int len=n.len;
while(len>=0){
if(n.c[len]!=m.c[len])
return n.c[len]<m.c[len];
len--;
}
return true;
}
return n.len<m.len;
}
int main(){
big[0].setnum("1");
big[1].setnum("2");
//out<<'"'<<'1'<<'"'<<',';
for(int i=2;i<500;i++){
add(big[i-2],big[i-1],big[i]);
}
int n,cnt1,cnt2;
while(scanf("%s%s",s,t)!=EOF){
a.setnum(s);
b.setnum(t);
if(a.iszero()&&b.iszero())return 0;
int cnt=0;
for(int i=0;i<N;){
bool f1=cmp(a,big[i]);
bool f2=cmp(big[i],b);
if(!f1){
i++;
}else if(!f2){
break;
}else{
cnt++;
i++;
}
}
printf("%d\n",cnt);
}
}