Row GCD

Row GCD

\(gcd\)

  • \(gcd(a,b)=gcd(a-b,b)\)
  • \(gcd(a_1+b_j,…,a_n+b_j)=gcd(a_i-a_{i-1},a[1]+b_j)\)
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 2e5 + 7, M = 30010;
ll a[N], b[N];

ll gcd(ll a,ll b)
{
	return b == 0 ? a : gcd(b, a % b);
}

int main() {
    IO;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 1; i <= m; ++i) cin >> b[i];
    sort(a + 1, a + 1 + n);
    ll g = 0;
    for (int i = 1; i <= n - 1; ++i) g = __gcd(g, a[i + 1] - a[i]);
    for (int i = 1; i <= m; ++i) cout << __gcd(g, a[1] + b[i]) << " ";
    return 0;
}

posted @ 2021-02-22 21:24  phr2000  阅读(53)  评论(0编辑  收藏  举报