-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLand.cpp
More file actions
71 lines (59 loc) · 1.51 KB
/
Land.cpp
File metadata and controls
71 lines (59 loc) · 1.51 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
// Function to calculate gcd (Greatest Common Divisor)
long long gcd(long long a, long long b) {
while (b != 0) {
long long temp = b;
b = a % b;
a = temp;
}
return a;
}
// Function to calculate lcm (Least Common Multiple)
long long lcm(long long a, long long b) {
return (a / gcd(a, b)) * b;
}
// Function to find the minimum subset that gives the maximum LCM
void solveTestCase() {
int N;
cin >> N;
vector<long long> arr(N);
// Read the array
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
// Step 1: Calculate the maximum LCM possible from the array
long long maxLCM = 1;
for (int i = 0; i < N; i++) {
maxLCM = lcm(maxLCM, arr[i]);
}
// Step 2: Try to find the minimum number of elements that can give this LCM
long long currentLCM;
int minElements = N;
for (int mask = 1; mask < (1 << N); mask++) {
currentLCM = 1;
int count = 0;
for (int i = 0; i < N; i++) {
if (mask & (1 << i)) {
currentLCM = lcm(currentLCM, arr[i]);
count++;
}
}
if (currentLCM == maxLCM) {
minElements = min(minElements, count);
}
}
// Output the result
cout << minElements << endl;
}
int main() {
int testCases;
cin >> testCases;
while (testCases--) {
solveTestCase();
}
return 0;
}