gcd为找最大公因数,数组要求从小到大,那么排序,表明下标,原序列下标和排序后序列下标不一,那么看能不能交换(能不能与最小的gcd)能yes不能no
代码
#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; int main(){ int n; scanf("%d",&n); while(n--){ int a; scanf("%d",&a); int s[a+1],b,ch[a+1]; for(int i=0;i<a;i++){ scanf("%d",&s[i]); ch[i]=s[i]; if(i==0)b=s[i]; if(s[i]<=b)b=s[i]; } sort(s,s+a);//冒泡炸了,这个快点 int c=0; for(int i=0;i<a;i++){ if(s[i]!=ch[i]&&ch[i]%b!=0){ printf("NO\n"); c=1; break; } } if(!c)printf("YES\n"); } return 0; }
浙公网安备 33010602011771号