1. 程式人生 > >【HDOJ6222】Heron and His Triangle(Java,二分,遞推)

【HDOJ6222】Heron and His Triangle(Java,二分,遞推)

ret 直接 for out ext n-1 multipl system angle

題意:讓你找這樣的一個三角形,三條邊為t,t-1,t+1,並且面積為整數,最後滿足t大於等於n。

n<=1e30

思路:直接推式子不會,打表找規律

f(n)=4*f(n-1)-f(n-2)(n>=3)

f(1)=4 f(2)=14

隊友用Java寫的,再寫個二分就行

 1 import java.math.BigInteger;
 2 import java.util.Arrays;
 3 import java.util.Scanner;
 4 
 5 
 6 public class Main {    
 7      public static void main(String[] args){
8 int t; 9 BigInteger[] a = new BigInteger[110]; 10 BigInteger n; 11 Scanner sc = new Scanner(System.in); 12 //System.out.println(new BigInteger("11").compareTo(new BigInteger("9"))); 13 a[1] = new BigInteger("4"); 14 a[2] = new BigInteger("14");
15 a[3] = new BigInteger("52"); 16 for(int j = 4; j <= 100; ++j){ 17 a[j] = a[j - 1].multiply(new BigInteger("4")).subtract(a[j - 2]); 18 } 19 t = sc.nextInt(); 20 for(int i = 1; i <= t; ++i){ 21 n = sc.nextBigInteger(); 22 //
System.out.println(n); 23 for(int j = 1; j <= 100; ++j){ 24 if(a[j].compareTo(n) >= 0){ 25 System.out.println(a[j]); 26 break; 27 } 28 } 29 } 30 }

【HDOJ6222】Heron and His Triangle(Java,二分,遞推)