ACM-ICPC 2017 Asia Shenyang F.Heron and His Triangle
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 that its 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) followed by T lines. Each line contains an integer N (1 ≤ N ≤ 1 0 30 10^{30} 1030)
Output
For each test case, output the smallest t in a line. If the Heron’s triangle required does not exist, output −1.
样例输入
4
1
2
3
4
样例输出
4
4
4
4
大意
一个三角形的三条边为t-1, t, t+1,给你一个整数n
求在三角形的面积是整数的情况下,t大于等于n的最小值
思路
我们由海伦公式( S = p ( p − a ) ( p − b ) ( p − c ) S=\sqrt{p(p-a)(p-b)(p-c)} S=p(p−a)(p−b)(p−c)其中 p = a + b + c 2 p=\frac{a+b+c}{2} p=2a+b+c)得
S = a 4 3 ( a 2 − 4 ) S=\frac{a}{4}\sqrt{3(a^2-4)} S=4a3(a2−4)。
根据题目要求可知,S为整数,打个表发现4,14,52,194…
乍看得不到规律,这时,我们可以使用这个网站OEIS
输入4,14,52,194查询就会找到规律
f[i]=f[i-1]*4-f[i-2]
数字很大,可以使用Java大数
这题其实是一个佩尔方程,关于这部分以后补充
代码
import java.math.BigInteger;
import java.util.Scanner;
import java.util.Vector;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
BigInteger n;
BigInteger x = new BigInteger("4");
BigInteger y = new BigInteger("14");
Vector<BigInteger> v=new Vector<BigInteger>();
v.add(x);
v.add(y);
for(int i=3;i<=60;i++){
BigInteger t = y;
y=y.multiply(BigInteger.valueOf(4)).subtract(x);
x=t;
v.add(y);
}
int t = cin.nextInt();
while(t-- != 0){
n = cin.nextBigInteger();
for(BigInteger i:v)
if(i.compareTo(n)>=0){
System.out.println(i);
break;
}
}
}
}