Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
InputThe first and only line contains two integers x and y (3 ≤ y < x ≤ 100 000) — the starting and ending equilateral triangle side lengths respectively.
Print a single integer — the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
6 3
4
8 5
3
22 4
6
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do .
In the second sample test, Memory can do .
In the third sample test, Memory can do:
.
题意:给出两个数X和Y,X代表修改之前的等边三角形的边长,Y代表修改之后的等边三角形的边长,要求在修改的过程中要保证是三角形(修改过程中可以不是等边三角形),每次只能修改一条边,修改的长度不限,问至少要修改几次才能边长为X修改成为边长为Y的等边三角形。
思路:反推法。
将边长有Y修改回X,在修改的过程中保证最长的边加第二长的边相加减一等于最短的边要修改为的长度,直到三条边都大于X即为修改完成。
设a,b,c为三边,切初始值都为Y,每次修改前都对边进行排序,按照a,b,c从小到大排列,之后修改最短的边长度为:a=b+c-1;
重复过程,直到a,b,c三边都大于X为止。
之前在做第一遍的时候是正着推的过程,每次修改最长边C=a+b+1;但是这样在最后快接近Y的时候特殊情况分类比较麻烦,考虑不全,部分数据会过不了,反推过程则是由三边都大于X截止循环重复,包含了所有特殊情况,简化了问题解决
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int x,y;
scanf("%d %d",&x,&y);
int a[3];
a[0]=a[1]=a[2]=y;
int cnt=0;
while(1)
{
sort(a,a+3);
a[0]=a[1]+a[2]-1;
cnt++;
if(a[0]>=x&&a[1]>=x&&a[2]>=x)
break;
}
printf("%d\n",cnt);
return 0;
}