题意:两人轮流攻击,主角每次只能闪避一个攻击,闪避后要休息一秒…问主角什么时候死。
解题思路:比较容易想到的是,两个数的最小公倍数就是答案。但是当两个数互质的时候就不是了,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;
}