给定两个数组,蒜头君可以执行任意次以下操作:
- 从第一个数组中取一个数 a,从第二个数组中取一个数 b,用 a + b 或者 a−b 替换 a。
问能否使得第一个数组中元素都变成相同的数?
题解:1.先考虑第二个数组中只有一个数x,即对一个数组元素ai+=c*x那么我们只需要看第一个数组中的数是否全部同余x就行了。
2.再考虑第二个数组中有两个数x,y ,即ai+=c*x+d*y,显然c*x+d*y一定是gcd的倍数,则我们看的即ai+=k*gcd(x,y),则我们看的是第一个数组中的数能否全部同于gcd(x,y)
3,继续扩展,对于多个数,我们先把gcd(x,y)堪称一个数在不断跟后面的数求gcd,最终我们看的是第一个数组是否全部同余整个数组的gcd
PS:蓝桥杯一定要用scanf!!!!否则多了会少整整一半的分!!!!!!!!!
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define int long long
#define IOS ios::sync_stdio(false);cin.tie(0);
using namespace std;
const int maxn=1e6+5;
int a[maxn],b[maxn];
main(){
int t;
cin>>t;
while(t--){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
int gcd;
cin>>gcd;
for(int i=2;i<=m;i++){
scanf("%d",&b[i]);
gcd=__gcd(gcd,b[i]);
}
int flag=1;
for(int i=1;i<n;i++){
if(a[i]%gcd!=a[i+1]%gcd)flag=0;
}
if(flag)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}