[codeforces 317]A. Perfect Pair

试题描述

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 xy 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 xy and m ( - 1018 ≤ xym ≤ 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.

输入示例

-  

输出示例


数据规模及约定

见“输入

题解

如果都是整数,那么不难发现增长率和斐波那契数列相同,就是指数级的,所以直接模拟即可。注意特判有负数的情况。如果只有一个负数,先把这个负数加成正的再模拟。如果两个都是负数,那么不可能再变大了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;
#define LL long long const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
LL read() {
LL x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} LL x, y, m; int main() {
x = read(); y = read(); m = read(); if(x > y) swap(x, y);
if(x <= 0 && y <= 0 && y < m) return puts("-1"), 0;
if(y >= m) return puts("0"), 0;
LL cnt = abs(x) % y ? abs(x) / y + 1 : abs(x) / y;
x += cnt * y; if(x > y) swap(x, y);
while(y < m) {
x = x + y;
if(x > y) swap(x, y);
cnt++;
} printf("%I64d\n", cnt); return 0;
}

最新文章

  1. MVC中的Html.Partial和Html.RenderPartial
  2. Runas命令:能让域用户/普通User用户以管理员身份运行指定程序。
  3. z-index 应用简单总结
  4. zabbix监控windows主机网卡流量
  5. uva 11520 - Fill the Square
  6. 【python cookbook】【数据结构与算法】4.找到最大或最小的N个元素
  7. open/close table on mysql
  8. ListView加载两种以上不同的布局
  9. 11招玩转黑客攻防——用Python,更安全
  10. 03:open-falcon报警定制
  11. Spring 注解方式引入配置文件
  12. 64位RHEL5系统上运行yum出现&quot;This system is not registered with RHN”的解决方法
  13. 使用 jquery-autocomplete插件 完成文本框输入自动填充联想效果 解决兼容IE输入中文问题
  14. memcached注意事项与应用范围、应用条件、限制
  15. mocha、chai、sinon和istanbul实现100%单元测试覆盖率
  16. 关于 while(1)和for(;;)效率问题的一点想法
  17. Java学习(if wihle switch for语句)
  18. Spring Cloud 微服务入门(一)--初识分布式及其发展历程
  19. (转)Linux下内存映射文件的用法简介
  20. 01-hibernate注解:类级别注解,@Entity,@Table,@Embeddable

热门文章

  1. mysql 根据查询结果集更新
  2. MYSQL select查询练习题
  3. css007 margin padding border
  4. GridView数据格式化
  5. IBatis 批量插入数据
  6. js 实现动态key value(JSON字符串注意事项:key和value都要用双引号,官网指定用双引号)
  7. Java字节流与字符流基本操作
  8. fcc的高级算法题
  9. DESCryptoServiceProvider
  10. Lua弱引用table