【JAVA大数+规律】HDU - 6222 F - Heron and His Triangle

F - Heron and His Triangle  HDU - 6222

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

输入n,输出>=n的最小k(要求k和k-1以及k+1能够拼出面积为整数的三角形)

预处理所有的答案

a[i]=a[i-1]*4-a[i-2]     //赛特兄推出的规律

import java.math.BigInteger;
import java.lang.*;
import java.math.BigDecimal;
import java.util.Scanner;


public class Main { 
	public static BigInteger num;
	public static void main(String args[])
	{
		Scanner cin=new Scanner(System.in);
		Integer T;
		T=cin.nextInt();
		BigInteger[] arr=new BigInteger[1005];
		arr[1]=BigInteger.valueOf(4);
		arr[2]=BigInteger.valueOf(14);
		String s="10000000000000000000000000000000";
		BigInteger temp=new BigInteger(s);
		int tt=0;
		for(int i=3;i<=1000;i++)
		{
			arr[i]=arr[i-1].multiply(BigInteger.valueOf(4));
			arr[i]=arr[i].subtract(arr[i-2]);
			tt=i;
			if(arr[i].compareTo(temp)>0) break;
		}
		for(int i=1;i<=T;i++)
		{
			BigInteger n;
			n=cin.nextBigInteger();
			for(int j=1;j<tt;j++)
			{
				if(n.compareTo(arr[j])<0||n.equals(arr[j])) 
				{
					System.out.println(arr[j]);
					break;
				}
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/82888860
今日推荐