HDU-6222 Heron and His Triangle

Heron and His Triangle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1423    Accepted Submission(s): 632


 

Problem Description

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

 

Source

2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

 

Recommend

jiangzijing2015

海伦公式 t-1  t  t+1 为三角形三边  使面积为整数的情况

a[i]=4*a[i-1]-a[i-2];

用大数乘法减法打表。

#include <iostream>
#include <cstdio>
using namespace std;
string visit[100]={"4","14","52",
"194"
,"724"
,"2702"
,"10084"
,"37634"
,"140452"
,"524174"
,"1956244"
,"7300802"
,"27246964"
,"101687054"
,"379501252"
,"1416317954"
,"5285770564"
,"19726764302"
,"73621286644"
,"274758382274"
,"1025412242452"
,"3826890587534"
,"14282150107684"
,"53301709843202"
,"198924689265124"
,"742397047217294"
,"2770663499604052"
,"10340256951198914"
,"38590364305191604"
,"144021200269567502"
,"537494436773078404"
,"2005956546822746114"
,"7486331750517906052"
,"27939370455248878094"
,"104271150070477606324"
,"389145229826661547202"
,"1452309769236168582484"
,"5420093847118012782734"
,"20228065619235882548452"
,"75492168629825517411074"
,"281740608900066187095844"
,"1051470266970439230972302"
,"3924140458981690736793364"
,"14645091568956323716201154"
,"54656225816843604128011252"
,"203979811698418092795843854"
,"761263020976828767055364164"
,"2841072272208896975425612802"
,"10603026067858759134647087044"
,"39571031999226139563162735374"
,"147681101929045799118003854452"
,"551153375716957056908852682434"
,"2056932400938782428517406875284"
,"7676576228038172657160774818702"
,"28649372511213908200125692399524"
,"106920913816817460143341994779394"
,"399034282756055932373242286718052"
,"1489216217207406269349627152092814"
,"5557830586073569145025266321653204"
,"20742106127086870310751438134520002"
,"77410593922273912097980486216426804"
,"288900269562008778081170506731187214"
};
int main()
{
  int t;
  cin>>t;

  while(t--)
  {
       string n;
       cin>>n;
       for(int i=0;i<=61;i++)
       {
           if(visit[i].size()>n.size())
           {
                cout<<visit[i]<<endl;
                break;
           }
        else if(visit[i].size()==n.size())   //字符型只能在长度相同的情况下比较
        {

                if (visit[i]>=n)
             {
               cout<<visit[i]<<endl;
               break;
             }
        }
       }
  }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xigongdali/article/details/82226677