前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >codeforces 317 A Perfect Pair

codeforces 317 A Perfect Pair

作者头像
xindoo
发布2021-01-22 12:55:47
2760
发布2021-01-22 12:55:47
举报
文章被收录于专栏:XINDOO的专栏XINDOO的专栏

A. Perfect Pair

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 %I64dspecifier.

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 test(s)

input

代码语言:javascript
复制
1 2 5

output

代码语言:javascript
复制
2

input

代码语言:javascript
复制
-1 4 15

output

代码语言:javascript
复制
4

input

代码语言:javascript
复制
0 -1 5

output

代码语言:javascript
复制
-1

题意:

给你x 和 y 还有M,让你通过变换x和y,使得其中一个大于或者等于m,变换的方法就是自身加上另外一个,如果可以通过若干步使满足条件,输出最小的步数,否则输出-1。

思路: 如果xy小于0且m大于0,那么肯定不可能变换到m,如果 x < m && y < m && x+y<0也肯定没办法达到m。

我先排除了输出-1的,然后再考虑如何计算最小的步数。我们主要在每一步中最小一个加上另一个就可以了,这是朴素的求法,但可能出现这样的情况 比如 -100000000 1 10000000 这样的话会循环100000000多次,肯定超时,所以我们要加快速度。

代码:

代码语言:javascript
复制
//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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-06-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档