Codeforces Round #188 (Div. 1) / 317A Perfect Pair(数学&优化)
来源:互联网 发布:mac上怎么做u盘启动盘 编辑:程序博客网 时间:2024/06/07 18:31
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?
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.
Print the minimum number of operations or "-1" (without quotes), if it is impossible to transform the given pair to the m-perfect one.
1 2 5
2
-1 4 15
4
0 -1 5
-1
注意x,y异号的情况可以优化。
优化后复杂度:O(log m)
完整代码:
/*30ms,0KB*/#include<cstdio>#include<cmath>#include<algorithm>using namespace std;int main(void){__int64 x, y, m, count = 0;scanf("%I64d%I64d%I64d", &x, &y, &m);if (m <= max(x, y))printf("0");else{if (x <= 0 && y <= 0)printf("-1");else{ ///x,y异号的情况可以优化一下if (x < 0 && y > 0){ count = ceil((double) - x / y); x+=count*y;}else if (x > 0 && y < 0){count = ceil((double) - y / x);y+=count*x;}while (max(x, y) < m){if (x < y)x += y;elsey += x ;++count;}printf("%I64d", count);}}return 0;}
- Codeforces Round #188 (Div. 1) / 317A Perfect Pair(数学&优化)
- codeforces 317 A Perfect Pair
- Codeforces Round #188 (Div. 2) A(数学划分)
- Codeforces Round #144 (Div. 2)---A. Perfect Permutation
- Codeforces Round #185 (Div. 1) / 311A The Closest Pair (“陷阱”题)
- cf 317A. Perfect Pair
- Codeforces Round #133 (Div. 2) A. Tiling with Hexagons(数学)
- Codeforces Beta Round #4 (Div. 2) / 4A Watermelon(数学)
- Codeforces Round #195 (Div. 2) A.(数学枚举)
- Codeforces Round #236 (Div. 2)A(贪心、数学)
- Codeforces Round #276 (Div. 2)A. Factory 数学+规律
- Codeforces Round #327 (Div. 2) (A. Wizards' Duel 简单数学)
- Codeforces Round #342 (Div. 2)(A)贪心,数学
- Codeforces Round #349 (Div. 2)-A. Pouring Rain(数学)
- Codeforces Round #317 (Div. 2) 571A. Lengthening Sticks 组合数学
- Codeforces Round #188 (Div. 2) / 318A Even Odds(简单数学)
- Codeforces Round #183 (Div. 1) / 303A Lucky Permutation Triple (强大的数学&想法题)
- Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪心、数学)
- hdu 1505 hdu 1506 hdu 2870 综合解答
- 对一组字符串排序,使子串总在父串之后
- hdu2870之DP
- VMware_VCP5.0 虚拟化认证原厂培训中文教材 PPT
- HDU 4565 So Easy!
- Codeforces Round #188 (Div. 1) / 317A Perfect Pair(数学&优化)
- poj-1200 Crazy Search
- Wing IDE 4.1使用笔记一修正一下框框字体显示不了中文
- HDOJ 1789
- struts2-常用功能
- Java String.Format() 方法及参数说明
- struts2的验证框架
- SDJZU Digital Roots
- 黑马程序员_11 TCP