ACM-ICPC 2017 Asia Shenyang F.Heron and His Triangle

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(pa)(pb)(pc) 其中 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(a24)

根据题目要求可知,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;
                }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值