GYM - 100812L Knights without Fear and Reproach (扩展欧几里得)

Knights without Fear and Reproach
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

They were all dead. The final lunge was an exclamation mark to everything that had led to this point. I wiped my sword from the blood of Dragon and sheathed it. And then it was all over. Devastated, I came out of the empty castle and wandered somewhere along a dirt road. But before I could think about what I would do now, I heard a piercing scream from behind: "Stop right now! Drop a sword and raise your hands up!". They were knights. Only knights scream like that before making a hit. If they had been bandits I would be already dead.

I turned back and saw two figures in heavy armor rushing towards me. They were Lancelot and Percival — two knights of the Round Table, known for their fast reprisal over renegades like me. In the Kingdom they were called the Cleaners. As for me, not the most suitable name: they usually left a lot of dirt.

I almost instantly read their technique. Each of them was preparing for some time, then hit instantly, then was preparing again for the same time, then hit again, and so on, while their victim was not fallen. Lancelot spent n seconds to prepare, and Percival — m seconds. I was too tired and could parry a hit only if the previous one was done more than a second ago, and there were no powers to counter-attack at all. It was the sense that Lady Luck was really a hooker, and you were fresh out of cash. The knights knew their job and the first hit I wouldn't be able to parry would finish me off. My story wouldn't have a happy end.

Input

The only line contains two integers separated by a space: n and m (1 ≤ n, m ≤ 2·109) — the intervals of time in seconds between hits of Lancelot and Percival correspondingly.

Output

Output a single integer — the number of seconds from the beginning of the fight when the protagonist will be killed.

Examples
input
9 6
output
18
input
7 11
output
22



题意:两人轮流攻击,主角每次只能闪避一个攻击,闪避后要休息一秒…问主角什么时候死。


解题思路:比较容易想到的是,两个数的最小公倍数就是答案。但是当两个数互质的时候就不是了,nx+my=1,实际上就是求这个方程的解。扩展欧几里得即可。


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <memory.h>
#include <bitset>
#include <map>
using namespace std;
typedef long long int ll;
ll N, M;

ll exgcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll ans = exgcd(b, a % b, x, y);
    ll tmp = x;
    x = y;
    y = tmp - a / b * y;
    return ans;
}

int main()
{

    ios::sync_with_stdio(false);
    cin >> N >> M;
    ll x, y;
    ll g = exgcd(N, M, x, y);
    ll ans = N * M / g;

    if (N == 1 || M == 1)
    {
        if (N == M)
            cout << "1" << endl;
        else
            cout << "2" << endl;
        return 0;
    }

    if (g == 1)
        ans = min(ans, max(abs(x * N), abs(y * M)));

    cout << ans << endl;

    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值