codeforces 317 A Perfect Pair

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

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

1 2 5

output

2

input

-1 4 15

output

4

input

0 -1 5

output

-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多次,肯定超时,所以我们要加快速度。


代码:

//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;
}
目录
相关文章
|
8月前
codeforces 285C - Building Permutation
题目大意是有一个含n个数的数组,你可以通过+1或者-1的操作使得其中的数是1--n中的数,且没有重复的数。 既然是这样的题意,那么我就应该把原数组中的数尽量往他最接近1--n中的位置放,然后求差绝对值之和,但有多个数,怎么使他们和最小,这样就要对其进行排序了,直接按大小给它们安排好位置,然后计算。
22 0
|
27天前
|
Java
hdu-1016-Prime Ring Problem
hdu-1016-Prime Ring Problem
14 0
|
1月前
G - Prime Ring Problem(深搜)
G - Prime Ring Problem(深搜)
|
8月前
|
算法
light oj 1258 - Making Huge Palindromes(KMP)
ight oj里这个题目是属于KMP分类的,但乍看好像不是kmp,因为只有一个字符串。要想的到一个回文串,把该字符串翻转接到原串后面必然是一个回文串,但并不一定是最短的。我们必须考虑怎么把两个串尽量融合在一起,这就要看翻转串的前段与原串的后段有多少是匹配的了,这里就用到了KMP算法。
18 1
|
9月前
|
算法
The kth great number(小根堆思想,模板题)
The kth great number(小根堆思想,模板题)
29 0
The kth great number(小根堆思想,模板题)
|
10月前
UVa11296 - Counting Solutions to an Integral Equation(枚举技巧)
UVa11296 - Counting Solutions to an Integral Equation(枚举技巧)
34 0
|
Unix Python
LeetCode 71. Simplify Path
给定文件的绝对路径(Unix下的路径)字符串,简化此字符串。
70 0
LeetCode 71. Simplify Path
LeetCode 367. Valid Perfect Square
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
78 0
LeetCode 367. Valid Perfect Square
LeetCode 279. Perfect Squares
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
52 0
LeetCode 279. Perfect Squares
|
Perl
AtCoder Beginner Contest 217 F - Make Pair (区间dp)
AtCoder Beginner Contest 217 F - Make Pair (区间dp)
100 0