Uva10183-how many Fibs? (Java large number + two points)
Topic links
The main topic: To you a, B, to ask for [A, a] how many fibs number.
Problem-solving ideas: Although A.B is very big, but in 10^100 within the fibs but not more than 500. This allows you to save these fibs in the array, and then each time two points to the location of a, B, and then you can get the number of fibs between.
Code:
Importjava.util.*;Importjava.math.*;Importjava.io.*;Importjava.lang.string.*; Public class Main {StaticBigInteger f[] =Newbiginteger[10005];Static intn =2; Public Static void Init() {f[1] = biginteger.valueof (1); f[2] = biginteger.valueof (2); BigInteger C = biginteger.valueof (Ten). POW ( -); for(inti =3; F[i-1].compareto (c)! =1; i++) {F[i] = f[i-1].add (F[i-2]); n++; } } Public Static int Upper_bound(BigInteger a) {intL =1;intR = N;intM, Num; while(L < R) {m = (L + r)/2; num = A.compareto (f[m]);if(Num <=0) r = m;ElseL = m +1; }returnR } Public Static void Main(String args[]) {Scanner cin =NewScanner (system.in); Init (); BigInteger A, B; while(Cin.hasnext ()) {a = Cin.nextbiginteger (); b = Cin.nextbiginteger ();if(A.equals (Biginteger.zero) && b.equals (Biginteger.zero)) Break;intx = Upper_bound (b);inty = Upper_bound (a)-1;if(F[x].compareto (b)! =0) x--; System.out.println (x-y); } }}
Uva10183-how many Fibs? (Java large number + two points)