-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD_Yet_Another_Array_Problem.cpp
More file actions
55 lines (53 loc) · 998 Bytes
/
D_Yet_Another_Array_Problem.cpp
File metadata and controls
55 lines (53 loc) · 998 Bytes
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+10;
vector<bool> is_prime(N, true);
void seive(){
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i < N; i++) {
if (is_prime[i]) {
for (int j = i * i; j < N; j += i)
is_prime[j] = false;
}
}
}
ll xcd(ll a,ll b){
while(b){
ll t=b;
b=a%b;
a=t;
}
return a;
}
ll isOK(ll x){
for (ll p = 2; p < N; ++p) {
if (is_prime[p] && x % p != 0)
return p;
}
for (ll y = N; y <= 1e5 + N; ++y) {
if (xcd(x, y) == 1)
return y;
}
if (x == 1)
return 2;
return -1;
}
int main(){
int t;
cin>>t;
seive();
while(t--){
int n;
cin>>n;
vector<ll> a(n);
ll x=0;
for(int i=0;i<n;i++){
cin>>a[i];
x=xcd(x,a[i]);
}
ll ans=isOK(x);
cout<<ans<<endl;
}
return 0;
}