POJ 2413 How many Fibs? (java大数)

How many Fibs?

Description

Recall the definition of the Fibonacci numbers:

f1 := 1
f2 := 2
fn := f

n-1

 + f

n-2

     (n>=3) 

Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b].

Input

The input contains several test cases. Each test case consists of 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

For each test case output on a single line the number of Fibonacci numbers fi with a<=fi<=b.

Sample Input

10 100
1234567890 9876543210
0 0

Sample Output

5
4

网上的博弈题分类中出了一个叛徒...明明不是博弈。java顺手ac了。
import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    public static void main(String args[]) {
        BigInteger a, b;
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            a = in.nextBigInteger();
            b = in.nextBigInteger();
            if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO)) break;
            int cnt = 0;
            BigInteger f[] = new BigInteger[5000];
            f[1] = BigInteger.valueOf(1); f[2] = BigInteger.valueOf(2);
            for(int i = 1; ; i++) {
                if(i >= 3)
                    f[i] = f[i - 1].add(f[i - 2]);
                if(f[i].compareTo(b) > 0) break;
                if(a.compareTo(f[i]) <= 0) {
                    cnt++;
                }
            }
            System.out.println(cnt);
        }
    }
}
时间: 2024-10-25 12:02:14

POJ 2413 How many Fibs? (java大数)的相关文章

POJ 2413 How many Fibs?#二分+大数加法

http://poj.org/problem?id=2413 #include<iostream> #include<cstdio> #include<cstring> using namespace std; //到第485个fib数才有100位 const int LAST=108; char res[500][110]; //存储fib数 char *pos[500]; //存储每个fib数的首地址 char* Addition(char *a,char *b,c

UVA10183 - How Many Fibs?(java大数+二分)

UVA10183 - How Many Fibs?(java大数+二分) 题目链接 题目大意:给你a,b,求[a,b]之间有多少个fibs数. 解题思路:虽然a.b很大,但是在10^100内的fibs却不超过500个.这样就可以先把这些fibs保存在数组中,然后每次二分去找a,b的位置,然后就可以得到之间有多少个fibs. 代码: import java.util.*; import java.math.*; import java.io.*; import java.lang.String.*

POJ 2413 How many Fibs? /HDOJ 1316 How Many Fibs?

题目来源:http://poj.org/problem?id=2413 / http://acm.hdu.edu.cn/showproblem.php?pid=1316 How many Fibs? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10742   Accepted: 3979 Description Recall the definition of the Fibonacci numbers: f1 :=

hdu--1316--How Many Fibs?(java大数)

How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6371    Accepted Submission(s): 2517 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := fn-1

HDU 1316 How Many Fibs? java大数(水

水一水.. import java.math.*; import java.util.*; import java.io.*; public class Main { BigInteger[] fib = new BigInteger[505]; public void work(){ fib[1] = BigInteger.ONE; fib[2] = BigInteger.valueOf(2); for(int i = 3; i <= 500; i++) fib[i] = fib[i-1].a

poj 2413 How many Fibs?

How many Fibs? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10748   Accepted: 3982 Description 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 Fibo

nyoj 73 比大小 【java大数】

java大数. 代码: import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); BigInteger a, b; BigInteger t = BigInteger.valueOf(0); a = cin.nextBigInteger(); b = cin.nextBi

UVA10303 - How Many Trees?(java大数+catalan数)

UVA10303 - How Many Trees?(java大数+catalan数) 题目链接 题目大意:给你1-N N个数,然后要求由这些数构成二叉搜索树,问有多少种这样的二叉搜索树. 解题思路:把前5项理出来,正好是1 2 5 14 42..就猜想是catalan数,结果也是对了.公式f(i + 1) = (4?i - 6)/ i; (i >= 2).结果很大,要用高精度. 代码: import java.util.*; import java.math.*; import java.io

hdu4927 Series 1(组合+公式 Java大数高精度运算)

题目链接: Series 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 423    Accepted Submission(s): 146 Problem Description Let A be an integral series {A1, A2, . . . , An}. The zero-order series o