Codeforces 712C Memory and De-Evolution
Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x
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 if he starts with the equilateral triangle of side length x.
- Examples
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
- Note
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的等邊三角形,Memory想要將其變成邊長為y的等邊三角形。
現規定Memory每秒能夠改變一條邊的大小,但要保證改變後的三條邊仍能構成一個三角形。
問最少需要多少時間才能變為邊長為y的等邊三角形?
【分析】
從邊長為6的等邊三角形變為邊長為3的等邊三角形。先將一邊變為3,則(6,6,3),如果再將一邊變成3,則(6,3,3)並不能組成三角形,所以只能先變為(6,4,3)然後變為(3,4,3),最後變為(3,3,3),一共4步,所以輸出4,此題很明顯是貪心,直接貪就是了,用一個數記錄需要的時間即可!此題需要注意的是最好選擇逆推法,由y->x,這樣就能保證合法且最快了!
【時間複雜度】O(n)
借鑑於:HERE
【程式碼實現】
#include<stdio.h> int main(){ int s[3]; int x,y,i; while(1){ scanf("%d%d",&x,&y); s[0]=s[1]=s[2]=y; for(i=0; s[0]<x||s[1]<x||s[2]<x; i++) s[i%3]=s[(i+1)%3]+s[(i+2)%3]-1; printf("%d\n",i); } return 0; }