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;
}
目录
相关文章
|
算法 Java
算法竞赛Java选手的语言快速熟悉指南
算法竞赛Java选手的语言快速熟悉指南
算法竞赛Java选手的语言快速熟悉指南
|
存储 开发者
浮点数| 学习笔记
快速学习浮点数。
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8072 2
|
11天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18755 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10140 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1524 5
|
9天前
|
云安全 数据采集 人工智能