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.
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<a,b> ;
cos<a,b> = (a^2+b^2-c^2)/(2*a*b) ;
sin<a,b>^2+cos<a,b>^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个左右。
//题意:
给定一个n, 求t-1,t,t+1作为3条边构成的三角形面积为正整数,t>=n,求最小t 。
//思路:
三角形面积 S=(1/2)*a*b*sin<a,b> ;
cos<a,b> = (a^2+b^2-c^2)/(2*a*b) ;
sin<a,b>^2+cos<a,b>^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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
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<lena-lenb;i++)
{
ans[i]='0';
}
ans[lena-lenb]='\0';
strcat(ans,b);
strcpy(tmp,b);
strcpy(b,ans);
int len=max(lena,lenb);
int flag=0;
memset(ans,0,sizeof(ans));
for(int i=len-1;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(lena<lenb)
return 0;
for(int i=0;i<lena;i++)
{
if(a[i]>b[i])
return 1;
else if(a[i]<b[i])
return 0;
}
return 2;
}
int main()
{
int T;
char n[40];
//打表
strcpy(num[0],"4");
strcpy(num[1],"14");
for(int i=2;;i++)
{
mul(num[i-1],4,num[i]);
sub(num[i],num[i-2]);
if(judge(num[i]))
break;
}
scanf("%d",&T);
while(T--)
{
scanf("%s",n);
if(strcmp(n,"1")==0||strcmp(n,"2")==0||strcmp(n,"3")==0||strcmp(n,"4")==0)
{
printf("4\n");
continue;
}
int flag=0;
int cnt;
for(int i=0;;i++)
{
int x=compare(n,num[i]);
if(flag==1&&x!=1)
{
cnt=i;
break;
}
if(x==1)
flag=1;
}
printf("%s\n",num[cnt]);
}
return 0;
}