10183-how Many fibs?
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_ problem&problem=1124
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 specification
The input contains several test cases. Each test case consists of the 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 specification
For each test case output in a single line the number of Fibonacci numbers fi and a<=fi<=b.
Sample Input
10 100
1234567890 9876543210
0 0
Sample Output
5
4
Start by playing the table and then enumerating the judgments from the beginning.
Complete code:
/*0.016s*/#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 105;///Note that this value is set to pay attention to intermediate result Char NUMSTR[MAXN], NUMSTR2[MAXN];
Input and output interface struct Bign {int len, S[MAXN];
Bign () {memset (s, 0, sizeof (s));
len = 1;
} bign (int num) {*this = num;
} bign (const char* num) {*this = num;
} bign operator = (const int num) {char S[MAXN];
sprintf (S, "%d", num);
*this = s;
return *this;
} bign operator = (const char* num) {len = strlen (num);
for (int i = 0; i < len; i++) S[i] = num[len-i-1] & 15;
return *this; ///Output Const char* str () const {if (len) {for (int i = 0; i < Len i++) numstr[i] = ' 0 ' + S[Len-i-1];
Numstr[len] = ' the ';
else strcpy (Numstr, "0");
return numstr;
///to the leading 0 void clean () {while (len > 1 &&!s[len-1]) len--;
///plus bign operator + (const bign& b) const {bign C;
C.len = 0;
for (int i = 0, g = 0; G | | | i < MAX (Len, B.len); i++) {int x = g;
if (i < len) x + = S[i];
if (I < B.len) x + = B.s[i];
c.s[c.len++] = x% 10;
g = X/10;
return C;
///minus bign operator-(const bign& B) const {bign C;
C.len = 0;
for (int i = 0, g = 0; i < len; i++) {int x = s[i]-G;
if (I < b.len) x-= B.s[i];
if (x >= 0) g = 0;
else {g = 1; x + 10;
} c.s[c.len++] = x;
} C.clean ();
return C;
///Multiply bign operator * (const bign& b) const {bign C;
C.len = len + B.len; for (int i = 0; i < len; i++) for (int j = 0; J < B.len; J +) C.s[i + j] = = S[i] * b
. S[j];
for (int i = 0; i < c.len-1 i++) {c.s[i + 1] + = c.s[i]/10;
C.s[i]%= 10;
} C.clean ();
return C;
}///except bign operator/(const bign &b) const {bign ret, cur = 0;
Ret.len = Len;
for (int i = len-1 i >= 0; i--) {cur = cur * 10;
Cur.s[0] = s[i];
while (cur >= b) {cur = b;
ret.s[i]++;
} Ret.clean ();
return ret; }///modulo, remainder bign operator% (const bign &b) Const {bign c = *this/b;
return *this-c * b;
BOOL operator < (const bign& b) Const {if (len!= b.len) return Len < B.len;
for (int i = len-1 i >= 0; i--) if (S[i]!= b.s[i]) return s[i] < b.s[i];
return false;
BOOL operator > (const bign& B) Const {return B < *this; BOOL operator <= (const bign& B) Const {return! (
b < *this); BOOL operator >= (const bign &b) Const {return! (
*this < b);
BOOL operator!= (const bign &b) Const {return b < *this | | *this < b; BOOL operator = = (CONST bign& b) Const {return! b < *this) &&!
(*this < b); } bign operator + = (Const Bign &a) {*this = *this + A;
return *this;
} bign Operator-= (const bign &a) {*this = *this-a;
return *this;
} bign operator *= (const bign &a) {*this = *this * A;
return *this;
} bign operator/= (const bign &a) {*this = *this/a;
return *this;
} bign operator%= (const bign &a) {*this = *this% A;
return *this;
}
};
Bign f[500] = {1, 1};
int main () {bign A, B;
int I, J;
for (i = 2; i < ++i) f[i] = F[i-1] + f[i-2];
while (scanf ("%s%s", Numstr, numstr2), a = Numstr, B = numstr2, b!= 0) {i = 1;
while (f[i++] < a);
I.;
j = i;
while (f[j++] <= b);
printf ("%d\n", j-i-1);
return 0; }
See more highlights in this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
/*0.372s*/
import java.io.*;
Import java.util.*;
Import java.math.*;
public class Main {
static final int maxn =;
static Scanner cin = new Scanner (new Bufferedinputstream (system.in));
public static void Main (string[] args) {
biginteger[] f = new BIGINTEGER[MAXN];
F[1] = Biginteger.one;
F[2] = new BigInteger ("2");
for (int i = 3; i < MAXN ++i)
f[i] = F[i-1].add (f[i-2));
while (true) {
BigInteger a = Cin.nextbiginteger (), B = Cin.nextbiginteger ();
if (B.compareto (biginteger.zero) = = 0) break
;
int i = 1;
while (F[i++].compareto (a) < 0)
;
I.;
int j = i;
while (F[j++].compareto (b) < 1)
;
System.out.println (J-i-1);
}}}