题意:给你n个数,和m个数
,
让你求每个 j (1<=j<=m) 的GCD(,
);
思路:从最基础的求最大公约数的原理开始走:
GCD(x,y)=GCD(x,y-x);
那么看整体,GCD(,
)
=GCD(,GCD(
),GCD(
),....,GCD(
) )
=GCD(,
,...,
);
那么我们 提前预处理取得GCD(,
,...,
)(取绝对值是因为取得结果有一部分可能是负数的),然后对每一个
取GCD就可以了。
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;
const int N = 1000000 + 100;
int n, m, h;
ll s[N], b[N];
int main()
{
int t;
cin >> n >> m;
for (int i = 1; i <= n; i++)
sc_ll(s[i]);
for (int i = 1; i <= m; i++)
sc_ll(b[i]);
ll res = 0;
for(int i =2;i<=n;i++)
{
if(i==2)res=abs(s[i]-s[1]);
else res=__gcd(res,abs(s[i]-s[1]));
}
for(int i =1;i<=m;i++)
cout<<__gcd(res,s[1]+b[i])<<" ";
return 0;
}