Codeforces 317 a perfect Pairs

Source: Internet
Author: User
Tags integer numbers
A. Perfect pairtime limit per test

1 second

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

Let us call a pair of integer numbersM-Perfect, if at least one number in the pair is greater
Than or equalM. Thus, the pairs (3, 3) and (0, 2) are 2-perfect while the pair (-1, 1) is not.

Two IntegersX,YAre written on the blackboard.
It is allowed to erase one of them and replace it with the sum of the numbers ,(XRegion + RegionY).

What is the minimum number of such operations one has to perform in order to make the given pair of IntegersM-Perfect?

Input

Single line of the input contains three IntegersX,YAndM(Cost-limit 1018 bytes ≤ bytesX,Y,MLimit ≤ limit 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 % i64dspecifier.

Output

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

Sample test (s) Input
1 2 5
Output
2
Input
-1 4 15
Output
4
Input
0 -1 5
Output
-1

Question:

Let's give you x, y, and M. Let you transform X and Y to make one of them greater than or equal to M. The transformation method is to add another one, if conditions can be met through several steps, the minimum number of steps can be output; otherwise,-1 can be output.

Ideas:
If xy is less than 0 and M is greater than 0, it is certainly impossible to change to M. If x <M & Y <M & X + Y <0, it is certainly impossible to reach M.

I first excluded the output-1, and then considered how to calculate the minimum number of steps. We can add the smallest one to the other in each step. This is a simple method, but this may happen. For example, if-100000000 1 10000000, it will loop for more than 100000000 times, it must have timed out, so we need to speed up.

Code:

//cf 317 A//2013-06-22-16.43#include <iostream>using namespace std;int main(){    __int64 x, y, m;    while (cin >> x >> y >> m)    {        if (x <= 0 && y <= 0)        {            if (x < m && y < m && x+y <= 0)            {                cout << "-1" << endl;                continue;            }        }        __int64 ans = 0;        int cnt = 0;        while (x < m && y < m)        {            if (x > y)            {                __int64 t = x; x = y; y = t;            }            if (x < 0 && y > 0 && -x > y)            {                ans += (-x)/y;                x += (-x)/y * y;            }            else            {                x = x+y;                ans++;            }        }        cout << ans << endl;    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.