-
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
96 lines (86 loc) · 1.93 KB
/
D_Yet_Another_Array_Problem.cpp
File metadata and controls
96 lines (86 loc) · 1.93 KB
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
87
88
89
90
91
92
93
94
95
96
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define input for(int &i:v) cin >> i;
#define sort sort(v.begin(),v.end());
void print(const vector<int>& v) {
for (int i : v) {
cout << i << " ";
}
cout << endl;
}
void print(queue<int> v) {
while(!v.empty()) {
cout << v.front() << " ";
v.pop();
}
cout << endl;
}
vector<int> sieve(int n) {
vector<bool> isPrime(n + 1, true);
isPrime[0] = isPrime[1] = false;
for(int i = 2; i * i <= n; ++i) {
if(isPrime[i]) {
for(int j = i * i; j <= n; j += i)
isPrime[j] = false;
}
}
vector<int>x;
for(int i=0;i<n+1;i++){
if(isPrime[i]){
x.push_back(i);
}
}
return x;
}
queue<int> getPrimeFactors(int x) {
queue<int> factors;
for (int i = 2; i * i <= x; ++i) {
if (x % i == 0) {
factors.push(i);
while (x % i == 0)
x /= i;
}
}
if (x > 1)
factors.push(x);
return factors;
}
void solve() {
int n; cin >> n;
set <int> v;
while(n--){
int x;
cin >> x;
v.insert(x);
}
// print(v);
// observation: the product of first few primes till 60 exceeds limits
// only check for these 53 values
vector<int>primes=sieve(60);
// print(primes);
for(int prime : primes){
bool dividesAll = true;
for(int x : v){
if(x % prime != 0){
dividesAll = false;
break;
}
}
if(!dividesAll){
cout << prime << endl;
return;
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t-- > 0) {
solve();
}
}