HDOJ6222Heron and His Triangle(Java,二分,递推)

Posted myx12345

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDOJ6222Heron and His Triangle(Java,二分,递推)相关的知识,希望对你有一定的参考价值。

题意:让你找这样的一个三角形,三条边为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      }

 

以上是关于HDOJ6222Heron and His Triangle(Java,二分,递推)的主要内容,如果未能解决你的问题,请参考以下文章

2017 ACM/ICPC 沈阳 F题 Heron and his triangle

F - Heron and His Triangle UVALive - 8206

HDOJ 5654 xiaoxin and his watermelon candy(离线+树状数组)

HDOJ 5654 xiaoxin and his watermelon candy(离线+树状数组)

Storm and Heron

Han Xin and His Troops