http://codeforces.com/contest/1459/problem/C
根据辗转相减法 gcd(a,b)=gcd(b,a-b)
gcd(a1+b,a2+b,a3+b,……,an+b)=gcd(a1+b,a2-a1,a3-a1,……,an-a1)
#include<cstdio> #include<algorithm> using namespace std; #define N 200001 typedef long long LL; LL a[N],b[N]; LL gcd(LL a,LL b) { return !b ? a : gcd(b,a%b); } int main() { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;++i) scanf("%lld",&a[i]); for(int i=1;i<=m;++i) scanf("%lld",&b[i]); LL g=0; for(int i=2;i<=n;++i) g=gcd(abs(a[i]-a[1]),g); for(int i=1;i<=m;++i) printf("%lld ",gcd(a[1]+b[i],g)); }