UVA10183-How Many Fibs? (Java large number + two points)

Source: Internet
Author: User

UVA10183-How Many Fibs? (Java large number + two points)

UVA10183-How Many Fibs? (Java large number + two points)

Question Link

Question: How many fiber numbers are there between a and B.

Solution: although a. B is very large, there are no more than 100 fibs in 10 ^ 500. In this way, we can first save these S in the array, and then find the location of a and B in the binary system each time, and then we can get the number of fibs between them.

Code:

import java.util.*;import java.math.*;import java.io.*;import java.lang.String.*;public class Main {    static BigInteger f[] = new BigInteger[10005];    static int n = 2;    public static void init () {        f[1] = BigInteger.valueOf(1);        f[2] = BigInteger.valueOf(2);        BigInteger c = BigInteger.valueOf(10).pow(100);        for (int i = 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) {        int l = 1;        int r = n;        int m, num;        while (l < r) {            m = (l + r) / 2;            num = a.compareTo(f[m]);            if (num <= 0)                r = m;            else                l = m + 1;        }        return r;             }    public static void main(String args[]) {        Scanner cin = new Scanner(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;            int x = upper_bound(b);            int y = upper_bound(a) - 1;            if (f[x].compareTo(b) != 0)                x--;            System.out.println(x - y);                        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.