Codeforces Round #188 (Div. 2) C. Perfect Pair 数学

B. Strings of Power

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/318/problem/C

Description

Let us call a pair of integer numbers m-perfect, if at least one number in the pair is greater than or equal to m. Thus, the pairs (3, 3) and (0, 2) are 2-perfect while the pair (-1, 1) is not.

Two integers x, y are written on the blackboard. It is allowed to erase one of them and replace it with the sum of the numbers, (x + y).

What is the minimum number of such operations one has to perform in order to make the given pair of integers m-perfect?

Input

Single line of the input contains three integers x, y and m ( - 1018 ≤ x, y, m ≤ 1018).

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preffered to use the cin, cout streams or the %I64d specifier.

Output

Print the minimum number of operations or "-1" (without quotes), if it is impossible to transform the given pair to the m-perfect one.

Sample Input

1 2 5

Sample Output

2

HINT

 

题意

给你x,y,每次你可以选择一个数,让他变成x+y,然后问最少多少步,可以使得x,y中的最大值大于等于k

题解:

这道题类似于fib数,所以我们只要把(x,y)变成(y,x+y)就好

注意,全程爆ll

代码:

 

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 4000001
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

int main()
{
    ll x=read(),y=read(),k=read();
    if(x>=k||y>=k)
    {
        cout<<"0"<<endl;
        return 0;
    }
    if(x<k&&y<k&&x<=0&&y<=0)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    ll ans=0;
    if(x>y)
        swap(x,y);
    if(x<0)
    {
        ans=(y-x)/y;
        x+=ans*y;
    }
    while(y<k)
    {
        ll tmp=x;
        x=y;
        y=tmp+y;
        ans++;
    }
    cout<<ans<<endl;
}

 

你可能感兴趣的:(codeforces)