Memory and De-Evolution(CodeForces 712C )
Description
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?
Input
The first and only line contains two integers x and y (3 ≤ y < x ≤ 100 000) — the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer — the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y
Sample Input
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Hint
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
In the second sample test, Memory can do .
In the third sample test, Memory can do:
.
題解:一開始,正常人的想法就是如何改變邊長為n的等邊三角形的邊,使得其變為邊長為m的等邊三角形。但是在不斷嘗試過程中,就會發現,貌似不是非常好處理。根據定理"三角形兩邊之和大於第三邊,兩邊之差小於第三邊",就很難確定最初應該把x減少為多少合適,如果減少太多,勢必會使得另外兩條邊減少速度過慢
(22,22,22)→(4,22,22)→(4,19,22)→(4,19,16)→(4,13,16)→(4,13,10)→(4,7,10)→(4,7,4)→(4,4,4)
但如果減少太少,同樣會影響速率。應該是個怎麼樣的度,總是把握不好。這時,把問題反過來想想,發現有出路。減法控制不好,相對來說加法就簡單得多。為了儘快使y->x,那每秒都要使得y增加得儘可能多,所以每次選取最短邊,將其變為另兩條邊之和-1("三角形兩邊之和大於第三邊"),直到三條邊都大於等於x為止。
程式碼如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define swap(a,b) (a=a+b,b=a-b,a=a-b)
#define maxn 320007
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e 2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
using namespace std;
int a[4];
int main()
{
int n,m;
cin>>n>>m;
a[0]=m,a[1]=m,a[2]=m;
int sum=0;
while(1)
{
if(a[0]==n&&a[1]==n&&a[2]==n)
break;
sum++;
sort(a,a+3);
a[0]=a[1]+a[2]-1;
if(a[0]>=n)
a[0]=n;
}
cout<<sum<<endl;
return 0;
}