斐波那契數列 大數加法hdu1316

來源:互聯網
上載者:User
A - How Many Fibs? Crawling in process... Crawling failed Time Limit:1000MS    Memory Limit:32768KB     64bit IO Format:%I64d & %I64u       

Description 我們定義斐波那契數列如下: f1=1 f2=2 f(n)=f(n-1)+f(n-2)(n>=3)   現在,給定兩個數a和b,計算有多少個斐波那契數列中的數在a和b之間(包含邊界)。

Input 輸入包含多組測試資料,每組測試資料都是兩個非負整數a和b,當a和b都等於0時,程式結束。0<=a<=b<=10^100,注意a和b都不會出現前置0的情況(即首位為0)。

Output

輸出在a和b之間(滿足a<=f(i)<=b)數的數量。

Sample Input 10 100 1234567890 9876543210 0 0 

Sample Output 5 4 

這種題用傳統的高精度太費時間,直接Java

import

java.io.*;   
import java.math.BigInteger;   
import java.util.*; 

public classMain {

public static void main(String[] args){
Scanner cin=newScanner(System.in);
BigInteger a[]=new BigInteger[10500];
a[1]=BigInteger.ONE;
a[2]=BigInteger.valueOf(2);
for(int i=3; i<10500; i++)
a[i]=a[i-1].add(a[i-2]);
int sum;
while(cin.hasNextBigInteger()){
sum=0;
BigInteger x=cin.nextBigInteger();
BigInteger y=cin.nextBigInteger();
if(x.equals(BigInteger.ZERO)&&y.equals(BigInteger.ZERO))
break;
for(int i=1;i<10500; i++){
if(a[i].compareTo(x)>=0&&a[i].compareTo(y)<=0)
sum++;
}
System.out.println(sum);
}
}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.