Question: Give a and B. Calculate the number of Fibonacci numbers in the range [a, B], where a <= B <= 10 ^ 100
For the moment, only the plus signs, equal to or greater than, and less than or equal
C ++ code
# Include <iostream>
# Include <fstream>
# Include <algorithm>
# Include <string>
# Include <set>
// # Include <map>
# Include <queue>
# Include <utility>
# Include <iomanip>
# Include <stack>
# Include <list>
# Include <vector>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <cmath>
# Include <ctime>
# Include <ctype. h>
Using namespace std;
// VC6 seems to require # include <iostream. h>, and the using namespace std should be commented out;
Class BigInteger {
Public:
BigInteger ()
{
For (int I = 0; I <2010; I ++)
Str [I] = '0 ';
}
Void display ()
{
Printf ("% s \ n", str );
}
Char * operator = (char * s)
{
Strcpy (str, s );
Len = strlen (s );
Return s;
}
Friend BigInteger operator + (BigInteger & a, BigInteger & B );
Friend bool operator >=( BigInteger & a, BigInteger & B );
Friend bool operator <= (BigInteger & a, BigInteger & B );
Char str [2010]; // the expression of a large number. It must be too big for this question ......
Int len; // the length of a large number, that is, the number of digits.
};
BigInteger operator + (BigInteger & a, BigInteger & B)
{
BigInteger tp, ta, tb, res;
Int k = a. len> B. len? A. len: B. len, w = 0, I;
// Flip
For (I = a. len-1; I> = 0; I --)
Ta. str [w ++] = a. str [I];
Ta. str [w] = 0;
W = 0;
For (I = B. len-1; I> = 0; I --)
Tb. str [w ++] = B. str [I];
Tb. str [w] = 0;
W = 0;
// Add by bit
For (I = 0; I <k; I ++)
{
If (ta. str [I] = 0)
Ta. str [I] = '0 ';
If (tb. str [I] = 0)
Tb. str [I] = '0 ';
Tp. str [I] = (ta. str [I]-'0') + (tb. str [I]-'0') + w) + '0 ';
W = 0;
If (tp. str [I]> '9 ')
Tp. str [I]-= 10, w = 1;
}
If (w> 0)
Tp. str [k] ++, k ++;
W = 0;
For (I = k-1; I> = 0; I --)
Res. str [w ++] = tp. str [I];
Res. str [w] = 0;
Res. len = k;
Return res;
}
Bool operator> = (BigInteger & a, BigInteger & B)
{
If (a. len> B. len)
Return true;
If (a. len = B. len & strcmp (a. str, B. str)> = 0)
Return true;
Return false;
}
Bool operator <= (BigInteger & a, BigInteger & B)
{
If (a. len <B. len)
Return true;
If (a. len = B. len & strcmp (a. str, B. str) <= 0)
Return true;
Return false;
}
BigInteger f [500], a, B;
Int main ()
{
Int I, map [110], start, end, res;
Char s [105], p [105];
F [1] = "1 ";
F [2] = "2 ";
Map [1] = 1;
For (I = 3; I <500; I ++)
{
F [I] = f [I-1] + f [I-2];
If (f [I]. len = f [I-1]. len)
Map [f [I]. len] = map [f [I-1]. len]; // length ing to location
Else map [f [I]. len] = I;
}
While (scanf ("% s", s, p ))
{
If (! Strcmp (s, "0 ")&&! Strcmp (p, "0 "))
Break;
A = s, B = p;
Start = map [a. len]; // read range based on the number of digits a and B
End = map [B. len + 1];
Res = 0;
For (I = start; I <= end; I ++) // I is the position, which is equivalent to I of f [I] And a subscript with a range of only 500
If (f [I]> = a & f [I] <= B)
Res ++;
Printf ("% d \ n", res );
}
Return 0;
}