Essa wanted to meet the most powerful number theorist of all time: Bahosain, but Bahosain does not waste his precious time, so he gave Essa this puzzle in order to test his abilities.
Given two arrays, the second array only has distinct elements, Essa can do the following as many times as he wants to make all numbers in first array equal.
1、Choose a number from the first array
2、Add or subtract from it a number in the second array
3、Replace the number in the first array with the result
Of course, Essa is unworthy of meeting the almighty Bahosain, and he can’t solve this puzzle on his own, so can you help him?
Input
The first line containing two space separated integers n,m (1≤n,k≤ 1 0 6 10^{6} 106) represent the length of the first and second array.
the second line contains n integers represent the first array (1≤a[i]≤ 1 0 9 10^{9} 109)
the third line contains m integers represent the second array (1≤b[i]≤ 1 0 9 10^{9} 109)
Output
Print Yes, if it’s possible to make all numbers in first array equal; and No in the opposite case.
Example
Input
5 2
3 6 7 2 5
2 4
Output
No
ax + by = gcd(a,b)一定有解
a[i+1] - a[i] = b[1] * k1 + b[2] * k2 +…+ b[n] * kn = k*gcd(b[1],b[2]…b[n])
所以a[i+1]-a[i]为gcd的倍数时有解
#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define ll long long int
typedef pair<int, int> PII;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 7;
int a[N], b[N];
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
//ax + by = gcd(a,b)一定有解
//a[i+1] - a[i] = b[1] * k1 + b[2] * k2 +...+ b[n] * kn = gcd(b[1],b[2]...b[n]);
signed main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++)
scanf("%d", &b[i]);
int g = 0;
for (int i = 1; i <= m; i++)
g = gcd(g, b[i]);
int flag = 0;
for (int i = 1; i < n; i++) {
int k = a[i + 1] - a[i];
if (k % g != 0) {
flag = 1;
break;
}
}
if (!flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}