//4_7_31: How many Fibs 区间内的Fibonacci数的个数 POJ2413 ZOJ1962
//计算得出Fibonacci数到10^100的时候具有475位
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
short F1[120],F2[120],ans[120];
char Fib[500][120];
int strcmp_me(char a[],char b[])
{
int lena = strlen(a);
int lenb = strlen(b);
if(lena > lenb) return 1;
else if(lenb > lena) return -1;
for(int i = 0;i < lena;i ++)
{
if(a[i] > b[i]) return 1;
else if(a[i] < b[i])return -1;
}
return 0;
}
int main()
{
short i,j,k,F1_cnt,F2_cnt,ans_cnt,temp,carry,start,end;
char a[120],b[120];
memset(F1,0,sizeof(F1));
memset(F2,0,sizeof(F2));
F1[0] = 1;
F2[0] = 2;
F1_cnt = F2_cnt = 1;
Fib[0][0] = '1';
Fib[0][1] = '\0';
Fib[1][0] = '2';
Fib[1][1] = '\0';
for(k = 2;k < 480;k ++)
{
memset(ans,0,sizeof(ans));
carry = 0;
for(i = 0;i < F2_cnt;i ++)
{
temp = carry;
carry = (F1[i] + F2[i] + temp) / 10;
ans[i] = (F1[i] + F2[i] + temp) % 10;
}
if(carry)
{
ans[i] = 1;
ans_cnt = i + 1;
}
else ans_cnt = i;
for(j = ans_cnt - 1;j >= 0;j --) Fib[k][ans_cnt - 1 - j] = (char)(ans[j] + '0');
Fib[k][ans_cnt] = '\0';
for(i = 0;i < 120;i ++) F1[i] = F2[i];
for(i = 0;i < 120;i ++) F2[i] = ans[i];
F1_cnt = F2_cnt;
F2_cnt = ans_cnt;
}
while(1)
{
scanf("%s%s",a,b);
if(strcmp(a,"0") == 0 && strcmp(b,"0") == 0) break;
for(i = 0;i < 480;i ++)
if(strcmp_me(Fib[i],a) >= 0)
{
start = i;
break;
}
for(i = 479;i >= 0;i --)
if(strcmp_me(Fib[i],b) <= 0)
{
end = i;
break;
}
printf("%d\n",end - start + 1);
}
return 0;
}
/*检测结果:通过POJ2413 ZOJ1962检测
10 100
5
1234567890 9876543210
4
1 1
1
1 2
2
1 3
3
1 5
4
2 5
3
2443454345453246525624562345 32444444444432454235623465645656546
34
0 0
请按任意键继续. . .
*/
POJ2413 ZOJ1962 How many Fibs
最新推荐文章于 2019-08-28 13:24:43 发布