fork download
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6. public static void main(String[] args) {
  7. Scanner src=new Scanner(System.in);
  8. BigInteger[] fib=new BigInteger[501];
  9. BigInteger a,b;//Declare a,b variable in BigInteger datatype.Because our input size 10^100.So a,b can be 10^100;
  10. fib[1]=BigInteger.valueOf(1);//fib index one initially declare in 1;
  11. fib[2]=BigInteger.valueOf(2);//fib index two initially declare in 2;
  12. for(int i=3;i<500;i++)//for input value 10^100,the fibo not greater than 500.For that firstly generate all fibo from 1 to 500.
  13. {
  14. fib[i]=fib[i-1].add(fib[i-2]);//Every Time Add previous two fib value;
  15. }
  16. while(src.hasNext())
  17. {
  18. a=src.nextBigInteger();//Taken input by a;
  19. b=src.nextBigInteger();//Taken input by b;
  20. int count=0;//Initially we keep zero in count value;
  21. if(a.compareTo(BigInteger.ZERO)==0 && b.compareTo(BigInteger.ZERO)==0)//If a and b are zero,Then break;
  22. {
  23. break;
  24. }
  25. else if(a.compareTo(BigInteger.ZERO)==0 && b.compareTo(BigInteger.ONE)==0)//If a is 0 but b is 1 value,That time our answer will be 1;
  26. {
  27. //System.out.println("1");
  28. System.out.println(1);
  29. continue;
  30. }
  31. else if(a.compareTo(BigInteger.ONE)==0 && b.compareTo(BigInteger.ONE)==0)//If a is 1 and b is 1,That time our answer will be 1;
  32. {
  33. //System.out.println("1");
  34. System.out.println(1);
  35. continue;
  36. }
  37. for(int i=1;i<500;i++)//If above condition aren't work,Then this statement are execute;
  38. {
  39. if(fib[i].compareTo(a)>=0 &&(fib[i].compareTo(b))<=0)//If f[i]-a>=0 and f[i]-b<=0,Then count are increase;
  40. {
  41. count++;
  42. }
  43. }
  44. System.out.println(count);//Finally We get our expected result;
  45. }
  46.  
  47. }
  48.  
  49. }
  50.  
Success #stdin #stdout 0.15s 321344KB
stdin
10 100
1234567890 9876543210
0 0
stdout
5
4