本文主要是介绍POJ2413二分,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
注意二分, 上界。
import java.beans.beancontext.BeanContext;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;public class Main{public static void main(String[] args) throws IOException{InputReader in = new InputReader(System.in) ;StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));PrintWriter out = new PrintWriter(System.out);BigInteger f ;BigInteger f1 = BigInteger.valueOf(1) ;BigInteger f2 = BigInteger.valueOf(2) ;BigInteger up = BigInteger.valueOf(10).pow(100) ;ArrayList<BigInteger> fibo = new ArrayList<BigInteger>() ;fibo.add(f1) ; fibo.add(f2) ;for( ; (f = f1.add(f2) ).compareTo(up) <= 0 ; ){fibo.add(f) ;f1 = f2 ;f2 = f ;}fibo.add(f1.add(f2)) ;for(;;){int t = 0 ;f1 = new BigInteger(in.next()) ;f2 = new BigInteger(in.next()) ;if(f1.compareTo(BigInteger.ZERO) == 0 && f2.compareTo(BigInteger.ZERO) == 0) break ;int l = 0 , r = fibo.size() - 1 , mid , lid = 0 , rid = 0 ;while(l <= r){mid = (l + r) >> 1 ;if(fibo.get(mid).compareTo(f1) >= 0){lid = mid ;r = mid - 1 ;}else l = mid + 1 ;}l = 0 ; r = fibo.size() - 1 ;while(l <= r){mid = (l + r) >> 1 ;if(fibo.get(mid).compareTo(f2) <= 0){rid = mid ;l = mid + 1 ;}else r = mid - 1 ;}out.println(rid - lid + 1) ; //out.flush() ;} out.flush() ;}
}class InputReader {public BufferedReader reader;public StringTokenizer tokenizer;public InputReader(InputStream stream) {reader = new BufferedReader(new InputStreamReader(stream), 32768);tokenizer = null;}public String next(){while(tokenizer == null || !tokenizer.hasMoreTokens()){try{tokenizer = new StringTokenizer(reader.readLine());}catch(IOException e){throw new RuntimeException(e);}}return tokenizer.nextToken();}public int nextInt() {return Integer.parseInt(next());}public double nextDouble() {return Double.parseDouble(next());}}
这篇关于POJ2413二分的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!