A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times b, b + a, b + 2a, b + 3a, ... and Morty screams at times d, d + c, d + 2c, d + 3c, ....
The Monster will catch them if at any point they scream at the same time, so it wants to know when it will catch them (the first time they scream at the same time) or that they will never scream at the same time.
The first line of input contains two integers a and b (1 ≤ a, b ≤ 100).
The second line contains two integers c and d (1 ≤ c, d ≤ 100).
Print the first time Rick and Morty will scream at the same time, or - 1 if they will never scream at the same time.
20 2 9 19
82
2 1 16 12
-1
In the first sample testcase, Rick's 5th scream and Morty's 8th time are at time 82.
In the second sample testcase, all Rick's screams will be at odd times and Morty's will be at even times, so they will never scream at the same time.
题目主要就是求使b+ax = d+cx有解
最开始的想法直接暴力,双城循环找最小的x.y
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
int main() {
int a;
int b;
int c;
int d;
cin >> a >> b;
cin >> c >> d;
int t = -1;
for(int i = 0;i < 1005;i++) {
for(int j = 0;j < 1005;j++) {
if((a*i - c*j) == (d-b)) {
if(t == -1) {
t = b + a*i;
} else {
t = min(t,b+a*i);
}
}
}
}
cout << t << endl;
return 0;
}
后来看到了网上一种可怕的解法,扩展欧几里得算法,我都开始怀疑怎么人脑能那么强大呢
扩展欧几里德算法是用来在已知a, b求解一组x,y,使它们满足贝祖等式: ax+by = gcd(a, b) =d(解一定存在,根据数论中的相关定理)
这题用扩展欧几里得的思路
a和-c存在整数x,y使ax-cy = gcd(a,-c),同时求出x,y
如果不存在d-b不是gcd(a,-c)倍数,那么一定无解
同时要使x和y都是非负整数
先使x为非负数
再用公式求y,让y也为非负数
最后输出结果
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
int ex_gcd(int a,int b,int &x,int &y) {
if(b == 0) {
x = 1;
y = 0;
return a;
}
int ans = ex_gcd(b,a%b,x,y);
int temp = x;
x = y;
y = temp - a/b * y;
return ans;
}
int main() {
int a;
int b;
int c;
int d;
int x,y;
cin >> a >> b;
cin >> c >> d;
int g = ex_gcd(a,-c,x,y);
if((d - b) % g) {
printf("-1");
return 0;
}
x = x * (d-b)/g;
x = x % (-c/g);
if(x < 0) {
x += fabs(-c/g);
}
y=((d-b)-a*x)/(-c);
while(y < 0){
x = x+fabs(-c/g);
y = ((d-b)-a*x)/(-c);
}
printf("%d\n",b+x*a);
return 0;
}
关于欧几里得算法,转自:http://blog.csdn.net/zhjchengfeng5/article/details/7786595
现在,我们知道了一定存在 x 和 y 使得 : a*x + b*y = gcd , 那么,怎么求出这个特解 x 和 y 呢?只需要在欧几里德算法的基础上加点改动就行了。
我们观察到:欧几里德算法停止的状态是: a= gcd , b = 0 ,那么,这是否能给我们求解 x y 提供一种思路呢?因为,这时候,只要 a = gcd 的系数是 1 ,那么只要 b 的系数是 0 或者其他值(无所谓是多少,反正任何数乘以 0 都等于 0 但是a 的系数一定要是 1),这时,我们就会有: a*1 + b*0 = gcd
当然这是最终状态,但是我们是否可以从最终状态反推到最初的状态呢?
假设当前我们要处理的是求出 a 和 b的最大公约数,并求出 x 和 y 使得 a*x + b*y= gcd ,而我们已经求出了下一个状态:b 和 a%b 的最大公约数,并且求出了一组x1 和y1 使得: b*x1 + (a%b)*y1 = gcd , 那么这两个相邻的状态之间是否存在一种关系呢?
我们知道: a%b = a - (a/b)*b(这里的 “/” 指的是整除,例如 5/2=2 , 1/3=0),那么,我们可以进一步得到:
gcd = b*x1 + (a-(a/b)*b)*y1
= b*x1 + a*y1 – (a/b)*b*y1
= a*y1 + b*(x1 – a/b*y1)
对比之前我们的状态:求一组 x 和 y 使得:a*x + b*y = gcd ,是否发现了什么?
这里:
x = y1
y = x1 – a/b*y1
int ex_gcd(int a,int b,int &x,int &y) {
if(b == 0) {
x = 1;
y = 0;
return a;
}
int ans = ex_gcd(b,a%b,x,y);
int temp = x;
x = y;
y = temp - a/b * y;
return ans;
}