题中还有还有两大坑点,然后我就wa了两发,第一个要开long long ,第二个记得要提前先算出来gcd(a2-a1,a3-a2,…,an-an-1) 这样只用算一遍,不能计算每个b[j] 的时候都算一遍,这样会TLE
AC代码
#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;constint N =2e5+10;
ll n, m;
ll a[N], b[N];
ll gcd(ll x, ll y){return y ?gcd(y, x % y): x;}intmain(){
cin >> 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 res=0;for(int i=2;i<=n;i++) res=gcd(res,a[i]-a[i-1]);for(int i=1;i<=m;i++){
cout<<abs(gcd(res,a[1]+b[i]))<<" ";}
cout<<endl;return0;}