给定 nn 个正整数,将它们分组,使得每组中任意两个数互质。
至少要分成多少个组?
输入格式
第一行是一个正整数 nn。
第二行是 nn 个不大于10000的正整数。
输出格式
一个正整数,即最少需要的组数。
数据范围
1≤n≤101≤n≤10
输入样例:
6
14 20 33 117 143 175
输出样例:
3
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10;
int n;
int p[N];
int group[N][N];
bool st[N];
int ans = N;
int gcd(int a, int b){
return b ? gcd(b, a % b) : a;
}
bool check(int group[], int gc, int i){
for (int j = 0; j < gc; j ++)
if (gcd( p[group[j]], p[i]) > 1)
return false;
return true;
}
void dfs(int g, int gc, int tc, int start){
if (g >= ans) return ;
if (tc == n) ans = g;
bool flag = true;
for (int i = start; i < n; i ++)
if (!st[i] && check(group[g], gc, i)){
st[i] = true;
group[g][gc] = i;
dfs(g, gc + 1,tc + 1, i + 1);
st[i] = false;
flag = false;
}
if (flag) dfs(g+1, 0, tc, 0);
}
int main(){
cin >> n;
for (int i = 0; i < n; i ++) cin >> p[i];
dfs(1,0,0,0);
cout << ans << endl;
return 0;
}