forked from shauryas1ngh/codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
C. mere array cf rd 655 div2.cp
86 lines (71 loc) · 1.48 KB
/
C. mere array cf rd 655 div2.cp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<bits/stdc++.h>
#define pi 3.14159265359
#define ll long long
#define wh(t) int t;cin>>t; while(t--)
#define speed ios::sync_with_stdio(0); cin.tie(0);
#define endl "\n"
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
#include<string.h>
//aditya ranaut- phoenix_aditya
const int mxn=1e5+5;
ll arr[mxn];
ll sorted[mxn];
ll gcd(ll a, ll b)
{
if(a==0)
return b;
else
return gcd(b%a,a);
}
ll arraygcd(vector<ll> outofplace, ll n)
{
ll result=outofplace[0];
for (int i = 1; i < n; i++)
{
result = gcd(outofplace[i], result);
if(result == 1)
{
return 1;
}
}
return result;
}
void solve()
{
ll n;
cin>>n;
f(i,0,n)
{cin>>arr[i];sorted[i]=arr[i];}
sort(sorted,sorted+n);
vector<ll> outofplace;
for(int i=0;i<n;i++)
{
if(arr[i]==sorted[i])
continue;
else
outofplace.push_back(arr[i]);
}
if(outofplace.size()==0)
{
cout<<"YES\n";
return;
}
sort(outofplace.begin(),outofplace.end());
for(auto x:outofplace)
{
if(gcd(sorted[0],x)!=sorted[0])
{
cout<<"NO\n";
return;
}
}
cout<<"YES\n";
}
int main()
{
speed;
wh(t)
{solve();}
return 0;
}