-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathE0079.cpp
49 lines (43 loc) · 919 Bytes
/
E0079.cpp
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
// Problem Code: LEDIV
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
int size = pow(10, 5);
vector<int> leastPrime(size + 1, 0);
void modifiedSieveOfEratosthenes() {
int i, j;
leastPrime[1] = 1;
for (i = 2; i * i <= size; i++)
if (leastPrime[i] == 0) {
leastPrime[i] = i;
for (j = 2 * i; j <= size; j += i)
if (leastPrime[j] == 0)
leastPrime[j] = i;
}
for (; i <= size; i++)
if (leastPrime[i] == 0)
leastPrime[i] = i;
}
int littleElephantAndDivisors(vector<int> A) {
int x, gcd = A[0];
for (int i=1 ; i < A.size() ; i++)
gcd = __gcd(A[i], gcd);
return leastPrime[gcd];
}
int main() {
modifiedSieveOfEratosthenes();
leastPrime[1] = -1;
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<int> A(N);
for (int i=0 ; i < N ; i++)
cin >> A[i];
cout << littleElephantAndDivisors(A) << endl;
}
return 0;
}