F - Heron and His Triangle UVALive - 8206
Posted Jozky86
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了F - Heron and His Triangle UVALive - 8206相关的知识,希望对你有一定的参考价值。
F - Heron and His Triangle UVALive - 8206
题意:
给你应该n,然后求一个最小的t,问长度为t-1,t,t+1所组成的三角形的面积为整数,t>=n
题解:
这题我一开始被题目的-1给迷惑了,以为筛出所有的T,然后我写了暴力求出尽可能多的t,如下
if(n<=4)printf("4\\n");
else if(n<=14)printf("14\\n");
else if(n<=52)printf("52\\n");
else if(n<=194)printf("194\\n");
else if(n<=724)printf("724\\n");
else if(n<=2702)printf("2702\\n");
else if(n<=10084)printf("10084\\n");
else if(n<=37634)printf("37634\\n");
else if(n<=140452)printf("140452\\n");
然后题目就没进展了,最后看了答案我吐了。。。根据上面的数推出公式 a[n] = a[n-1] *4+a[n-2]
(哭哭哭) 孩子推不出来啊
代码:
#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;
}
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
static final int MAXN=128;
static BigInteger []x = new BigInteger[MAXN];
static BigInteger []y = new BigInteger[MAXN];
static BigInteger []t = new BigInteger[MAXN];
static void init() {
x[0]=BigInteger.valueOf(2);
y[0]=BigInteger.valueOf(1);
t[0]=BigInteger.valueOf(4);
BigInteger d=BigInteger.valueOf(3);
for(int i=1;i<MAXN;i++) {
x[i]=x[0].multiply(x[i-1]).add(y[i-1].multiply(d));
y[i]=y[0].multiply(x[i-1]).add(x[0].multiply(y[i-1]));
t[i]=x[i].multiply(BigInteger.valueOf(2));
}
}
public static void main(String []args) {
Scanner in=new Scanner(System.in);
int cas=in.nextInt();
BigInteger n;
init();
while(cas>0) {
--cas;
n=in.nextBigInteger();
for(int i=0;i<MAXN;++i) {
if(n.compareTo(t[i])!=1) {
System.out.println(t[i]);
break;
}
}
}
}
}
以上是关于F - Heron and His Triangle UVALive - 8206的主要内容,如果未能解决你的问题,请参考以下文章
F - Heron and His Triangle UVALive - 8206
Heron and His Triangle HDU - 6222(pell 大数)
F.Lucky Pascal Triangl(lucas+数位dp)