A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integers t−1, t, t+ 1 and thatits area is an integer. Now, for given n you need to find a Heron’s triangle associated with the smallest t bigger
than or equal to n.
Input
The input contains multiple test cases. The first line of a multiple input is an integer T (1 ≤ T ≤ 30000) followedby T lines. Each line contains an integer N (1 ≤ N ≤ 10^30).
Output
For each test case, output the smallest t in a line. If the Heron’s triangle required does not exist, output -1.
Sample Input
4 1 2 3 4
Sample Output
4 4 4 4
打表发现数列规律 f[n]=4*f[n-1]-f[n-2]
大数写JAVA!
package a;
import java.util.Scanner;
import java.math.BigInteger;
public class A {
public static void main(String[] args)
{
BigInteger[] num=new BigInteger[105];
num[0]=BigInteger.valueOf(4);
num[1]=BigInteger.valueOf(14);
for(int i=2;i<100;i++)
{
num[i]=num[i-1].multiply(num[0]);
num[i]=num[i].subtract(num[i-2]);
}
Scanner cin=new Scanner(System.in);
int t=cin.nextInt();
while(t--!=0)
{
BigInteger n=cin.nextBigInteger();
for(int i=0;i<99;i++)
{
if(num[i].compareTo(n)!=-1)
{
System.out.println(num[i]);
break;
}
}
}
}
}