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.
InputThe 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).
OutputFor each test case, output the smallest t in a line. If the Heron’s triangle required does not exist, output -1.Sample Input41234
Sample Output
4444 题目是求大于等于的最小t使t,t-1,t+1构成的三角形的面积是一个整数 然后就是打表找规律。。 做题的时候一直在想用推出来的公式打表,结果最好看题解竟然是一个大数找规律。唉。 因为是大数所以用java做的。
import java.math.*; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner cin=new Scanner(new BufferedInputStream(System.in)); BigInteger res[] = new BigInteger[100]; res[0] = BigInteger.valueOf(4L); res[1] = BigInteger.valueOf(14L); for (int i = 2;i < 100;i++) { res[i] = res[i-1].multiply(new BigInteger("4")).subtract(res[i-2]); } while (cin.hasNext()) { int t = cin.nextInt(); for (int ca = 1;ca <= t;ca++) { BigInteger n = cin.nextBigInteger(); int i = 0; for (i = 0;i < 100;i++) { if (n.compareTo(res[i]) != 1) break; } System.out.println(res[i]); } } cin.close(); } }