CF 317A (Perfect Pair-generalized Fib sequence p, q = 1 properties 2 & amp; Addition growth pole)

Source: Internet
Author: User

A. Perfect Pair
Time limit per test1 second
Memory limit per test256 megabytes
Inputstandard input
Outputstandard 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 numbers + keys 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 (cost-loss 1018 ≤ cost x, y, m ≤ cost 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
Note
In the first sample the following sequence of operations is suitable: (1, 2) (3, 2) (5, 2 ).

In the second sample: (-1, 4) (3, 4) (7, 4) (11, 4) (15, 4 ).

Finally, in the third sample x, y cannot be made positive, hence there is no proper sequence of operations.

 

First, although I use the Sx formula (which can be divided into two parts), this question can be violent (the growth pole of addition is very large)

You only need to set the negative number.


[Cpp]
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <iostream>
# Include <algorithm>
# Include <functional>
# Include <cmath>
# Include <cctype>
Using namespace std;
# Define For (I, n) for (int I = 1; I <= n; I ++)
# Define Rep (I, n) for (int I = 0; I <n; I ++)
# Define Fork (I, k, n) for (int I = k; I <= n; I ++)
# Define ForD (I, n) for (int I = n; I --)
# Define Forp (x) for (int p = pre [x]; p = next [p])
# Define RepD (I, n) for (int I = n; I> = 0; I --)
# Define MEM (a) memset (a, 0, sizeof ())
# Define MEMI (a) memset (a, 127, sizeof ())
# Define MEMi (a) memset (a, 128, sizeof ())
# Define INF (1e18)
# Define MAXN (1000000)
Long x, y, m;
Long f [MAXN] = {0, 1 };
Int n = 0;
Long work ()
{
If (max (x, y)> = m) return 0;
Long j = 0;
If (x = 0 & y = 0) return-1;
If (x <0 & y <0) return-1;
Bool B = 0;
While (max (x, y) <m)
{
If (x + y <= min (x, y) return-1;
If (x> y) swap (x, y );
If (x> 0 & y> 0)
{
Int k = 1;
While (f [k] * x + f [k + 1] * y <m) k ++;
J + = k;
Return j;
}
Else if (! B & x <0 & y> 0)
{
Long k =-x/y;
If (m <0) k = (m-x)/y;
J + = k;
X + = k * y;
B = 1;
}
Else
{
J ++;
X + = y;
}
}
Return j;
}
Int main ()
{
// Freopen (". in", "r", stdin );
// Freopen (". out", "w", stdout );
 
For (n = 3; f [n-1] <INF; n ++) f [n] = f [n-1] + f [N-2];
N --;
// For (I, n) cout <f [I] <'';
While (cin> x> y> m)
Cout <work () <endl;
 
Return 0;
}

# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <iostream>
# Include <algorithm>
# Include <functional>
# Include <cmath>
# Include <cctype>
Using namespace std;
# Define For (I, n) for (int I = 1; I <= n; I ++)
# Define Rep (I, n) for (int I = 0; I <n; I ++)
# Define Fork (I, k, n) for (int I = k; I <= n; I ++)
# Define ForD (I, n) for (int I = n; I --)
# Define Forp (x) for (int p = pre [x]; p = next [p])
# Define RepD (I, n) for (int I = n; I> = 0; I --)
# Define MEM (a) memset (a, 0, sizeof ())
# Define MEMI (a) memset (a, 127, sizeof ())
# Define MEMi (a) memset (a, 128, sizeof ())
# Define INF (1e18)
# Define MAXN (1000000)
Long x, y, m;
Long f [MAXN] = {0, 1 };
Int n = 0;
Long work ()
{
If (max (x, y)> = m) return 0;
Long j = 0;
If (x = 0 & y = 0) return-1;
If (x <0 & y <0) return-1;
Bool B = 0;
While (max (x, y) <m)
{
If (x + y <= min (x, y) return-1;
If (x> y) swap (x, y );
If (x> 0 & y> 0)
{
Int k = 1;
While (f [k] * x + f [k + 1] * y <m) k ++;
J + = k;
Return j;
}
Else if (! B & x <0 & y> 0)
{
Long k =-x/y;
If (m <0) k = (m-x)/y;
J + = k;
X + = k * y;
B = 1;
}
Else
{
J ++;
X + = y;
}
}
Return j;
}
Int main ()
{
// Freopen (". in", "r", stdin );
// Freopen (". out", "w", stdout );

For (n = 3; f [n-1] <INF; n ++) f [n] = f [n-1] + f [N-2];
N --;
// For (I, n) cout <f [I] <'';
While (cin> x> y> m)
Cout <work () <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.