Heron and His Triangle(HDU 6222 找规律+大数)

Heron and His Triangle

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


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亚洲区沈阳站-重现赛(感谢东北大学)


//题意
给定一个n, 求t-1,t,t+1作为3条边构成的三角形面积为正整数,t>=n,求最小t 。

//思路
三角形面积 S=(1/2)*a*b*sin ;
cos = (a^2+b^2-c^2)/(2*a*b) ;
sin^2+cos^2=1 ;
a=a; b=a+1; c=a+2;

=>S = (a+1)*sqrt(3*(a-1)*(a+3))/4 ; S要为正整数 ;

=>写个小程序枚举满足条件的t,得:t = 4, 14, 52, 194, 724 ...

=>规律: t[i]=t[i-1]*4-t[i-2] ;

按照上述规律,大数打表即可:由于t大致是按照4的指数上升的,所以10^32内,t大致是150个左右。

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

char num[500][40];

//大数乘法
void mul(char a[], int x,char (&res)[40])
{
    int len=strlen(a);
    int flag=0;
    char ans[40];
    for(int i=len-1;i>=0;i--)
    {
        int val=(a[i]-'0')*4+flag;
        ans[i]=val%10+'0';
        flag=val/10;
    }
    ans[len]='\0';
    res[0]='\0';
    if(flag)
    {
        res[0]=flag+'0';
        res[1]='\0';
        strcat(res,ans);
    }
    else
    {
        strcpy(res,ans);
    }
}

//大数减法
void sub(char (&a)[40], char b[])
{
    char ans[40];
    char tmp[40];
    int lena=strlen(a);
    int lenb=strlen(b);
    for(int i=0;i=0;i--)
    {
        int val=a[i]-b[i]-flag;
        if(val<0)
        {
            ans[i]=val+10+'0';
            flag=1;
        }
        else
        {
            ans[i]=val+'0';
            flag=0;
        }
    }
    ans[len]='\0';
    strcpy(a,ans);
    strcpy(b,tmp);
}

int judge(char a[])
{
    int len=strlen(a);
    if(len>=32)
        return 1;
    else
        return 0;
}

//比较两个数大小
int compare(char a[], char b[])
{
    int lena=strlen(a);
    int lenb=strlen(b);
    if(lena>lenb)
        return 1;
    else if(lenab[i])
            return 1;
        else if(a[i]


你可能感兴趣的:(ACM-日常训练)