题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=6222
赛中拉的一道题目,打表的时候没开long long,规律找到后面不对头了。
先直接海伦公式打表找整数面积即可,发现4 14 52 194 724.....
可以发现s[i]=4*s[i-1]-s[i-2],直接大数就行了。·
由于限定条件t>=n;
所以直接循环判断即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<queue>
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
int a[1000],b[1000],c[1000];
char s[1000][1000];
char S[1000];
void init()
{
int i,j;
strcpy(s[1],"4");
strcpy(s[2],"14");
for(i=3;i<1000;i++)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
int len1=strlen(s[i-1]),len2=strlen(s[i-2]);
for(j=0;j<len1;j++)
{
a[len1-j]=s[i-1][j]-'0';
}
for(j=0;j<len2;j++)
b[len2-j]=s[i-2][j]-'0';
j=1;
int t1=0;
while(j<=len1)
{
int w=4*a[j]+t1;
a[j]=w%10;
t1=w/10;
j++;
}
while(t1)
{
a[j++]=t1%10;
t1/=10;
}
while(a[len1+1]!=0)
len1++;
int zlen=len1;
for(j=1;j<=zlen;j++)
{
c[j]=a[j]-b[j];
}
for(j=1;j<=zlen;j++)
{
if(c[j]<0)
{
c[j]+=10;
c[j+1]--;
}
}
while(c[zlen]==0)
zlen--;
int y=0;
for(j=zlen;j>=1;j--)
{
s[i][y++]=c[j]+'0';
}
}
}
int main()
{
init();
int T;
// for(int i=1;i<10;i++)
// {
// printf("%s\n",s[i]);
// }
// return 0;
scanf("%d",&T);
while(T--)
{
int i,j;
cin>>S;
int temp=strlen(S);
bool flag=false;
for(i=1;i<1000;i++)
{
if(strcmp(S,s[i])==0||(temp==strlen(s[i])&&strcmp(S,s[i])<0)||temp<strlen(s[i]))
{
flag=true;
break;
}
}
if(!flag)
printf("-1\n");
else
cout<<s[i]<<endl;
}
return 0;
}