题目网址:https://codeforc.es/contest/1155/problem/C
题目大意:给你n个数和m个公差,问是否可以确定一个首项和在m个公差里找到一个公差,使得n个数包含在确定出的数列中
题解:差分+gcd
对于n个数,比如,3,12,27,先考虑这三个数,3与12的公差是9,12与27的公差是15,这三个数若要组成等差数列,可设公差是d,那么,d扩大几倍可以是9或者15,则d必是9与15的公因数,所以先确定最大的d,在考率m个公差中是否有d的因子即可。
1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int maxn=3e5+7; 5 ll a[maxn],b[maxn],c[maxn]; 6 int main() 7 { 8 ll n,m; 9 cin>>n>>m; 10 for(int i=1;i<=n;i++) scanf("%I64d",&a[i]); 11 for(int i=1;i<=m;i++) scanf("%I64d",&b[i]); 12 int tot=0; 13 for(int i=1;i<=n-1;i++) c[++tot]=a[i+1]-a[i]; 14 sort(a+1,a+1+n); 15 ll res=c[1]; 16 for(int i=2;i<=tot;i++) res=__gcd(res,c[i]); 17 for(int i=1;i<=m;i++) { 18 if(res%b[i]==0) { 19 cout<<"YES"<<endl; 20 printf("%I64d %d ",a[1],i); 21 return 0; 22 } 23 } 24 cout<<"NO"<<endl; 25 }