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
| #include <iostream> #include <algorithm>
using namespace std;
const int N = 101;
int n, res; int a[N], group[N][N]; bool st[N];
int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); }
bool check(int group[], int gc, int i) { for (int j = 0; j < gc; ++j) { if (gcd(a[group[j]], a[i]) > 1) return false; } return true; }
void dfs(int g, int gc, int tc, int i) { if (g >= res) return; if (tc == n) res = g; bool flag = true; for (int j = i; j < n; ++j) { if (!st[j] && check(group[g], gc, j)) { st[j] = true; group[g][gc] = j; dfs(g, gc + 1, tc + 1, j + 1); st[j] = false; flag = false; } } if (flag) { dfs(g + 1, 0, tc, 0); } }
int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; res = 0x3f3f3f3f; dfs(1, 0, 0, 0); cout << res; }
|