Meaning of the question:
Given two arrays a, b, for each number in the b array, find gcd(a[1]+b[j],a[2]+b[j],a[3]+b [j],…).
Solution:
This question mainly examines the nature of gcd, if you know it, you can go directly to the second. The properties are as follows:
gcd(a[1],a[2],a[3],a[4],...)=gcd(a[1],a[2]-a[1],a[3]- a[2],a[4]-a[3],...).
Then the formula for the title becomes gcd(a[1]+b[j],a[2]-a[1],a[3]-a[2],a[4]-a[3] ,...)
So we first preprocess gcd(a[2]-a[1],a[3]-a[2],a[4]-a[3],...). Finally, enumerate for the answer.
Note: Sort before preprocessing, otherwise there may be negative numbers in subtraction.
Code:
#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int>pii;
const int MAXN=2e5+5;
const int mod=77797;
ll a[MAXN];
ll b[MAXN];
ll gcd(ll x,ll y)
{
return y?gcd(y,x%y):x;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n);
for(int i=1;i<=m;i++)
{
cin>>b[i];
}
if(n==1)
{
for(int i=1;i<=m;i++)
{
printf("%lld ",b[i]+a[1]);
}
}
else
{
ll gd=a[n]-a[n-1];
for(int i=n-1;i>=2;i--)
{
gd=gcd(gd,a[i]-a[i-1]);
}
for(int i=1;i<=m;i++)
{
ll gg=gd;
gg=gcd(gg,a[1]+b[i]);
printf("%lld ",gg);
}
}
}